Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/accessibility/FontSizePrefs.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/accessibility/FontSizePrefs.java b/chrome/android/java/src/org/chromium/chrome/browser/accessibility/FontSizePrefs.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..daefb8971bb7c73bbb74a698005f74338eb1e9b5 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/accessibility/FontSizePrefs.java |
| @@ -0,0 +1,180 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.accessibility; |
| + |
| +import android.content.Context; |
| +import android.content.SharedPreferences; |
| +import android.preference.PreferenceManager; |
| + |
| +import org.chromium.base.CalledByNative; |
| +import org.chromium.base.NativeCall; |
| +import org.chromium.base.ThreadUtils; |
| +import org.chromium.chrome.browser.profiles.Profile; |
| + |
| +import java.util.HashMap; |
| +import java.util.Map; |
| + |
| +/** |
| + * Interface for Font Scale Factor, Force Enable Zoom, and User Set Force |
| + * Enable Zoom preferences. |
| + */ |
| +public class FontSizePrefs { |
| + |
| + public static final String PREF_FORCE_ENABLE_ZOOM = "force_enable_zoom"; |
| + public static final String PREF_TEXT_SCALE = "text_scale"; |
| + public static final String PREF_USER_SET_FORCE_ENABLE_ZOOM = "user_set_force_enable_zoom"; |
| + |
| + public static final float FORCE_ENABLE_ZOOM_THRESHOLD_MULTIPLIER = 1.3f; |
|
robliao
2014/07/25 16:49:19
How was this number determined? Discuss that in a
sunangel
2014/07/25 19:22:56
Done...comment was used from old AccessibilityPref
|
| + |
| + private long mFontSizePrefsAndroidPtr; |
| + private SharedPreferences mSharedPreferences; |
|
robliao
2014/07/25 16:49:19
Make the constructor initialized variables final.
sunangel
2014/07/25 19:22:56
Done.
|
| + private Map<Observer, FontSizePrefsObserverWrapper> mObserverMap; |
| + private static HashMap<Profile, FontSizePrefs> sFontSizeMap = |
| + new HashMap<Profile, FontSizePrefs>(); |
| + |
| + /* |
| + * Observer interface for observing changes in FontScaleFactor and ForceEnableZoom. |
| + * Note that this observer does not observe changes in UserSetForceEnableZoom/=. |
| + */ |
| + public interface Observer { |
| + void onChangeFontSize(float newFontSize); |
| + void onChangeForceEnableZoom(boolean enabled); |
| + } |
| + |
| + /* |
| + * Wrapper for FontSizePrefsObserverAndroid. |
| + */ |
| + public class FontSizePrefsObserverWrapper { |
| + private final Observer mFontSizePrefsObserver; |
| + private final long mNativeFontSizePrefsObserverWrapperPtr; |
| + |
| + public FontSizePrefsObserverWrapper(Observer observer) { |
| + mNativeFontSizePrefsObserverWrapperPtr = nativeInitObserverAndroid(); |
| + mFontSizePrefsObserver = observer; |
| + } |
| + |
| + public long getNativePtr() { |
| + return mNativeFontSizePrefsObserverWrapperPtr; |
| + } |
| + |
| + public void destroy() { |
| + nativeDestroyObserverAndroid(mNativeFontSizePrefsObserverWrapperPtr); |
| + } |
| + |
| + @CalledByNative("FontSizePrefsObserverWrapper") |
| + public void onChangeFontSize(float newFontSize) { |
| + mFontSizePrefsObserver.onChangeFontSize(newFontSize); |
| + } |
| + |
| + @CalledByNative("FontSizePrefsObserverWrapper") |
| + public void onChangeForceEnableZoom(boolean enabled) { |
| + mFontSizePrefsObserver.onChangeForceEnableZoom(enabled); |
| + } |
| + |
| + @NativeCall("FontSizePrefsObserverWrapper") |
| + private native long nativeInitObserverAndroid(); |
| + |
| + @NativeCall("FontSizePrefsObserverWrapper") |
| + private native void nativeDestroyObserverAndroid(long nativeFontSizePrefsObserverAndroid); |
| + } |
| + |
| + public FontSizePrefs(Profile profile, Context context) { |
| + mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); |
| + mFontSizePrefsAndroidPtr = nativeInit(profile); |
| + mObserverMap = new HashMap<Observer, FontSizePrefsObserverWrapper>(); |
| + } |
| + |
| + public static FontSizePrefs getForProfile(Profile profile, Context context) { |
| + ThreadUtils.assertOnUiThread(); |
| + FontSizePrefs fontSizePrefs = sFontSizeMap.get(profile); |
| + if (fontSizePrefs == null) { |
| + fontSizePrefs = new FontSizePrefs(profile, context); |
| + sFontSizeMap.put(profile, fontSizePrefs); |
| + } |
| + return fontSizePrefs; |
| + } |
| + |
| + public void addObserver(Observer obs) { |
| + FontSizePrefsObserverWrapper wrappedObserver = new |
| + FontSizePrefsObserverWrapper(obs); |
| + nativeAddObserver(mFontSizePrefsAndroidPtr, wrappedObserver.getNativePtr()); |
| + mObserverMap.put(obs, wrappedObserver); |
| + } |
| + |
| + public void removeObserver(Observer obs) { |
| + FontSizePrefsObserverWrapper wrappedObserver = mObserverMap.remove(obs); |
| + if (wrappedObserver != null) { |
|
robliao
2014/07/25 16:49:19
When are you expecting this to be null? This sound
sunangel
2014/07/25 19:22:56
If someone called for example, removeObserver(new
|
| + nativeRemoveObserver(mFontSizePrefsAndroidPtr, wrappedObserver.getNativePtr()); |
| + wrappedObserver.destroy(); |
| + } |
| + } |
| + |
| + public boolean getPrefUserSetForceEnableZoom() { |
| + return mSharedPreferences.getBoolean(PREF_USER_SET_FORCE_ENABLE_ZOOM, |
| + false); |
| + } |
| + |
| + public void setFontScaleFactor(float font) { |
| + float oldTextScale = getFontScaleFactor(); |
| + nativeSetFontScaleFactor(mFontSizePrefsAndroidPtr, font); |
|
robliao
2014/07/25 16:49:19
Consider having a corresponding java side setFontS
sunangel
2014/07/25 19:22:56
Done.
|
| + float newTextScale = getFontScaleFactor(); |
| + float threshold = FORCE_ENABLE_ZOOM_THRESHOLD_MULTIPLIER; |
| + if (oldTextScale < threshold |
| + && newTextScale >= threshold |
| + && !getForceEnableZoom()) { |
| + // If the slider is moved from below the threshold to above the threshold, we check |
| + // force enable zoom even if the user has previously set it. |
| + setForceEnableZoom(true); |
| + onUserSetForceEnableZoomChanged(false); |
| + } else if (oldTextScale >= threshold |
| + && newTextScale < threshold |
| + && !getPrefUserSetForceEnableZoom()) { |
| + // If the slider is moved from above the threshold to below it, we only |
| + // uncheck force enable zoom if it was not set manually. |
| + setForceEnableZoom(false); |
| + } |
| + } |
| + |
| + public void onUserSetForceEnableZoomChanged(boolean enabled) { |
| + SharedPreferences.Editor sharedPreferencesEditor = mSharedPreferences.edit(); |
| + sharedPreferencesEditor.putBoolean(PREF_USER_SET_FORCE_ENABLE_ZOOM, enabled); |
| + sharedPreferencesEditor.apply(); |
| + } |
| + |
| + public void onDestroy() { |
| + nativeDestroy(mFontSizePrefsAndroidPtr); |
| + } |
| + |
| + public void setForceEnableZoom(boolean enabled) { |
| + nativeSetForceEnableZoom(mFontSizePrefsAndroidPtr, enabled); |
| + } |
| + |
| + public float getFontScaleFactor() { |
| + return nativeGetFontScaleFactor(mFontSizePrefsAndroidPtr); |
| + } |
| + |
| + public boolean getForceEnableZoom() { |
| + return nativeGetForceEnableZoom(mFontSizePrefsAndroidPtr); |
| + } |
| + |
| + private native void nativeAddObserver(long nativeFontSizePrefsAndroid, |
| + long nativeObserverPtr); |
| + |
| + private native void nativeRemoveObserver(long nativeFontSizePrefsAndroid, |
| + long nativeObserverPtr); |
| + |
| + private native long nativeInit(Profile profile); |
| + |
| + private native void nativeSetFontScaleFactor(long nativeFontSizePrefsAndroid, float font); |
| + |
| + private native float nativeGetFontScaleFactor(long nativeFontSizePrefsAndroid); |
| + |
| + private native boolean nativeGetForceEnableZoom(long nativeFontSizePrefsAndroid); |
| + |
| + private native void nativeSetForceEnableZoom(long nativeFontSizePrefsAndroid, boolean enabled); |
| + |
| + private native void nativeDestroy(long nativeFontSizePrefsAndroid); |
| + |
| +} |