blob: e2348a53575e0730ced610d930bc2f9fc5cc1e11 [file] [log] [blame]
Michael Kolba2b2ba82010-08-04 17:54:03 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
Michael Kolb377ea312011-02-17 14:36:56 -08004 * 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
Michael Kolba2b2ba82010-08-04 17:54:03 -07007 *
Michael Kolb377ea312011-02-17 14:36:56 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Michael Kolba2b2ba82010-08-04 17:54:03 -07009 *
10 * Unless required by applicable law or agreed to in writing, software
Michael Kolb377ea312011-02-17 14:36:56 -080011 * 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.
Michael Kolba2b2ba82010-08-04 17:54:03 -070015 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolba2b2ba82010-08-04 17:54:03 -070018
19import android.content.Context;
John Reck8ee633f2011-08-09 16:00:35 -070020import android.graphics.Canvas;
Michael Kolba2b2ba82010-08-04 17:54:03 -070021import android.util.AttributeSet;
22import android.view.View;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080023import org.codeaurora.swe.WebChromeClient;
24import org.codeaurora.swe.WebView;
25import org.codeaurora.swe.WebViewClient;
Michael Kolba2b2ba82010-08-04 17:54:03 -070026
27import java.util.Map;
28
29/**
Michael Kolb377ea312011-02-17 14:36:56 -080030 * Manage WebView scroll events
Michael Kolba2b2ba82010-08-04 17:54:03 -070031 */
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080032public class BrowserWebView extends WebView implements WebView.TitleBarDelegate {
Enrico Ros1f5a0952014-11-18 20:15:48 -080033 private static final boolean ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -070034
John Reck718a24d2011-08-12 11:08:30 -070035 public interface OnScrollChangedListener {
36 void onScrollChanged(int l, int t, int oldl, int oldt);
37 }
38
Romain Guy860662a2011-01-10 12:57:22 -080039 private boolean mBackgroundRemoved = false;
John Reck0f602f32011-07-07 15:38:43 -070040 private TitleBar mTitleBar;
John Reck718a24d2011-08-12 11:08:30 -070041 private OnScrollChangedListener mOnScrollChangedListener;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -080042 private WebChromeClient mWebChromeClient;
43 private WebViewClient mWebViewClient;
Michael Kolba2b2ba82010-08-04 17:54:03 -070044
45 /**
46 * @param context
47 * @param attrs
48 * @param defStyle
49 * @param javascriptInterfaces
50 */
John Reckb9a051b2011-03-18 11:55:48 -070051 public BrowserWebView(Context context, AttributeSet attrs, int defStyle,
Michael Kolba2b2ba82010-08-04 17:54:03 -070052 Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080053 super(context, attrs, defStyle, privateBrowsing);
54 this.setJavascriptInterfaces(javascriptInterfaces);
Michael Kolba2b2ba82010-08-04 17:54:03 -070055 }
56
57 /**
58 * @param context
59 * @param attrs
60 * @param defStyle
61 */
John Reckb9a051b2011-03-18 11:55:48 -070062 public BrowserWebView(
Michael Kolb377ea312011-02-17 14:36:56 -080063 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070064 super(context, attrs, defStyle, privateBrowsing);
65 }
66
67 /**
68 * @param context
69 * @param attrs
70 */
John Reckb9a051b2011-03-18 11:55:48 -070071 public BrowserWebView(Context context, AttributeSet attrs) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070072 super(context, attrs);
73 }
74
75 /**
76 * @param context
77 */
John Reckb9a051b2011-03-18 11:55:48 -070078 public BrowserWebView(Context context) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070079 super(context);
Michael Kolb2814a362011-05-19 15:49:41 -070080 }
81
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -080082 @Override
83 public void setWebChromeClient(WebChromeClient client) {
84 mWebChromeClient = client;
85 super.setWebChromeClient(client);
86 }
87
88 public WebChromeClient getWebChromeClient() {
89 return mWebChromeClient;
90 }
91
92 @Override
93 public void setWebViewClient(WebViewClient client) {
94 mWebViewClient = client;
95 super.setWebViewClient(client);
96 }
97
98 public WebViewClient getWebViewClient() {
99 return mWebViewClient;
100 }
101
Michael Kolb4923c222012-04-02 16:18:36 -0700102 public void setTitleBar(TitleBar title) {
103 mTitleBar = title;
104 }
105
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000106 // From TitleBarDelegate
Michael Kolb08a687a2011-04-22 16:13:02 -0700107 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000108 public int getTitleHeight() {
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800109 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
110 }
111
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000112 // From TitleBarDelegate
Michael Kolba2b2ba82010-08-04 17:54:03 -0700113 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000114 public void onSetEmbeddedTitleBar(final View title) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800115 // TODO: Remove this method; it is never invoked.
Michael Kolba2b2ba82010-08-04 17:54:03 -0700116 }
117
Michael Kolb377ea312011-02-17 14:36:56 -0800118 public boolean hasTitleBar() {
119 return (mTitleBar != null);
120 }
121
Michael Kolb377ea312011-02-17 14:36:56 -0800122 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800123 public void onDraw(Canvas c) {
Michael Kolbf2628922011-03-09 17:15:28 -0800124 super.onDraw(c);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800125
126 // if enabled, removes the background from the main view (assumes coverage with opaqueness)
127 if (ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION) {
128 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
129 mBackgroundRemoved = true;
130 post(new Runnable() {
131 public void run() {
132 getRootView().setBackgroundDrawable(null);
133 }
134 });
135 }
Michael Kolb377ea312011-02-17 14:36:56 -0800136 }
137 }
138
John Reck8ee633f2011-08-09 16:00:35 -0700139 public void drawContent(Canvas c) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800140 //super.drawContent(c);
John Reck8ee633f2011-08-09 16:00:35 -0700141 }
142
John Reck718a24d2011-08-12 11:08:30 -0700143 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800144 public void onScrollChanged(int l, int t, int oldl, int oldt) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800145 // NOTE: this function seems to not be called when the WebView is scrolled (it may be fine)
John Reck718a24d2011-08-12 11:08:30 -0700146 super.onScrollChanged(l, t, oldl, oldt);
Michael Kolb4923c222012-04-02 16:18:36 -0700147 if (mTitleBar != null) {
148 mTitleBar.onScrollChanged();
149 }
John Reck718a24d2011-08-12 11:08:30 -0700150 if (mOnScrollChangedListener != null) {
151 mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
152 }
153 }
154
155 public void setOnScrollChangedListener(OnScrollChangedListener listener) {
156 mOnScrollChangedListener = listener;
157 }
158
John Reckbd546312011-09-19 11:47:52 -0700159 @Override
160 public boolean showContextMenuForChild(View originalView) {
161 return false;
162 }
163
John Reckd1d87312012-03-08 13:25:00 -0800164 @Override
165 public void destroy() {
166 BrowserSettings.getInstance().stopManagingSettings(getSettings());
167 super.destroy();
168 }
169
Michael Kolba2b2ba82010-08-04 17:54:03 -0700170}