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..5af990c3bc1bb0f00616ebb847596e2014e960ad |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/accessibility/FontSizePrefs.java |
| @@ -0,0 +1,190 @@ |
| +// 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"; |
| + |
| + /** |
| + * This value gives the threshold beyond which force enable zoom is automatically turned on. It |
| + * is chosen such that force enable zoom will be activated when the accessibility large text |
| + * setting is on (i.e. this value should be the same as or lesser than the font size scale used |
| + * by accessiblity large text). |
| + */ |
| + public static final float FORCE_ENABLE_ZOOM_THRESHOLD_MULTIPLIER = 1.3f; |
| + |
| + private final long mFontSizePrefsAndroidPtr; |
| + private final SharedPreferences mSharedPreferences; |
| + private final 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 23:49:34
When is this expected to be null?
sunangel
2014/07/28 21:17:20
If someone calls removeObserver(new Observer)...Id
robliao
2014/07/30 22:09:07
It should be okay to crash here. Callers should kn
sunangel
2014/07/31 00:29:26
Tommy asked me to add this in because it was origi
|
| + nativeRemoveObserver(mFontSizePrefsAndroidPtr, wrappedObserver.getNativePtr()); |
| + wrappedObserver.destroy(); |
| + } |
| + } |
| + |
| + public boolean getPrefUserSetForceEnableZoom() { |
| + return mSharedPreferences.getBoolean(PREF_USER_SET_FORCE_ENABLE_ZOOM, |
| + false); |
| + } |
| + |
| + public void setAndProcessFontScaleFactor(float font) { |
|
robliao
2014/07/25 23:49:34
fontScaleFactor
Also, add a javadoc for this meth
sunangel
2014/07/28 21:17:19
if you meant change the param name to fontScaleFac
|
| + float oldTextScale = getFontScaleFactor(); |
| + setFontScaleFactor(font); |
|
robliao
2014/07/25 23:49:34
Okay, now that I've had more time to look at this,
sunangel
2014/07/28 21:17:19
Yes, can do that. On 2014/07/25 23:49:34, robliao
|
| + float newTextScale = getFontScaleFactor(); |
| + float threshold = FORCE_ENABLE_ZOOM_THRESHOLD_MULTIPLIER; |
| + if (oldTextScale < threshold |
|
robliao
2014/07/25 23:49:34
I think you can take both of these...
oldTextScale
sunangel
2014/07/28 21:17:19
I don't think this works...
For example, old=0.5,
robliao
2014/07/28 21:27:50
Ah, gotcha. I think I now get what this code does.
|
| + && 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. |
|
robliao
2014/07/25 23:49:34
We can gather this much from the code. Is there an
sunangel
2014/07/28 21:17:20
I am not quite sure but these were the comments in
|
| + 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 setFontScaleFactor(float font) { |
| + nativeSetFontScaleFactor(mFontSizePrefsAndroidPtr, font); |
| + } |
| + |
| + public void onUserSetForceEnableZoomChanged(boolean enabled) { |
|
robliao
2014/07/25 23:49:34
Does this need to be public?
sunangel
2014/07/28 21:17:19
Yes, because inner class cannot distinguish when t
|
| + 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() { |
|
robliao
2014/07/25 23:49:34
Order these so that related gets and sets are grou
sunangel
2014/07/28 21:17:20
Done.
|
| + 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); |
| + |
| +} |