1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.browser;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.util.AttributeSet;
22import android.view.View;
23import android.webkit.WebView;
24import android.webkit.WebViewClassic;
25
26import java.util.Map;
27
28/**
29 * Manage WebView scroll events
30 */
31public class BrowserWebView extends WebView implements WebViewClassic.TitleBarDelegate {
32
33    public interface OnScrollChangedListener {
34        void onScrollChanged(int l, int t, int oldl, int oldt);
35    }
36
37    private boolean mBackgroundRemoved = false;
38    private TitleBar mTitleBar;
39    private OnScrollChangedListener mOnScrollChangedListener;
40
41    /**
42     * @param context
43     * @param attrs
44     * @param defStyle
45     * @param javascriptInterfaces
46     */
47    public BrowserWebView(Context context, AttributeSet attrs, int defStyle,
48            Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
49        super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing);
50    }
51
52    /**
53     * @param context
54     * @param attrs
55     * @param defStyle
56     */
57    public BrowserWebView(
58            Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
59        super(context, attrs, defStyle, privateBrowsing);
60    }
61
62    /**
63     * @param context
64     * @param attrs
65     */
66    public BrowserWebView(Context context, AttributeSet attrs) {
67        super(context, attrs);
68    }
69
70    /**
71     * @param context
72     */
73    public BrowserWebView(Context context) {
74        super(context);
75    }
76
77    public void setTitleBar(TitleBar title) {
78        mTitleBar = title;
79    }
80
81    // From TitleBarDelegate
82    @Override
83    public int getTitleHeight() {
84        return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
85    }
86
87    // From TitleBarDelegate
88    @Override
89    public void onSetEmbeddedTitleBar(final View title) {
90    }
91
92    public boolean hasTitleBar() {
93        return (mTitleBar != null);
94    }
95
96    @Override
97    protected void onDraw(Canvas c) {
98        super.onDraw(c);
99        if (!mBackgroundRemoved && getRootView().getBackground() != null) {
100            mBackgroundRemoved = true;
101            post(new Runnable() {
102                public void run() {
103                    getRootView().setBackgroundDrawable(null);
104                }
105            });
106        }
107    }
108
109    public void drawContent(Canvas c) {
110        onDraw(c);
111    }
112
113    @Override
114    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
115        super.onScrollChanged(l, t, oldl, oldt);
116        if (mTitleBar != null) {
117            mTitleBar.onScrollChanged();
118        }
119        if (mOnScrollChangedListener != null) {
120            mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
121        }
122    }
123
124    public void setOnScrollChangedListener(OnScrollChangedListener listener) {
125        mOnScrollChangedListener = listener;
126    }
127
128    @Override
129    public boolean showContextMenuForChild(View originalView) {
130        return false;
131    }
132
133    @Override
134    public void destroy() {
135        BrowserSettings.getInstance().stopManagingSettings(getSettings());
136        super.destroy();
137    }
138
139}
140