FontSizePreview.java revision eabe5dabe4e56a78c5d6cf99e6f171452bad2f23
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 android.content.Context;
20import android.content.res.Resources;
21import android.util.AttributeSet;
22import android.view.View;
23import android.webkit.WebSettings;
24import android.webkit.WebView;
25
26import com.android.browser.BrowserSettings;
27import com.android.browser.R;
28import com.android.browser.WebViewProperties;
29
30public class FontSizePreview extends WebViewPreview {
31
32    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>";
33
34    String mHtml;
35
36    public FontSizePreview(
37            Context context, AttributeSet attrs, int defStyle) {
38        super(context, attrs, defStyle);
39    }
40
41    public FontSizePreview(Context context, AttributeSet attrs) {
42        super(context, attrs);
43    }
44
45    public FontSizePreview(Context context) {
46        super(context);
47    }
48
49    @Override
50    protected void init(Context context) {
51        super.init(context);
52        Resources res = context.getResources();
53        Object[] visualNames = res.getStringArray(R.array.pref_text_size_choices);
54        mHtml = String.format(HTML_FORMAT, visualNames);
55    }
56
57    @Override
58    protected void updatePreview() {
59        if (mWebView == null) return;
60
61        WebSettings ws = mWebView.getSettings();
62        BrowserSettings bs = BrowserSettings.getInstance();
63        ws.setMinimumFontSize(bs.getMinimumFontSize());
64        ws.setTextZoom(bs.getTextZoom());
65        mWebView.loadData(mHtml, "text/html", "utf-8");
66    }
67
68    @Override
69    protected void setupWebView(WebView view) {
70        super.setupWebView(view);
71        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
72    }
73
74}
75