blob: f5851df667c224693ad49f0093b27bb734da01f8 [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
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
17package com.android.browser;
18
19import android.app.ActionBar;
20import android.app.Activity;
21import android.content.Context;
22import android.content.res.Configuration;
23import android.content.res.Resources;
24import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
26import android.graphics.PixelFormat;
27import android.graphics.drawable.Drawable;
28import android.os.Bundle;
29import android.text.TextUtils;
30import android.util.Log;
31import android.view.ActionMode;
32import android.view.Gravity;
33import android.view.LayoutInflater;
34import android.view.Menu;
35import android.view.MenuItem;
36import android.view.View;
Michael Kolb1514bb72010-11-22 09:11:48 -080037import android.view.View.OnClickListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070038import android.view.ViewGroup;
Michael Kolb1514bb72010-11-22 09:11:48 -080039import android.view.ViewGroup.LayoutParams;
Michael Kolb8233fac2010-10-26 16:08:53 -070040import android.view.WindowManager;
41import android.webkit.WebChromeClient;
42import android.webkit.WebHistoryItem;
43import android.webkit.WebView;
44import android.widget.FrameLayout;
Michael Kolb1514bb72010-11-22 09:11:48 -080045import android.widget.ImageButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.widget.LinearLayout;
47import android.widget.Toast;
48
Michael Kolb1bf23132010-11-19 12:55:12 -080049import java.util.List;
50
Michael Kolb8233fac2010-10-26 16:08:53 -070051/**
52 * UI interface definitions
53 */
54public class BaseUi implements UI, WebViewFactory {
55
56 private static final String LOGTAG = "BaseUi";
57
58 private static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
59 new FrameLayout.LayoutParams(
60 ViewGroup.LayoutParams.MATCH_PARENT,
61 ViewGroup.LayoutParams.MATCH_PARENT);
62
63 private static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
64 new FrameLayout.LayoutParams(
65 ViewGroup.LayoutParams.MATCH_PARENT,
66 ViewGroup.LayoutParams.MATCH_PARENT,
67 Gravity.CENTER);
68
69 Activity mActivity;
70 UiController mUiController;
71 TabControl mTabControl;
Michael Kolb77df4562010-11-19 14:49:34 -080072 private Tab mActiveTab;
Michael Kolb8233fac2010-10-26 16:08:53 -070073
74 private Drawable mSecLockIcon;
75 private Drawable mMixLockIcon;
76
77 private boolean mXLargeScreenSize;
78 private FrameLayout mBrowserFrameLayout;
79 private FrameLayout mContentView;
80 private FrameLayout mCustomViewContainer;
81 private TitleBarBase mTitleBar;
82 private TitleBarBase mFakeTitleBar;
83 private TabBar mTabBar;
84
85 private View mCustomView;
86 private WebChromeClient.CustomViewCallback mCustomViewCallback;
87
88 private CombinedBookmarkHistoryView mComboView;
89
90 private LinearLayout mErrorConsoleContainer = null;
91
92 private Toast mStopToast;
93 private ActiveTabsPage mActiveTabsPage;
94
95 // the default <video> poster
96 private Bitmap mDefaultVideoPoster;
97 // the video progress view
98 private View mVideoProgressView;
99
100 boolean mExtendedMenuOpen;
101 boolean mOptionsMenuOpen;
102
103 private boolean mActivityPaused;
104
105 public BaseUi(Activity browser, UiController controller) {
106 mActivity = browser;
107 mUiController = controller;
108 mTabControl = controller.getTabControl();
109 Resources res = mActivity.getResources();
110 mSecLockIcon = res.getDrawable(R.drawable.ic_secure);
111 mMixLockIcon = res.getDrawable(R.drawable.ic_partial_secure);
112
113
114 mXLargeScreenSize = (res.getConfiguration().screenLayout
115 & Configuration.SCREENLAYOUT_SIZE_MASK)
116 == Configuration.SCREENLAYOUT_SIZE_XLARGE;
117
118 FrameLayout frameLayout = (FrameLayout) mActivity.getWindow()
119 .getDecorView().findViewById(android.R.id.content);
120 mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(mActivity)
121 .inflate(R.layout.custom_screen, null);
122 mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(
123 R.id.main_content);
124 mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout
125 .findViewById(R.id.error_console);
126 mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
127 .findViewById(R.id.fullscreen_custom_content);
128 frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
129
130 if (mXLargeScreenSize) {
131 mTitleBar = new TitleBarXLarge(mActivity, mUiController);
132 mTitleBar.setProgress(100);
133 mFakeTitleBar = new TitleBarXLarge(mActivity, mUiController);
134 ActionBar actionBar = mActivity.getActionBar();
135 mTabBar = new TabBar(mActivity, mUiController, this);
136 actionBar.setCustomNavigationMode(mTabBar);
137 } else {
138 mTitleBar = new TitleBar(mActivity, mUiController);
139 // mTitleBar will be always be shown in the fully loaded mode on
140 // phone
141 mTitleBar.setProgress(100);
142 mFakeTitleBar = new TitleBar(mActivity, mUiController);
143 }
144 }
145
146 // webview factory
147
148 @Override
149 public WebView createWebView(boolean privateBrowsing) {
150 // Create a new WebView
151 ScrollWebView w = new ScrollWebView(mActivity, null,
152 android.R.attr.webViewStyle, privateBrowsing);
153 w.setScrollbarFadingEnabled(true);
154 w.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
155 w.setMapTrackballToArrowKeys(false); // use trackball directly
156 // Enable the built-in zoom
157 w.getSettings().setBuiltInZoomControls(true);
158 if (mXLargeScreenSize) {
159 w.setScrollListener(mTabBar);
160 w.getSettings().setDisplayZoomControls(false);
161 }
162
163 // Add this WebView to the settings observer list and update the
164 // settings
165 final BrowserSettings s = BrowserSettings.getInstance();
166 s.addObserver(w.getSettings()).update(s, null);
167 return w;
168 }
169
Michael Kolb1514bb72010-11-22 09:11:48 -0800170 @Override
171 public WebView createSubWebView(boolean privateBrowsing) {
172 ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing);
173 if (mXLargeScreenSize) {
174 // no scroll listener for subview
175 web.setScrollListener(null);
176 }
177 return web;
178 }
179
Michael Kolb8233fac2010-10-26 16:08:53 -0700180 void stopWebViewScrolling() {
181 ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView();
182 if (web != null) {
183 web.stopScroll();
184 }
185 }
186
187 private void cancelStopToast() {
188 if (mStopToast != null) {
189 mStopToast.cancel();
190 mStopToast = null;
191 }
192 }
193
194 // lifecycle
195
196 public void onPause() {
197 // FIXME: This removes the active tabs page and resets the menu to
198 // MAIN_MENU. A better solution might be to do this work in onNewIntent
199 // but then we would need to save it in onSaveInstanceState and restore
200 // it in onCreate/onRestoreInstanceState
201 if (mActiveTabsPage != null) {
202 mUiController.removeActiveTabsPage(true);
203 }
Michael Kolb7a5cf472010-11-30 13:23:53 -0800204 if (isCustomViewShowing()) {
205 onHideCustomView();
206 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 cancelStopToast();
208 mActivityPaused = true;
209 }
210
211 public void onResume() {
212 mActivityPaused = false;
213 }
214
215 public void onDestroy() {
216 hideFakeTitleBar();
217 }
218
219 public void onConfigurationChanged(Configuration config) {
220 }
221
222 // key handling
223
224 @Override
225 public boolean onBackKey() {
226 if (mActiveTabsPage != null) {
227 // if tab page is showing, hide it
228 mUiController.removeActiveTabsPage(true);
229 return true;
230 }
231 if (mComboView != null) {
232 if (!mComboView.onBackPressed()) {
233 mUiController.removeComboView();
234 }
235 return true;
236 }
237 if (mCustomView != null) {
238 mUiController.hideCustomView();
239 return true;
240 }
241 return false;
242 }
243
244 // WebView callbacks
245
246 @Override
247 public void onPageStarted(Tab tab, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700248 if (tab.inForeground()) {
249 resetLockIcon(tab, url);
250 setUrlTitle(tab, url, null);
251 setFavicon(tab, favicon);
252 }
John Reckef074262010-12-02 16:09:14 -0800253 if (mXLargeScreenSize) {
254 mTabBar.onPageStarted(tab, url, favicon);
255 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700256 }
257
258 @Override
259 public void onPageFinished(Tab tab, String url) {
260 if (mXLargeScreenSize) {
261 mTabBar.onPageFinished(tab);
262 }
263 if (tab.inForeground()) {
264 // Reset the title and icon in case we stopped a provisional load.
265 resetTitleAndIcon(tab);
266 // Update the lock icon image only once we are done loading
267 updateLockIconToLatest(tab);
268 }
269 }
270
271 @Override
272 public void onPageStopped(Tab tab) {
273 cancelStopToast();
274 if (tab.inForeground()) {
275 mStopToast = Toast
276 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
277 mStopToast.show();
278 }
279 }
280
281 @Override
282 public void onProgressChanged(Tab tab, int progress) {
283 if (mXLargeScreenSize) {
284 mTabBar.onProgress(tab, progress);
285 }
286 if (tab.inForeground()) {
287 mFakeTitleBar.setProgress(progress);
288 if (progress == 100) {
289 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
290 hideFakeTitleBar();
291 }
292 } else {
293 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
294 showFakeTitleBar();
295 }
296 }
297 }
298 }
299
300 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800301 public boolean needsRestoreAllTabs() {
302 return mXLargeScreenSize;
303 }
304
305 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700306 public void addTab(Tab tab) {
307 if (mXLargeScreenSize) {
308 mTabBar.onNewTab(tab);
309 }
310 }
311
312 @Override
313 public void setActiveTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800314 if ((tab != mActiveTab) && (mActiveTab != null)) {
315 removeTabFromContentView(mActiveTab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700316 }
Michael Kolb77df4562010-11-19 14:49:34 -0800317 mActiveTab = tab;
Michael Kolb8233fac2010-10-26 16:08:53 -0700318 attachTabToContentView(tab);
319 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
320
321 WebView view = tab.getWebView();
Michael Kolb77df4562010-11-19 14:49:34 -0800322 // TabControl.setCurrentTab has been called before this,
323 // so the tab is guaranteed to have a webview
324 if (view == null) {
325 Log.e(LOGTAG, "active tab with no webview detected");
326 return;
327 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700328 view.setEmbeddedTitleBar(mTitleBar);
329 if (tab.isInVoiceSearchMode()) {
330 showVoiceTitleBar(tab.getVoiceDisplayTitle());
331 } else {
332 revertVoiceTitleBar(tab);
333 }
334
335 if (mXLargeScreenSize) {
336 // Request focus on the top window.
337 mTabBar.onSetActiveTab(tab);
338 }
339 resetTitleIconAndProgress(tab);
340 updateLockIconToLatest(tab);
341 tab.getTopWindow().requestFocus();
342 }
343
344 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800345 public void updateTabs(List<Tab> tabs) {
346 if (mXLargeScreenSize) {
347 mTabBar.updateTabs(tabs);
348 }
349 }
350
351 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700352 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800353 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700354 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800355 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700356 }
357 if (mXLargeScreenSize) {
358 mTabBar.onRemoveTab(tab);
359 }
360 }
361
362 @Override
363 public void detachTab(Tab tab) {
364 removeTabFromContentView(tab);
365 }
366
367 @Override
368 public void attachTab(Tab tab) {
369 attachTabToContentView(tab);
370 }
371
372 private void attachTabToContentView(Tab tab) {
373 if (tab.getWebView() == null) {
374 return;
375 }
376 View container = tab.getViewContainer();
377 WebView mainView = tab.getWebView();
378 // Attach the WebView to the container and then attach the
379 // container to the content view.
380 FrameLayout wrapper =
381 (FrameLayout) container.findViewById(R.id.webview_wrapper);
382 ViewGroup parent = (ViewGroup) mainView.getParent();
383 if (parent != wrapper) {
384 if (parent != null) {
385 Log.w(LOGTAG, "mMainView already has a parent in"
386 + " attachTabToContentView!");
387 parent.removeView(mainView);
388 }
389 wrapper.addView(mainView);
390 } else {
391 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
392 + " attachTabToContentView!");
393 }
394 parent = (ViewGroup) container.getParent();
395 if (parent != mContentView) {
396 if (parent != null) {
397 Log.w(LOGTAG, "mContainer already has a parent in"
398 + " attachTabToContentView!");
399 parent.removeView(container);
400 }
401 mContentView.addView(container, COVER_SCREEN_PARAMS);
402 } else {
403 Log.w(LOGTAG, "mContainer is already attached to content in"
404 + " attachTabToContentView!");
405 }
406 mUiController.attachSubWindow(tab);
407 }
408
409 private void removeTabFromContentView(Tab tab) {
410 // Remove the container that contains the main WebView.
411 WebView mainView = tab.getWebView();
412 View container = tab.getViewContainer();
413 if (mainView == null) {
414 return;
415 }
416 // Remove the container from the content and then remove the
417 // WebView from the container. This will trigger a focus change
418 // needed by WebView.
419 FrameLayout wrapper =
420 (FrameLayout) container.findViewById(R.id.webview_wrapper);
421 wrapper.removeView(mainView);
422 mContentView.removeView(container);
423 mUiController.endActionMode();
424 mUiController.removeSubWindow(tab);
425 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
426 if (errorConsole != null) {
427 mErrorConsoleContainer.removeView(errorConsole);
428 }
429 mainView.setEmbeddedTitleBar(null);
430 }
431
Michael Kolba713ec82010-11-29 17:27:06 -0800432 @Override
433 public void onSetWebView(Tab tab, WebView webView) {
434 View container = tab.getViewContainer();
435 if (container == null) {
436 // The tab consists of a container view, which contains the main
437 // WebView, as well as any other UI elements associated with the tab.
438 container = mActivity.getLayoutInflater().inflate(R.layout.tab,
439 null);
440 tab.setViewContainer(container);
441 }
442 if (tab.getWebView() != webView) {
443 // Just remove the old one.
444 FrameLayout wrapper =
445 (FrameLayout) container.findViewById(R.id.webview_wrapper);
446 wrapper.removeView(tab.getWebView());
447 }
448 }
449
Michael Kolb8233fac2010-10-26 16:08:53 -0700450 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800451 * create a sub window container and webview for the tab
452 * Note: this methods operates through side-effects for now
453 * it sets both the subView and subViewContainer for the given tab
454 * @param tab tab to create the sub window for
455 * @param subView webview to be set as a subwindow for the tab
456 */
457 @Override
458 public void createSubWindow(Tab tab, WebView subView) {
459 View subViewContainer = mActivity.getLayoutInflater().inflate(
460 R.layout.browser_subwindow, null);
461 ViewGroup inner = (ViewGroup) subViewContainer
462 .findViewById(R.id.inner_container);
463 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
464 LayoutParams.MATCH_PARENT));
465 final ImageButton cancel = (ImageButton) subViewContainer
466 .findViewById(R.id.subwindow_close);
467 final WebView cancelSubView = subView;
468 cancel.setOnClickListener(new OnClickListener() {
469 public void onClick(View v) {
470 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
471 }
472 });
473 tab.setSubWebView(subView);
474 tab.setSubViewContainer(subViewContainer);
475 }
476
477 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700478 * Remove the sub window from the content view.
479 */
480 @Override
481 public void removeSubWindow(View subviewContainer) {
482 mContentView.removeView(subviewContainer);
483 mUiController.endActionMode();
484 }
485
486 /**
487 * Attach the sub window to the content view.
488 */
489 @Override
490 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800491 if (container.getParent() != null) {
492 // already attached, remove first
493 ((ViewGroup) container.getParent()).removeView(container);
494 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700495 mContentView.addView(container, COVER_SCREEN_PARAMS);
496 }
497
498 void showFakeTitleBar() {
499 if (!isFakeTitleBarShowing() && mActiveTabsPage == null &&
500 !mActivityPaused) {
501 WebView mainView = mUiController.getCurrentWebView();
502 // if there is no current WebView, don't show the faked title bar;
503 if (mainView == null) {
504 return;
505 }
506 // Do not need to check for null, since the current tab will have
507 // at least a main WebView, or we would have returned above.
508 if (mUiController.isInCustomActionMode()) {
509 // Do not show the fake title bar, while a custom ActionMode
510 // (i.e. find or select) is showing.
511 return;
512 }
513 if (mXLargeScreenSize) {
514 mContentView.addView(mFakeTitleBar);
515 mTabBar.onShowTitleBar();
516 } else {
517 WindowManager manager = (WindowManager)
518 mActivity.getSystemService(Context.WINDOW_SERVICE);
519
520 // Add the title bar to the window manager so it can receive
521 // touches
522 // while the menu is up
523 WindowManager.LayoutParams params =
524 new WindowManager.LayoutParams(
525 ViewGroup.LayoutParams.MATCH_PARENT,
526 ViewGroup.LayoutParams.WRAP_CONTENT,
527 WindowManager.LayoutParams.TYPE_APPLICATION,
528 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
529 PixelFormat.TRANSLUCENT);
530 params.gravity = Gravity.TOP;
531 boolean atTop = mainView.getScrollY() == 0;
532 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
533 manager.addView(mFakeTitleBar, params);
534 }
535 }
536 }
537
538 void hideFakeTitleBar() {
539 if (!isFakeTitleBarShowing()) return;
540 if (mXLargeScreenSize) {
541 mContentView.removeView(mFakeTitleBar);
542 mTabBar.onHideTitleBar();
543 } else {
544 WindowManager.LayoutParams params =
545 (WindowManager.LayoutParams) mFakeTitleBar.getLayoutParams();
546 WebView mainView = mUiController.getCurrentWebView();
547 // Although we decided whether or not to animate based on the
548 // current
549 // scroll position, the scroll position may have changed since the
550 // fake title bar was displayed. Make sure it has the appropriate
551 // animation/lack thereof before removing.
552 params.windowAnimations =
553 mainView != null && mainView.getScrollY() == 0 ?
554 0 : R.style.TitleBar;
555 WindowManager manager = (WindowManager) mActivity
556 .getSystemService(Context.WINDOW_SERVICE);
557 manager.updateViewLayout(mFakeTitleBar, params);
558 manager.removeView(mFakeTitleBar);
559 }
560 }
561
562 boolean isFakeTitleBarShowing() {
563 return (mFakeTitleBar.getParent() != null);
564 }
565
566 @Override
567 public void showComboView(boolean startWithHistory, Bundle extras) {
568 mComboView = new CombinedBookmarkHistoryView(mActivity,
569 mUiController,
570 startWithHistory ?
571 CombinedBookmarkHistoryView.FRAGMENT_ID_HISTORY
572 : CombinedBookmarkHistoryView.FRAGMENT_ID_BOOKMARKS,
573 extras);
574 mTitleBar.setVisibility(View.GONE);
575 hideFakeTitleBar();
576 mContentView.addView(mComboView, COVER_SCREEN_PARAMS);
577 }
578
579 /**
580 * dismiss the ComboPage
581 */
582 @Override
583 public void hideComboView() {
584 if (mComboView != null) {
585 mContentView.removeView(mComboView);
586 mTitleBar.setVisibility(View.VISIBLE);
587 mComboView = null;
588 }
589 }
590
591 @Override
592 public void showCustomView(View view,
593 WebChromeClient.CustomViewCallback callback) {
594 // if a view already exists then immediately terminate the new one
595 if (mCustomView != null) {
596 callback.onCustomViewHidden();
597 return;
598 }
599
600 // Add the custom view to its container.
601 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
602 mCustomView = view;
603 mCustomViewCallback = callback;
604 // Hide the content view.
605 mContentView.setVisibility(View.GONE);
606 // Finally show the custom view container.
607 setStatusBarVisibility(false);
608 mCustomViewContainer.setVisibility(View.VISIBLE);
609 mCustomViewContainer.bringToFront();
610 }
611
612 @Override
613 public void onHideCustomView() {
614 if (mCustomView == null)
615 return;
616
617 // Hide the custom view.
618 mCustomView.setVisibility(View.GONE);
619 // Remove the custom view from its container.
620 mCustomViewContainer.removeView(mCustomView);
621 mCustomView = null;
622 mCustomViewContainer.setVisibility(View.GONE);
623 mCustomViewCallback.onCustomViewHidden();
624 // Show the content view.
625 setStatusBarVisibility(true);
626 mContentView.setVisibility(View.VISIBLE);
627 }
628
629 @Override
630 public boolean isCustomViewShowing() {
631 return mCustomView != null;
632 }
633
634 @Override
635 public void showVoiceTitleBar(String title) {
636 mTitleBar.setInVoiceMode(true);
637 mTitleBar.setDisplayTitle(title);
638 mFakeTitleBar.setInVoiceMode(true);
639 mFakeTitleBar.setDisplayTitle(title);
640 }
641
642 @Override
643 public void revertVoiceTitleBar(Tab tab) {
644 mTitleBar.setInVoiceMode(false);
645 String url = tab.getCurrentUrl();
646 mTitleBar.setDisplayTitle(url);
647 mFakeTitleBar.setInVoiceMode(false);
648 mFakeTitleBar.setDisplayTitle(url);
649 }
650
651 // -------------------------------------------------------------------------
652
653 @Override
654 public void resetTitleAndRevertLockIcon(Tab tab) {
655 tab.revertLockIcon();
656 updateLockIconToLatest(tab);
657 resetTitleIconAndProgress(tab);
658 }
659
660 /**
661 * Resets the lock icon. This method is called when we start a new load and
662 * know the url to be loaded.
663 */
664 private void resetLockIcon(Tab tab, String url) {
665 // Save the lock-icon state (we revert to it if the load gets cancelled)
666 tab.resetLockIcon(url);
667 updateLockIconImage(Tab.LOCK_ICON_UNSECURE);
668 }
669
670 /**
671 * Update the lock icon to correspond to our latest state.
672 */
673 private void updateLockIconToLatest(Tab t) {
674 if (t != null) {
675 updateLockIconImage(t.getLockIconType());
676 }
677 }
678
679 /**
680 * Reset the title, favicon, and progress.
681 */
682 private void resetTitleIconAndProgress(Tab tab) {
683 WebView current = tab.getWebView();
684 if (current == null) {
685 return;
686 }
687 resetTitleAndIcon(current);
688 int progress = current.getProgress();
689 current.getWebChromeClient().onProgressChanged(current, progress);
690 }
691
692 @Override
693 public void resetTitleAndIcon(Tab tab) {
694 WebView current = tab.getWebView();
695 if (current != null) {
696 resetTitleAndIcon(current);
697 }
698 }
699
700 // Reset the title and the icon based on the given item.
701 private void resetTitleAndIcon(WebView view) {
702 WebHistoryItem item = view.copyBackForwardList().getCurrentItem();
703 Tab tab = mTabControl.getTabFromView(view);
704 if (item != null) {
705 setUrlTitle(tab, item.getUrl(), item.getTitle());
706 setFavicon(tab, item.getFavicon());
707 } else {
John Reckef074262010-12-02 16:09:14 -0800708 setUrlTitle(tab, null, mActivity.getString(R.string.new_tab));
Michael Kolb8233fac2010-10-26 16:08:53 -0700709 setFavicon(tab, null);
710 }
711 }
712
713 /**
714 * Updates the lock-icon image in the title-bar.
715 */
716 private void updateLockIconImage(int lockIconType) {
717 Drawable d = null;
718 if (lockIconType == Tab.LOCK_ICON_SECURE) {
719 d = mSecLockIcon;
720 } else if (lockIconType == Tab.LOCK_ICON_MIXED) {
721 d = mMixLockIcon;
722 }
723 mTitleBar.setLock(d);
724 mFakeTitleBar.setLock(d);
725 }
726
727 // active tabs page
728
729 public void showActiveTabsPage() {
730 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
731 mTitleBar.setVisibility(View.GONE);
732 hideFakeTitleBar();
733 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
734 mActiveTabsPage.requestFocus();
735 }
736
737 /**
738 * Remove the active tabs page.
739 * @param needToAttach If true, the active tabs page did not attach a tab
740 * to the content view, so we need to do that here.
741 */
742 public void removeActiveTabsPage() {
743 mContentView.removeView(mActiveTabsPage);
744 mTitleBar.setVisibility(View.VISIBLE);
745 mActiveTabsPage = null;
746 }
747
748 // action mode callbacks
749
750 @Override
751 public void onActionModeStarted(ActionMode mode) {
752 // hide the fake title bar when CAB is shown
753 hideFakeTitleBar();
754 }
755
756 @Override
757 public void onActionModeFinished(boolean inLoad) {
758 if (inLoad) {
759 // the titlebar was removed when the CAB was shown
760 // if the page is loading, show it again
761 showFakeTitleBar();
762 }
763 }
764
765 // menu handling callbacks
766
767 @Override
768 public void onOptionsMenuOpened() {
769 mOptionsMenuOpen = true;
770 // options menu opened, show fake title bar
771 showFakeTitleBar();
772 }
773
774 @Override
775 public void onExtendedMenuOpened() {
776 // Switching the menu to expanded view, so hide the
777 // title bar.
778 mExtendedMenuOpen = true;
779 hideFakeTitleBar();
780 }
781
782 @Override
783 public void onOptionsMenuClosed(boolean inLoad) {
784 mOptionsMenuOpen = false;
785 if (!inLoad) {
786 hideFakeTitleBar();
787 }
788 }
789
790 @Override
791 public void onExtendedMenuClosed(boolean inLoad) {
792 mExtendedMenuOpen = false;
793 if (inLoad) {
794 showFakeTitleBar();
795 }
796 }
797
798 @Override
799 public void onContextMenuCreated(Menu menu) {
800 hideFakeTitleBar();
801 }
802
803 @Override
804 public void onContextMenuClosed(Menu menu, boolean inLoad) {
805 if (inLoad) {
806 showFakeTitleBar();
807 }
808 }
809
810 @Override
811 public void onScroll(boolean titleVisible) {
812 if (mTabBar != null) {
813 mTabBar.onScroll(titleVisible);
814 }
815 }
816
817 // error console
818
819 @Override
820 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
821 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
822 if (flag) {
823 // Setting the show state of the console will cause it's the layout
824 // to be inflated.
825 if (errorConsole.numberOfErrors() > 0) {
826 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
827 } else {
828 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
829 }
830 if (errorConsole.getParent() != null) {
831 mErrorConsoleContainer.removeView(errorConsole);
832 }
833 // Now we can add it to the main view.
834 mErrorConsoleContainer.addView(errorConsole,
835 new LinearLayout.LayoutParams(
836 ViewGroup.LayoutParams.MATCH_PARENT,
837 ViewGroup.LayoutParams.WRAP_CONTENT));
838 } else {
839 mErrorConsoleContainer.removeView(errorConsole);
840 }
841 }
842
843 private void setStatusBarVisibility(boolean visible) {
844 int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
845 mActivity.getWindow().setFlags(flag,
846 WindowManager.LayoutParams.FLAG_FULLSCREEN);
847 }
848
849 @Override
850 public void setUrlTitle(Tab tab, String url, String title) {
851 if (TextUtils.isEmpty(title)) {
John Reckef074262010-12-02 16:09:14 -0800852 title = url;
Michael Kolb8233fac2010-10-26 16:08:53 -0700853 }
854 if (tab.isInVoiceSearchMode()) return;
855 if (tab.inForeground()) {
856 mTitleBar.setDisplayTitle(url);
857 mFakeTitleBar.setDisplayTitle(url);
858 }
859 if (mXLargeScreenSize) {
860 mTabBar.onUrlAndTitle(tab, url, title);
861 }
862 }
863
864 // Set the favicon in the title bar.
865 @Override
866 public void setFavicon(Tab tab, Bitmap icon) {
867 mTitleBar.setFavicon(icon);
868 mFakeTitleBar.setFavicon(icon);
869 if (mXLargeScreenSize) {
870 mTabBar.onFavicon(tab, icon);
871 }
872 }
873 @Override
874 public boolean showsWeb() {
875 return mCustomView == null && mActiveTabsPage == null
876 && mComboView == null;
877 }
878
879 @Override
880 public void onPrepareOptionsMenu(Menu menu) {
881 if (!mXLargeScreenSize) {
882 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
883 newtab.setEnabled(mUiController.getTabControl().canCreateNewTab());
884 }
885 }
886
887 // -------------------------------------------------------------------------
888 // Helper function for WebChromeClient
889 // -------------------------------------------------------------------------
890
891 @Override
892 public Bitmap getDefaultVideoPoster() {
893 if (mDefaultVideoPoster == null) {
894 mDefaultVideoPoster = BitmapFactory.decodeResource(
895 mActivity.getResources(), R.drawable.default_video_poster);
896 }
897 return mDefaultVideoPoster;
898 }
899
900 @Override
901 public View getVideoLoadingProgressView() {
902 if (mVideoProgressView == null) {
903 LayoutInflater inflater = LayoutInflater.from(mActivity);
904 mVideoProgressView = inflater.inflate(
905 R.layout.video_loading_progress, null);
906 }
907 return mVideoProgressView;
908 }
909
910}