Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 20 | import android.content.res.Configuration; |
| 21 | import android.content.res.Resources; |
| 22 | import android.graphics.Bitmap; |
| 23 | import android.graphics.BitmapFactory; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 24 | import android.graphics.drawable.Drawable; |
| 25 | import android.os.Bundle; |
| 26 | import android.text.TextUtils; |
| 27 | import android.util.Log; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 28 | import android.view.Gravity; |
| 29 | import android.view.LayoutInflater; |
| 30 | import android.view.Menu; |
| 31 | import android.view.MenuItem; |
| 32 | import android.view.View; |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 33 | import android.view.View.OnClickListener; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 34 | import android.view.ViewGroup; |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 35 | import android.view.ViewGroup.LayoutParams; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 36 | import android.view.WindowManager; |
Michael Kolb | 3a69628 | 2010-12-05 13:23:24 -0800 | [diff] [blame] | 37 | import android.view.inputmethod.InputMethodManager; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 38 | import android.webkit.WebChromeClient; |
| 39 | import android.webkit.WebHistoryItem; |
| 40 | import android.webkit.WebView; |
| 41 | import android.widget.FrameLayout; |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 42 | import android.widget.ImageButton; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 43 | import android.widget.LinearLayout; |
| 44 | import android.widget.Toast; |
| 45 | |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 46 | import java.util.List; |
| 47 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 48 | /** |
| 49 | * UI interface definitions |
| 50 | */ |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 51 | public abstract class BaseUi implements UI, WebViewFactory { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 52 | |
| 53 | private static final String LOGTAG = "BaseUi"; |
| 54 | |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 55 | protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 56 | new FrameLayout.LayoutParams( |
| 57 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 58 | ViewGroup.LayoutParams.MATCH_PARENT); |
| 59 | |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 60 | protected static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 61 | new FrameLayout.LayoutParams( |
| 62 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 63 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 64 | Gravity.CENTER); |
| 65 | |
| 66 | Activity mActivity; |
| 67 | UiController mUiController; |
| 68 | TabControl mTabControl; |
Michael Kolb | 77df456 | 2010-11-19 14:49:34 -0800 | [diff] [blame] | 69 | private Tab mActiveTab; |
Michael Kolb | 3a69628 | 2010-12-05 13:23:24 -0800 | [diff] [blame] | 70 | private InputMethodManager mInputManager; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 71 | |
| 72 | private Drawable mSecLockIcon; |
| 73 | private Drawable mMixLockIcon; |
| 74 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 75 | private FrameLayout mBrowserFrameLayout; |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 76 | protected FrameLayout mContentView; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 77 | private FrameLayout mCustomViewContainer; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 78 | |
| 79 | private View mCustomView; |
| 80 | private WebChromeClient.CustomViewCallback mCustomViewCallback; |
| 81 | |
| 82 | private CombinedBookmarkHistoryView mComboView; |
| 83 | |
| 84 | private LinearLayout mErrorConsoleContainer = null; |
| 85 | |
| 86 | private Toast mStopToast; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 87 | |
| 88 | // the default <video> poster |
| 89 | private Bitmap mDefaultVideoPoster; |
| 90 | // the video progress view |
| 91 | private View mVideoProgressView; |
| 92 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 93 | private boolean mActivityPaused; |
| 94 | |
| 95 | public BaseUi(Activity browser, UiController controller) { |
| 96 | mActivity = browser; |
| 97 | mUiController = controller; |
| 98 | mTabControl = controller.getTabControl(); |
| 99 | Resources res = mActivity.getResources(); |
Michael Kolb | 3a69628 | 2010-12-05 13:23:24 -0800 | [diff] [blame] | 100 | mInputManager = (InputMethodManager) |
| 101 | browser.getSystemService(Activity.INPUT_METHOD_SERVICE); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 102 | mSecLockIcon = res.getDrawable(R.drawable.ic_secure); |
| 103 | mMixLockIcon = res.getDrawable(R.drawable.ic_partial_secure); |
| 104 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 105 | FrameLayout frameLayout = (FrameLayout) mActivity.getWindow() |
| 106 | .getDecorView().findViewById(android.R.id.content); |
| 107 | mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(mActivity) |
| 108 | .inflate(R.layout.custom_screen, null); |
| 109 | mContentView = (FrameLayout) mBrowserFrameLayout.findViewById( |
| 110 | R.id.main_content); |
| 111 | mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout |
| 112 | .findViewById(R.id.error_console); |
| 113 | mCustomViewContainer = (FrameLayout) mBrowserFrameLayout |
| 114 | .findViewById(R.id.fullscreen_custom_content); |
| 115 | frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS); |
| 116 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 119 | /** |
| 120 | * common webview initialization |
| 121 | * @param w the webview to initialize |
| 122 | */ |
| 123 | protected void initWebViewSettings(WebView w) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 124 | w.setScrollbarFadingEnabled(true); |
| 125 | w.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); |
| 126 | w.setMapTrackballToArrowKeys(false); // use trackball directly |
| 127 | // Enable the built-in zoom |
| 128 | w.getSettings().setBuiltInZoomControls(true); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 129 | |
| 130 | // Add this WebView to the settings observer list and update the |
| 131 | // settings |
| 132 | final BrowserSettings s = BrowserSettings.getInstance(); |
| 133 | s.addObserver(w.getSettings()).update(s, null); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | private void cancelStopToast() { |
| 137 | if (mStopToast != null) { |
| 138 | mStopToast.cancel(); |
| 139 | mStopToast = null; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // lifecycle |
| 144 | |
| 145 | public void onPause() { |
Michael Kolb | 7a5cf47 | 2010-11-30 13:23:53 -0800 | [diff] [blame] | 146 | if (isCustomViewShowing()) { |
| 147 | onHideCustomView(); |
| 148 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 149 | cancelStopToast(); |
| 150 | mActivityPaused = true; |
| 151 | } |
| 152 | |
| 153 | public void onResume() { |
| 154 | mActivityPaused = false; |
| 155 | } |
| 156 | |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 157 | protected boolean isActivityPaused() { |
| 158 | return mActivityPaused; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | public void onConfigurationChanged(Configuration config) { |
| 162 | } |
| 163 | |
| 164 | // key handling |
| 165 | |
| 166 | @Override |
| 167 | public boolean onBackKey() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 168 | if (mComboView != null) { |
| 169 | if (!mComboView.onBackPressed()) { |
| 170 | mUiController.removeComboView(); |
| 171 | } |
| 172 | return true; |
| 173 | } |
| 174 | if (mCustomView != null) { |
| 175 | mUiController.hideCustomView(); |
| 176 | return true; |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | // WebView callbacks |
| 182 | |
| 183 | @Override |
| 184 | public void onPageStarted(Tab tab, String url, Bitmap favicon) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 185 | if (tab.inForeground()) { |
| 186 | resetLockIcon(tab, url); |
| 187 | setUrlTitle(tab, url, null); |
| 188 | setFavicon(tab, favicon); |
| 189 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | @Override |
Leon Scroggins | 4cd9779 | 2010-12-03 15:31:56 -0500 | [diff] [blame] | 193 | public void bookmarkedStatusHasChanged(Tab tab) { |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 194 | // no op in base case |
Leon Scroggins | 4cd9779 | 2010-12-03 15:31:56 -0500 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 198 | public void onPageFinished(Tab tab, String url) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 199 | if (tab.inForeground()) { |
| 200 | // Reset the title and icon in case we stopped a provisional load. |
| 201 | resetTitleAndIcon(tab); |
| 202 | // Update the lock icon image only once we are done loading |
| 203 | updateLockIconToLatest(tab); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void onPageStopped(Tab tab) { |
| 209 | cancelStopToast(); |
| 210 | if (tab.inForeground()) { |
| 211 | mStopToast = Toast |
| 212 | .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT); |
| 213 | mStopToast.show(); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | @Override |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 218 | public boolean needsRestoreAllTabs() { |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 219 | return false; |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 223 | public void addTab(Tab tab) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void setActiveTab(Tab tab) { |
Michael Kolb | 77df456 | 2010-11-19 14:49:34 -0800 | [diff] [blame] | 228 | if ((tab != mActiveTab) && (mActiveTab != null)) { |
| 229 | removeTabFromContentView(mActiveTab); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 230 | } |
Michael Kolb | 77df456 | 2010-11-19 14:49:34 -0800 | [diff] [blame] | 231 | mActiveTab = tab; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 232 | attachTabToContentView(tab); |
| 233 | setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 236 | Tab getActiveTab() { |
| 237 | return mActiveTab; |
| 238 | } |
| 239 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 240 | @Override |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 241 | public void updateTabs(List<Tab> tabs) { |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 245 | public void removeTab(Tab tab) { |
Michael Kolb | 77df456 | 2010-11-19 14:49:34 -0800 | [diff] [blame] | 246 | if (mActiveTab == tab) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 247 | removeTabFromContentView(tab); |
Michael Kolb | 77df456 | 2010-11-19 14:49:34 -0800 | [diff] [blame] | 248 | mActiveTab = null; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 249 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public void detachTab(Tab tab) { |
| 254 | removeTabFromContentView(tab); |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void attachTab(Tab tab) { |
| 259 | attachTabToContentView(tab); |
| 260 | } |
| 261 | |
| 262 | private void attachTabToContentView(Tab tab) { |
| 263 | if (tab.getWebView() == null) { |
| 264 | return; |
| 265 | } |
| 266 | View container = tab.getViewContainer(); |
| 267 | WebView mainView = tab.getWebView(); |
| 268 | // Attach the WebView to the container and then attach the |
| 269 | // container to the content view. |
| 270 | FrameLayout wrapper = |
| 271 | (FrameLayout) container.findViewById(R.id.webview_wrapper); |
| 272 | ViewGroup parent = (ViewGroup) mainView.getParent(); |
| 273 | if (parent != wrapper) { |
| 274 | if (parent != null) { |
| 275 | Log.w(LOGTAG, "mMainView already has a parent in" |
| 276 | + " attachTabToContentView!"); |
| 277 | parent.removeView(mainView); |
| 278 | } |
| 279 | wrapper.addView(mainView); |
| 280 | } else { |
| 281 | Log.w(LOGTAG, "mMainView is already attached to wrapper in" |
| 282 | + " attachTabToContentView!"); |
| 283 | } |
| 284 | parent = (ViewGroup) container.getParent(); |
| 285 | if (parent != mContentView) { |
| 286 | if (parent != null) { |
| 287 | Log.w(LOGTAG, "mContainer already has a parent in" |
| 288 | + " attachTabToContentView!"); |
| 289 | parent.removeView(container); |
| 290 | } |
| 291 | mContentView.addView(container, COVER_SCREEN_PARAMS); |
| 292 | } else { |
| 293 | Log.w(LOGTAG, "mContainer is already attached to content in" |
| 294 | + " attachTabToContentView!"); |
| 295 | } |
| 296 | mUiController.attachSubWindow(tab); |
| 297 | } |
| 298 | |
| 299 | private void removeTabFromContentView(Tab tab) { |
| 300 | // Remove the container that contains the main WebView. |
| 301 | WebView mainView = tab.getWebView(); |
| 302 | View container = tab.getViewContainer(); |
| 303 | if (mainView == null) { |
| 304 | return; |
| 305 | } |
| 306 | // Remove the container from the content and then remove the |
| 307 | // WebView from the container. This will trigger a focus change |
| 308 | // needed by WebView. |
| 309 | FrameLayout wrapper = |
| 310 | (FrameLayout) container.findViewById(R.id.webview_wrapper); |
| 311 | wrapper.removeView(mainView); |
| 312 | mContentView.removeView(container); |
| 313 | mUiController.endActionMode(); |
| 314 | mUiController.removeSubWindow(tab); |
| 315 | ErrorConsoleView errorConsole = tab.getErrorConsole(false); |
| 316 | if (errorConsole != null) { |
| 317 | mErrorConsoleContainer.removeView(errorConsole); |
| 318 | } |
| 319 | mainView.setEmbeddedTitleBar(null); |
| 320 | } |
| 321 | |
Michael Kolb | a713ec8 | 2010-11-29 17:27:06 -0800 | [diff] [blame] | 322 | @Override |
| 323 | public void onSetWebView(Tab tab, WebView webView) { |
| 324 | View container = tab.getViewContainer(); |
| 325 | if (container == null) { |
| 326 | // The tab consists of a container view, which contains the main |
| 327 | // WebView, as well as any other UI elements associated with the tab. |
| 328 | container = mActivity.getLayoutInflater().inflate(R.layout.tab, |
| 329 | null); |
| 330 | tab.setViewContainer(container); |
| 331 | } |
| 332 | if (tab.getWebView() != webView) { |
| 333 | // Just remove the old one. |
| 334 | FrameLayout wrapper = |
| 335 | (FrameLayout) container.findViewById(R.id.webview_wrapper); |
| 336 | wrapper.removeView(tab.getWebView()); |
| 337 | } |
| 338 | } |
| 339 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 340 | /** |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 341 | * create a sub window container and webview for the tab |
| 342 | * Note: this methods operates through side-effects for now |
| 343 | * it sets both the subView and subViewContainer for the given tab |
| 344 | * @param tab tab to create the sub window for |
| 345 | * @param subView webview to be set as a subwindow for the tab |
| 346 | */ |
| 347 | @Override |
| 348 | public void createSubWindow(Tab tab, WebView subView) { |
| 349 | View subViewContainer = mActivity.getLayoutInflater().inflate( |
| 350 | R.layout.browser_subwindow, null); |
| 351 | ViewGroup inner = (ViewGroup) subViewContainer |
| 352 | .findViewById(R.id.inner_container); |
| 353 | inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT, |
| 354 | LayoutParams.MATCH_PARENT)); |
| 355 | final ImageButton cancel = (ImageButton) subViewContainer |
| 356 | .findViewById(R.id.subwindow_close); |
| 357 | final WebView cancelSubView = subView; |
| 358 | cancel.setOnClickListener(new OnClickListener() { |
| 359 | public void onClick(View v) { |
| 360 | cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView); |
| 361 | } |
| 362 | }); |
| 363 | tab.setSubWebView(subView); |
| 364 | tab.setSubViewContainer(subViewContainer); |
| 365 | } |
| 366 | |
| 367 | /** |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 368 | * Remove the sub window from the content view. |
| 369 | */ |
| 370 | @Override |
| 371 | public void removeSubWindow(View subviewContainer) { |
| 372 | mContentView.removeView(subviewContainer); |
| 373 | mUiController.endActionMode(); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Attach the sub window to the content view. |
| 378 | */ |
| 379 | @Override |
| 380 | public void attachSubWindow(View container) { |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 381 | if (container.getParent() != null) { |
| 382 | // already attached, remove first |
| 383 | ((ViewGroup) container.getParent()).removeView(container); |
| 384 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 385 | mContentView.addView(container, COVER_SCREEN_PARAMS); |
| 386 | } |
| 387 | |
| 388 | void showFakeTitleBar() { |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 389 | if (!isFakeTitleBarShowing() && !isActivityPaused()) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 390 | WebView mainView = mUiController.getCurrentWebView(); |
| 391 | // if there is no current WebView, don't show the faked title bar; |
| 392 | if (mainView == null) { |
| 393 | return; |
| 394 | } |
| 395 | // Do not need to check for null, since the current tab will have |
| 396 | // at least a main WebView, or we would have returned above. |
| 397 | if (mUiController.isInCustomActionMode()) { |
| 398 | // Do not show the fake title bar, while a custom ActionMode |
| 399 | // (i.e. find or select) is showing. |
| 400 | return; |
| 401 | } |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 402 | attachFakeTitleBar(mainView); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 406 | protected abstract void attachFakeTitleBar(WebView mainView); |
| 407 | |
| 408 | protected abstract void hideFakeTitleBar(); |
| 409 | |
| 410 | protected abstract boolean isFakeTitleBarShowing(); |
| 411 | |
| 412 | protected abstract TitleBarBase getFakeTitleBar(); |
| 413 | |
| 414 | protected abstract TitleBarBase getEmbeddedTitleBar(); |
| 415 | |
| 416 | @Override |
| 417 | public void showVoiceTitleBar(String title) { |
| 418 | getEmbeddedTitleBar().setInVoiceMode(true); |
| 419 | getEmbeddedTitleBar().setDisplayTitle(title); |
| 420 | getFakeTitleBar().setInVoiceMode(true); |
| 421 | getFakeTitleBar().setDisplayTitle(title); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 424 | @Override |
| 425 | public void revertVoiceTitleBar(Tab tab) { |
| 426 | getEmbeddedTitleBar().setInVoiceMode(false); |
| 427 | String url = tab.getCurrentUrl(); |
| 428 | getEmbeddedTitleBar().setDisplayTitle(url); |
| 429 | getFakeTitleBar().setInVoiceMode(false); |
| 430 | getFakeTitleBar().setDisplayTitle(url); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | @Override |
| 434 | public void showComboView(boolean startWithHistory, Bundle extras) { |
John Reck | 439c9a5 | 2010-12-14 10:04:39 -0800 | [diff] [blame] | 435 | if (mComboView != null) { |
| 436 | return; |
| 437 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 438 | mComboView = new CombinedBookmarkHistoryView(mActivity, |
| 439 | mUiController, |
| 440 | startWithHistory ? |
| 441 | CombinedBookmarkHistoryView.FRAGMENT_ID_HISTORY |
| 442 | : CombinedBookmarkHistoryView.FRAGMENT_ID_BOOKMARKS, |
| 443 | extras); |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 444 | getEmbeddedTitleBar().setVisibility(View.GONE); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 445 | hideFakeTitleBar(); |
Michael Kolb | 3a69628 | 2010-12-05 13:23:24 -0800 | [diff] [blame] | 446 | dismissIME(); |
| 447 | if (mActiveTab != null) { |
| 448 | WebView web = mActiveTab.getWebView(); |
| 449 | mActiveTab.putInBackground(); |
| 450 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 451 | mContentView.addView(mComboView, COVER_SCREEN_PARAMS); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * dismiss the ComboPage |
| 456 | */ |
| 457 | @Override |
| 458 | public void hideComboView() { |
| 459 | if (mComboView != null) { |
| 460 | mContentView.removeView(mComboView); |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 461 | getEmbeddedTitleBar().setVisibility(View.VISIBLE); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 462 | mComboView = null; |
| 463 | } |
Michael Kolb | 3a69628 | 2010-12-05 13:23:24 -0800 | [diff] [blame] | 464 | if (mActiveTab != null) { |
| 465 | mActiveTab.putInForeground(); |
| 466 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | @Override |
| 470 | public void showCustomView(View view, |
| 471 | WebChromeClient.CustomViewCallback callback) { |
| 472 | // if a view already exists then immediately terminate the new one |
| 473 | if (mCustomView != null) { |
| 474 | callback.onCustomViewHidden(); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | // Add the custom view to its container. |
| 479 | mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER); |
| 480 | mCustomView = view; |
| 481 | mCustomViewCallback = callback; |
| 482 | // Hide the content view. |
| 483 | mContentView.setVisibility(View.GONE); |
| 484 | // Finally show the custom view container. |
| 485 | setStatusBarVisibility(false); |
| 486 | mCustomViewContainer.setVisibility(View.VISIBLE); |
| 487 | mCustomViewContainer.bringToFront(); |
| 488 | } |
| 489 | |
| 490 | @Override |
| 491 | public void onHideCustomView() { |
| 492 | if (mCustomView == null) |
| 493 | return; |
| 494 | |
| 495 | // Hide the custom view. |
| 496 | mCustomView.setVisibility(View.GONE); |
| 497 | // Remove the custom view from its container. |
| 498 | mCustomViewContainer.removeView(mCustomView); |
| 499 | mCustomView = null; |
| 500 | mCustomViewContainer.setVisibility(View.GONE); |
| 501 | mCustomViewCallback.onCustomViewHidden(); |
| 502 | // Show the content view. |
| 503 | setStatusBarVisibility(true); |
| 504 | mContentView.setVisibility(View.VISIBLE); |
| 505 | } |
| 506 | |
| 507 | @Override |
| 508 | public boolean isCustomViewShowing() { |
| 509 | return mCustomView != null; |
| 510 | } |
| 511 | |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 512 | protected void dismissIME() { |
Michael Kolb | 3a69628 | 2010-12-05 13:23:24 -0800 | [diff] [blame] | 513 | if (mInputManager.isActive()) { |
| 514 | mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(), |
| 515 | 0); |
| 516 | } |
| 517 | } |
| 518 | |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 519 | @Override |
| 520 | public boolean showsWeb() { |
| 521 | return mCustomView == null |
| 522 | && mComboView == null; |
| 523 | } |
| 524 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 525 | // ------------------------------------------------------------------------- |
| 526 | |
| 527 | @Override |
| 528 | public void resetTitleAndRevertLockIcon(Tab tab) { |
| 529 | tab.revertLockIcon(); |
| 530 | updateLockIconToLatest(tab); |
| 531 | resetTitleIconAndProgress(tab); |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Resets the lock icon. This method is called when we start a new load and |
| 536 | * know the url to be loaded. |
| 537 | */ |
| 538 | private void resetLockIcon(Tab tab, String url) { |
| 539 | // Save the lock-icon state (we revert to it if the load gets cancelled) |
| 540 | tab.resetLockIcon(url); |
| 541 | updateLockIconImage(Tab.LOCK_ICON_UNSECURE); |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Update the lock icon to correspond to our latest state. |
| 546 | */ |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 547 | protected void updateLockIconToLatest(Tab t) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 548 | if (t != null) { |
| 549 | updateLockIconImage(t.getLockIconType()); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Reset the title, favicon, and progress. |
| 555 | */ |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 556 | protected void resetTitleIconAndProgress(Tab tab) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 557 | WebView current = tab.getWebView(); |
| 558 | if (current == null) { |
| 559 | return; |
| 560 | } |
Michael Kolb | 41d3b94 | 2010-12-05 11:35:20 -0800 | [diff] [blame] | 561 | resetTitleAndIcon(tab, current); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 562 | int progress = current.getProgress(); |
| 563 | current.getWebChromeClient().onProgressChanged(current, progress); |
| 564 | } |
| 565 | |
| 566 | @Override |
| 567 | public void resetTitleAndIcon(Tab tab) { |
| 568 | WebView current = tab.getWebView(); |
| 569 | if (current != null) { |
Michael Kolb | 41d3b94 | 2010-12-05 11:35:20 -0800 | [diff] [blame] | 570 | resetTitleAndIcon(tab, current); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
| 574 | // Reset the title and the icon based on the given item. |
Michael Kolb | 41d3b94 | 2010-12-05 11:35:20 -0800 | [diff] [blame] | 575 | private void resetTitleAndIcon(Tab tab, WebView view) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 576 | WebHistoryItem item = view.copyBackForwardList().getCurrentItem(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 577 | if (item != null) { |
| 578 | setUrlTitle(tab, item.getUrl(), item.getTitle()); |
| 579 | setFavicon(tab, item.getFavicon()); |
| 580 | } else { |
John Reck | ef07426 | 2010-12-02 16:09:14 -0800 | [diff] [blame] | 581 | setUrlTitle(tab, null, mActivity.getString(R.string.new_tab)); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 582 | setFavicon(tab, null); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Updates the lock-icon image in the title-bar. |
| 588 | */ |
| 589 | private void updateLockIconImage(int lockIconType) { |
| 590 | Drawable d = null; |
| 591 | if (lockIconType == Tab.LOCK_ICON_SECURE) { |
| 592 | d = mSecLockIcon; |
| 593 | } else if (lockIconType == Tab.LOCK_ICON_MIXED) { |
| 594 | d = mMixLockIcon; |
| 595 | } |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 596 | getEmbeddedTitleBar().setLock(d); |
| 597 | getFakeTitleBar().setLock(d); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 600 | @Override |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 601 | public void setUrlTitle(Tab tab, String url, String title) { |
| 602 | if (TextUtils.isEmpty(title)) { |
| 603 | title = url; |
Michael Kolb | 81b6f83 | 2010-12-12 12:44:27 -0800 | [diff] [blame] | 604 | } |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 605 | if (tab.isInVoiceSearchMode()) return; |
| 606 | if (tab.inForeground()) { |
| 607 | getEmbeddedTitleBar().setDisplayTitle(url); |
| 608 | getFakeTitleBar().setDisplayTitle(url); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | // Set the favicon in the title bar. |
| 613 | @Override |
| 614 | public void setFavicon(Tab tab, Bitmap icon) { |
| 615 | getEmbeddedTitleBar().setFavicon(icon); |
| 616 | getFakeTitleBar().setFavicon(icon); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | @Override |
| 620 | public void onActionModeFinished(boolean inLoad) { |
| 621 | if (inLoad) { |
| 622 | // the titlebar was removed when the CAB was shown |
| 623 | // if the page is loading, show it again |
| 624 | showFakeTitleBar(); |
| 625 | } |
| 626 | } |
| 627 | |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 628 | // active tabs page |
| 629 | |
| 630 | public void showActiveTabsPage() { |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Remove the active tabs page. |
| 635 | */ |
| 636 | public void removeActiveTabsPage() { |
| 637 | } |
| 638 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 639 | // menu handling callbacks |
| 640 | |
| 641 | @Override |
| 642 | public void onOptionsMenuOpened() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | @Override |
| 646 | public void onExtendedMenuOpened() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | @Override |
| 650 | public void onOptionsMenuClosed(boolean inLoad) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | @Override |
| 654 | public void onExtendedMenuClosed(boolean inLoad) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | @Override |
| 658 | public void onContextMenuCreated(Menu menu) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | @Override |
| 662 | public void onContextMenuClosed(Menu menu, boolean inLoad) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | // error console |
| 666 | |
| 667 | @Override |
| 668 | public void setShouldShowErrorConsole(Tab tab, boolean flag) { |
| 669 | ErrorConsoleView errorConsole = tab.getErrorConsole(true); |
| 670 | if (flag) { |
| 671 | // Setting the show state of the console will cause it's the layout |
| 672 | // to be inflated. |
| 673 | if (errorConsole.numberOfErrors() > 0) { |
| 674 | errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED); |
| 675 | } else { |
| 676 | errorConsole.showConsole(ErrorConsoleView.SHOW_NONE); |
| 677 | } |
| 678 | if (errorConsole.getParent() != null) { |
| 679 | mErrorConsoleContainer.removeView(errorConsole); |
| 680 | } |
| 681 | // Now we can add it to the main view. |
| 682 | mErrorConsoleContainer.addView(errorConsole, |
| 683 | new LinearLayout.LayoutParams( |
| 684 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 685 | ViewGroup.LayoutParams.WRAP_CONTENT)); |
| 686 | } else { |
| 687 | mErrorConsoleContainer.removeView(errorConsole); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | private void setStatusBarVisibility(boolean visible) { |
| 692 | int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN; |
| 693 | mActivity.getWindow().setFlags(flag, |
| 694 | WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 695 | } |
| 696 | |
| 697 | @Override |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 698 | public void onPrepareOptionsMenu(Menu menu) { |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 699 | final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id); |
| 700 | newtab.setEnabled(mUiController.getTabControl().canCreateNewTab()); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | // ------------------------------------------------------------------------- |
| 704 | // Helper function for WebChromeClient |
| 705 | // ------------------------------------------------------------------------- |
| 706 | |
| 707 | @Override |
| 708 | public Bitmap getDefaultVideoPoster() { |
| 709 | if (mDefaultVideoPoster == null) { |
| 710 | mDefaultVideoPoster = BitmapFactory.decodeResource( |
| 711 | mActivity.getResources(), R.drawable.default_video_poster); |
| 712 | } |
| 713 | return mDefaultVideoPoster; |
| 714 | } |
| 715 | |
| 716 | @Override |
| 717 | public View getVideoLoadingProgressView() { |
| 718 | if (mVideoProgressView == null) { |
| 719 | LayoutInflater inflater = LayoutInflater.from(mActivity); |
| 720 | mVideoProgressView = inflater.inflate( |
| 721 | R.layout.video_loading_progress, null); |
| 722 | } |
| 723 | return mVideoProgressView; |
| 724 | } |
| 725 | |
Michael Kolb | 843510f | 2010-12-09 10:51:49 -0800 | [diff] [blame] | 726 | @Override |
| 727 | public void showMaxTabsWarning() { |
| 728 | Toast warning = Toast.makeText(mActivity, |
| 729 | mActivity.getString(R.string.max_tabs_warning), |
| 730 | Toast.LENGTH_SHORT); |
| 731 | warning.show(); |
| 732 | } |
| 733 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 734 | } |