blob: 9215fe5127d2bf27860c8f19a643aeedb75b64f0 [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;
114 }
115
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000116 // From TitleBarDelegate
Michael Kolb08a687a2011-04-22 16:13:02 -0700117 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000118 public int getTitleHeight() {
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800119 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
120 }
121
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000122 // From TitleBarDelegate
Michael Kolba2b2ba82010-08-04 17:54:03 -0700123 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000124 public void onSetEmbeddedTitleBar(final View title) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800125 // TODO: Remove this method; it is never invoked.
Michael Kolba2b2ba82010-08-04 17:54:03 -0700126 }
127
Michael Kolb377ea312011-02-17 14:36:56 -0800128 public boolean hasTitleBar() {
129 return (mTitleBar != null);
130 }
131
Michael Kolb377ea312011-02-17 14:36:56 -0800132 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800133 public void onDraw(Canvas c) {
Michael Kolbf2628922011-03-09 17:15:28 -0800134 super.onDraw(c);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800135
136 // if enabled, removes the background from the main view (assumes coverage with opaqueness)
137 if (ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION) {
138 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
139 mBackgroundRemoved = true;
140 post(new Runnable() {
141 public void run() {
142 getRootView().setBackgroundDrawable(null);
143 }
144 });
145 }
Michael Kolb377ea312011-02-17 14:36:56 -0800146 }
147 }
148
John Reck8ee633f2011-08-09 16:00:35 -0700149 public void drawContent(Canvas c) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800150 //super.drawContent(c);
John Reck8ee633f2011-08-09 16:00:35 -0700151 }
152
John Reck718a24d2011-08-12 11:08:30 -0700153 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800154 public void onScrollChanged(int l, int t, int oldl, int oldt) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800155 // NOTE: this function seems to not be called when the WebView is scrolled (it may be fine)
John Reck718a24d2011-08-12 11:08:30 -0700156 super.onScrollChanged(l, t, oldl, oldt);
Michael Kolb4923c222012-04-02 16:18:36 -0700157 if (mTitleBar != null) {
158 mTitleBar.onScrollChanged();
159 }
John Reck718a24d2011-08-12 11:08:30 -0700160 if (mOnScrollChangedListener != null) {
161 mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
162 }
163 }
164
165 public void setOnScrollChangedListener(OnScrollChangedListener listener) {
166 mOnScrollChangedListener = listener;
167 }
168
John Reckbd546312011-09-19 11:47:52 -0700169 @Override
170 public boolean showContextMenuForChild(View originalView) {
171 return false;
172 }
173
John Reckd1d87312012-03-08 13:25:00 -0800174 @Override
175 public void destroy() {
176 BrowserSettings.getInstance().stopManagingSettings(getSettings());
177 super.destroy();
178 }
179
Michael Kolba2b2ba82010-08-04 17:54:03 -0700180}