blob: 0159cb7135c4048ef9529aeb3b54009e4b98710f [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;
Vivek Sekhar6bd32172015-05-06 15:48:07 -070021import android.content.res.Resources;
Michael Kolba2b2ba82010-08-04 17:54:03 -070022import android.util.AttributeSet;
Sagar Dhawan49f85cf2015-07-10 16:54:20 -070023import android.view.KeyEvent;
Michael Kolba2b2ba82010-08-04 17:54:03 -070024import android.view.View;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080025import org.codeaurora.swe.WebChromeClient;
26import org.codeaurora.swe.WebView;
27import org.codeaurora.swe.WebViewClient;
Michael Kolba2b2ba82010-08-04 17:54:03 -070028
29import java.util.Map;
30
31/**
Michael Kolb377ea312011-02-17 14:36:56 -080032 * Manage WebView scroll events
Michael Kolba2b2ba82010-08-04 17:54:03 -070033 */
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080034public class BrowserWebView extends WebView implements WebView.TitleBarDelegate {
Enrico Ros1f5a0952014-11-18 20:15:48 -080035 private static final boolean ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -070036
John Reck718a24d2011-08-12 11:08:30 -070037 public interface OnScrollChangedListener {
38 void onScrollChanged(int l, int t, int oldl, int oldt);
39 }
40
Romain Guy860662a2011-01-10 12:57:22 -080041 private boolean mBackgroundRemoved = false;
John Reck0f602f32011-07-07 15:38:43 -070042 private TitleBar mTitleBar;
John Reck718a24d2011-08-12 11:08:30 -070043 private OnScrollChangedListener mOnScrollChangedListener;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -080044 private WebChromeClient mWebChromeClient;
45 private WebViewClient mWebViewClient;
Michael Kolba2b2ba82010-08-04 17:54:03 -070046
47 /**
48 * @param context
49 * @param attrs
50 * @param defStyle
51 * @param javascriptInterfaces
52 */
John Reckb9a051b2011-03-18 11:55:48 -070053 public BrowserWebView(Context context, AttributeSet attrs, int defStyle,
Michael Kolba2b2ba82010-08-04 17:54:03 -070054 Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080055 super(context, attrs, defStyle, privateBrowsing);
56 this.setJavascriptInterfaces(javascriptInterfaces);
Michael Kolba2b2ba82010-08-04 17:54:03 -070057 }
58
59 /**
60 * @param context
61 * @param attrs
62 * @param defStyle
63 */
John Reckb9a051b2011-03-18 11:55:48 -070064 public BrowserWebView(
Stewart Chaoe0e132e2014-12-01 18:28:22 -050065 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing, boolean backgroundTab) {
66 super(context, attrs, defStyle, privateBrowsing, backgroundTab);
67 }
68
69 /**
70 * @param context
71 * @param attrs
72 * @param defStyle
73 */
74 public BrowserWebView(
Michael Kolb377ea312011-02-17 14:36:56 -080075 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070076 super(context, attrs, defStyle, privateBrowsing);
77 }
78
79 /**
80 * @param context
81 * @param attrs
82 */
John Reckb9a051b2011-03-18 11:55:48 -070083 public BrowserWebView(Context context, AttributeSet attrs) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070084 super(context, attrs);
85 }
86
87 /**
88 * @param context
89 */
John Reckb9a051b2011-03-18 11:55:48 -070090 public BrowserWebView(Context context) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070091 super(context);
Michael Kolb2814a362011-05-19 15:49:41 -070092 }
93
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -080094 @Override
95 public void setWebChromeClient(WebChromeClient client) {
96 mWebChromeClient = client;
97 super.setWebChromeClient(client);
98 }
99
100 public WebChromeClient getWebChromeClient() {
101 return mWebChromeClient;
102 }
103
104 @Override
105 public void setWebViewClient(WebViewClient client) {
106 mWebViewClient = client;
107 super.setWebViewClient(client);
108 }
109
110 public WebViewClient getWebViewClient() {
111 return mWebViewClient;
112 }
113
Michael Kolb4923c222012-04-02 16:18:36 -0700114 public void setTitleBar(TitleBar title) {
115 mTitleBar = title;
Vivek Sekhar6bd32172015-05-06 15:48:07 -0700116 enableTopControls(true);
117 }
118
119 public void enableTopControls(boolean shinkViewport) {
120 Resources res = getContext().getResources();
121 int titlebarHeight = (int) (res.getDimension(R.dimen.toolbar_height)
122 / res.getDisplayMetrics().density);
123 setTopControlsHeight(titlebarHeight, shinkViewport);
Michael Kolb4923c222012-04-02 16:18:36 -0700124 }
125
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000126 // From TitleBarDelegate
Michael Kolb08a687a2011-04-22 16:13:02 -0700127 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000128 public int getTitleHeight() {
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800129 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
130 }
131
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000132 // From TitleBarDelegate
Michael Kolba2b2ba82010-08-04 17:54:03 -0700133 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000134 public void onSetEmbeddedTitleBar(final View title) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800135 // TODO: Remove this method; it is never invoked.
Michael Kolba2b2ba82010-08-04 17:54:03 -0700136 }
137
Michael Kolb377ea312011-02-17 14:36:56 -0800138 public boolean hasTitleBar() {
139 return (mTitleBar != null);
140 }
141
Michael Kolb377ea312011-02-17 14:36:56 -0800142 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800143 public void onDraw(Canvas c) {
Michael Kolbf2628922011-03-09 17:15:28 -0800144 super.onDraw(c);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800145
146 // if enabled, removes the background from the main view (assumes coverage with opaqueness)
147 if (ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION) {
148 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
149 mBackgroundRemoved = true;
150 post(new Runnable() {
151 public void run() {
152 getRootView().setBackgroundDrawable(null);
153 }
154 });
155 }
Michael Kolb377ea312011-02-17 14:36:56 -0800156 }
157 }
158
John Reck8ee633f2011-08-09 16:00:35 -0700159 public void drawContent(Canvas c) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800160 //super.drawContent(c);
John Reck8ee633f2011-08-09 16:00:35 -0700161 }
162
John Reck718a24d2011-08-12 11:08:30 -0700163 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800164 public void onScrollChanged(int l, int t, int oldl, int oldt) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800165 // NOTE: this function seems to not be called when the WebView is scrolled (it may be fine)
John Reck718a24d2011-08-12 11:08:30 -0700166 super.onScrollChanged(l, t, oldl, oldt);
Michael Kolb4923c222012-04-02 16:18:36 -0700167 if (mTitleBar != null) {
168 mTitleBar.onScrollChanged();
169 }
John Reck718a24d2011-08-12 11:08:30 -0700170 if (mOnScrollChangedListener != null) {
171 mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
172 }
173 }
174
175 public void setOnScrollChangedListener(OnScrollChangedListener listener) {
176 mOnScrollChangedListener = listener;
177 }
178
John Reckbd546312011-09-19 11:47:52 -0700179 @Override
180 public boolean showContextMenuForChild(View originalView) {
181 return false;
182 }
183
John Reckd1d87312012-03-08 13:25:00 -0800184 @Override
185 public void destroy() {
186 BrowserSettings.getInstance().stopManagingSettings(getSettings());
187 super.destroy();
188 }
189
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700190 @Override
191 public boolean dispatchKeyEventPreIme(KeyEvent event) {
192 Tab currentTab = mTitleBar.getUiController().getCurrentTab();
193 if (currentTab != null && currentTab.isKeyboardShowing()){
194 // Try to detect the "back" key that dismisses the keyboard
195 if(event.getAction() == KeyEvent.ACTION_DOWN &&
196 event.getKeyCode() == KeyEvent.KEYCODE_BACK)
197 mWebViewClient.onKeyboardStateChange(false);
198 }
199 return super.dispatchKeyEventPreIme(event);
200 }
201
Michael Kolba2b2ba82010-08-04 17:54:03 -0700202}