Accessibility tweaks

 Min font size now shows pt value
 Text size replaced with text zoom

Change-Id: Ic5906be094425bc5099a17571868c7a994a6cb86
diff --git a/res/layout/font_size_widget.xml b/res/layout/font_size_widget.xml
new file mode 100644
index 0000000..296f5bf
--- /dev/null
+++ b/res/layout/font_size_widget.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/text"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:minWidth="50sp"
+    android:textAppearance="?android:attr/textAppearanceSmall"
+    android:textColor="?android:attr/textColorSecondary"
+    android:layout_gravity="center_vertical"
+/>
+
diff --git a/res/layout/min_font_size.xml b/res/layout/min_font_size.xml
deleted file mode 100644
index d4d1314..0000000
--- a/res/layout/min_font_size.xml
+++ /dev/null
@@ -1,38 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2011 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
-
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="wrap_content"
-    android:orientation="vertical"
-    android:paddingRight="6dip"
-    android:paddingTop="6dip"
-    android:paddingBottom="6dip"
-    android:paddingLeft="8dip">
-
-    <TextView
-        android:text="@string/pref_min_font_size"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:textAppearance="?android:attr/textAppearanceMedium" />
-
-    <SeekBar
-        android:id="@+id/seekbar"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content" />
-
-</LinearLayout>
-
diff --git a/res/values/strings.xml b/res/values/strings.xml
index d2515d9..c846965 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -510,9 +510,7 @@
     <string name="pref_security_accept_cookies">Accept cookies</string>
     <!-- Settings summary -->
     <string name="pref_security_accept_cookies_summary">Allow sites to save and read \"cookie\" data</string>
-    <!-- Settings label -->
-    <string name="pref_text_size">Text size</string>
-    <!-- Settings text size options; appear in Text size dialog box -->
+    <!-- Settings text size options; appear in Text size dialog box [CHAR LIMIT=30] -->
     <string-array name="pref_text_size_choices">
         <item>Tiny</item>
         <item>Small</item>
@@ -520,18 +518,12 @@
         <item>Large</item>
         <item>Huge</item>
     </string-array>
-    <!-- Title of dialog for setting the text size -->
-    <string name="pref_text_size_dialogtitle">Text size</string>
-    <!-- Do not translate -->
-    <string-array name="pref_text_size_values" translatable="false">
-        <item><xliff:g>SMALLEST</xliff:g></item>
-        <item><xliff:g>SMALLER</xliff:g></item>
-        <item><xliff:g>NORMAL</xliff:g></item>
-        <item><xliff:g>LARGER</xliff:g></item>
-        <item><xliff:g>LARGEST</xliff:g></item>
-    </string-array>
     <!-- Label for minimum font size [CHAR LIMIT=30] -->
     <string name="pref_min_font_size">Minimum font size</string>
+    <!-- Label for the current minimum font size value [CHAR LIMIT=6] -->
+    <string name="pref_min_font_size_value"><xliff:g id="font_size">%d</xliff:g>pt</string>
+    <!-- Label for text zoom (percent) [CHAR LIMIT=30] -->
+    <string name="pref_text_zoom">Text zoom</string>
     <!-- Label for whether or not to force-enable user scalablity (aka, zoom) [CHAR LIMIT=30] -->
     <string name="pref_force_userscalable">Force enable zoom</string>
     <!-- Summary for whether or not to force-enable user scalablity (aka, zoom) [CHAR LIMIT=30] -->
diff --git a/res/xml/accessibility_preferences.xml b/res/xml/accessibility_preferences.xml
index 6109a64..a7124f4 100644
--- a/res/xml/accessibility_preferences.xml
+++ b/res/xml/accessibility_preferences.xml
@@ -26,19 +26,17 @@
         <com.android.browser.preferences.WebViewPreview
             android:title="@string/preview" />
 
-        <ListPreference
-            android:key="text_size"
-            android:title="@string/pref_text_size"
-            android:defaultValue="NORMAL"
-            android:entries="@array/pref_text_size_choices"
-            android:entryValues="@array/pref_text_size_values"
-            android:dialogTitle="@string/pref_text_size_dialogtitle" />
+        <com.android.browser.preferences.FontSizePreference
+            android:key="text_zoom"
+            android:title="@string/pref_text_zoom"
+            android:defaultValue="10"
+            android:max="30" />
 
         <com.android.browser.preferences.FontSizePreference
             android:key="min_font_size"
             android:title="@string/pref_min_font_size"
             android:defaultValue="0"
