blob: 7c16d7b212446fbeac59f9739e5da2b1860102bd [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(
Stewart Chaoe0e132e2014-12-01 18:28:22 -050063 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing, boolean backgroundTab) {
64 super(context, attrs, defStyle, privateBrowsing, backgroundTab);
65 }
66
67 /**
68 * @param context
69 * @param attrs
70 * @param defStyle
71 */
72 public BrowserWebView(
Michael Kolb377ea312011-02-17 14:36:56 -080073 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070074 super(context, attrs, defStyle, privateBrowsing);
75 }
76
77 /**
78 * @param context
79 * @param attrs
80 */
John Reckb9a051b2011-03-18 11:55:48 -070081 public BrowserWebView(Context context, AttributeSet attrs) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070082 super(context, attrs);
83 }
84
85 /**
86 * @param context
87 */
John Reckb9a051b2011-03-18 11:55:48 -070088 public BrowserWebView(Context context) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070089 super(context);
Michael Kolb2814a362011-05-19 15:49:41 -070090 }
91
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -080092 @Override
93 public void setWebChromeClient(WebChromeClient client) {
94 mWebChromeClient = client;
95 super.setWebChromeClient(client);
96 }
97
98 public WebChromeClient getWebChromeClient() {
99 return mWebChromeClient;
100 }
101
102 @Override
103 public void setWebViewClient(WebViewClient client) {
104 mWebViewClient = client;
105 super.setWebViewClient(client);
106 }
107
108 public WebViewClient getWebViewClient() {
109 return mWebViewClient;
110 }
111
Michael Kolb4923c222012-04-02 16:18:36 -0700112 public void setTitleBar(TitleBar title) {
113 mTitleBar = title;
Vivek Sekhar953d16e2015-04-02 09:52:05 -0700114 setTopControlsHeight(52);
Michael Kolb4923c222012-04-02 16:18:36 -0700115 }
116
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000117 // From TitleBarDelegate
Michael Kolb08a687a2011-04-22 16:13:02 -0700118 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000119 public int getTitleHeight() {
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800120 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
121 }
122
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000123 // From TitleBarDelegate
Michael Kolba2b2ba82010-08-04 17:54:03 -0700124 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000125 public void onSetEmbeddedTitleBar(final View title) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800126 // TODO: Remove this method; it is never invoked.
Michael Kolba2b2ba82010-08-04 17:54:03 -0700127 }
128
Michael Kolb377ea312011-02-17 14:36:56 -0800129 public boolean hasTitleBar() {
130 return (mTitleBar != null);
131 }
132
Michael Kolb377ea312011-02-17 14:36:56 -0800133 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800134 public void onDraw(Canvas c) {
Michael Kolbf2628922011-03-09 17:15:28 -0800135 super.onDraw(c);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800136
137 // if enabled, removes the background from the main view (assumes coverage with opaqueness)
138 if (ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION) {
139 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
140 mBackgroundRemoved = true;
141 post(new Runnable() {
142 public void run() {
143 getRootView().setBackgroundDrawable(null);
144 }
145 });
146 }
Michael Kolb377ea312011-02-17 14:36:56 -0800147 }
148 }
149
John Reck8ee633f2011-08-09 16:00:35 -0700150 public void drawContent(Canvas c) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800151 //super.drawContent(c);
John Reck8ee633f2011-08-09 16:00:35 -0700152 }
153
John Reck718a24d2011-08-12 11:08:30 -0700154 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800155 public void onScrollChanged(int l, int t, int oldl, int oldt) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800156 // NOTE: this function seems to not be called when the WebView is scrolled (it may be fine)
John Reck718a24d2011-08-12 11:08:30 -0700157 super.onScrollChanged(l, t, oldl, oldt);
Michael Kolb4923c222012-04-02 16:18:36 -0700158 if (mTitleBar != null) {
159 mTitleBar.onScrollChanged();
160 }
John Reck718a24d2011-08-12 11:08:30 -0700161 if (mOnScrollChangedListener != null) {
162 mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
163 }
164 }
165
166 public void setOnScrollChangedListener(OnScrollChangedListener listener) {
167 mOnScrollChangedListener = listener;
168 }
169
John Reckbd546312011-09-19 11:47:52 -0700170 @Override
171 public boolean showContextMenuForChild(View originalView) {
172 return false;
173 }
174
John Reckd1d87312012-03-08 13:25:00 -0800175 @Override
176 public void destroy() {
177 BrowserSettings.getInstance().stopManagingSettings(getSettings());
178 super.destroy();
179 }
180
Michael Kolba2b2ba82010-08-04 17:54:03 -0700181}