1eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck/*
2eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck * Copyright (C) 2011 The Android Open Source Project
3eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck *
4eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck * Licensed under the Apache License, Version 2.0 (the "License");
5eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck * you may not use this file except in compliance with the License.
6eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck * You may obtain a copy of the License at
7eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck *
8eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck *      http://www.apache.org/licenses/LICENSE-2.0
9eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck *
10eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck * Unless required by applicable law or agreed to in writing, software
11eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck * distributed under the License is distributed on an "AS IS" BASIS,
12eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck * See the License for the specific language governing permissions and
14eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck * limitations under the License.
15eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck */
16eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck
17eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reckpackage com.android.browser.preferences;
18eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck
19eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reckimport android.content.Context;
20eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reckimport android.content.res.Resources;
21eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reckimport android.util.AttributeSet;
22eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reckimport android.view.View;
23eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reckimport android.webkit.WebSettings;
24eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reckimport android.webkit.WebView;
25eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck
26eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reckimport com.android.browser.BrowserSettings;
27eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reckimport com.android.browser.R;
28eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck
29eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reckpublic class FontSizePreview extends WebViewPreview {
30eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck
31600928560852c7b9ffffbfc91ee9fe1016465329John Reck    static final String HTML_FORMAT = "<!DOCTYPE html><html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><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>";
32eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck
33eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck    String mHtml;
34eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck
35eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck    public FontSizePreview(
36eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck            Context context, AttributeSet attrs, int defStyle) {
37eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck        super(context, attrs, defStyle);
38eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck    }
39eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck
40eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck    public FontSizePreview(Context context, AttributeSet attrs) {
41eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck        super(context, attrs);
42eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck    }
43eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck
44eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck    public FontSizePreview(Context context) {
45eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck        super(context);
46eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck    }
47eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck
48eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck    @Override
49eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck    protected void init(Context context) {
50eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck        super.init(context);
51eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck        Resources res = context.getResources();
52eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck        Object[] visualNames = res.getStringArray(R.array.pref_text_size_choices);
53eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck        mHtml = String.format(HTML_FORMAT, visualNames);
54eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck    }
55eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck
56eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck    @Override
5796998e27a38534626e7bca6054aa8ddb2f8ca4e2John Reck    protected void updatePreview(boolean forceReload) {
58eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck        if (mWebView == null) return;
59eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck
60eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck        WebSettings ws = mWebView.getSettings();
61eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck        BrowserSettings bs = BrowserSettings.getInstance();
62eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck        ws.setMinimumFontSize(bs.getMinimumFontSize());
63eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck        ws.setTextZoom(bs.getTextZoom());
64600928560852c7b9ffffbfc91ee9fe1016465329John Reck        mWebView.loadDataWithBaseURL(null, mHtml, "text/html", "utf-8", null);
65eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck    }
66eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck
67eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck    @Override
68eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck    protected void setupWebView(WebView view) {
69eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck        super.setupWebView(view);
70eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
71eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck    }
72eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck
73eabe5dabe4e56a78c5d6cf99e6f171452bad2f23John Reck}
74