blob: 75f595ba50ec035e03b3988e7112e9e051f1ed24 [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;
23import android.view.View;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080024import org.codeaurora.swe.WebChromeClient;
25import org.codeaurora.swe.WebView;
26import org.codeaurora.swe.WebViewClient;
Michael Kolba2b2ba82010-08-04 17:54:03 -070027
28import java.util.Map;
29
30/**
Michael Kolb377ea312011-02-17 14:36:56 -080031 * Manage WebView scroll events
Michael Kolba2b2ba82010-08-04 17:54:03 -070032 */
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080033public class BrowserWebView extends WebView implements WebView.TitleBarDelegate {
Enrico Ros1f5a0952014-11-18 20:15:48 -080034 private static final boolean ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -070035
John Reck718a24d2011-08-12 11:08:30 -070036 public interface OnScrollChangedListener {
37 void onScrollChanged(int l, int t, int oldl, int oldt);
38 }
39
Romain Guy860662a2011-01-10 12:57:22 -080040 private boolean mBackgroundRemoved = false;
John Reck0f602f32011-07-07 15:38:43 -070041 private TitleBar mTitleBar;
John Reck718a24d2011-08-12 11:08:30 -070042 private OnScrollChangedListener mOnScrollChangedListener;
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -080043 private WebChromeClient mWebChromeClient;
44 private WebViewClient mWebViewClient;
Michael Kolba2b2ba82010-08-04 17:54:03 -070045
46 /**
47 * @param context
48 * @param attrs
49 * @param defStyle
50 * @param javascriptInterfaces
51 */
John Reckb9a051b2011-03-18 11:55:48 -070052 public BrowserWebView(Context context, AttributeSet attrs, int defStyle,
Michael Kolba2b2ba82010-08-04 17:54:03 -070053 Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080054 super(context, attrs, defStyle, privateBrowsing);
55 this.setJavascriptInterfaces(javascriptInterfaces);
Michael Kolba2b2ba82010-08-04 17:54:03 -070056 }
57
58 /**
59 * @param context
60 * @param attrs
61 * @param defStyle
62 */
John Reckb9a051b2011-03-18 11:55:48 -070063 public BrowserWebView(
Stewart Chaoe0e132e2014-12-01 18:28:22 -050064 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing, boolean backgroundTab) {
65 super(context, attrs, defStyle, privateBrowsing, backgroundTab);
66 }
67
68 /**
69 * @param context
70 * @param attrs
71 * @param defStyle
72 */
73 public BrowserWebView(
Michael Kolb377ea312011-02-17 14:36:56 -080074 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070075 super(context, attrs, defStyle, privateBrowsing);
76 }
77
78 /**
79 * @param context
80 * @param attrs
81 */
John Reckb9a051b2011-03-18 11:55:48 -070082 public BrowserWebView(Context context, AttributeSet attrs) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070083 super(context, attrs);
84 }
85
86 /**
87 * @param context
88 */
John Reckb9a051b2011-03-18 11:55:48 -070089 public BrowserWebView(Context context) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070090 super(context);
Michael Kolb2814a362011-05-19 15:49:41 -070091 }
92
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -080093 @Override
94 public void setWebChromeClient(WebChromeClient client) {
95 mWebChromeClient = client;
96 super.setWebChromeClient(client);
97 }
98
99 public WebChromeClient getWebChromeClient() {
100 return mWebChromeClient;
101 }
102
103 @Override
104 public void setWebViewClient(WebViewClient client) {
105 mWebViewClient = client;
106 super.setWebViewClient(client);
107 }
108
109 public WebViewClient getWebViewClient() {
110 return mWebViewClient;
111 }
112
Michael Kolb4923c222012-04-02 16:18:36 -0700113 public void setTitleBar(TitleBar title) {
114 mTitleBar = title;
Vivek Sekhar6bd32172015-05-06 15:48:07 -0700115 enableTopControls(true);
116 }
117
118 public void enableTopControls(boolean shinkViewport) {
119 Resources res = getContext().getResources();
120 int titlebarHeight = (int) (res.getDimension(R.dimen.toolbar_height)
121 / res.getDisplayMetrics().density);
122 setTopControlsHeight(titlebarHeight, shinkViewport);
Michael Kolb4923c222012-04-02 16:18:36 -0700123 }
124
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000125 // From TitleBarDelegate
Michael Kolb08a687a2011-04-22 16:13:02 -0700126 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000127 public int getTitleHeight() {
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800128 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
129 }
130
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000131 // From TitleBarDelegate
Michael Kolba2b2ba82010-08-04 17:54:03 -0700132 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000133 public void onSetEmbeddedTitleBar(final View title) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800134 // TODO: Remove this method; it is never invoked.
Michael Kolba2b2ba82010-08-04 17:54:03 -0700135 }
136
Michael Kolb377ea312011-02-17 14:36:56 -0800137 public boolean hasTitleBar() {
138 return (mTitleBar != null);
139 }
140
Michael Kolb377ea312011-02-17 14:36:56 -0800141 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800142 public void onDraw(Canvas c) {
Michael Kolbf2628922011-03-09 17:15:28 -0800143 super.onDraw(c);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800144
145 // if enabled, removes the background from the main view (assumes coverage with opaqueness)
146 if (ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION) {
147 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
148 mBackgroundRemoved = true;
149 post(new Runnable() {
150 public void run() {
151 getRootView().setBackgroundDrawable(null);
152 }
153 });
154 }
Michael Kolb377ea312011-02-17 14:36:56 -0800155 }
156 }
157
John Reck8ee633f2011-08-09 16:00:35 -0700158 public void drawContent(Canvas c) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800159 //super.drawContent(c);
John Reck8ee633f2011-08-09 16:00:35 -0700160 }
161
John Reck718a24d2011-08-12 11:08:30 -0700162 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800163 public void onScrollChanged(int l, int t, int oldl, int oldt) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800164 // NOTE: this function seems to not be called when the WebView is scrolled (it may be fine)
John Reck718a24d2011-08-12 11:08:30 -0700165 super.onScrollChanged(l, t, oldl, oldt);
Michael Kolb4923c222012-04-02 16:18:36 -0700166 if (mTitleBar != null) {
167 mTitleBar.onScrollChanged();
168 }
John Reck718a24d2011-08-12 11:08:30 -0700169 if (mOnScrollChangedListener != null) {
170 mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
171 }
172 }
173
174 public void setOnScrollChangedListener(OnScrollChangedListener listener) {
175 mOnScrollChangedListener = listener;
176 }
177
John Reckbd546312011-09-19 11:47:52 -0700178 @Override
179 public boolean showContextMenuForChild(View originalView) {
180 return false;
181 }
182
John Reckd1d87312012-03-08 13:25:00 -0800183 @Override
184 public void destroy() {
185 BrowserSettings.getInstance().stopManagingSettings(getSettings());
186 super.destroy();
187 }
188
Michael Kolba2b2ba82010-08-04 17:54:03 -0700189}