Commit 4df2cd8f authored by Henrique Nakashima's avatar Henrique Nakashima Committed by Commit Bot

Move SharedPrefs from FontSizePrefs to ChromePreferenceKeys.

Register them in ChromePreferenceKeys and use SharedPreferencesManager
consistently instead of SharedPreferences directly.

Bug: 1022108
Change-Id: I3a781c86f9cd280b2aaf5f5227fff18a7770f289
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2001718Reviewed-by: default avatarMichael Thiessen <mthiesse@chromium.org>
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Cr-Commit-Position: refs/heads/master@{#732904}
parent a27651c9
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
package org.chromium.chrome.browser.accessibility; package org.chromium.chrome.browser.accessibility;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
...@@ -15,6 +14,8 @@ import org.chromium.base.ObserverList; ...@@ -15,6 +14,8 @@ import org.chromium.base.ObserverList;
import org.chromium.base.ThreadUtils; import org.chromium.base.ThreadUtils;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.NativeMethods; import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
/** /**
* Singleton class for accessing these font size-related preferences: * Singleton class for accessing these font size-related preferences:
...@@ -37,9 +38,6 @@ public class FontSizePrefs { ...@@ -37,9 +38,6 @@ public class FontSizePrefs {
private static final float EPSILON = 0.001f; private static final float EPSILON = 0.001f;
static final String PREF_USER_SET_FORCE_ENABLE_ZOOM = "user_set_force_enable_zoom";
static final String PREF_USER_FONT_SCALE_FACTOR = "user_font_scale_factor";
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
private static FontSizePrefs sFontSizePrefs; private static FontSizePrefs sFontSizePrefs;
...@@ -103,10 +101,8 @@ public class FontSizePrefs { ...@@ -103,10 +101,8 @@ public class FontSizePrefs {
* Sets the userFontScaleFactor. This should be a value between .5 and 2. * Sets the userFontScaleFactor. This should be a value between .5 and 2.
*/ */
public void setUserFontScaleFactor(float userFontScaleFactor) { public void setUserFontScaleFactor(float userFontScaleFactor) {
SharedPreferences.Editor sharedPreferencesEditor = SharedPreferencesManager.getInstance().writeFloat(
ContextUtils.getAppSharedPreferences().edit(); ChromePreferenceKeys.FONT_USER_FONT_SCALE_FACTOR, userFontScaleFactor);
sharedPreferencesEditor.putFloat(PREF_USER_FONT_SCALE_FACTOR, userFontScaleFactor);
sharedPreferencesEditor.apply();
setFontScaleFactor(userFontScaleFactor * getSystemFontScale()); setFontScaleFactor(userFontScaleFactor * getSystemFontScale());
} }
...@@ -114,8 +110,9 @@ public class FontSizePrefs { ...@@ -114,8 +110,9 @@ public class FontSizePrefs {
* Returns the userFontScaleFactor. This is the value that should be displayed to the user. * Returns the userFontScaleFactor. This is the value that should be displayed to the user.
*/ */
public float getUserFontScaleFactor() { public float getUserFontScaleFactor() {
SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferences(); SharedPreferencesManager sharedPreferences = SharedPreferencesManager.getInstance();
float userFontScaleFactor = sharedPreferences.getFloat(PREF_USER_FONT_SCALE_FACTOR, 0f); float userFontScaleFactor =
sharedPreferences.readFloat(ChromePreferenceKeys.FONT_USER_FONT_SCALE_FACTOR, 0f);
if (userFontScaleFactor == 0f) { if (userFontScaleFactor == 0f) {
float fontScaleFactor = getFontScaleFactor(); float fontScaleFactor = getFontScaleFactor();
...@@ -129,9 +126,8 @@ public class FontSizePrefs { ...@@ -129,9 +126,8 @@ public class FontSizePrefs {
userFontScaleFactor = userFontScaleFactor =
MathUtils.clamp(fontScaleFactor / getSystemFontScale(), 0.5f, 2f); MathUtils.clamp(fontScaleFactor / getSystemFontScale(), 0.5f, 2f);
} }
SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit(); sharedPreferences.writeFloat(
sharedPreferencesEditor.putFloat(PREF_USER_FONT_SCALE_FACTOR, userFontScaleFactor); ChromePreferenceKeys.FONT_USER_FONT_SCALE_FACTOR, userFontScaleFactor);
sharedPreferencesEditor.apply();
} }
return userFontScaleFactor; return userFontScaleFactor;
} }
...@@ -175,17 +171,15 @@ public class FontSizePrefs { ...@@ -175,17 +171,15 @@ public class FontSizePrefs {
} }
private void setForceEnableZoom(boolean enabled, boolean fromUser) { private void setForceEnableZoom(boolean enabled, boolean fromUser) {
SharedPreferences.Editor sharedPreferencesEditor = SharedPreferencesManager.getInstance().writeBoolean(
ContextUtils.getAppSharedPreferences().edit(); ChromePreferenceKeys.FONT_USER_SET_FORCE_ENABLE_ZOOM, fromUser);
sharedPreferencesEditor.putBoolean(PREF_USER_SET_FORCE_ENABLE_ZOOM, fromUser);
sharedPreferencesEditor.apply();
FontSizePrefsJni.get().setForceEnableZoom( FontSizePrefsJni.get().setForceEnableZoom(
mFontSizePrefsAndroidPtr, FontSizePrefs.this, enabled); mFontSizePrefsAndroidPtr, FontSizePrefs.this, enabled);
} }
private boolean getUserSetForceEnableZoom() { private boolean getUserSetForceEnableZoom() {
return ContextUtils.getAppSharedPreferences().getBoolean( return SharedPreferencesManager.getInstance().readBoolean(
PREF_USER_SET_FORCE_ENABLE_ZOOM, false); ChromePreferenceKeys.FONT_USER_SET_FORCE_ENABLE_ZOOM, false);
} }
private void setFontScaleFactor(float fontScaleFactor) { private void setFontScaleFactor(float fontScaleFactor) {
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
package org.chromium.chrome.browser.accessibility; package org.chromium.chrome.browser.accessibility;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
...@@ -15,10 +14,11 @@ import org.junit.Rule; ...@@ -15,10 +14,11 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.chromium.base.ContextUtils;
import org.chromium.base.test.BaseJUnit4ClassRunner; import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.chrome.browser.accessibility.FontSizePrefs.FontSizePrefsObserver; import org.chromium.chrome.browser.accessibility.FontSizePrefs.FontSizePrefsObserver;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.chrome.test.ChromeBrowserTestRule; import org.chromium.chrome.test.ChromeBrowserTestRule;
import org.chromium.content_public.browser.test.util.TestThreadUtils; import org.chromium.content_public.browser.test.util.TestThreadUtils;
...@@ -42,10 +42,9 @@ public class FontSizePrefsTest { ...@@ -42,10 +42,9 @@ public class FontSizePrefsTest {
} }
private void resetSharedPrefs() { private void resetSharedPrefs() {
SharedPreferences.Editor editor = ContextUtils.getAppSharedPreferences().edit(); SharedPreferencesManager prefs = SharedPreferencesManager.getInstance();
editor.remove(FontSizePrefs.PREF_USER_SET_FORCE_ENABLE_ZOOM); prefs.removeKey(ChromePreferenceKeys.FONT_USER_SET_FORCE_ENABLE_ZOOM);
editor.remove(FontSizePrefs.PREF_USER_FONT_SCALE_FACTOR); prefs.removeKey(ChromePreferenceKeys.FONT_USER_FONT_SCALE_FACTOR);
editor.apply();
} }
@Test @Test
...@@ -148,8 +147,8 @@ public class FontSizePrefsTest { ...@@ -148,8 +147,8 @@ public class FontSizePrefsTest {
// Delete PREF_USER_FONT_SCALE_FACTOR. This simulates the condition just after upgrading to // Delete PREF_USER_FONT_SCALE_FACTOR. This simulates the condition just after upgrading to
// M51, when userFontScaleFactor was added. // M51, when userFontScaleFactor was added.
SharedPreferences.Editor editor = ContextUtils.getAppSharedPreferences().edit(); SharedPreferencesManager prefs = SharedPreferencesManager.getInstance();
editor.remove(FontSizePrefs.PREF_USER_FONT_SCALE_FACTOR).apply(); prefs.removeKey(ChromePreferenceKeys.FONT_USER_FONT_SCALE_FACTOR);
// Intial userFontScaleFactor should be set to fontScaleFactor / systemFontScale. // Intial userFontScaleFactor should be set to fontScaleFactor / systemFontScale.
Assert.assertEquals(1.5f, getUserFontScaleFactor(), EPSILON); Assert.assertEquals(1.5f, getUserFontScaleFactor(), EPSILON);
......
...@@ -393,6 +393,9 @@ public final class ChromePreferenceKeys { ...@@ -393,6 +393,9 @@ public final class ChromePreferenceKeys {
public static final String FLAGS_CACHED_TAB_GROUPS_ANDROID_ENABLED = public static final String FLAGS_CACHED_TAB_GROUPS_ANDROID_ENABLED =
"tab_group_android_enabled"; "tab_group_android_enabled";
public static final String FONT_USER_FONT_SCALE_FACTOR = "user_font_scale_factor";
public static final String FONT_USER_SET_FORCE_ENABLE_ZOOM = "user_set_force_enable_zoom";
/** Keys used to save settings related to homepage. */ /** Keys used to save settings related to homepage. */
public static final String HOMEPAGE_CUSTOM_URI = "homepage_custom_uri"; public static final String HOMEPAGE_CUSTOM_URI = "homepage_custom_uri";
public static final String HOMEPAGE_ENABLED = "homepage"; public static final String HOMEPAGE_ENABLED = "homepage";
...@@ -748,6 +751,8 @@ public final class ChromePreferenceKeys { ...@@ -748,6 +751,8 @@ public final class ChromePreferenceKeys {
FLAGS_CACHED_START_SURFACE_ENABLED, FLAGS_CACHED_START_SURFACE_ENABLED,
FLAGS_CACHED_SWAP_PIXEL_FORMAT_TO_FIX_CONVERT_FROM_TRANSLUCENT, FLAGS_CACHED_SWAP_PIXEL_FORMAT_TO_FIX_CONVERT_FROM_TRANSLUCENT,
FLAGS_CACHED_TAB_GROUPS_ANDROID_ENABLED, FLAGS_CACHED_TAB_GROUPS_ANDROID_ENABLED,
FONT_USER_FONT_SCALE_FACTOR,
FONT_USER_SET_FORCE_ENABLE_ZOOM,
HOMEPAGE_CUSTOM_URI, HOMEPAGE_CUSTOM_URI,
HOMEPAGE_ENABLED, HOMEPAGE_ENABLED,
HOMEPAGE_USE_DEFAULT_URI, HOMEPAGE_USE_DEFAULT_URI,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment