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.SharedPreferences;
21import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
22import android.preference.Preference;
23import android.preference.PreferenceManager;
24import android.util.AttributeSet;
25import android.view.View;
26import android.view.ViewGroup;
27import android.webkit.WebView;
28
29import com.android.browser.R;
30
31public abstract class WebViewPreview extends Preference
32        implements OnSharedPreferenceChangeListener {
33
34    protected WebView mWebView;
35
36    public WebViewPreview(
37            Context context, AttributeSet attrs, int defStyle) {
38        super(context, attrs, defStyle);
39        init(context);
40    }
41
42    public WebViewPreview(Context context, AttributeSet attrs) {
43        super(context, attrs);
44        init(context);
45    }
46
47    public WebViewPreview(Context context) {
48        super(context);
49        init(context);
50    }
51
52    protected void init(Context context) {
53        setLayoutResource(R.layout.webview_preview);
54    }
55
56    protected abstract void updatePreview(boolean forceReload);
57
58    protected void setupWebView(WebView view) {}
59
60    @Override
61    protected View onCreateView(ViewGroup parent) {
62        View root = super.onCreateView(parent);
63        WebView webView = (WebView) root.findViewById(R.id.webview);
64        // Tell WebView to really, truly ignore all touch events. No, seriously,
65        // ignore them all. And don't show scrollbars.
66        webView.setFocusable(false);
67        webView.setFocusableInTouchMode(false);
68        webView.setClickable(false);
69        webView.setLongClickable(false);
70        webView.setHorizontalScrollBarEnabled(false);
71        webView.setVerticalScrollBarEnabled(false);
72        setupWebView(webView);
73        return root;
74    }
75
76    @Override
77    protected void onBindView(View view) {
78        super.onBindView(view);
79        mWebView = (WebView) view.findViewById(R.id.webview);
80        updatePreview(true);
81    }
82
83    @Override
84    protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
85        super.onAttachedToHierarchy(preferenceManager);
86        getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
87    }
88
89    @Override
90    protected void onPrepareForRemoval() {
91        getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
92        super.onPrepareForRemoval();
93    }
94
95    @Override
96    public void onSharedPreferenceChanged(
97            SharedPreferences sharedPreferences, String key) {
98        updatePreview(false);
99    }
100
101}
102