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; |
Vivek Sekhar | 6bd3217 | 2015-05-06 15:48:07 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 22 | import android.util.AttributeSet; |
Sagar Dhawan | 49f85cf | 2015-07-10 16:54:20 -0700 | [diff] [blame] | 23 | import android.view.KeyEvent; |
Vivek Sekhar | d4de616 | 2015-07-21 15:01:45 -0700 | [diff] [blame^] | 24 | import android.view.MotionEvent; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 25 | import android.view.View; |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 26 | import org.codeaurora.swe.WebChromeClient; |
| 27 | import org.codeaurora.swe.WebView; |
| 28 | import org.codeaurora.swe.WebViewClient; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 29 | |
| 30 | import java.util.Map; |
| 31 | |
| 32 | /** |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 33 | * Manage WebView scroll events |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 34 | */ |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 35 | public class BrowserWebView extends WebView implements WebView.TitleBarDelegate { |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 36 | private static final boolean ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION = true; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 37 | |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 38 | public interface OnScrollChangedListener { |
| 39 | void onScrollChanged(int l, int t, int oldl, int oldt); |
| 40 | } |
| 41 | |
Romain Guy | 860662a | 2011-01-10 12:57:22 -0800 | [diff] [blame] | 42 | private boolean mBackgroundRemoved = false; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 43 | private TitleBar mTitleBar; |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 44 | private OnScrollChangedListener mOnScrollChangedListener; |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 45 | private WebChromeClient mWebChromeClient; |
| 46 | private WebViewClient mWebViewClient; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * @param context |
| 50 | * @param attrs |
| 51 | * @param defStyle |
| 52 | * @param javascriptInterfaces |
| 53 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 54 | public BrowserWebView(Context context, AttributeSet attrs, int defStyle, |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 55 | Map<String, Object> javascriptInterfaces, boolean privateBrowsing) { |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 56 | super(context, attrs, defStyle, privateBrowsing); |
| 57 | this.setJavascriptInterfaces(javascriptInterfaces); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param context |
| 62 | * @param attrs |
| 63 | * @param defStyle |
| 64 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 65 | public BrowserWebView( |
Stewart Chao | e0e132e | 2014-12-01 18:28:22 -0500 | [diff] [blame] | 66 | Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing, boolean backgroundTab) { |
| 67 | super(context, attrs, defStyle, privateBrowsing, backgroundTab); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param context |
| 72 | * @param attrs |
| 73 | * @param defStyle |
| 74 | */ |
| 75 | public BrowserWebView( |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 76 | Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 77 | super(context, attrs, defStyle, privateBrowsing); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @param context |
| 82 | * @param attrs |
| 83 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 84 | public BrowserWebView(Context context, AttributeSet attrs) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 85 | super(context, attrs); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param context |
| 90 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 91 | public BrowserWebView(Context context) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 92 | super(context); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 95 | @Override |
| 96 | public void setWebChromeClient(WebChromeClient client) { |
| 97 | mWebChromeClient = client; |
| 98 | super.setWebChromeClient(client); |
| 99 | } |
| 100 | |
| 101 | public WebChromeClient getWebChromeClient() { |
| 102 | return mWebChromeClient; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void setWebViewClient(WebViewClient client) { |
| 107 | mWebViewClient = client; |
| 108 | super.setWebViewClient(client); |
| 109 | } |
| 110 | |
| 111 | public WebViewClient getWebViewClient() { |
| 112 | return mWebViewClient; |
| 113 | } |
| 114 | |
Michael Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 115 | public void setTitleBar(TitleBar title) { |
| 116 | mTitleBar = title; |
Vivek Sekhar | 6bd3217 | 2015-05-06 15:48:07 -0700 | [diff] [blame] | 117 | enableTopControls(true); |
| 118 | } |
| 119 | |
| 120 | public void enableTopControls(boolean shinkViewport) { |
| 121 | Resources res = getContext().getResources(); |
| 122 | int titlebarHeight = (int) (res.getDimension(R.dimen.toolbar_height) |
| 123 | / res.getDisplayMetrics().density); |
| 124 | setTopControlsHeight(titlebarHeight, shinkViewport); |
Michael Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 127 | // From TitleBarDelegate |
Michael Kolb | 08a687a | 2011-04-22 16:13:02 -0700 | [diff] [blame] | 128 | @Override |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 129 | public int getTitleHeight() { |
Michael Kolb | 29ccf8a | 2011-02-23 16:13:24 -0800 | [diff] [blame] | 130 | return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0; |
| 131 | } |
| 132 | |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 133 | // From TitleBarDelegate |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 134 | @Override |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 135 | public void onSetEmbeddedTitleBar(final View title) { |
Jonathan Dixon | e1d6dfc | 2012-12-17 13:39:17 -0800 | [diff] [blame] | 136 | // TODO: Remove this method; it is never invoked. |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 139 | public boolean hasTitleBar() { |
| 140 | return (mTitleBar != null); |
| 141 | } |
| 142 | |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 143 | @Override |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 144 | public void onDraw(Canvas c) { |
Michael Kolb | f262892 | 2011-03-09 17:15:28 -0800 | [diff] [blame] | 145 | super.onDraw(c); |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 146 | |
| 147 | // if enabled, removes the background from the main view (assumes coverage with opaqueness) |
| 148 | if (ENABLE_ROOTVIEW_BACKREMOVAL_OPTIMIZATION) { |
| 149 | if (!mBackgroundRemoved && getRootView().getBackground() != null) { |
| 150 | mBackgroundRemoved = true; |
| 151 | post(new Runnable() { |
| 152 | public void run() { |
| 153 | getRootView().setBackgroundDrawable(null); |
| 154 | } |
| 155 | }); |
| 156 | } |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 160 | public void drawContent(Canvas c) { |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 161 | //super.drawContent(c); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 162 | } |
| 163 | |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 164 | @Override |
Vivek Sekhar | d4de616 | 2015-07-21 15:01:45 -0700 | [diff] [blame^] | 165 | public boolean onTouchEvent(MotionEvent event) { |
| 166 | // block touch event if title bar is selected |
| 167 | if (mTitleBar.isEditingUrl()) { |
| 168 | requestFocus(); |
| 169 | return true; |
| 170 | } |
| 171 | else |
| 172 | return super.onTouchEvent(event); |
| 173 | } |
| 174 | |
| 175 | @Override |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 176 | public void onScrollChanged(int l, int t, int oldl, int oldt) { |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 177 | // 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] | 178 | super.onScrollChanged(l, t, oldl, oldt); |
Michael Kolb | 4923c22 | 2012-04-02 16:18:36 -0700 | [diff] [blame] | 179 | if (mTitleBar != null) { |
| 180 | mTitleBar.onScrollChanged(); |
| 181 | } |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 182 | if (mOnScrollChangedListener != null) { |
| 183 | mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | public void setOnScrollChangedListener(OnScrollChangedListener listener) { |
| 188 | mOnScrollChangedListener = listener; |
| 189 | } |
| 190 | |
John Reck | bd54631 | 2011-09-19 11:47:52 -0700 | [diff] [blame] | 191 | @Override |
| 192 | public boolean showContextMenuForChild(View originalView) { |
| 193 | return false; |
| 194 | } |
| 195 | |
John Reck | d1d8731 | 2012-03-08 13:25:00 -0800 | [diff] [blame] | 196 | @Override |
| 197 | public void destroy() { |
| 198 | BrowserSettings.getInstance().stopManagingSettings(getSettings()); |
| 199 | super.destroy(); |
| 200 | } |
| 201 | |
Sagar Dhawan | 49f85cf | 2015-07-10 16:54:20 -0700 | [diff] [blame] | 202 | @Override |
| 203 | public boolean dispatchKeyEventPreIme(KeyEvent event) { |
| 204 | Tab currentTab = mTitleBar.getUiController().getCurrentTab(); |
| 205 | if (currentTab != null && currentTab.isKeyboardShowing()){ |
| 206 | // Try to detect the "back" key that dismisses the keyboard |
| 207 | if(event.getAction() == KeyEvent.ACTION_DOWN && |
| 208 | event.getKeyCode() == KeyEvent.KEYCODE_BACK) |
| 209 | mWebViewClient.onKeyboardStateChange(false); |
| 210 | } |
| 211 | return super.dispatchKeyEventPreIme(event); |
| 212 | } |
| 213 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 214 | } |