• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.browser;
18 
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.webkit.WebChromeClient;
24 import android.webkit.WebStorage;
25 import android.webkit.WebView;
26 import android.webkit.WebViewClient;
27 
28 import java.util.Map;
29 
30 /**
31  * Manage WebView scroll events
32  */
33 public class BrowserWebView extends WebView {
34 
35     public interface OnScrollChangedListener {
onScrollChanged(int l, int t, int oldl, int oldt)36         void onScrollChanged(int l, int t, int oldl, int oldt);
37     }
38 
39     private boolean mBackgroundRemoved = false;
40     private TitleBar mTitleBar;
41     private OnScrollChangedListener mOnScrollChangedListener;
42     private WebChromeClient mWebChromeClient;
43     private WebViewClient mWebViewClient;
44 
45     /**
46      * @param context
47      * @param attrs
48      * @param defStyle
49      * @param javascriptInterfaces
50      */
BrowserWebView(Context context, AttributeSet attrs, int defStyle, Map<String, Object> javascriptInterfaces, boolean privateBrowsing)51     public BrowserWebView(Context context, AttributeSet attrs, int defStyle,
52             Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
53         super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing);
54     }
55 
56     /**
57      * @param context
58      * @param attrs
59      * @param defStyle
60      */
BrowserWebView( Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing)61     public BrowserWebView(
62             Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
63         super(context, attrs, defStyle, privateBrowsing);
64     }
65 
66     /**
67      * @param context
68      * @param attrs
69      */
BrowserWebView(Context context, AttributeSet attrs)70     public BrowserWebView(Context context, AttributeSet attrs) {
71         super(context, attrs);
72     }
73 
74     /**
75      * @param context
76      */
BrowserWebView(Context context)77     public BrowserWebView(Context context) {
78         super(context);
79     }
80 
81     @Override
setWebChromeClient(WebChromeClient client)82     public void setWebChromeClient(WebChromeClient client) {
83         mWebChromeClient = client;
84         super.setWebChromeClient(client);
85     }
86 
getWebChromeClient()87     public WebChromeClient getWebChromeClient() {
88       return mWebChromeClient;
89     }
90 
91     @Override
setWebViewClient(WebViewClient client)92     public void setWebViewClient(WebViewClient client) {
93         mWebViewClient = client;
94         super.setWebViewClient(client);
95     }
96 
getWebViewClient()97     public WebViewClient getWebViewClient() {
98       return mWebViewClient;
99     }
100 
setTitleBar(TitleBar title)101     public void setTitleBar(TitleBar title) {
102         mTitleBar = title;
103     }
104 
getTitleHeight()105     public int getTitleHeight() {
106         return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
107     }
108 
hasTitleBar()109     public boolean hasTitleBar() {
110         return (mTitleBar != null);
111     }
112 
113     @Override
onDraw(Canvas c)114     protected void onDraw(Canvas c) {
115         super.onDraw(c);
116         if (!mBackgroundRemoved && getRootView().getBackground() != null) {
117             mBackgroundRemoved = true;
118             post(new Runnable() {
119                 public void run() {
120                     getRootView().setBackgroundDrawable(null);
121                 }
122             });
123         }
124     }
125 
drawContent(Canvas c)126     public void drawContent(Canvas c) {
127         onDraw(c);
128     }
129 
130     @Override
onScrollChanged(int l, int t, int oldl, int oldt)131     protected void onScrollChanged(int l, int t, int oldl, int oldt) {
132         super.onScrollChanged(l, t, oldl, oldt);
133         if (mTitleBar != null) {
134             mTitleBar.onScrollChanged();
135         }
136         if (mOnScrollChangedListener != null) {
137             mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
138         }
139     }
140 
setOnScrollChangedListener(OnScrollChangedListener listener)141     public void setOnScrollChangedListener(OnScrollChangedListener listener) {
142         mOnScrollChangedListener = listener;
143     }
144 
145     @Override
showContextMenuForChild(View originalView)146     public boolean showContextMenuForChild(View originalView) {
147         return false;
148     }
149 
150     @Override
destroy()151     public void destroy() {
152         BrowserSettings.getInstance().stopManagingSettings(getSettings());
153         super.destroy();
154     }
155 
156 }
157