Bug 1460874 - Part 5: Move out SharedPreferences watching. r=geckoview-reviewers,snorp
☠☠ backed out by cd616c3e3eea ☠ ☠
authorJan Henning <jh+bugzilla@buttercookie.de>
Wed, 13 Feb 2019 20:10:23 +0000
changeset 458947 81c4e88003a5
parent 458946 02f24dec5ae4
child 458948 78f59df737fc
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 5: Move out SharedPreferences watching. r=geckoview-reviewers,snorp For easier testing in Fennec, we want to continue watching the respective SharedPreferences key and toggle the listener in response to *that* instead of directly wiring it into our Settings menu (GeckoPreferences), however because that functionality is Fennec-specific, we move that logic out of the font scale listener itself. In conjunction with this, we also decouple the enabled state of the listener from its attached state. The enabled state can now be toggled at all times, but unless the listener is also attached to a context, it simply won't have any practical effect. Differential Revision: https://phabricator.services.mozilla.com/D17746
mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java
mobile/android/base/java/org/mozilla/gecko/GeckoFontScaleListener.java
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java
@@ -6,16 +6,17 @@ package org.mozilla.gecko;
 
 import android.Manifest;
 import android.app.Activity;
 import android.app.Application;
 import android.app.Service;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.content.res.Configuration;
 import android.graphics.Bitmap;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Environment;
 import android.os.Parcelable;
 import android.os.Process;
 import android.os.SystemClock;
@@ -44,16 +45,17 @@ import org.mozilla.gecko.icons.Icons;
 import org.mozilla.gecko.lwt.LightweightTheme;
 import org.mozilla.gecko.mdns.MulticastDNSManager;
 import org.mozilla.gecko.media.AudioFocusAgent;
 import org.mozilla.gecko.mozglue.SafeIntent;
 import org.mozilla.gecko.notifications.NotificationClient;
 import org.mozilla.gecko.notifications.NotificationHelper;
 import org.mozilla.gecko.permissions.Permissions;
 import org.mozilla.gecko.preferences.DistroSharedPrefsImport;
+import org.mozilla.gecko.preferences.GeckoPreferences;
 import org.mozilla.gecko.pwa.PwaUtils;
 import org.mozilla.gecko.telemetry.TelemetryBackgroundReceiver;
 import org.mozilla.gecko.util.ActivityResultHandler;
 import org.mozilla.gecko.util.BitmapUtils;
 import org.mozilla.gecko.util.BundleEventListener;
 import org.mozilla.gecko.util.EventCallback;
 import org.mozilla.gecko.util.GeckoBundle;
 import org.mozilla.gecko.util.HardwareUtils;
@@ -67,17 +69,18 @@ import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.InputStream;
 import java.io.IOException;
 import java.lang.reflect.Method;
 import java.net.URL;
 import java.util.UUID;
 
 public class GeckoApplication extends Application
-                              implements HapticFeedbackDelegate {
+                              implements HapticFeedbackDelegate,
+                                         SharedPreferences.OnSharedPreferenceChangeListener {
     private static final String LOG_TAG = "GeckoApplication";
     public static final String ACTION_DEBUG = "org.mozilla.gecko.DEBUG";
     private static final String MEDIA_DECODING_PROCESS_CRASH = "MEDIA_DECODING_PROCESS_CRASH";
 
     private boolean mInBackground;
     private boolean mPausedGecko;
     private boolean mIsInitialResume;
 
@@ -207,28 +210,35 @@ public class GeckoApplication extends Ap
         });
 
         GeckoNetworkManager.getInstance().stop();
     }
 
     public void onApplicationForeground() {
         if (mIsInitialResume) {
             GeckoBatteryManager.getInstance().start(this);
-            GeckoFontScaleListener.getInstance().attachToContext(this);
+            initFontScaleListener();
             GeckoNetworkManager.getInstance().start(this);
             mIsInitialResume = false;
         } else if (mPausedGecko) {
             GeckoThread.onResume();
             mPausedGecko = false;
             GeckoNetworkManager.getInstance().start(this);
         }
 
         mInBackground = false;
     }
 
