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; |
Sagar Dhawan | ca9ecfb | 2015-08-10 17:27:58 -0700 | [diff] [blame^] | 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.BitmapFactory; |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 22 | import android.graphics.Canvas; |
Vivek Sekhar | 6bd3217 | 2015-05-06 15:48:07 -0700 | [diff] [blame] | 23 | import android.content.res.Resources; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Sagar Dhawan | 49f85cf | 2015-07-10 16:54:20 -0700 | [diff] [blame] | 25 | import android.view.KeyEvent; |
Vivek Sekhar | d4de616 | 2015-07-21 15:01:45 -0700 | [diff] [blame] | 26 | import android.view.MotionEvent; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 27 | import android.view.View; |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 28 | import org.codeaurora.swe.WebChromeClient; |
| 29 | import org.codeaurora.swe.WebView; |
| 30 | import org.codeaurora.swe.WebViewClient; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 31 | |
| 32 | import java.util.Map; |
| 33 | |
| 34 | /** |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 35 | * Manage WebView scroll events |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 36 | */ |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 37 | public class BrowserWebView extends WebView implements WebView.TitleBarDelegate { |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 38 | private static final boolean ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION = true; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 39 | |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 40 | public interface OnScrollChangedListener { |
| 41 | void onScrollChanged(int l, int t, int oldl, int oldt); |
| 42 | } |
| 43 | |
Romain Guy | 860662a | 2011-01-10 12:57:22 -0800 | [diff] [blame] | 44 | private boolean mBackgroundRemoved = false; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 45 | private TitleBar mTitleBar; |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 46 | private OnScrollChangedListener mOnScrollChangedListener; |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 47 | private WebChromeClient mWebChromeClient; |
| 48 | private WebViewClient mWebViewClient; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * @param context |
| 52 | * @param attrs |
| 53 | * @param defStyle |
| 54 | * @param javascriptInterfaces |
| 55 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 56 | public BrowserWebView(Context context, AttributeSet attrs, int defStyle, |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 57 | Map<String, Object> javascriptInterfaces, boolean privateBrowsing) { |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 58 | super(context, attrs, defStyle, privateBrowsing); |
| 59 | this.setJavascriptInterfaces(javascriptInterfaces); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param context |
| 64 | * @param attrs |
| 65 | * @param defStyle |
| 66 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 67 | public BrowserWebView( |
Stewart Chao | e0e132e | 2014-12-01 18:28:22 -0500 | [diff] [blame] | 68 | 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 Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 78 | Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 79 | super(context, attrs, defStyle, privateBrowsing); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param context |
| 84 | * @param attrs |
| 85 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 86 | public BrowserWebView(Context context, AttributeSet attrs) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 87 | super(context, attrs); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @param context |
| 92 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 93 | public BrowserWebView(Context context) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 94 | super(context); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 97 | @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 Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 117 | public void setTitleBar(TitleBar title) { |
| 118 | mTitleBar = title; |
Vivek Sekhar | 6bd3217 | 2015-05-06 15:48:07 -0700 | [diff] [blame] | 119 | 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 Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 129 | // From TitleBarDelegate |
Michael Kolb | 08a687a | 2011-04-22 16:13:02 -0700 | [diff] [blame] | 130 | @Override |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 131 | public int getTitleHeight() { |
Michael Kolb | 29ccf8a | 2011-02-23 16:13:24 -0800 | [diff] [blame] | 132 | return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0; |
| 133 | } |
| 134 | |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 135 | // From TitleBarDelegate |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 136 | @Override |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 137 | public void onSetEmbeddedTitleBar(final View title) { |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 138 | // TODO: Remove this method; it is never invoked. |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 141 | public boolean hasTitleBar() { |
| 142 | return (mTitleBar != null); |
| 143 | } |
| 144 | |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 145 | @Override |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 146 | public void onDraw(Canvas c) { |
Michael Kolb | f262892 | 2011-03-09 17:15:28 -0800 | [diff] [blame] | 147 | super.onDraw(c); |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 148 | |
| 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 Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 162 | public void drawContent(Canvas c) { |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 163 | //super.drawContent(c); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 164 | } |
| 165 | |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 166 | @Override |
Vivek Sekhar | d4de616 | 2015-07-21 15:01:45 -0700 | [diff] [blame] | 167 | 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 Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 178 | public void onScrollChanged(int l, int t, int oldl, int oldt) { |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 179 | // 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] | 180 | super.onScrollChanged(l, t, oldl, oldt); |
Michael Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 181 | if (mTitleBar != null) { |
| 182 | mTitleBar.onScrollChanged(); |
| 183 | } |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 184 | 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 Reck | bd54631 | 2011-09-19 11:47:52 -0700 | [diff] [blame] | 193 | @Override |
| 194 | public boolean showContextMenuForChild(View originalView) { |
| 195 | return false; |
| 196 | } |
| 197 | |
John Reck | d1d8731 | 2012-03-08 13:25:00 -0800 | [diff] [blame] | 198 | @Override |
| 199 | public void destroy() { |
| 200 | BrowserSettings.getInstance().stopManagingSettings(getSettings()); |
| 201 | super.destroy(); |
| 202 | } |
| 203 | |
Sagar Dhawan | 49f85cf | 2015-07-10 16:54:20 -0700 | [diff] [blame] | 204 | @Override |
Sagar Dhawan | ca9ecfb | 2015-08-10 17:27:58 -0700 | [diff] [blame^] | 205 | 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 Dhawan | 49f85cf | 2015-07-10 16:54:20 -0700 | [diff] [blame] | 215 | 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 Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 226 | } |