Home | History | Annotate | Download | only in browser
      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 
     19 import android.app.ActionBar;
     20 import android.app.Activity;
     21 import android.content.res.Resources;
     22 import android.graphics.Bitmap;
     23 import android.graphics.drawable.BitmapDrawable;
     24 import android.graphics.drawable.Drawable;
     25 import android.graphics.drawable.LayerDrawable;
     26 import android.graphics.drawable.PaintDrawable;
     27 import android.os.Bundle;
     28 import android.os.Handler;
     29 import android.util.Log;
     30 import android.view.ActionMode;
     31 import android.view.KeyEvent;
     32 import android.view.Menu;
     33 import android.view.MenuItem;
     34 import android.webkit.WebView;
     35 import android.webkit.WebViewClassic;
     36 
     37 import java.util.List;
     38 
     39 /**
     40  * Ui for xlarge screen sizes
     41  */
     42 public class XLargeUi extends BaseUi {
     43 
     44     private static final String LOGTAG = "XLargeUi";
     45 
     46     private PaintDrawable mFaviconBackground;
     47 
     48     private ActionBar mActionBar;
     49     private TabBar mTabBar;
     50 
     51     private NavigationBarTablet mNavBar;
     52 
     53     private Handler mHandler;
     54 
     55     /**
     56      * @param browser
     57      * @param controller
     58      */
     59     public XLargeUi(Activity browser, UiController controller) {
     60         super(browser, controller);
     61         mHandler = new Handler();
     62         mNavBar = (NavigationBarTablet) mTitleBar.getNavigationBar();
     63         mTabBar = new TabBar(mActivity, mUiController, this);
     64         mActionBar = mActivity.getActionBar();
     65         setupActionBar();
     66         setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
     67     }
     68 
     69     private void setupActionBar() {
     70         mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
     71         mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
     72         mActionBar.setCustomView(mTabBar);
     73     }
     74 
     75     public void showComboView(ComboViews startWith, Bundle extras) {
     76         super.showComboView(startWith, extras);
     77         if (mUseQuickControls) {
     78             mActionBar.show();
     79         }
     80     }
     81 
     82     @Override
     83     public void setUseQuickControls(boolean useQuickControls) {
     84         super.setUseQuickControls(useQuickControls);
     85         checkHideActionBar();
     86         if (!useQuickControls) {
     87             mActionBar.show();
     88         }
     89         mTabBar.setUseQuickControls(mUseQuickControls);
     90         // We need to update the tabs with this change
     91         for (Tab t : mTabControl.getTabs()) {
     92             t.updateShouldCaptureThumbnails();
     93         }
     94     }
     95 
     96     private void checkHideActionBar() {
     97         if (mUseQuickControls) {
     98             mHandler.post(new Runnable() {
     99                 public void run() {
    100                     mActionBar.hide();
    101                 }
    102             });
    103         }
    104     }
    105 
    106     @Override
    107     public void onResume() {
    108         super.onResume();
    109         mNavBar.clearCompletions();
    110         checkHideActionBar();
    111     }
    112 
    113     @Override
    114     public void onDestroy() {
    115         hideTitleBar();
    116     }
    117 
    118     void stopWebViewScrolling() {
    119         BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
    120         if (web != null) {
    121             WebViewClassic.fromWebView(web).stopScroll();
    122         }
    123     }
    124 
    125     @Override
    126     public boolean onPrepareOptionsMenu(Menu menu) {
    127         MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
    128         if (bm != null) {
    129             bm.setVisible(false);
    130         }
    131         return true;
    132     }
    133 
    134 
    135     // WebView callbacks
    136 
    137     @Override
    138     public void addTab(Tab tab) {
    139         mTabBar.onNewTab(tab);
    140     }
    141 
    142     protected void onAddTabCompleted(Tab tab) {
    143         checkHideActionBar();
    144     }
    145 
    146     @Override
    147     public void setActiveTab(final Tab tab) {
    148         mTitleBar.cancelTitleBarAnimation(true);
    149         mTitleBar.setSkipTitleBarAnimations(true);
    150         super.setActiveTab(tab);
    151         BrowserWebView view = (BrowserWebView) tab.getWebView();
    152         // TabControl.setCurrentTab has been called before this,
    153         // so the tab is guaranteed to have a webview
    154         if (view == null) {
    155             Log.e(LOGTAG, "active tab with no webview detected");
    156             return;
    157         }
    158         mTabBar.onSetActiveTab(tab);
    159         updateLockIconToLatest(tab);
    160         mTitleBar.setSkipTitleBarAnimations(false);
    161     }
    162 
    163     @Override
    164     public void updateTabs(List<Tab> tabs) {
    165         mTabBar.updateTabs(tabs);
    166         checkHideActionBar();
    167     }
    168 
    169     @Override
    170     public void removeTab(Tab tab) {
    171         mTitleBar.cancelTitleBarAnimation(true);
    172         mTitleBar.setSkipTitleBarAnimations(true);
    173         super.removeTab(tab);
    174         mTabBar.onRemoveTab(tab);
    175         mTitleBar.setSkipTitleBarAnimations(false);
    176     }
    177 
    178     protected void onRemoveTabCompleted(Tab tab) {
    179         checkHideActionBar();
    180     }
    181 
    182     int getContentWidth() {
    183         if (mContentView != null) {
    184             return mContentView.getWidth();
    185         }
    186         return 0;
    187     }
    188 
    189     @Override
    190     public void editUrl(boolean clearInput, boolean forceIME) {
    191         if (mUseQuickControls) {
    192             mTitleBar.setShowProgressOnly(false);
    193         }
    194         super.editUrl(clearInput, forceIME);
    195     }
    196 
    197     // action mode callbacks
    198 
    199     @Override
    200     public void onActionModeStarted(ActionMode mode) {
    201         if (!mTitleBar.isEditingUrl()) {
    202             // hide the title bar when CAB is shown
    203             hideTitleBar();
    204         }
    205     }
    206 
    207     @Override
    208     public void onActionModeFinished(boolean inLoad) {
    209         checkHideActionBar();
    210         if (inLoad) {
    211             // the titlebar was removed when the CAB was shown
    212             // if the page is loading, show it again
    213             if (mUseQuickControls) {
    214                 mTitleBar.setShowProgressOnly(true);
    215             }
    216             showTitleBar();
    217         }
    218     }
    219 
    220     @Override
    221     protected void updateNavigationState(Tab tab) {
    222         mNavBar.updateNavigationState(tab);
    223     }
    224 
    225     @Override
    226     public void setUrlTitle(Tab tab) {
    227         super.setUrlTitle(tab);
    228         mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
    229     }
    230 
    231     // Set the favicon in the title bar.
    232     @Override
    233     public void setFavicon(Tab tab) {
    234         super.setFavicon(tab);
    235         mTabBar.onFavicon(tab, tab.getFavicon());
    236     }
    237 
    238     @Override
    239     public void onHideCustomView() {
    240         super.onHideCustomView();
    241         checkHideActionBar();
    242     }
    243 
    244     @Override
    245     public boolean dispatchKey(int code, KeyEvent event) {
    246         if (mActiveTab != null) {
    247             WebView web = mActiveTab.getWebView();
    248             if (event.getAction() == KeyEvent.ACTION_DOWN) {
    249                 switch (code) {
    250                     case KeyEvent.KEYCODE_TAB:
    251                     case KeyEvent.KEYCODE_DPAD_UP:
    252                     case KeyEvent.KEYCODE_DPAD_LEFT:
    253                         if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
    254                             editUrl(false, false);
    255                             return true;
    256                         }
    257                 }
    258                 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
    259                 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
    260                     editUrl(true, false);
    261                     return mContentView.dispatchKeyEvent(event);
    262                 }
    263             }
    264         }
    265         return false;
    266     }
    267 
    268     private boolean isTypingKey(KeyEvent evt) {
    269         return evt.getUnicodeChar() > 0;
    270     }
    271 
    272     TabBar getTabBar() {
    273         return mTabBar;
    274     }
    275 
    276     @Override
    277     public boolean shouldCaptureThumbnails() {
    278         return mUseQuickControls;
    279     }
    280 
    281     private Drawable getFaviconBackground() {
    282         if (mFaviconBackground == null) {
    283             mFaviconBackground = new PaintDrawable();
    284             Resources res = mActivity.getResources();
    285             mFaviconBackground.getPaint().setColor(
    286                     res.getColor(R.color.tabFaviconBackground));
    287             mFaviconBackground.setCornerRadius(
    288                     res.getDimension(R.dimen.tab_favicon_corner_radius));
    289         }
    290         return mFaviconBackground;
    291     }
    292 
    293     @Override
    294     public Drawable getFaviconDrawable(Bitmap icon) {
    295         Drawable[] array = new Drawable[2];
    296         array[0] = getFaviconBackground();
    297         if (icon == null) {
    298             array[1] = mGenericFavicon;
    299         } else {
    300             array[1] = new BitmapDrawable(mActivity.getResources(), icon);
    301         }
    302         LayerDrawable d = new LayerDrawable(array);
    303         d.setLayerInset(1, 2, 2, 2, 2);
    304         return d;
    305     }
    306 
    307 }
    308