WebViewPreview.java revision 8fc22a1c7846b4c7c7b74d1ec59ffc59c2695c15
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.View; 30import android.view.ViewGroup; 31import android.webkit.WebSettings; 32import android.webkit.WebView; 33 34public class WebViewPreview extends Preference implements OnSharedPreferenceChangeListener { 35 36 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>"; 37 38 String mHtml; 39 private WebView mWebView; 40 41 public WebViewPreview( 42 Context context, AttributeSet attrs, int defStyle) { 43 super(context, attrs, defStyle); 44 init(context); 45 } 46 47 public WebViewPreview(Context context, AttributeSet attrs) { 48 super(context, attrs); 49 init(context); 50 } 51 52 public WebViewPreview(Context context) { 53 super(context); 54 init(context); 55 } 56 57 void init(Context context) { 58 Resources res = context.getResources(); 59 Object[] visualNames = res.getStringArray(R.array.pref_text_size_choices); 60 mHtml = String.format(HTML_FORMAT, visualNames); 61 setLayoutResource(R.layout.webview_preview); 62 } 63 64 void updatePreview() { 65 if (mWebView == null) return; 66 67 WebSettings ws = mWebView.getSettings(); 68 BrowserSettings bs = BrowserSettings.getInstance(); 69 ws.setMinimumFontSize(bs.getMinimumFontSize()); 70 ws.setTextSize(bs.getTextSize()); 71 mWebView.loadData(mHtml, "text/html", "utf-8"); 72 } 73 74 @Override 75 protected View onCreateView(ViewGroup parent) { 76 View root = super.onCreateView(parent); 77 WebView webView = (WebView) root.findViewById(R.id.webview); 78 webView.setFocusable(false); 79 webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 80 return root; 81 } 82 83 @Override 84 protected void onBindView(View view) { 85 super.onBindView(view); 86 mWebView = (WebView) view.findViewById(R.id.webview); 87 updatePreview(); 88 } 89 90 @Override 91 protected void onAttachedToHierarchy(PreferenceManager preferenceManager) { 92 super.onAttachedToHierarchy(preferenceManager); 93 getSharedPreferences().registerOnSharedPreferenceChangeListener(this); 94 } 95 96 @Override 97 protected void onPrepareForRemoval() { 98 getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); 99 super.onPrepareForRemoval(); 100 } 101 102 @Override 103 public void onSharedPreferenceChanged( 104 SharedPreferences sharedPreferences, String key) { 105 updatePreview(); 106 } 107 108} 109