blob: 5f8944fc8dda3714f48c406d0335f6ef7fbc0a3c [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 }
204 cancelStopToast();
205 mActivityPaused = true;
206 }
207
208 public void onResume() {
209 mActivityPaused = false;
210 }
211
212 public void onDestroy() {
213 hideFakeTitleBar();
214 }
215
216 public void onConfigurationChanged(Configuration config) {
217 }
218
219 // key handling
220
221 @Override
222 public boolean onBackKey() {
223 if (mActiveTabsPage != null) {
224 // if tab page is showing, hide it
225 mUiController.removeActiveTabsPage(true);
226 return true;
227 }
228 if (mComboView != null) {
229 if (!mComboView.onBackPressed()) {
230 mUiController.removeComboView();
231 }
232 return true;
233 }
234 if (mCustomView != null) {
235 mUiController.hideCustomView();
236 return true;
237 }
238 return false;
239 }
240
241 // WebView callbacks
242
243 @Override
244 public void onPageStarted(Tab tab, String url, Bitmap favicon) {
245 if (mXLargeScreenSize) {
246 mTabBar.onPageStarted(tab, url, favicon);
247 }
248 if (tab.inForeground()) {
249 resetLockIcon(tab, url);
250 setUrlTitle(tab, url, null);
251 setFavicon(tab, favicon);
252 }
253
254 }
255
256 @Override
257 public void onPageFinished(Tab tab, String url) {
258 if (mXLargeScreenSize) {
259 mTabBar.onPageFinished(tab);
260 }
261 if (tab.inForeground()) {
262 // Reset the title and icon in case we stopped a provisional load.
263 resetTitleAndIcon(tab);
264 // Update the lock icon image only once we are done loading
265 updateLockIconToLatest(tab);
266 }
267 }
268
269 @Override
270 public void onPageStopped(Tab tab) {
271 cancelStopToast();
272 if (tab.inForeground()) {
273 mStopToast = Toast
274 .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
275 mStopToast.show();
276 }
277 }
278
279 @Override
280 public void onProgressChanged(Tab tab, int progress) {
281 if (mXLargeScreenSize) {
282 mTabBar.onProgress(tab, progress);
283 }
284 if (tab.inForeground()) {
285 mFakeTitleBar.setProgress(progress);
286 if (progress == 100) {
287 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
288 hideFakeTitleBar();
289 }
290 } else {
291 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
292 showFakeTitleBar();
293 }
294 }
295 }
296 }
297
298 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800299 public boolean needsRestoreAllTabs() {
300 return mXLargeScreenSize;
301 }
302
303 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700304 public void addTab(Tab tab) {
305 if (mXLargeScreenSize) {
306 mTabBar.onNewTab(tab);
307 }
308 }
309
310 @Override
311 public void setActiveTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800312 if ((tab != mActiveTab) && (mActiveTab != null)) {
313 removeTabFromContentView(mActiveTab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700314 }
Michael Kolb77df4562010-11-19 14:49:34 -0800315 mActiveTab = tab;
Michael Kolb8233fac2010-10-26 16:08:53 -0700316 attachTabToContentView(tab);
317 setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
318
319 WebView view = tab.getWebView();
Michael Kolb77df4562010-11-19 14:49:34 -0800320 // TabControl.setCurrentTab has been called before this,
321 // so the tab is guaranteed to have a webview
322 if (view == null) {
323 Log.e(LOGTAG, "active tab with no webview detected");
324 return;
325 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700326 view.setEmbeddedTitleBar(mTitleBar);
327 if (tab.isInVoiceSearchMode()) {
328 showVoiceTitleBar(tab.getVoiceDisplayTitle());
329 } else {
330 revertVoiceTitleBar(tab);
331 }
332
333 if (mXLargeScreenSize) {
334 // Request focus on the top window.
335 mTabBar.onSetActiveTab(tab);
336 }
337 resetTitleIconAndProgress(tab);
338 updateLockIconToLatest(tab);
339 tab.getTopWindow().requestFocus();
340 }
341
342 @Override
Michael Kolb1bf23132010-11-19 12:55:12 -0800343 public void updateTabs(List<Tab> tabs) {
344 if (mXLargeScreenSize) {
345 mTabBar.updateTabs(tabs);
346 }
347 }
348
349 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700350 public void removeTab(Tab tab) {
Michael Kolb77df4562010-11-19 14:49:34 -0800351 if (mActiveTab == tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700352 removeTabFromContentView(tab);
Michael Kolb77df4562010-11-19 14:49:34 -0800353 mActiveTab = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700354 }
355 if (mXLargeScreenSize) {
356 mTabBar.onRemoveTab(tab);
357 }
358 }
359
360 @Override
361 public void detachTab(Tab tab) {
362 removeTabFromContentView(tab);
363 }
364
365 @Override
366 public void attachTab(Tab tab) {
367 attachTabToContentView(tab);
368 }
369
370 private void attachTabToContentView(Tab tab) {
371 if (tab.getWebView() == null) {
372 return;
373 }
374 View container = tab.getViewContainer();
375 WebView mainView = tab.getWebView();
376 // Attach the WebView to the container and then attach the
377 // container to the content view.
378 FrameLayout wrapper =
379 (FrameLayout) container.findViewById(R.id.webview_wrapper);
380 ViewGroup parent = (ViewGroup) mainView.getParent();
381 if (parent != wrapper) {
382 if (parent != null) {
383 Log.w(LOGTAG, "mMainView already has a parent in"
384 + " attachTabToContentView!");
385 parent.removeView(mainView);
386 }
387 wrapper.addView(mainView);
388 } else {
389 Log.w(LOGTAG, "mMainView is already attached to wrapper in"
390 + " attachTabToContentView!");
391 }
392 parent = (ViewGroup) container.getParent();
393 if (parent != mContentView) {
394 if (parent != null) {
395 Log.w(LOGTAG, "mContainer already has a parent in"
396 + " attachTabToContentView!");
397 parent.removeView(container);
398 }
399 mContentView.addView(container, COVER_SCREEN_PARAMS);
400 } else {
401 Log.w(LOGTAG, "mContainer is already attached to content in"
402 + " attachTabToContentView!");
403 }
404 mUiController.attachSubWindow(tab);
405 }
406
407 private void removeTabFromContentView(Tab tab) {
408 // Remove the container that contains the main WebView.
409 WebView mainView = tab.getWebView();
410 View container = tab.getViewContainer();
411 if (mainView == null) {
412 return;
413 }
414 // Remove the container from the content and then remove the
415 // WebView from the container. This will trigger a focus change
416 // needed by WebView.
417 FrameLayout wrapper =
418 (FrameLayout) container.findViewById(R.id.webview_wrapper);
419 wrapper.removeView(mainView);
420 mContentView.removeView(container);
421 mUiController.endActionMode();
422 mUiController.removeSubWindow(tab);
423 ErrorConsoleView errorConsole = tab.getErrorConsole(false);
424 if (errorConsole != null) {
425 mErrorConsoleContainer.removeView(errorConsole);
426 }
427 mainView.setEmbeddedTitleBar(null);
428 }
429
430 /**
Michael Kolb1514bb72010-11-22 09:11:48 -0800431 * create a sub window container and webview for the tab
432 * Note: this methods operates through side-effects for now
433 * it sets both the subView and subViewContainer for the given tab
434 * @param tab tab to create the sub window for
435 * @param subView webview to be set as a subwindow for the tab
436 */
437 @Override
438 public void createSubWindow(Tab tab, WebView subView) {
439 View subViewContainer = mActivity.getLayoutInflater().inflate(
440 R.layout.browser_subwindow, null);
441 ViewGroup inner = (ViewGroup) subViewContainer
442 .findViewById(R.id.inner_container);
443 inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
444 LayoutParams.MATCH_PARENT));
445 final ImageButton cancel = (ImageButton) subViewContainer
446 .findViewById(R.id.subwindow_close);
447 final WebView cancelSubView = subView;
448 cancel.setOnClickListener(new OnClickListener() {
449 public void onClick(View v) {
450 cancelSubView.getWebChromeClient().onCloseWindow(cancelSubView);
451 }
452 });
453 tab.setSubWebView(subView);
454 tab.setSubViewContainer(subViewContainer);
455 }
456
457 /**
Michael Kolb8233fac2010-10-26 16:08:53 -0700458 * Remove the sub window from the content view.
459 */
460 @Override
461 public void removeSubWindow(View subviewContainer) {
462 mContentView.removeView(subviewContainer);
463 mUiController.endActionMode();
464 }
465
466 /**
467 * Attach the sub window to the content view.
468 */
469 @Override
470 public void attachSubWindow(View container) {
Michael Kolb1514bb72010-11-22 09:11:48 -0800471 if (container.getParent() != null) {
472 // already attached, remove first
473 ((ViewGroup) container.getParent()).removeView(container);
474 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700475 mContentView.addView(container, COVER_SCREEN_PARAMS);
476 }
477
478 void showFakeTitleBar() {
479 if (!isFakeTitleBarShowing() && mActiveTabsPage == null &&
480 !mActivityPaused) {
481 WebView mainView = mUiController.getCurrentWebView();
482 // if there is no current WebView, don't show the faked title bar;
483 if (mainView == null) {
484 return;
485 }
486 // Do not need to check for null, since the current tab will have
487 // at least a main WebView, or we would have returned above.
488 if (mUiController.isInCustomActionMode()) {
489 // Do not show the fake title bar, while a custom ActionMode
490 // (i.e. find or select) is showing.
491 return;
492 }
493 if (mXLargeScreenSize) {
494 mContentView.addView(mFakeTitleBar);
495 mTabBar.onShowTitleBar();
496 } else {
497 WindowManager manager = (WindowManager)
498 mActivity.getSystemService(Context.WINDOW_SERVICE);
499
500 // Add the title bar to the window manager so it can receive
501 // touches
502 // while the menu is up
503 WindowManager.LayoutParams params =
504 new WindowManager.LayoutParams(
505 ViewGroup.LayoutParams.MATCH_PARENT,
506 ViewGroup.LayoutParams.WRAP_CONTENT,
507 WindowManager.LayoutParams.TYPE_APPLICATION,
508 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
509 PixelFormat.TRANSLUCENT);
510 params.gravity = Gravity.TOP;
511 boolean atTop = mainView.getScrollY() == 0;
512 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
513 manager.addView(mFakeTitleBar, params);
514 }
515 }
516 }
517
518 void hideFakeTitleBar() {
519 if (!isFakeTitleBarShowing()) return;
520 if (mXLargeScreenSize) {
521 mContentView.removeView(mFakeTitleBar);
522 mTabBar.onHideTitleBar();
523 } else {
524 WindowManager.LayoutParams params =
525 (WindowManager.LayoutParams) mFakeTitleBar.getLayoutParams();
526 WebView mainView = mUiController.getCurrentWebView();
527 // Although we decided whether or not to animate based on the
528 // current
529 // scroll position, the scroll position may have changed since the
530 // fake title bar was displayed. Make sure it has the appropriate
531 // animation/lack thereof before removing.
532 params.windowAnimations =
533 mainView != null && mainView.getScrollY() == 0 ?
534 0 : R.style.TitleBar;
535 WindowManager manager = (WindowManager) mActivity
536 .getSystemService(Context.WINDOW_SERVICE);
537 manager.updateViewLayout(mFakeTitleBar, params);
538 manager.removeView(mFakeTitleBar);
539 }
540 }
541
542 boolean isFakeTitleBarShowing() {
543 return (mFakeTitleBar.getParent() != null);
544 }
545
546 @Override
547 public void showComboView(boolean startWithHistory, Bundle extras) {
548 mComboView = new CombinedBookmarkHistoryView(mActivity,
549 mUiController,
550 startWithHistory ?
551 CombinedBookmarkHistoryView.FRAGMENT_ID_HISTORY
552 : CombinedBookmarkHistoryView.FRAGMENT_ID_BOOKMARKS,
553 extras);
554 mTitleBar.setVisibility(View.GONE);
555 hideFakeTitleBar();
556 mContentView.addView(mComboView, COVER_SCREEN_PARAMS);
557 }
558
559 /**
560 * dismiss the ComboPage
561 */
562 @Override
563 public void hideComboView() {
564 if (mComboView != null) {
565 mContentView.removeView(mComboView);
566 mTitleBar.setVisibility(View.VISIBLE);
567 mComboView = null;
568 }
569 }
570
571 @Override
572 public void showCustomView(View view,
573 WebChromeClient.CustomViewCallback callback) {
574 // if a view already exists then immediately terminate the new one
575 if (mCustomView != null) {
576 callback.onCustomViewHidden();
577 return;
578 }
579
580 // Add the custom view to its container.
581 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
582 mCustomView = view;
583 mCustomViewCallback = callback;
584 // Hide the content view.
585 mContentView.setVisibility(View.GONE);
586 // Finally show the custom view container.
587 setStatusBarVisibility(false);
588 mCustomViewContainer.setVisibility(View.VISIBLE);
589 mCustomViewContainer.bringToFront();
590 }
591
592 @Override
593 public void onHideCustomView() {
594 if (mCustomView == null)
595 return;
596
597 // Hide the custom view.
598 mCustomView.setVisibility(View.GONE);
599 // Remove the custom view from its container.
600 mCustomViewContainer.removeView(mCustomView);
601 mCustomView = null;
602 mCustomViewContainer.setVisibility(View.GONE);
603 mCustomViewCallback.onCustomViewHidden();
604 // Show the content view.
605 setStatusBarVisibility(true);
606 mContentView.setVisibility(View.VISIBLE);
607 }
608
609 @Override
610 public boolean isCustomViewShowing() {
611 return mCustomView != null;
612 }
613
614 @Override
615 public void showVoiceTitleBar(String title) {
616 mTitleBar.setInVoiceMode(true);
617 mTitleBar.setDisplayTitle(title);
618 mFakeTitleBar.setInVoiceMode(true);
619 mFakeTitleBar.setDisplayTitle(title);
620 }
621
622 @Override
623 public void revertVoiceTitleBar(Tab tab) {
624 mTitleBar.setInVoiceMode(false);
625 String url = tab.getCurrentUrl();
626 mTitleBar.setDisplayTitle(url);
627 mFakeTitleBar.setInVoiceMode(false);
628 mFakeTitleBar.setDisplayTitle(url);
629 }
630
631 // -------------------------------------------------------------------------
632
633 @Override
634 public void resetTitleAndRevertLockIcon(Tab tab) {
635 tab.revertLockIcon();
636 updateLockIconToLatest(tab);
637 resetTitleIconAndProgress(tab);
638 }
639
640 /**
641 * Resets the lock icon. This method is called when we start a new load and
642 * know the url to be loaded.
643 */
644 private void resetLockIcon(Tab tab, String url) {
645 // Save the lock-icon state (we revert to it if the load gets cancelled)
646 tab.resetLockIcon(url);
647 updateLockIconImage(Tab.LOCK_ICON_UNSECURE);
648 }
649
650 /**
651 * Update the lock icon to correspond to our latest state.
652 */
653 private void updateLockIconToLatest(Tab t) {
654 if (t != null) {
655 updateLockIconImage(t.getLockIconType());
656 }
657 }
658
659 /**
660 * Reset the title, favicon, and progress.
661 */
662 private void resetTitleIconAndProgress(Tab tab) {
663 WebView current = tab.getWebView();
664 if (current == null) {
665 return;
666 }
667 resetTitleAndIcon(current);
668 int progress = current.getProgress();
669 current.getWebChromeClient().onProgressChanged(current, progress);
670 }
671
672 @Override
673 public void resetTitleAndIcon(Tab tab) {
674 WebView current = tab.getWebView();
675 if (current != null) {
676 resetTitleAndIcon(current);
677 }
678 }
679
680 // Reset the title and the icon based on the given item.
681 private void resetTitleAndIcon(WebView view) {
682 WebHistoryItem item = view.copyBackForwardList().getCurrentItem();
683 Tab tab = mTabControl.getTabFromView(view);
684 if (item != null) {
685 setUrlTitle(tab, item.getUrl(), item.getTitle());
686 setFavicon(tab, item.getFavicon());
687 } else {
688 setUrlTitle(tab, null, null);
689 setFavicon(tab, null);
690 }
691 }
692
693 /**
694 * Updates the lock-icon image in the title-bar.
695 */
696 private void updateLockIconImage(int lockIconType) {
697 Drawable d = null;
698 if (lockIconType == Tab.LOCK_ICON_SECURE) {
699 d = mSecLockIcon;
700 } else if (lockIconType == Tab.LOCK_ICON_MIXED) {
701 d = mMixLockIcon;
702 }
703 mTitleBar.setLock(d);
704 mFakeTitleBar.setLock(d);
705 }
706
707 // active tabs page
708
709 public void showActiveTabsPage() {
710 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
711 mTitleBar.setVisibility(View.GONE);
712 hideFakeTitleBar();
713 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
714 mActiveTabsPage.requestFocus();
715 }
716
717 /**
718 * Remove the active tabs page.
719 * @param needToAttach If true, the active tabs page did not attach a tab
720 * to the content view, so we need to do that here.
721 */
722 public void removeActiveTabsPage() {
723 mContentView.removeView(mActiveTabsPage);
724 mTitleBar.setVisibility(View.VISIBLE);
725 mActiveTabsPage = null;
726 }
727
728 // action mode callbacks
729
730 @Override
731 public void onActionModeStarted(ActionMode mode) {
732 // hide the fake title bar when CAB is shown
733 hideFakeTitleBar();
734 }
735
736 @Override
737 public void onActionModeFinished(boolean inLoad) {
738 if (inLoad) {
739 // the titlebar was removed when the CAB was shown
740 // if the page is loading, show it again
741 showFakeTitleBar();
742 }
743 }
744
745 // menu handling callbacks
746
747 @Override
748 public void onOptionsMenuOpened() {
749 mOptionsMenuOpen = true;
750 // options menu opened, show fake title bar
751 showFakeTitleBar();
752 }
753
754 @Override
755 public void onExtendedMenuOpened() {
756 // Switching the menu to expanded view, so hide the
757 // title bar.
758 mExtendedMenuOpen = true;
759 hideFakeTitleBar();
760 }
761
762 @Override
763 public void onOptionsMenuClosed(boolean inLoad) {
764 mOptionsMenuOpen = false;
765 if (!inLoad) {
766 hideFakeTitleBar();
767 }
768 }
769
770 @Override
771 public void onExtendedMenuClosed(boolean inLoad) {
772 mExtendedMenuOpen = false;
773 if (inLoad) {
774 showFakeTitleBar();
775 }
776 }
777
778 @Override
779 public void onContextMenuCreated(Menu menu) {
780 hideFakeTitleBar();
781 }
782
783 @Override
784 public void onContextMenuClosed(Menu menu, boolean inLoad) {
785 if (inLoad) {
786 showFakeTitleBar();
787 }
788 }
789
790 @Override
791 public void onScroll(boolean titleVisible) {
792 if (mTabBar != null) {
793 mTabBar.onScroll(titleVisible);
794 }
795 }
796
797 // error console
798
799 @Override
800 public void setShouldShowErrorConsole(Tab tab, boolean flag) {
801 ErrorConsoleView errorConsole = tab.getErrorConsole(true);
802 if (flag) {
803 // Setting the show state of the console will cause it's the layout
804 // to be inflated.
805 if (errorConsole.numberOfErrors() > 0) {
806 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
807 } else {
808 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
809 }
810 if (errorConsole.getParent() != null) {
811 mErrorConsoleContainer.removeView(errorConsole);
812 }
813 // Now we can add it to the main view.
814 mErrorConsoleContainer.addView(errorConsole,
815 new LinearLayout.LayoutParams(
816 ViewGroup.LayoutParams.MATCH_PARENT,
817 ViewGroup.LayoutParams.WRAP_CONTENT));
818 } else {
819 mErrorConsoleContainer.removeView(errorConsole);
820 }
821 }
822
823 private void setStatusBarVisibility(boolean visible) {
824 int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
825 mActivity.getWindow().setFlags(flag,
826 WindowManager.LayoutParams.FLAG_FULLSCREEN);
827 }
828
829 @Override
830 public void setUrlTitle(Tab tab, String url, String title) {
831 if (TextUtils.isEmpty(title)) {
832 if (TextUtils.isEmpty(url)) {
833 title = mActivity.getResources()
834 .getString(R.string.title_bar_loading);
835 } else {
836 title = url;
837 }
838 }
839 if (tab.isInVoiceSearchMode()) return;
840 if (tab.inForeground()) {
841 mTitleBar.setDisplayTitle(url);
842 mFakeTitleBar.setDisplayTitle(url);
843 }
844 if (mXLargeScreenSize) {
845 mTabBar.onUrlAndTitle(tab, url, title);
846 }
847 }
848
849 // Set the favicon in the title bar.
850 @Override
851 public void setFavicon(Tab tab, Bitmap icon) {
852 mTitleBar.setFavicon(icon);
853 mFakeTitleBar.setFavicon(icon);
854 if (mXLargeScreenSize) {
855 mTabBar.onFavicon(tab, icon);
856 }
857 }
858 @Override
859 public boolean showsWeb() {
860 return mCustomView == null && mActiveTabsPage == null
861 && mComboView == null;
862 }
863
864 @Override
865 public void onPrepareOptionsMenu(Menu menu) {
866 if (!mXLargeScreenSize) {
867 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
868 newtab.setEnabled(mUiController.getTabControl().canCreateNewTab());
869 }
870 }
871
872 // -------------------------------------------------------------------------
873 // Helper function for WebChromeClient
874 // -------------------------------------------------------------------------
875
876 @Override
877 public Bitmap getDefaultVideoPoster() {
878 if (mDefaultVideoPoster == null) {
879 mDefaultVideoPoster = BitmapFactory.decodeResource(
880 mActivity.getResources(), R.drawable.default_video_poster);
881 }
882 return mDefaultVideoPoster;
883 }
884
885 @Override
886 public View getVideoLoadingProgressView() {
887 if (mVideoProgressView == null) {
888 LayoutInflater inflater = LayoutInflater.from(mActivity);
889 mVideoProgressView = inflater.inflate(
890 R.layout.video_loading_progress, null);
891 }
892 return mVideoProgressView;
893 }
894
895}