-            android:max="18" />
+            android:max="20" />
 
     </PreferenceCategory>
 
diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java
index 3aef08f..fae59e2 100644
--- a/src/com/android/browser/BrowserSettings.java
+++ b/src/com/android/browser/BrowserSettings.java
@@ -87,6 +87,11 @@
     // Aka, the lower bounds for the min font size range
     // which is 1:5..24
     private static final int MIN_FONT_SIZE_OFFSET = 5;
+    // The initial value in the text zoom range
+    // This is what represents 100% in the SeekBarPreference range
+    private static final int TEXT_ZOOM_START_VAL = 10;
+    // The size of a single step in the text zoom range, in percent
+    private static final int TEXT_ZOOM_STEP = 5;
 
     private static BrowserSettings sInstance;
 
@@ -116,6 +121,31 @@
             // This is a release build, always startup with debug disabled
             setDebugEnabled(false);
         }
+        if (mPrefs.contains(PREF_TEXT_SIZE)) {
+            /*
+             * Update from TextSize enum to zoom percent
+             * SMALLEST is 50%
+             * SMALLER is 75%
+             * NORMAL is 100%
+             * LARGER is 150%
+             * LARGEST is 200%
+             */
+            switch (getTextSize()) {
+            case SMALLEST:
+                setTextZoom(50);
+                break;
+            case SMALLER:
+                setTextZoom(75);
+                break;
+            case LARGER:
+                setTextZoom(150);
+                break;
+            case LARGEST:
+                setTextZoom(200);
+                break;
+            }
+            mPrefs.edit().remove(PREF_TEXT_SIZE).apply();
+        }
         mAutofillHandler = new AutofillHandler(mContext);
         mManagedSettings = new LinkedList<WeakReference<WebSettings>>();
         mCustomUserAgents = new WeakHashMap<WebSettings, String>();
@@ -158,7 +188,7 @@
         settings.setMinimumLogicalFontSize(getMinimumFontSize());
         settings.setForceUserScalable(forceEnableUserScalable());
         settings.setPluginState(getPluginState());
-        settings.setTextSize(getTextSize());
+        settings.setTextZoom(getTextZoom());
         settings.setAutoFillEnabled(isAutofillEnabled());
         settings.setLayoutAlgorithm(getLayoutAlgorithm());
         settings.setJavaScriptCanOpenWindowsAutomatically(blockPopupWindows());
@@ -417,29 +447,51 @@
         }
     }
 
+    public static int getAdjustedMinimumFontSize(int rawValue) {
+        rawValue++; // Preference starts at 0, min font at 1
+        if (rawValue > 1) {
+            rawValue += (MIN_FONT_SIZE_OFFSET - 2);
+        }
+        return rawValue;
+    }
+
+    public static int getAdjustedTextZoom(int rawValue) {
+        rawValue = (rawValue - TEXT_ZOOM_START_VAL) * TEXT_ZOOM_STEP;
+        return rawValue + 100;
+    }
+
+    static int getRawTextZoom(int percent) {
+        return (percent - 100) / TEXT_ZOOM_STEP + TEXT_ZOOM_START_VAL;
+    }
+
     // -----------------------------
     // getter/setters for accessibility_preferences.xml
     // -----------------------------
 
