WebViewPreview.java revision a8825b2a789eb0dbeca6d350197869c5ab812ae1
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.R;
21
22import android.content.Context;
23import android.content.SharedPreferences;
24import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
25import android.content.res.Resources;
26import android.preference.Preference;
27import android.preference.PreferenceManager;
28import android.util.AttributeSet;
29import android.view.LayoutInflater;
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    // 80 char line width limit? Rules are made to be broken.
38    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>";
39
40    String HTML;
41    private View mRoot;
42    private WebView mWebView;
43
44    public WebViewPreview(
45            Context context, AttributeSet attrs, int defStyle) {
46        super(context, attrs, defStyle);
47        init(context);
48    }
49
50    public WebViewPreview(Context context, AttributeSet attrs) {
51        super(context, attrs);
52        init(context);
53    }
54
55    public WebViewPreview(Context context) {
56        super(context);
57        init(context);
58    }
59
60    void init(Context context) {
61        Resources res = context.getResources();
62        Object[] visualNames = res.getStringArray(R.array.pref_text_size_choices);
63        HTML = String.format(HTML_FORMAT, visualNames);
64    }
65
66    void updatePreview() {
67        if (mWebView == null) return;
68
69        WebSettings ws = mWebView.getSettings();
70        BrowserSettings bs = BrowserSettings.getInstance();
71        ws.setMinimumFontSize(bs.getMinimumFontSize());
72        ws.setTextSize(bs.getTextSize());
73        mWebView.loadData(HTML, "text/html", "utf-8");
74    }
75
76    @Override
77    public View getView(View convertView, ViewGroup parent) {
78        if (mWebView == null) {
79            LayoutInflater inflater = LayoutInflater.from(getContext());
80            mRoot = inflater.inflate(R.layout.webview_preview, parent, false);
81            mWebView = (WebView) mRoot.findViewById(R.id.webview);
82            mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
83        }
84        updatePreview();
85        return mRoot;
86    }
87
88    @Override
89    protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
90        super.onAttachedToHierarchy(preferenceManager);
91        getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
92    }
93
94    @Override
95    protected void onPrepareForRemoval() {
96        getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
97        super.onPrepareForRemoval();
98    }
99
100    @Override
101    public void onSharedPreferenceChanged(
102            SharedPreferences sharedPreferences, String key) {
103        updatePreview();
104    }
105
106}
107