+    private void initFontScaleListener() {
+        final SharedPreferences prefs = GeckoSharedPrefs.forApp(this);
+        prefs.registerOnSharedPreferenceChangeListener(this);
+        onSharedPreferenceChanged(prefs, GeckoPreferences.PREFS_SYSTEM_FONT_SIZE);
+        GeckoFontScaleListener.getInstance().attachToContext(this);
+    }
+
     private static GeckoRuntime sGeckoRuntime;
     public static GeckoRuntime getRuntime() {
         return sGeckoRuntime;
     }
 
     public static GeckoRuntime ensureRuntime(@NonNull Context context) {
         if (sGeckoRuntime != null) {
             return sGeckoRuntime;
@@ -417,16 +427,17 @@ public class GeckoApplication extends Ap
                 "Share:Text",
                 null);
         EventDispatcher.getInstance().unregisterBackgroundThreadListener(mListener,
                 "PushServiceAndroidGCM:Configure",
                 "Bookmark:Insert",
                 "Image:SetAs",
                 "Profile:Create",
                 null);
+        GeckoSharedPrefs.forApp(this).unregisterOnSharedPreferenceChangeListener(this);
 
         GeckoService.unregister();
     }
 
     /* package */ boolean initPushService() {
         // It's fine to throw GCM initialization onto a background thread; the registration process requires
         // network access, so is naturally asynchronous.  This, of course, races against Gecko page load of
         // content requiring GCM-backed services, like Web Push.  There's nothing to be done here.
@@ -904,9 +915,17 @@ public class GeckoApplication extends Ap
     @Override // HapticFeedbackDelegate
     public void performHapticFeedback(final int effect) {
         final Activity currentActivity =
                 GeckoActivityMonitor.getInstance().getCurrentActivity();
         if (currentActivity != null) {
             currentActivity.getWindow().getDecorView().performHapticFeedback(effect);
         }
     }
+
+    @Override // OnSharedPreferenceChangeListener
+    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
+        if (GeckoPreferences.PREFS_SYSTEM_FONT_SIZE.equals(key)) {
+            final boolean enabled = prefs.getBoolean(GeckoPreferences.PREFS_SYSTEM_FONT_SIZE, false);
+            GeckoFontScaleListener.getInstance().setEnabled(enabled);
+        }
+    }
 }
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoFontScaleListener.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoFontScaleListener.java
@@ -1,45 +1,43 @@
 /* -*- 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.preferences.GeckoPreferences;
 import org.mozilla.gecko.util.ThreadUtils;
 
 import android.annotation.SuppressLint;
 import android.content.ContentResolver;
 import android.content.Context;
-import android.content.SharedPreferences;
 import android.database.ContentObserver;
 import android.net.Uri;
 import android.provider.Settings;
 import android.support.annotation.UiThread;
 import android.util.Log;
 
 class GeckoFontScaleListener
-        extends ContentObserver
-        implements SharedPreferences.OnSharedPreferenceChangeListener {
+        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 boolean mAttached;
+    private boolean mEnabled;
     private boolean mRunning;
 
     public static GeckoFontScaleListener getInstance() {
         return sInstance;
     }
 
     private GeckoFontScaleListener() {
         // Ensure the ContentObserver callback runs on the UI thread.
@@ -49,37 +47,56 @@ class GeckoFontScaleListener
     public void attachToContext(final Context context) {
         ThreadUtils.assertOnUiThread();
 
         if (mAttached) {
             Log.w(LOGTAG, "Already attached!");
             return;
         }
 
+        mAttached = true;
         mApplicationContext = context.getApplicationContext();
-        SharedPreferences prefs = GeckoSharedPrefs.forApp(mApplicationContext);
-        prefs.registerOnSharedPreferenceChangeListener(this);
-        onPrefChange(prefs);
-        mAttached = true;
+        onEnabledChange();
     }
 
     public void detachFromContext() {
         ThreadUtils.assertOnUiThread();
 
         if (!mAttached) {
             Log.w(LOGTAG, "Already detached!");
             return;
         }
 
-        GeckoSharedPrefs.forApp(mApplicationContext).unregisterOnSharedPreferenceChangeListener(this);
         stop();
         mApplicationContext = null;
         mAttached = false;
     }
 
+    public void setEnabled(boolean enabled) {
+        ThreadUtils.assertOnUiThread();
+        mEnabled = enabled;
+        onEnabledChange();
+    }
+
+    public boolean getEnabled() {
+        return mEnabled;
+    }
+
+    private void onEnabledChange() {
+        if (!mAttached) {
+            return;
+        }
+
+        if (mEnabled) {
+            start();
+        } else {
+            stop();
+        }
+    }
+
     private void start() {
         if (mRunning) {
             return;
         }
 
         ContentResolver contentResolver = mApplicationContext.getContentResolver();
         Uri fontSizeSetting = Settings.System.getUriFor(Settings.System.FONT_SCALE);
         contentResolver.registerContentObserver(fontSizeSetting, false, this);
@@ -99,46 +116,26 @@ class GeckoFontScaleListener
 
         mRunning = false;
     }
 
     private void onSystemFontScaleChange(final ContentResolver contentResolver, boolean stopping) {
         float fontScale;
         int fontInflation;
 
-        if (!stopping) { // Pref was flipped to "On" or system font scale changed.
+        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);
-        } else { // Pref was flipped to "Off".
+        } else { // We were turned off.
             fontScale = DEFAULT_FONT_SCALE;
             fontInflation = FONT_INFLATION_OFF;
         }
 
         PrefsHelper.setPref(PREF_FONT_INFLATION, fontInflation);
         PrefsHelper.setPref(PREF_SYSTEM_FONT_SCALE, Math.round(fontScale * 100));
     }
 
-    private void onPrefChange(final SharedPreferences prefs) {
-        boolean useSystemFontScale = prefs.getBoolean(GeckoPreferences.PREFS_SYSTEM_FONT_SIZE, false);
-
-        if (useSystemFontScale) {
-            start();
-        } else {
-            stop();
-        }
-    }
-
     @UiThread // See constructor.
     @Override
     public void onChange(boolean selfChange) {
         onSystemFontScaleChange(mApplicationContext.getContentResolver(), false);
     }
-
-    @UiThread // According to the docs.
-    @Override
-    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
-        if (!GeckoPreferences.PREFS_SYSTEM_FONT_SIZE.equals(key)) {
-            return;
-        }
-
-        onPrefChange(sharedPreferences);
-    }
 }