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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jan Henning 2019-02-14 20:42:30 +00:00
Родитель 2b75da3bc6
Коммит b3ee3af690
2 изменённых файлов: 49 добавлений и 33 удалений

Просмотреть файл

@ -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);
}
}
}

Просмотреть файл

@ -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);
}
}