Bug 1460874 - Part 10: Set font size settings in listener using GeckoRuntimeSettings. r=geckoview-reviewers,snorp
☠☠ backed out by cd616c3e3eea ☠ ☠
authorJan Henning <jh+bugzilla@buttercookie.de>
Wed, 13 Feb 2019 20:10:59 +0000
changeset 458952 2020cb134d1f
parent 458951 9fc145133f20
child 458953 3d4297e781cb
push id35552
push usershindli@mozilla.com
push dateThu, 14 Feb 2019 04:39:44 +0000
treeherdermozilla-central@c6829642e2d0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgeckoview-reviewers, snorp
bugs1460874
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1460874 - Part 10: Set font size settings in listener using GeckoRuntimeSettings. r=geckoview-reviewers,snorp Differential Revision: https://phabricator.services.mozilla.com/D17750
mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoFontScaleListener.java
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoFontScaleListener.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoFontScaleListener.java
@@ -1,16 +1,17 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
 import org.mozilla.gecko.util.ThreadUtils;
+import org.mozilla.geckoview.GeckoRuntimeSettings;
 
 import android.annotation.SuppressLint;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.database.ContentObserver;
 import android.net.Uri;
 import android.provider.Settings;
 import android.support.annotation.UiThread;
@@ -21,76 +22,79 @@ import android.util.Log;
  * in accordance with the device's OS font scale setting.
  *
  * @see android.provider.Settings.System#FONT_SCALE
  */
 public final class GeckoFontScaleListener
         extends ContentObserver {
     private static final String LOGTAG = "GeckoFontScaleListener";
 
-    private static final String PREF_SYSTEM_FONT_SCALE = "font.size.systemFontScale";
-    private static final String PREF_FONT_INFLATION = "font.size.inflation.minTwips";
-    private static final int FONT_INFLATION_OFF = 0;
-    private static final int FONT_INFLATION_ON_DEFAULT_VALUE = 120;
-    private static final float DEFAULT_FONT_SCALE = 1.0f;
-
     // We're referencing the *application* context, so this is in fact okay.
     @SuppressLint("StaticFieldLeak")
     private static final GeckoFontScaleListener sInstance = new GeckoFontScaleListener();
 
     private Context mApplicationContext;
+    private GeckoRuntimeSettings mSettings;
+
     private boolean mAttached;
     private boolean mEnabled;
     private boolean mRunning;
 
+    private float mPrevGeckoFontScale;
+    private boolean mPrevFontInflationState;
+
     public static GeckoFontScaleListener getInstance() {
         return sInstance;
     }
 
     private GeckoFontScaleListener() {
         // Ensure the ContentObserver callback runs on the UI thread.
         super(ThreadUtils.getUiHandler());
     }
 
     /**
      * Prepare the GeckoFontScaleListener for usage. If it has been previously enabled, it will
      * now start actively working.
      */
-    public void attachToContext(final Context context) {
+    public void attachToContext(final Context context,
+                                final GeckoRuntimeSettings settings) {
         ThreadUtils.assertOnUiThread();
 
         if (mAttached) {
             Log.w(LOGTAG, "Already attached!");
             return;
         }
 
         mAttached = true;
+        mSettings = settings;
         mApplicationContext = context.getApplicationContext();
         onEnabledChange();
     }
 
     /**
      * Detaches the context and also stops the GeckoFontScaleListener if it was previously enabled.
+     * This will also restore the previously used font size settings.
      */
     public void detachFromContext() {
         ThreadUtils.assertOnUiThread();
 
         if (!mAttached) {
             Log.w(LOGTAG, "Already detached!");
             return;
         }
 
         stop();
         mApplicationContext = null;
+        mSettings = null;
         mAttached = false;
     }
 
     /**
      * Controls whether the GeckoFontScaleListener should automatically adjust font sizes for web
-     * content in Gecko.
+     * content in Gecko. When disabling, this will restore the previously used font size settings.
      *
      * <p>This method can be called at any time, but the GeckoFontScaleListener won't start actively
      * adjusting font sizes until it has been attached to a context.
      *
      * @param enabled True if automatic font size setting should be enabled.
      */
     public void setEnabled(boolean enabled) {
         ThreadUtils.assertOnUiThread();
@@ -119,16 +123,18 @@ public final class GeckoFontScaleListene
         }
     }
 
     private void start() {
         if (mRunning) {
             return;
         }
 
+        mPrevGeckoFontScale = mSettings.getFontSizeFactor();
+        mPrevFontInflationState = mSettings.getFontInflationEnabled();
         ContentResolver contentResolver = mApplicationContext.getContentResolver();
         Uri fontSizeSetting = Settings.System.getUriFor(Settings.System.FONT_SCALE);
         contentResolver.registerContentObserver(fontSizeSetting, false, this);
         onSystemFontScaleChange(contentResolver, false);
 
         mRunning = true;
     }
 
@@ -141,28 +147,28 @@ public final class GeckoFontScaleListene
         contentResolver.unregisterContentObserver(this);
         onSystemFontScaleChange(contentResolver, /*stopping*/ true);
 
         mRunning = false;
     }
 
     private void onSystemFontScaleChange(final ContentResolver contentResolver, boolean stopping) {
         float fontScale;
-        int fontInflation;
+        boolean fontInflationEnabled;
 
         if (!stopping) { // Either we were enabled, or else the system font scale changed.
-            fontScale = Settings.System.getFloat(contentResolver, Settings.System.FONT_SCALE, DEFAULT_FONT_SCALE);
-            fontInflation = Math.round(FONT_INFLATION_ON_DEFAULT_VALUE * fontScale);
+            fontScale = Settings.System.getFloat(contentResolver, Settings.System.FONT_SCALE, mPrevGeckoFontScale);
+            fontInflationEnabled = true;
         } else { // We were turned off.
-            fontScale = DEFAULT_FONT_SCALE;
-            fontInflation = FONT_INFLATION_OFF;
+            fontScale = mPrevGeckoFontScale;
+            fontInflationEnabled = mPrevFontInflationState;
         }
 
-        PrefsHelper.setPref(PREF_FONT_INFLATION, fontInflation);
-        PrefsHelper.setPref(PREF_SYSTEM_FONT_SCALE, Math.round(fontScale * 100));
+        mSettings.setFontInflationEnabled(fontInflationEnabled);
+        mSettings.setFontSizeFactor(fontScale);
     }
 
     @UiThread // See constructor.
     @Override
     public void onChange(boolean selfChange) {
         onSystemFontScaleChange(mApplicationContext.getContentResolver(), false);
     }
 }
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java
@@ -208,17 +208,17 @@ public final class GeckoRuntime implemen
             }
         }
 
         GeckoAppShell.useMaxScreenDepth(settings.getUseMaxScreenDepth());
         GeckoAppShell.setDisplayDensityOverride(settings.getDisplayDensityOverride());
         GeckoAppShell.setDisplayDpiOverride(settings.getDisplayDpiOverride());
         GeckoAppShell.setScreenSizeOverride(settings.getScreenSizeOverride());
         GeckoAppShell.setCrashHandlerService(settings.getCrashHandler());
-        GeckoFontScaleListener.getInstance().attachToContext(context);
+        GeckoFontScaleListener.getInstance().attachToContext(context, settings);
 
         final GeckoThread.InitInfo info = new GeckoThread.InitInfo();
         info.args = settings.getArguments();
         info.extras = settings.getExtras();
         info.flags = flags;
         info.prefs = settings.getPrefsMap();
 
         if (!GeckoThread.init(info)) {