blob: 9aa044c6db91cf605b2ca9a9183c1e550d0dfd2f [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;
Vivek Sekhard4de6162015-07-21 15:01:45 -070024import android.view.MotionEvent;
Michael Kolba2b2ba82010-08-04 17:54:03 -070025import android.view.View;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080026import org.codeaurora.swe.WebChromeClient;
27import org.codeaurora.swe.WebView;
28import org.codeaurora.swe.WebViewClient;
Michael Kolba2b2ba82010-08-04 17:54:03 -070029
30import java.util.Map;
31
32/**
Michael Kolb377ea312011-02-17 14:36:56 -080033 * Manage WebView scroll events
Michael Kolba2b2ba82010-08-04 17:54:03 -070034 */
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080035public class BrowserWebView extends WebView implements WebView.TitleBarDelegate {
Enrico Ros1f5a0952014-11-18 20:15:48 -080036 private static final boolean ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -070037
John Reck718a24d2011-08-12 11:08:30 -070038 public interface OnScrollChangedListener {
39 void onScrollChanged(int l, int t, int oldl, int oldt);
40 }
41
Romain Guy860662a2011-01-10 12:57:22 -080042 private boolean mBackgroundRemoved = false;
John Reck0f602f32011-07-07 15:38:43 -070043 private TitleBar mTitleBar;
John Reck718a24d2011-08-12 11:08:30 -070044 private OnScrollChangedListener mOnScrollChangedListener;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -080045 private WebChromeClient mWebChromeClient;
46 private WebViewClient mWebViewClient;
Michael Kolba2b2ba82010-08-04 17:54:03 -070047
48 /**
49 * @param context
50 * @param attrs
51 * @param defStyle
52 * @param javascriptInterfaces
53 */
John Reckb9a051b2011-03-18 11:55:48 -070054 public BrowserWebView(Context context, AttributeSet attrs, int defStyle,
Michael Kolba2b2ba82010-08-04 17:54:03 -070055 Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080056 super(context, attrs, defStyle, privateBrowsing);
57 this.setJavascriptInterfaces(javascriptInterfaces);
Michael Kolba2b2ba82010-08-04 17:54:03 -070058 }
59
60 /**
61 * @param context
62 * @param attrs
63 * @param defStyle
64 */
John Reckb9a051b2011-03-18 11:55:48 -070065 public BrowserWebView(
Stewart Chaoe0e132e2014-12-01 18:28:22 -050066 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing, boolean backgroundTab) {
67 super(context, attrs, defStyle, privateBrowsing, backgroundTab);
68 }
69
70 /**
71 * @param context
72 * @param attrs
73 * @param defStyle
74 */
75 public BrowserWebView(
Michael Kolb377ea312011-02-17 14:36:56 -080076 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070077 super(context, attrs, defStyle, privateBrowsing);
78 }
79
80 /**
81 * @param context
82 * @param attrs
83 */
John Reckb9a051b2011-03-18 11:55:48 -070084 public BrowserWebView(Context context, AttributeSet attrs) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070085 super(context, attrs);
86 }
87
88 /**
89 * @param context
90 */
John Reckb9a051b2011-03-18 11:55:48 -070091 public BrowserWebView(Context context) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070092 super(context);
Michael Kolb2814a362011-05-19 15:49:41 -070093 }
94
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -080095 @Override
96 public void setWebChromeClient(WebChromeClient client) {
97 mWebChromeClient = client;
98 super.setWebChromeClient(client);
99 }
100
101 public WebChromeClient getWebChromeClient() {
102 return mWebChromeClient;
103 }
104
105 @Override
106 public void setWebViewClient(WebViewClient client) {
107 mWebViewClient = client;
108 super.setWebViewClient(client);
109 }
110
111 public WebViewClient getWebViewClient() {
112 return mWebViewClient;
113 }
114
Michael Kolb4923c222012-04-02 16:18:36 -0700115 public void setTitleBar(TitleBar title) {
116 mTitleBar = title;
Vivek Sekhar6bd32172015-05-06 15:48:07 -0700117 enableTopControls(true);
118 }
119
120 public void enableTopControls(boolean shinkViewport) {
121 Resources res = getContext().getResources();
122 int titlebarHeight = (int) (res.getDimension(R.dimen.toolbar_height)
123 / res.getDisplayMetrics().density);
124 setTopControlsHeight(titlebarHeight, shinkViewport);
Michael Kolb4923c222012-04-02 16:18:36 -0700125 }
126
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000127 // From TitleBarDelegate
Michael Kolb08a687a2011-04-22 16:13:02 -0700128 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000129 public int getTitleHeight() {
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800130 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
131 }
132
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000133 // From TitleBarDelegate
Michael Kolba2b2ba82010-08-04 17:54:03 -0700134 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000135 public void onSetEmbeddedTitleBar(final View title) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800136 // TODO: Remove this method; it is never invoked.
Michael Kolba2b2ba82010-08-04 17:54:03 -0700137 }
138
Michael Kolb377ea312011-02-17 14:36:56 -0800139 public boolean hasTitleBar() {
140 return (mTitleBar != null);
141 }
142
Michael Kolb377ea312011-02-17 14:36:56 -0800143 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800144 public void onDraw(Canvas c) {
Michael Kolbf2628922011-03-09 17:15:28 -0800145 super.onDraw(c);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800146
147 // if enabled, removes the background from the main view (assumes coverage with opaqueness)
148 if (ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION) {
149 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
150 mBackgroundRemoved = true;
151 post(new Runnable() {
152 public void run() {
153 getRootView().setBackgroundDrawable(null);
154 }
155 });
156 }
Michael Kolb377ea312011-02-17 14:36:56 -0800157 }
158 }
159
John Reck8ee633f2011-08-09 16:00:35 -0700160 public void drawContent(Canvas c) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800161 //super.drawContent(c);
John Reck8ee633f2011-08-09 16:00:35 -0700162 }
163
John Reck718a24d2011-08-12 11:08:30 -0700164 @Override
Vivek Sekhard4de6162015-07-21 15:01:45 -0700165 public boolean onTouchEvent(MotionEvent event) {
166 // block touch event if title bar is selected
167 if (mTitleBar.isEditingUrl()) {
168 requestFocus();
169 return true;
170 }
171 else
172 return super.onTouchEvent(event);
173 }
174
175 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800176 public void onScrollChanged(int l, int t, int oldl, int oldt) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800177 // NOTE: this function seems to not be called when the WebView is scrolled (it may be fine)
John Reck718a24d2011-08-12 11:08:30 -0700178 super.onScrollChanged(l, t, oldl, oldt);
Michael Kolb4923c222012-04-02 16:18:36 -0700179 if (mTitleBar != null) {
180 mTitleBar.onScrollChanged();
181 }
John Reck718a24d2011-08-12 11:08:30 -0700182 if (mOnScrollChangedListener != null) {
183 mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
184 }
185 }
186
187 public void setOnScrollChangedListener(OnScrollChangedListener listener) {
188 mOnScrollChangedListener = listener;
189 }
190
John Reckbd546312011-09-19 11:47:52 -0700191 @Override
192 public boolean showContextMenuForChild(View originalView) {
193 return false;
194 }
195
John Reckd1d87312012-03-08 13:25:00 -0800196 @Override
197 public void destroy() {
198 BrowserSettings.getInstance().stopManagingSettings(getSettings());
199 super.destroy();
200 }
201
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700202 @Override
203 public boolean dispatchKeyEventPreIme(KeyEvent event) {
204 Tab currentTab = mTitleBar.getUiController().getCurrentTab();
205 if (currentTab != null && currentTab.isKeyboardShowing()){
206 // Try to detect the "back" key that dismisses the keyboard
207 if(event.getAction() == KeyEvent.ACTION_DOWN &&
208 event.getKeyCode() == KeyEvent.KEYCODE_BACK)
209 mWebViewClient.onKeyboardStateChange(false);
210 }
211 return super.dispatchKeyEventPreIme(event);
212 }
213
Michael Kolba2b2ba82010-08-04 17:54:03 -0700214}