Add preview for contrast

 Bug: 5135321
 Bug: 5118011
 Add a preview for the contrast accisibility settings.
 Rename FontSizePreference to the more generic SeekBarSummaryPreference
 as it has nothing to do with font size specifically.

Change-Id: Id516d63d62822cf026af3ce531ec33eb48ae667a
diff --git a/res/xml/accessibility_preferences.xml b/res/xml/accessibility_preferences.xml
index db5a539..ac96e6e 100644
--- a/res/xml/accessibility_preferences.xml
+++ b/res/xml/accessibility_preferences.xml
@@ -23,16 +23,16 @@
         android:defaultValue="false" />
 
     <PreferenceCategory android:title="@string/pref_font_size_category">
-        <com.android.browser.preferences.WebViewPreview
+        <com.android.browser.preferences.FontSizePreview
             android:title="@string/preview" />
 
-        <com.android.browser.preferences.FontSizePreference
+        <com.android.browser.preferences.SeekBarSummaryPreference
             android:key="text_zoom"
             android:title="@string/pref_text_zoom"
             android:defaultValue="10"
             android:max="30" />
 
-        <com.android.browser.preferences.FontSizePreference
+        <com.android.browser.preferences.SeekBarSummaryPreference
             android:key="min_font_size"
             android:title="@string/pref_min_font_size"
             android:defaultValue="0"
@@ -41,6 +41,8 @@
     </PreferenceCategory>
 
     <PreferenceCategory android:title="@string/pref_inverted_category">
+        <com.android.browser.preferences.InvertedContrastPreview
+            android:title="@string/preview" />
 
         <CheckBoxPreference
             android:key="inverted"
@@ -48,9 +50,10 @@
             android:title="@string/pref_inverted"
             android:summary="@string/pref_inverted_summary" />
 
-        <com.android.browser.preferences.FontSizePreference
+        <com.android.browser.preferences.SeekBarSummaryPreference
             android:key="inverted_contrast"
             android:title="@string/pref_inverted_contrast"
+            android:dependency="inverted"
             android:defaultValue="0"
             android:max="20" />
 
diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java
index b3f7689..ad42146 100644
--- a/src/com/android/browser/BrowserSettings.java
+++ b/src/com/android/browser/BrowserSettings.java
@@ -250,10 +250,10 @@
             settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
         }
 
-        settings.setProperty(PREF_INVERTED,
+        settings.setProperty(WebViewProperties.gfxInvertedScreen,
                 useInvertedRendering() ? "true" : "false");
 
-        settings.setProperty(PREF_INVERTED_CONTRAST,
+        settings.setProperty(WebViewProperties.gfxInvertedScreenContrast,
                 Float.toString(getInvertedContrast()));
     }
 
@@ -742,10 +742,6 @@
         return 1 + (mPrefs.getInt(PREF_INVERTED_CONTRAST, 0) / 10f);
     }
 
-    public void setInvertedContrast(int contrast) {
-        mPrefs.edit().putInt(PREF_INVERTED_CONTRAST, contrast).apply();
-    }
-
     // -----------------------------
     // getter/setters for privacy_security_preferences.xml
     // -----------------------------
