WebViewPreview.java revision 5d513105dff9f453872be40f8bed2391396cf9c7
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser.preferences;
18
19import com.android.browser.BrowserSettings;
20import com.android.browser.PreferenceKeys;
21import com.android.browser.R;
22
23import android.content.Context;
24import android.content.SharedPreferences;
25import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
26import android.content.res.Resources;
27import android.preference.Preference;
28import android.preference.PreferenceManager;
29import android.util.AttributeSet;
30import android.view.View;
31import android.view.ViewGroup;
32import android.webkit.WebSettings;
33import android.webkit.WebView;
34
35public class WebViewPreview extends Preference implements OnSharedPreferenceChangeListener {
36
37    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>";
38
39    String mHtml;
40    private WebView mWebView;
41
42    public WebViewPreview(
43            Context context, AttributeSet attrs, int defStyle) {
44        super(context, attrs, defStyle);
45        init(context);
46    }
47
48    public WebViewPreview(Context context, AttributeSet attrs) {
49        super(context, attrs);
50        init(context);
51    }
52
53    public WebViewPreview(Context context) {
54        super(context);
55        init(context);
56    }
57
58    void init(Context context) {
59        Resources res = context.getResources();
60        Object[] visualNames = res.getStringArray(R.array.pref_text_size_choices);
61        mHtml = String.format(HTML_FORMAT, visualNames);
62        setLayoutResource(R.layout.webview_preview);
63    }
64
65    void updatePreview() {
66        if (mWebView == null) return;
67
68        WebSettings ws = mWebView.getSettings();
69        BrowserSettings bs = BrowserSettings.getInstance();
70        ws.setMinimumFontSize(bs.getMinimumFontSize());
71        ws.setTextZoom(bs.getTextZoom());
72        ws.setProperty(PreferenceKeys.PREF_INVERTED_CONTRAST, Float.toString(bs.getInvertedContrast()));
73        mWebView.loadData(mHtml, "text/html", "utf-8");
74    }
75
76    @Override
77    protected View onCreateView(ViewGroup parent) {
78        View root = super.onCreateView(parent);
79        WebView webView = (WebView) root.findViewById(R.id.webview);
80        webView.setFocusable(false);
81        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
82        return root;
83    }
84
85    @Override
86    protected void onBindView(View view) {
87        super.onBindView(view);
88        mWebView = (WebView) view.findViewById(R.id.webview);
89        updatePreview();
90    }
91
92    @Override
93    protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
94        super.onAttachedToHierarchy(preferenceManager);
95        getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
96    }
97
98    @Override
99    protected void onPrepareForRemoval() {
100        getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
101        super.onPrepareForRemoval();
102    }
103
104    @Override
105    public void onSharedPreferenceChanged(
106            SharedPreferences sharedPreferences, String key) {
107        updatePreview();
108    }
109
110}
111