diff --git a/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java b/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java index 801f59526fbc..fc765f5f132c 100644 --- a/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java +++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java @@ -11,6 +11,7 @@ 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; @@ -49,6 +50,7 @@ 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; @@ -72,7 +74,8 @@ 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"; @@ -212,7 +215,7 @@ public class GeckoApplication extends Application public void onApplicationForeground() { if (mIsInitialResume) { GeckoBatteryManager.getInstance().start(this); - GeckoFontScaleListener.getInstance().attachToContext(this); + initFontScaleListener(); GeckoNetworkManager.getInstance().start(this); mIsInitialResume = false; } else if (mPausedGecko) { @@ -224,6 +227,13 @@ public class GeckoApplication extends Application 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; @@ -422,6 +432,7 @@ public class GeckoApplication extends Application "Image:SetAs", "Profile:Create", null); + GeckoSharedPrefs.forApp(this).unregisterOnSharedPreferenceChangeListener(this); GeckoService.unregister(); } @@ -909,4 +920,12 @@ public class GeckoApplication extends Application 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); + } + } } diff --git a/mobile/android/base/java/org/mozilla/gecko/GeckoFontScaleListener.java b/mobile/android/base/java/org/mozilla/gecko/GeckoFontScaleListener.java index c8b0eccb09c9..bbd7f6db16bd 100644 --- a/mobile/android/base/java/org/mozilla/gecko/GeckoFontScaleListener.java +++ b/mobile/android/base/java/org/mozilla/gecko/GeckoFontScaleListener.java @@ -5,13 +5,11 @@ 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; @@ -19,8 +17,7 @@ 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"; @@ -35,6 +32,7 @@ class GeckoFontScaleListener private Context mApplicationContext; private boolean mAttached; + private boolean mEnabled; private boolean mRunning; public static GeckoFontScaleListener getInstance() { @@ -54,11 +52,9 @@ class GeckoFontScaleListener return; } - mApplicationContext = context.getApplicationContext(); - SharedPreferences prefs = GeckoSharedPrefs.forApp(mApplicationContext); - prefs.registerOnSharedPreferenceChangeListener(this); - onPrefChange(prefs); mAttached = true; + mApplicationContext = context.getApplicationContext(); + onEnabledChange(); } public void detachFromContext() { @@ -69,12 +65,33 @@ class GeckoFontScaleListener 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; @@ -104,10 +121,10 @@ class GeckoFontScaleListener 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; } @@ -116,29 +133,9 @@ class GeckoFontScaleListener 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); - } }