diff --git a/src/com/android/browser/WebViewProperties.java b/src/com/android/browser/WebViewProperties.java
new file mode 100644
index 0000000..9a656d7
--- /dev/null
+++ b/src/com/android/browser/WebViewProperties.java
@@ -0,0 +1,21 @@
+/*
+ * 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.
+ */
+package com.android.browser;
+
+public interface WebViewProperties {
+    static final String gfxInvertedScreen = "inverted";
+    static final String gfxInvertedScreenContrast = "inverted_contrast";
+}
diff --git a/src/com/android/browser/preferences/FontSizePreview.java b/src/com/android/browser/preferences/FontSizePreview.java
new file mode 100644
index 0000000..7311e80
--- /dev/null
+++ b/src/com/android/browser/preferences/FontSizePreview.java
@@ -0,0 +1,74 @@
+/*
+ * 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.
+ */
+
+package com.android.browser.preferences;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.util.AttributeSet;
+import android.view.View;
+import android.webkit.WebSettings;
+import android.webkit.WebView;
+
+import com.android.browser.BrowserSettings;
+import com.android.browser.R;
+import com.android.browser.WebViewProperties;
+
+public class FontSizePreview extends WebViewPreview {
+
+    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;
+
+    public FontSizePreview(
+            Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+    }
+
+    public FontSizePreview(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public FontSizePreview(Context context) {
+        super(context);
+    }
+
+    @Override
+    protected void init(Context context) {
+        super.init(context);
+        Resources res = context.getResources();
+        Object[] visualNames = res.getStringArray(R.array.pref_text_size_choices);
+        mHtml = String.format(HTML_FORMAT, visualNames);
+    }
+
+    @Override
+    protected void updatePreview() {
+        if (mWebView == null) return;
+
+        WebSettings ws = mWebView.getSettings();
+        BrowserSettings bs = BrowserSettings.getInstance();
+        ws.setMinimumFontSize(bs.getMinimumFontSize());
+        ws.setTextZoom(bs.getTextZoom());
+        mWebView.loadData(mHtml, "text/html", "utf-8");
+    }
+
+    @Override
+    protected void setupWebView(WebView view) {
+        super.setupWebView(view);
+        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
+    }
+
+}
diff --git a/src/com/android/browser/preferences/InvertedContrastPreview.java b/src/com/android/browser/preferences/InvertedContrastPreview.java
new file mode 100644
index 0000000..a2d2910
--- /dev/null
+++ b/src/com/android/browser/preferences/InvertedContrastPreview.java
@@ -0,0 +1,88 @@
+/*
+ * 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.
+ */
+
+package com.android.browser.preferences;
+
+import android.content.Context;
+import android.text.TextUtils;
+import android.util.AttributeSet;
+import android.webkit.WebSettings;
+
+import com.android.browser.BrowserSettings;
+import com.android.browser.WebViewProperties;
+
+public class InvertedContrastPreview extends WebViewPreview {
+
+    static final String IMG_ROOT = "content://com.android.browser.home/res/raw/";
+    static final String[] THUMBS = new String[] {
+        "thumb_google",
+        "thumb_amazon",
+        "thumb_cnn",
+        "thumb_espn",
+        "", // break
+        "thumb_bbc",
+        "thumb_nytimes",
+        "thumb_weatherchannel",
+        "thumb_picasa",
+    };
+
+    String mHtml;
+
+    public InvertedContrastPreview(
+            Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+    }
+
+    public InvertedContrastPreview(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public InvertedContrastPreview(Context context) {
+        super(context);
+    }
+
+    @Override
+    protected void init(Context context) {
+        super.init(context);
+        StringBuilder builder = new StringBuilder("<html><body style=\"width: 1000px\">");
+        for (String thumb : THUMBS) {
+            if (TextUtils.isEmpty(thumb)) {
+                builder.append("<br />");
+                continue;
+            }
+            builder.append("<img src=\"");
+            builder.append(IMG_ROOT);
+            builder.append(thumb);
+            builder.append("\" />&nbsp;");
+        }
+        builder.append("</body></html>");
+        mHtml = builder.toString();
+    }
+
+    @Override
+    protected void updatePreview() {
+        if (mWebView == null) return;
+
+        WebSettings ws = mWebView.getSettings();
+        BrowserSettings bs = BrowserSettings.getInstance();
+        ws.setProperty(WebViewProperties.gfxInvertedScreen,
+                bs.useInvertedRendering() ? "true" : "false");
+        ws.setProperty(WebViewProperties.gfxInvertedScreenContrast,
+                Float.toString(bs.getInvertedContrast()));
+        mWebView.loadData(mHtml, "text/html", "utf-8");
+    }
+
+}
diff --git a/src/com/android/browser/preferences/FontSizePreference.java b/src/com/android/browser/preferences/SeekBarSummaryPreference.java
similarity index 90%
rename from src/com/android/browser/preferences/FontSizePreference.java
rename to src/com/android/browser/preferences/SeekBarSummaryPreference.java
index 0ff1e6d..481fbc7 100644
--- a/src/com/android/browser/preferences/FontSizePreference.java
+++ b/src/com/android/browser/preferences/SeekBarSummaryPreference.java
@@ -26,23 +26,23 @@
 
 import com.android.browser.R;
 
-public class FontSizePreference extends SeekBarPreference {
+public class SeekBarSummaryPreference extends SeekBarPreference {
 
     CharSequence mSummary;
     TextView mSummaryView;
 
-    public FontSizePreference(
+    public SeekBarSummaryPreference(
             Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
         init();
     }
 
-    public FontSizePreference(Context context, AttributeSet attrs) {
+    public SeekBarSummaryPreference(Context context, AttributeSet attrs) {
         super(context, attrs);
         init();
     }
 
-    public FontSizePreference(Context context) {
+    public SeekBarSummaryPreference(Context context) {
         super(context);
         init();
     }
diff --git a/src/com/android/browser/preferences/WebViewPreview.java b/src/com/android/browser/preferences/WebViewPreview.java
index 27c4f00..4117388 100644
--- a/src/com/android/browser/preferences/WebViewPreview.java
+++ b/src/com/android/browser/preferences/WebViewPreview.java
@@ -16,28 +16,22 @@
 
 package com.android.browser.preferences;
 
-import com.android.browser.BrowserSettings;
-import com.android.browser.PreferenceKeys;
-import com.android.browser.R;
-
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
-import android.content.res.Resources;
 import android.preference.Preference;
 import android.preference.PreferenceManager;
 import android.util.AttributeSet;
 import android.view.View;
 import android.view.ViewGroup;
-import android.webkit.WebSettings;
 import android.webkit.WebView;
 
-public class WebViewPreview extends Preference implements OnSharedPreferenceChangeListener {
+import com.android.browser.R;
 
-    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>";
+public abstract class WebViewPreview extends Preference
+        implements OnSharedPreferenceChangeListener {
 
-    String mHtml;
-    private WebView mWebView;
+    protected WebView mWebView;
 
     public WebViewPreview(
             Context context, AttributeSet attrs, int defStyle) {
@@ -55,30 +49,20 @@
         init(context);
     }
 
-    void init(Context context) {
-        Resources res = context.getResources();
-        Object[] visualNames = res.getStringArray(R.array.pref_text_size_choices);
-        mHtml = String.format(HTML_FORMAT, visualNames);
+    protected void init(Context context) {
         setLayoutResource(R.layout.webview_preview);
     }
 
-    void updatePreview() {
-        if (mWebView == null) return;
+    protected abstract void updatePreview();
 
-        WebSettings ws = mWebView.getSettings();
-        BrowserSettings bs = BrowserSettings.getInstance();
-        ws.setMinimumFontSize(bs.getMinimumFontSize());
-        ws.setTextZoom(bs.getTextZoom());
-        ws.setProperty(PreferenceKeys.PREF_INVERTED_CONTRAST, Float.toString(bs.getInvertedContrast()));
-        mWebView.loadData(mHtml, "text/html", "utf-8");
-    }
+    protected void setupWebView(WebView view) {}
 
     @Override
     protected View onCreateView(ViewGroup parent) {
         View root = super.onCreateView(parent);
         WebView webView = (WebView) root.findViewById(R.id.webview);
         webView.setFocusable(false);
-        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
+        setupWebView(webView);
         return root;
     }