blob: afcca1a59152b486a45b3a407bff6f2c6f8ec8bb [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;
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -070020import android.graphics.Bitmap;
21import android.graphics.BitmapFactory;
John Reck8ee633f2011-08-09 16:00:35 -070022import android.graphics.Canvas;
Vivek Sekhar6bd32172015-05-06 15:48:07 -070023import android.content.res.Resources;
Michael Kolba2b2ba82010-08-04 17:54:03 -070024import android.util.AttributeSet;
Sagar Dhawan49f85cf2015-07-10 16:54:20 -070025import android.view.KeyEvent;
Vivek Sekhard4de6162015-07-21 15:01:45 -070026import android.view.MotionEvent;
Michael Kolba2b2ba82010-08-04 17:54:03 -070027import android.view.View;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080028import org.codeaurora.swe.WebChromeClient;
29import org.codeaurora.swe.WebView;
30import org.codeaurora.swe.WebViewClient;
Michael Kolba2b2ba82010-08-04 17:54:03 -070031
32import java.util.Map;
33
34/**
Michael Kolb377ea312011-02-17 14:36:56 -080035 * Manage WebView scroll events
Michael Kolba2b2ba82010-08-04 17:54:03 -070036 */
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080037public class BrowserWebView extends WebView implements WebView.TitleBarDelegate {
Enrico Ros1f5a0952014-11-18 20:15:48 -080038 private static final boolean ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -070039
John Reck718a24d2011-08-12 11:08:30 -070040 public interface OnScrollChangedListener {
41 void onScrollChanged(int l, int t, int oldl, int oldt);
42 }
43
Romain Guy860662a2011-01-10 12:57:22 -080044 private boolean mBackgroundRemoved = false;
John Reck0f602f32011-07-07 15:38:43 -070045 private TitleBar mTitleBar;
John Reck718a24d2011-08-12 11:08:30 -070046 private OnScrollChangedListener mOnScrollChangedListener;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -080047 private WebChromeClient mWebChromeClient;
48 private WebViewClient mWebViewClient;
Michael Kolba2b2ba82010-08-04 17:54:03 -070049
50 /**
51 * @param context
52 * @param attrs
53 * @param defStyle
54 * @param javascriptInterfaces
55 */
John Reckb9a051b2011-03-18 11:55:48 -070056 public BrowserWebView(Context context, AttributeSet attrs, int defStyle,
Michael Kolba2b2ba82010-08-04 17:54:03 -070057 Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080058 super(context, attrs, defStyle, privateBrowsing);
59 this.setJavascriptInterfaces(javascriptInterfaces);
Michael Kolba2b2ba82010-08-04 17:54:03 -070060 }
61
62 /**
63 * @param context
64 * @param attrs
65 * @param defStyle
66 */
John Reckb9a051b2011-03-18 11:55:48 -070067 public BrowserWebView(
Stewart Chaoe0e132e2014-12-01 18:28:22 -050068 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing, boolean backgroundTab) {
69 super(context, attrs, defStyle, privateBrowsing, backgroundTab);
70 }
71
72 /**
73 * @param context
74 * @param attrs
75 * @param defStyle
76 */
77 public BrowserWebView(
Michael Kolb377ea312011-02-17 14:36:56 -080078 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070079 super(context, attrs, defStyle, privateBrowsing);
80 }
81
82 /**
83 * @param context
84 * @param attrs
85 */
John Reckb9a051b2011-03-18 11:55:48 -070086 public BrowserWebView(Context context, AttributeSet attrs) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070087 super(context, attrs);
88 }
89
90 /**
91 * @param context
92 */
John Reckb9a051b2011-03-18 11:55:48 -070093 public BrowserWebView(Context context) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070094 super(context);
Michael Kolb2814a362011-05-19 15:49:41 -070095 }
96
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -080097 @Override
98 public void setWebChromeClient(WebChromeClient client) {
99 mWebChromeClient = client;
100 super.setWebChromeClient(client);
101 }
102
103 public WebChromeClient getWebChromeClient() {
104 return mWebChromeClient;
105 }
106
107 @Override
108 public void setWebViewClient(WebViewClient client) {
109 mWebViewClient = client;
110 super.setWebViewClient(client);
111 }
112
113 public WebViewClient getWebViewClient() {
114 return mWebViewClient;
115 }
116
Michael Kolb4923c222012-04-02 16:18:36 -0700117 public void setTitleBar(TitleBar title) {
118 mTitleBar = title;
Vivek Sekhar6bd32172015-05-06 15:48:07 -0700119 enableTopControls(true);
120 }
121
122 public void enableTopControls(boolean shinkViewport) {
123 Resources res = getContext().getResources();
124 int titlebarHeight = (int) (res.getDimension(R.dimen.toolbar_height)
125 / res.getDisplayMetrics().density);
126 setTopControlsHeight(titlebarHeight, shinkViewport);
Michael Kolb4923c222012-04-02 16:18:36 -0700127 }
128
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000129 // From TitleBarDelegate
Michael Kolb08a687a2011-04-22 16:13:02 -0700130 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000131 public int getTitleHeight() {
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800132 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
133 }
134
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000135 // From TitleBarDelegate
Michael Kolba2b2ba82010-08-04 17:54:03 -0700136 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000137 public void onSetEmbeddedTitleBar(final View title) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800138 // TODO: Remove this method; it is never invoked.
Michael Kolba2b2ba82010-08-04 17:54:03 -0700139 }
140
Michael Kolb377ea312011-02-17 14:36:56 -0800141 public boolean hasTitleBar() {
142 return (mTitleBar != null);
143 }
144
Michael Kolb377ea312011-02-17 14:36:56 -0800145 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800146 public void onDraw(Canvas c) {
Michael Kolbf2628922011-03-09 17:15:28 -0800147 super.onDraw(c);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800148
149 // if enabled, removes the background from the main view (assumes coverage with opaqueness)
150 if (ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION) {
151 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
152 mBackgroundRemoved = true;
153 post(new Runnable() {
154 public void run() {
155 getRootView().setBackgroundDrawable(null);
156 }
157 });
158 }
Michael Kolb377ea312011-02-17 14:36:56 -0800159 }
160 }
161
John Reck8ee633f2011-08-09 16:00:35 -0700162 public void drawContent(Canvas c) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800163 //super.drawContent(c);
John Reck8ee633f2011-08-09 16:00:35 -0700164 }
165
John Reck718a24d2011-08-12 11:08:30 -0700166 @Override
Vivek Sekhard4de6162015-07-21 15:01:45 -0700167 public boolean onTouchEvent(MotionEvent event) {
168 // block touch event if title bar is selected
169 if (mTitleBar.isEditingUrl()) {
170 requestFocus();
171 return true;
172 }
173 else
174 return super.onTouchEvent(event);
175 }
176
177 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800178 public void onScrollChanged(int l, int t, int oldl, int oldt) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800179 // NOTE: this function seems to not be called when the WebView is scrolled (it may be fine)
John Reck718a24d2011-08-12 11:08:30 -0700180 super.onScrollChanged(l, t, oldl, oldt);
Michael Kolb4923c222012-04-02 16:18:36 -0700181 if (mTitleBar != null) {
182 mTitleBar.onScrollChanged();
183 }
John Reck718a24d2011-08-12 11:08:30 -0700184 if (mOnScrollChangedListener != null) {
185 mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
186 }
187 }
188
189 public void setOnScrollChangedListener(OnScrollChangedListener listener) {
190 mOnScrollChangedListener = listener;
191 }
192
John Reckbd546312011-09-19 11:47:52 -0700193 @Override
194 public boolean showContextMenuForChild(View originalView) {
195 return false;
196 }
197
John Reckd1d87312012-03-08 13:25:00 -0800198 @Override
199 public void destroy() {
200 BrowserSettings.getInstance().stopManagingSettings(getSettings());
201 super.destroy();
202 }
203
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700204 @Override
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700205 public Bitmap getFavicon() {
206 Tab currentTab = mTitleBar.getUiController().getCurrentTab();
207 if (currentTab != null){
208 return currentTab.getFavicon();
209 }
210 else return BitmapFactory.decodeResource(
211 this.getResources(), R.drawable.ic_deco_favicon_normal);
212 }
213
214 @Override
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700215 public boolean dispatchKeyEventPreIme(KeyEvent event) {
216 Tab currentTab = mTitleBar.getUiController().getCurrentTab();
217 if (currentTab != null && currentTab.isKeyboardShowing()){
218 // Try to detect the "back" key that dismisses the keyboard
219 if(event.getAction() == KeyEvent.ACTION_DOWN &&
220 event.getKeyCode() == KeyEvent.KEYCODE_BACK)
221 mWebViewClient.onKeyboardStateChange(false);
222 }
223 return super.dispatchKeyEventPreIme(event);
224 }
225
Michael Kolba2b2ba82010-08-04 17:54:03 -0700226}