Bug 1460874 - Expose font size/inflation options via GeckoRuntimeSettings. r?snorp draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Sun, 15 Jul 2018 20:16:12 +0200
changeset 818586 2d4ae7dc58e39d55695f296ff3952a4d9a3e5873
parent 818585 fd39721f5f8972722e1b863133e70f3e618c6ec4
push id116286
push usermozilla@buttercookie.de
push dateSun, 15 Jul 2018 19:05:58 +0000
reviewerssnorp
bugs1460874
milestone63.0a1
Bug 1460874 - Expose font size/inflation options via GeckoRuntimeSettings. r?snorp MozReview-Commit-ID: K67j9l4EsOm
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntimeSettings.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntimeSettings.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntimeSettings.java
@@ -224,16 +224,40 @@ public final class GeckoRuntimeSettings 
          * @param enabled A flag determining whether or not web console messages should be
          *                printed to logcat.
          * @return The builder instance.
          */
         public @NonNull Builder consoleOutput(boolean enabled) {
             mSettings.mConsoleOutput.set(enabled);
             return this;
         }
+
+        /**
+         * Set a font size factor that will operate as a global default text zoom.
+         *
+         * @param fontSizeFactor The factor to be used for scaling all text. Setting a value of 0
+         *                       disables both this feature and font inflation.
+         * @return The builder instance.
+         */
+        public @NonNull Builder fontSizeFactor(float fontSizeFactor) {
+            mSettings.setFontSizeFactor(fontSizeFactor);
+            return this;
+        }
+
+        /**
+         * Set whether or not font inflation for non mobile-friendly pages should be enabled.
+         * The magnitude of font inflation applied depends on the font size factor currently in use.
+         *
+         * @param enabled A flag determining whether or not font inflation should be enabled.
+         * @return The builder instance.
+         */
+        public @NonNull Builder fontInflation(boolean enabled) {
+            mSettings.setFontInflationEnabled(enabled);
+            return this;
+        }
     }
 
     /* package */ GeckoRuntime runtime;
     /* package */ boolean mUseContentProcess;
     /* package */ String[] mArgs;
     /* package */ Bundle mExtras;
     /* package */ int prefCount;
 
@@ -280,24 +304,29 @@ public final class GeckoRuntimeSettings 
         "network.cookie.lifetimePolicy", COOKIE_LIFETIME_NORMAL);
     /* package */ Pref<Integer> mCookieLifetimeDays = new Pref<Integer>(
         "network.cookie.lifetime.days", 90);
     /* package */ Pref<String> mTrackingProtection = new Pref<String>(
         "urlclassifier.trackingTable",
         TrackingProtection.buildPrefValue(TrackingProtectionDelegate.CATEGORY_ALL));
     /* package */ Pref<Boolean> mConsoleOutput = new Pref<Boolean>(
         "geckoview.console.enabled", false);
+    /* package */ Pref<Integer> mFontSizeFactor = new Pref<>(
+            "font.size.systemFontScale", 100);
+    /* package */ Pref<Integer> mFontInflationMinTwips = new Pref<>(
+            "font.size.inflation.minTwips", 0);
 
     /* package */ boolean mNativeCrashReporting;
     /* package */ boolean mJavaCrashReporting;
     /* package */ boolean mDebugPause;
 
     private final Pref<?>[] mPrefs = new Pref<?>[] {
         mCookieBehavior, mCookieLifetime, mCookieLifetimeDays, mConsoleOutput,
-        mJavaScript, mRemoteDebugging, mTrackingProtection, mWebFonts
+        mJavaScript, mFontInflationMinTwips, mFontSizeFactor, mRemoteDebugging,
+        mTrackingProtection, mWebFonts
     };
 
     /* package */ GeckoRuntimeSettings() {
         this(null);
     }
 
     /* package */ GeckoRuntimeSettings(final @Nullable GeckoRuntimeSettings settings) {
         if (BuildConfig.DEBUG && prefCount != mPrefs.length) {
@@ -609,16 +638,71 @@ public final class GeckoRuntimeSettings 
      * Get whether or not web console messages are sent to logcat.
      *
      * @return This GeckoRuntimeSettings instance.
      */
     public boolean getConsoleOutputEnabled() {
         return mConsoleOutput.get();
     }
 
+    private static int FONT_INFLATION_BASE_VALUE = 120;
+
+    /**
+     * Set a font size factor that will operate as a global default text zoom.
+     *
+     * @param fontSizeFactor The factor to be used for scaling all text. Setting a value of 0
+     *                       disables both this feature and font inflation.
+     * @return This GeckoRuntimeSettings instance.
+     */
+    public @NonNull GeckoRuntimeSettings setFontSizeFactor(float fontSizeFactor) {
+        if (fontSizeFactor < 0) {
+            throw new IllegalArgumentException("fontSizeFactor cannot be < 0");
+        }
+
+        final int fontSizePercentage = Math.round(fontSizeFactor * 100);
+        mFontSizeFactor.set(Math.round(fontSizePercentage));
+        if (getFontInflationEnabled()) {
+            final int scaledFontInflation = Math.round(FONT_INFLATION_BASE_VALUE * fontSizeFactor);
+            mFontInflationMinTwips.set(scaledFontInflation);
+        }
+        return this;
+    }
+
+    /**
+     * Gets the currently applied font size factor.
+     *
+     * @return The currently applied font size factor.
+     */
+    public float getFontSizeFactor() {
+        return mFontSizeFactor.get() / 100f;
+    }
+
+    /**
+     * Set whether or not font inflation for non mobile-friendly pages should be enabled.
+     * The magnitude of font inflation applied depends on the font size factor currently in use.
+     *
+     * @param enabled A flag determining whether or not font inflation should be enabled.
+     * @return This GeckoRuntimeSettings instance.
+     */
+    public @NonNull GeckoRuntimeSettings setFontInflationEnabled(boolean enabled) {
+        final int minTwips =
+                enabled ? Math.round(FONT_INFLATION_BASE_VALUE * getFontSizeFactor()) : 0;
+        mFontInflationMinTwips.set(minTwips);
+        return this;
+    }
+
+    /**
+     * Get whether or not font inflation for non mobile-friendly pages is currently enabled.
+     *
+     * @return True if font inflation is enabled.
+     */
+    public boolean getFontInflationEnabled() {
+        return mFontInflationMinTwips.get() > 0;
+    }
+
     @Override // Parcelable
     public int describeContents() {
         return 0;
     }
 
     @Override // Parcelable
     public void writeToParcel(Parcel out, int flags) {
         ParcelableUtils.writeBoolean(out, mUseContentProcess);