-    // TODO: Cache
-    public TextSize getTextSize() {
+    @Deprecated
+    private TextSize getTextSize() {
         String textSize = mPrefs.getString(PREF_TEXT_SIZE, "NORMAL");
         return TextSize.valueOf(textSize);
     }
 
     public int getMinimumFontSize() {
         int minFont = mPrefs.getInt(PREF_MIN_FONT_SIZE, 0);
-        minFont++; // Preference starts at 0, min font at 1
-        if (minFont > 1) {
-            minFont += MIN_FONT_SIZE_OFFSET;
-        }
-        return minFont;
+        return getAdjustedMinimumFontSize(minFont);
     }
 
     public boolean forceEnableUserScalable() {
         return mPrefs.getBoolean(PREF_FORCE_USERSCALABLE, false);
     }
 
+    public int getTextZoom() {
+        int textZoom = mPrefs.getInt(PREF_TEXT_ZOOM, 10);
+        return getAdjustedTextZoom(textZoom);
+    }
+
+    public void setTextZoom(int percent) {
+        mPrefs.edit().putInt(PREF_TEXT_ZOOM, getRawTextZoom(percent)).apply();
+    }
+
     // -----------------------------
     // getter/setters for advanced_preferences.xml
     // -----------------------------
diff --git a/src/com/android/browser/PreferenceKeys.java b/src/com/android/browser/PreferenceKeys.java
index f7230df..c90213f 100644
--- a/src/com/android/browser/PreferenceKeys.java
+++ b/src/com/android/browser/PreferenceKeys.java
@@ -26,6 +26,7 @@
     // ----------------------
     static final String PREF_MIN_FONT_SIZE = "min_font_size";
     static final String PREF_TEXT_SIZE = "text_size";
+    static final String PREF_TEXT_ZOOM = "text_zoom";
     static final String PREF_FORCE_USERSCALABLE = "force_userscalable";
 
     // ----------------------
diff --git a/src/com/android/browser/preferences/AccessibilityPreferencesFragment.java b/src/com/android/browser/preferences/AccessibilityPreferencesFragment.java
index d083f43..b7d06a1 100644
--- a/src/com/android/browser/preferences/AccessibilityPreferencesFragment.java
+++ b/src/com/android/browser/preferences/AccessibilityPreferencesFragment.java
@@ -16,33 +16,44 @@
 
 package com.android.browser.preferences;
 
-import com.android.browser.PreferenceKeys;
-import com.android.browser.R;
-
-import android.content.res.Resources;
+import android.content.Context;
 import android.os.Bundle;
 import android.preference.Preference;
 import android.preference.PreferenceFragment;
-import android.view.View;
+
+import com.android.browser.BrowserSettings;
+import com.android.browser.PreferenceKeys;
+import com.android.browser.R;
+
+import java.text.NumberFormat;
 
 public class AccessibilityPreferencesFragment extends PreferenceFragment
         implements Preference.OnPreferenceChangeListener {
 
+    NumberFormat mFormat;
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         addPreferencesFromResource(R.xml.accessibility_preferences);
+        BrowserSettings settings = BrowserSettings.getInstance();
+        mFormat = NumberFormat.getPercentInstance();
 
-        Preference e = findPreference(PreferenceKeys.PREF_TEXT_SIZE);
+        Preference e = findPreference(PreferenceKeys.PREF_MIN_FONT_SIZE);
         e.setOnPreferenceChangeListener(this);
-        e.setSummary(getVisualTextSizeName(
-                getPreferenceScreen().getSharedPreferences()
-                .getString(PreferenceKeys.PREF_TEXT_SIZE, null)) );
+        updateMinFontSummary(e, settings.getMinimumFontSize());
+        e = findPreference(PreferenceKeys.PREF_TEXT_ZOOM);
+        e.setOnPreferenceChangeListener(this);
+        updateTextZoomSummary(e, settings.getTextZoom());
     }
 
-    @Override
-    public void onViewCreated(View view, Bundle savedInstanceState) {
-        super.onViewCreated(view, savedInstanceState);
+    void updateMinFontSummary(Preference pref, int minFontSize) {
+        Context c = getActivity();
+        pref.setSummary(c.getString(R.string.pref_min_font_size_value, minFontSize));
+    }
+
+    void updateTextZoomSummary(Preference pref, int textZoom) {
+        pref.setSummary(mFormat.format(textZoom / 100.0));
     }
 
     @Override
@@ -53,31 +64,15 @@
             return false;
         }
 
-        if (pref.getKey().equals(PreferenceKeys.PREF_TEXT_SIZE)) {
-            pref.setSummary(getVisualTextSizeName((String) objValue));
-            return true;
+        if (PreferenceKeys.PREF_MIN_FONT_SIZE.equals(pref.getKey())) {
+            updateMinFontSummary(pref, BrowserSettings
+                    .getAdjustedMinimumFontSize((Integer) objValue));
         }
-        return false;
-    }
-
-    private CharSequence getVisualTextSizeName(String enumName) {
-        Resources res = getActivity().getResources();
-        CharSequence[] visualNames = res.getTextArray(R.array.pref_text_size_choices);
-        CharSequence[] enumNames = res.getTextArray(R.array.pref_text_size_values);
-
-        // Sanity check
-        if (visualNames.length != enumNames.length) {
-            return "";
+        if (PreferenceKeys.PREF_TEXT_ZOOM.equals(pref.getKey())) {
+            updateTextZoomSummary(pref, BrowserSettings
+                    .getAdjustedTextZoom((Integer) objValue));
         }
-
-        int length = enumNames.length;
-        for (int i = 0; i < length; i++) {
-            if (enumNames[i].equals(enumName)) {
-                return visualNames[i];
-            }
-        }
-
-        return "";
+        return true;
     }
 
 }
\ No newline at end of file
diff --git a/src/com/android/browser/preferences/FontSizePreference.java b/src/com/android/browser/preferences/FontSizePreference.java
index 266a584..0ff1e6d 100644
--- a/src/com/android/browser/preferences/FontSizePreference.java
+++ b/src/com/android/browser/preferences/FontSizePreference.java
@@ -18,22 +18,62 @@
 
 import android.content.Context;
 import android.preference.SeekBarPreference;
+import android.text.TextUtils;
 import android.util.AttributeSet;
+import android.view.View;
 import android.widget.SeekBar;
+import android.widget.TextView;
+
+import com.android.browser.R;
 
 public class FontSizePreference extends SeekBarPreference {
 
+    CharSequence mSummary;
+    TextView mSummaryView;
+
     public FontSizePreference(
             Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
+        init();
     }
 
     public FontSizePreference(Context context, AttributeSet attrs) {
         super(context, attrs);
+        init();
     }
 
     public FontSizePreference(Context context) {
         super(context);
+        init();
+    }
+
+    void init() {
+        setWidgetLayoutResource(R.layout.font_size_widget);
+    }
+
+    @Override
+    public void setSummary(CharSequence summary) {
+        mSummary = summary;
+        if (mSummaryView != null) {
+            mSummaryView.setText(mSummary);
+        }
+    }
+
+    @Override
+    public CharSequence getSummary() {
+        return null;
+    }
+
+    @Override
+    protected void onBindView(View view) {
+        super.onBindView(view);
+        mSummaryView = (TextView) view.findViewById(R.id.text);
+        if (TextUtils.isEmpty(mSummary)) {
+            mSummaryView.setVisibility(View.GONE);
+        } else {
+            mSummaryView.setVisibility(View.VISIBLE);
+            mSummaryView.setText(mSummary);
+        }
     }
 
     @Override
diff --git a/src/com/android/browser/preferences/WebViewPreview.java b/src/com/android/browser/preferences/WebViewPreview.java
index dca4d19..1886bed 100644
--- a/src/com/android/browser/preferences/WebViewPreview.java
+++ b/src/com/android/browser/preferences/WebViewPreview.java
@@ -33,7 +33,7 @@
 
 public class WebViewPreview extends Preference implements OnSharedPreferenceChangeListener {
 
-    static final String HTML_FORMAT = "<html><head><style type=\"text/css\">p { margin: 2px auto;}</style><body><p style=\"font-size: .4em\">%s</p><p style=\"font-size: .7em\">%s</p><p style=\"font-size: 1em\">%s</p><p style=\"font-size: 1.3em\">%s</p><p style=\"font-size: 1.6em\">%s</p></body></html>";
+    static final String HTML_FORMAT = "<html><head><style type=\"text/css\">p { margin: 2px auto;}</style><body><p style=\"font-size: 4pt\">%s</p><p style=\"font-size: 8pt\">%s</p><p style=\"font-size: 10pt\">%s</p><p style=\"font-size: 14pt\">%s</p><p style=\"font-size: 18pt\">%s</p></body></html>";
 
     String mHtml;
     private WebView mWebView;
@@ -67,7 +67,7 @@
         WebSettings ws = mWebView.getSettings();
         BrowserSettings bs = BrowserSettings.getInstance();
         ws.setMinimumFontSize(bs.getMinimumFontSize());
-        ws.setTextSize(bs.getTextSize());
+        ws.setTextZoom(bs.getTextZoom());
         mWebView.loadData(mHtml, "text/html", "utf-8");
     }