Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 4 | * 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 Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 7 | * |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 11 | * 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 Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 20 | import android.graphics.Canvas; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 21 | import android.util.AttributeSet; |
| 22 | import android.view.View; |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 23 | import org.codeaurora.swe.WebChromeClient; |
| 24 | import org.codeaurora.swe.WebView; |
| 25 | import org.codeaurora.swe.WebViewClient; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.Map; |
| 28 | |
| 29 | /** |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 30 | * Manage WebView scroll events |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 31 | */ |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 32 | public class BrowserWebView extends WebView implements WebView.TitleBarDelegate { |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 33 | private static final boolean ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION = true; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 34 | |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 35 | public interface OnScrollChangedListener { |
| 36 | void onScrollChanged(int l, int t, int oldl, int oldt); |
| 37 | } |
| 38 | |
Romain Guy | 860662a | 2011-01-10 12:57:22 -0800 | [diff] [blame] | 39 | private boolean mBackgroundRemoved = false; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 40 | private TitleBar mTitleBar; |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 41 | private OnScrollChangedListener mOnScrollChangedListener; |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 42 | private WebChromeClient mWebChromeClient; |
| 43 | private WebViewClient mWebViewClient; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * @param context |
| 47 | * @param attrs |
| 48 | * @param defStyle |
| 49 | * @param javascriptInterfaces |
| 50 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 51 | public BrowserWebView(Context context, AttributeSet attrs, int defStyle, |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 52 | Map<String, Object> javascriptInterfaces, boolean privateBrowsing) { |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 53 | super(context, attrs, defStyle, privateBrowsing); |
| 54 | this.setJavascriptInterfaces(javascriptInterfaces); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param context |
| 59 | * @param attrs |
| 60 | * @param defStyle |
| 61 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 62 | public BrowserWebView( |
Stewart Chao | e0e132e | 2014-12-01 18:28:22 -0500 | [diff] [blame^] | 63 | 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 Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 73 | Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 74 | super(context, attrs, defStyle, privateBrowsing); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param context |
| 79 | * @param attrs |
| 80 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 81 | public BrowserWebView(Context context, AttributeSet attrs) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 82 | super(context, attrs); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @param context |
| 87 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 88 | public BrowserWebView(Context context) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 89 | super(context); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 92 | @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 Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 112 | public void setTitleBar(TitleBar title) { |
| 113 | mTitleBar = title; |
| 114 | } |
| 115 | |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 116 | // From TitleBarDelegate |
Michael Kolb | 08a687a | 2011-04-22 16:13:02 -0700 | [diff] [blame] | 117 | @Override |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 118 | public int getTitleHeight() { |
Michael Kolb | 29ccf8a | 2011-02-23 16:13:24 -0800 | [diff] [blame] | 119 | return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0; |
| 120 | } |
| 121 | |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 122 | // From TitleBarDelegate |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 123 | @Override |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 124 | public void onSetEmbeddedTitleBar(final View title) { |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 125 | // TODO: Remove this method; it is never invoked. |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 128 | public boolean hasTitleBar() { |
| 129 | return (mTitleBar != null); |
| 130 | } |
| 131 | |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 132 | @Override |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 133 | public void onDraw(Canvas c) { |
Michael Kolb | f262892 | 2011-03-09 17:15:28 -0800 | [diff] [blame] | 134 | super.onDraw(c); |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 135 | |
| 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 Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 149 | public void drawContent(Canvas c) { |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 150 | //super.drawContent(c); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 151 | } |
| 152 | |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 153 | @Override |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 154 | public void onScrollChanged(int l, int t, int oldl, int oldt) { |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 155 | // NOTE: this function seems to not be called when the WebView is scrolled (it may be fine) |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 156 | super.onScrollChanged(l, t, oldl, oldt); |
Michael Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 157 | if (mTitleBar != null) { |
| 158 | mTitleBar.onScrollChanged(); |
| 159 | } |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 160 | 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 Reck | bd54631 | 2011-09-19 11:47:52 -0700 | [diff] [blame] | 169 | @Override |
| 170 | public boolean showContextMenuForChild(View originalView) { |
| 171 | return false; |
| 172 | } |
| 173 | |
John Reck | d1d8731 | 2012-03-08 13:25:00 -0800 | [diff] [blame] | 174 | @Override |
| 175 | public void destroy() { |
| 176 | BrowserSettings.getInstance().stopManagingSettings(getSettings()); |
| 177 | super.destroy(); |
| 178 | } |
| 179 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 180 | } |