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..5f3433cce5221500e03b816b736edb5f8f669147 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/accessibility/FontSizePrefs.java |
| @@ -0,0 +1,182 @@ |
| +// 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. |
|
nyquist
2014/08/06 23:39:25
This is not an interface. Could you try to describ
sunangel
2014/08/07 18:50:53
Okay tried to describe it better.
They are not so
|
| + */ |
| +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"; |
| + |
| + private final long mFontSizePrefsAndroidPtr; |
| + private final Profile mProfile; |
| + private final SharedPreferences mSharedPreferences; |
| + private final Map<Observer, FontSizePrefsObserverWrapper> mObserverMap; |
| + private final static HashMap<Profile, FontSizePrefs> sFontSizeMap = |
|
nyquist
2014/08/06 23:39:25
static before members.
sunangel
2014/08/07 18:50:53
Done.
|
| + new HashMap<Profile, FontSizePrefs>(); |
| + |
| + /* |
|
nyquist
2014/08/06 23:39:25
/** throughout this file for these style comments.
sunangel
2014/08/07 18:50:53
Done.
|
| + * Observer interface for observing changes in FontScaleFactor and ForceEnableZoom. Note that |
| + * this observer does not observe changes in UserSetForceEnableZoom. |
|
nyquist
2014/08/06 23:39:25
Why? This sounds unreasonable.
sunangel
2014/08/07 18:50:53
Haha okay changed this.
On 2014/08/06 23:39:25, n
|
| + */ |
| + public interface Observer { |
| + void onChangeFontSize(float newFontSize); |
| + void onChangeForceEnableZoom(boolean enabled); |
| + } |
| + |
| + /* |
| + * Wrapper for FontSizePrefsObserverAndroid. |
| + */ |
| + public static class FontSizePrefsObserverWrapper { |
|
nyquist
2014/08/06 23:39:25
can this class be private?
sunangel
2014/08/07 18:50:53
Done.
|
| + 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) { |
|
nyquist
2014/08/06 23:39:25
private?
sunangel
2014/08/07 18:50:53
Done.
|
| + mProfile = profile; |
| + mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); |
|
nyquist
2014/08/06 23:39:26
Shouldn't there be an observer for the shared pref
sunangel
2014/08/07 18:50:53
Done.
|
| + mFontSizePrefsAndroidPtr = nativeInit(profile); |
| + mObserverMap = new HashMap<Observer, FontSizePrefsObserverWrapper>(); |
| + } |
| + |
| + /* |
| + * Returns the FontSizePrefs corresponding to the inputted Profile. If no FontSizePrefs existed, |
| + * this method will create one. |
| + */ |
| + 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; |
| + } |
| + |
| + /* |
| + * Adds the observer to listen for Font Scale and Force Enable Zoom preferences. |
| + * @return true if the observerMap was changed as a result of the call. |
| + */ |
| + public boolean addObserver(Observer obs) { |
| + if (mObserverMap.containsKey(obs)) return false; |
| + FontSizePrefsObserverWrapper wrappedObserver = |
| + new FontSizePrefsObserverWrapper(obs); |
| + nativeAddObserver(mFontSizePrefsAndroidPtr, wrappedObserver.getNativePtr()); |
| + mObserverMap.put(obs, wrappedObserver); |
| + return true; |
| + } |
| + |
| + /* |
| + * Removes the observer and unregisters it from Font Scale and Force Enable Zoom changes. |
| + * @return true if an observer was removed as a result of the call. |
| + */ |
| + public boolean removeObserver(Observer obs) { |
| + FontSizePrefsObserverWrapper wrappedObserver = mObserverMap.remove(obs); |
| + if (wrappedObserver == null) return false; |
| + nativeRemoveObserver(mFontSizePrefsAndroidPtr, wrappedObserver.getNativePtr()); |
| + wrappedObserver.destroy(); |
| + return true; |
| + } |
| + |
| + /* |
| + * Sets UserSetForceEnableZoom. This is the only one of three preferences stored through |
| + * SharedPreferences. |
| + */ |
| + public void setUserSetForceEnableZoom(boolean enabled) { |
| + SharedPreferences.Editor sharedPreferencesEditor = mSharedPreferences.edit(); |
| + sharedPreferencesEditor.putBoolean(PREF_USER_SET_FORCE_ENABLE_ZOOM, enabled); |
|
nyquist
2014/08/06 23:39:25
Are these per profile? I don't think they are, and
sunangel
2014/08/07 18:50:53
Done.
|
| + sharedPreferencesEditor.apply(); |
| + } |
| + |
| + /* |
| + * Returns true if user has manually set ForceEnableZoom and false otherwise. |
| + */ |
| + public boolean getUserSetForceEnableZoom() { |
| + return mSharedPreferences.getBoolean(PREF_USER_SET_FORCE_ENABLE_ZOOM, |
| + false); |
| + } |
| + |
| + public void setFontScaleFactor(float fontScaleFactor) { |
| + nativeSetFontScaleFactor(mFontSizePrefsAndroidPtr, fontScaleFactor); |
| + } |
| + |
| + public float getFontScaleFactor() { |
| + return nativeGetFontScaleFactor(mFontSizePrefsAndroidPtr); |
| + } |
| + |
| + public void setForceEnableZoom(boolean enabled) { |
| + nativeSetForceEnableZoom(mFontSizePrefsAndroidPtr, enabled); |
| + } |
| + |
| + public boolean getForceEnableZoom() { |
| + return nativeGetForceEnableZoom(mFontSizePrefsAndroidPtr); |
| + } |
| + |
| + public void destroy() { |
| + sFontSizeMap.remove(mProfile); |
| + nativeDestroy(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 nativeDestroy(long nativeFontSizePrefsAndroid); |
| + |
| + 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); |
| +} |