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.Gravity;
     32 import android.view.KeyEvent;
     33 import android.view.View;
     34 import android.view.ViewGroup;
     35 import android.webkit.WebChromeClient.CustomViewCallback;
     36 import android.webkit.WebView;
     37 
     38 import java.util.List;
     39 
     40 /**
     41  * Ui for xlarge screen sizes
     42  */
     43 public class XLargeUi extends BaseUi {
     44 
     45     private static final String LOGTAG = "XLargeUi";
     46 
     47     private PaintDrawable mFaviconBackground;
     48 
     49     private ActionBar mActionBar;
     50     private TabBar mTabBar;
     51 
     52     private NavigationBarTablet mNavBar;
     53 
     54     private PieControlXLarge mPieControl;
     55     private Handler mHandler;
     56 
     57     /**
     58      * @param browser
     59      * @param controller
     60      */
     61     public XLargeUi(Activity browser, UiController controller) {
     62         super(browser, controller);
     63         mHandler = new Handler();
     64         mNavBar = (NavigationBarTablet) mTitleBar.getNavigationBar();
     65         mTabBar = new TabBar(mActivity, mUiController, this);
     66         mActionBar = mActivity.getActionBar();
     67         setupActionBar();
     68         setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
     69     }
     70 
     71     private void setupActionBar() {
     72         mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
     73         mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
     74         mActionBar.setCustomView(mTabBar);
     75     }
     76 
     77     public void showComboView(ComboViews startWith, Bundle extras) {
     78         super.showComboView(startWith, extras);
     79         if (mUseQuickControls) {
     80             mActionBar.show();
     81         }
     82     }
     83 
     84     @Override
     85     public void setUseQuickControls(boolean useQuickControls) {
     86         mUseQuickControls = useQuickControls;
     87         mTitleBar.setUseQuickControls(mUseQuickControls);
     88         if (useQuickControls) {
     89             checkTabCount();
     90             mPieControl = new PieControlXLarge(mActivity, mUiController, this);
     91             mPieControl.attachToContainer(mContentView);
     92             WebView web = getWebView();
     93             if (web != null) {
     94                 web.setEmbeddedTitleBar(null);
     95 
     96             }
     97         } else {
     98             mActivity.getActionBar().show();
     99             if (mPieControl != null) {
    100                 mPieControl.removeFromContainer(mContentView);
    101             }
    102             WebView web = getWebView();
    103             if (web != null) {
    104                 if (mTitleBar.getParent() != null) {
    105                     ViewGroup p = (ViewGroup) mTitleBar.getParent();
    106                     p.removeView(mTitleBar);
    107                 }
    108                 web.setEmbeddedTitleBar(mTitleBar);
    109             }
    110             setTitleGravity(Gravity.NO_GRAVITY);
    111         }
    112         mTabBar.setUseQuickControls(mUseQuickControls);
    113         // We need to update the tabs with this change
    114         for (Tab t : mTabControl.getTabs()) {
    115             t.updateShouldCaptureThumbnails();
    116         }
    117         updateUrlBarAutoShowManagerTarget();
    118     }
    119 
    120     private void checkTabCount() {
    121         if (mUseQuickControls) {
    122             mHandler.post(new Runnable() {
    123                 public void run() {
    124                     mActionBar.hide();
    125                 }
    126             });
    127         }
    128     }
    129 
    130     @Override
    131     public void onResume() {
    132         super.onResume();
    133         if (!BrowserSettings.getInstance().useInstantSearch()) {
    134             mNavBar.clearCompletions();
    135         }
    136     }
    137 
    138     @Override
    139     public void onDestroy() {
    140         hideTitleBar();
    141     }
    142 
    143     void stopWebViewScrolling() {
    144         BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
    145         if (web != null) {
    146             web.stopScroll();
    147         }
    148     }
    149 
    150     // WebView callbacks
    151 
    152     @Override
    153     public void onProgressChanged(Tab tab) {
    154         int progress = tab.getLoadProgress();
    155         mTabBar.onProgress(tab, progress);
    156         if (tab.inForeground()) {
    157             mTitleBar.setProgress(progress);
    158         }
    159     }
    160 
    161     @Override
    162     public void addTab(Tab tab) {
    163         mTabBar.onNewTab(tab);
    164     }
    165 
    166     protected void onAddTabCompleted(Tab tab) {
    167         checkTabCount();
    168     }
    169 
    170     @Override
    171     public void setActiveTab(final Tab tab) {
    172         mTitleBar.cancelTitleBarAnimation(true);
    173         mTitleBar.setSkipTitleBarAnimations(true);
    174         super.setActiveTab(tab);
    175         BrowserWebView view = (BrowserWebView) tab.getWebView();
    176         // TabControl.setCurrentTab has been called before this,
    177         // so the tab is guaranteed to have a webview
    178         if (view == null) {
    179             Log.e(LOGTAG, "active tab with no webview detected");
    180             return;
    181         }
    182         // Request focus on the top window.
    183         if (mUseQuickControls) {
    184             mPieControl.forceToTop(mContentView);
    185         } else {
    186             // check if title bar is already attached by animation
    187             if (mTitleBar.getParent() == null) {
    188                 view.setEmbeddedTitleBar(mTitleBar);
    189             }
    190         }
    191         mTabBar.onSetActiveTab(tab);
    192         if (tab.isInVoiceSearchMode()) {
    193             showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
    194         } else {
    195             revertVoiceTitleBar(tab);
    196         }
    197         updateLockIconToLatest(tab);
    198         tab.getTopWindow().requestFocus();
    199         mTitleBar.setSkipTitleBarAnimations(false);
    200     }
    201 
    202     @Override
    203     public void updateTabs(List<Tab> tabs) {
    204         mTabBar.updateTabs(tabs);
    205         checkTabCount();
    206     }
    207 
    208     @Override
    209     public void removeTab(Tab tab) {
    210         mTitleBar.cancelTitleBarAnimation(true);
    211         mTitleBar.setSkipTitleBarAnimations(true);
    212         super.removeTab(tab);
    213         mTabBar.onRemoveTab(tab);
    214         mTitleBar.setSkipTitleBarAnimations(false);
    215     }
    216 
    217     protected void onRemoveTabCompleted(Tab tab) {
    218         checkTabCount();
    219     }
    220 
    221     int getContentWidth() {
    222         if (mContentView != null) {
    223             return mContentView.getWidth();
    224         }
    225         return 0;
    226     }
    227 
    228     @Override
    229     public void editUrl(boolean clearInput) {
    230         if (mUseQuickControls) {
    231             mTitleBar.setShowProgressOnly(false);
    232         }
    233         super.editUrl(clearInput);
    234     }
    235 
    236     void stopEditingUrl() {
    237         mTitleBar.getNavigationBar().stopEditingUrl();
    238     }
    239 
    240     @Override
    241     protected void showTitleBar() {
    242         if (canShowTitleBar()) {
    243             mTitleBar.show();
    244         }
    245     }
    246 
    247     @Override
    248     protected void hideTitleBar() {
    249         if (isTitleBarShowing()) {
    250             mTitleBar.hide();
    251         }
    252     }
    253 
    254     @Override
    255     protected void setTitleGravity(int gravity) {
    256         if (!mUseQuickControls) {
    257             super.setTitleGravity(gravity);
    258         }
    259     }
    260 
    261     // action mode callbacks
    262 
    263     @Override
    264     public void onActionModeStarted(ActionMode mode) {
    265         if (!mTitleBar.isEditingUrl()) {
    266             // hide the title bar when CAB is shown
    267             hideTitleBar();
    268         }
    269     }
    270 
    271     @Override
    272     public void onActionModeFinished(boolean inLoad) {
    273         checkTabCount();
    274         if (inLoad) {
    275             // the titlebar was removed when the CAB was shown
    276             // if the page is loading, show it again
    277             if (mUseQuickControls) {
    278                 mTitleBar.setShowProgressOnly(true);
    279             }
    280             showTitleBar();
    281         }
    282     }
    283 
    284     @Override
    285     protected void updateNavigationState(Tab tab) {
    286         mNavBar.updateNavigationState(tab);
    287     }
    288 
    289     @Override
    290     public void setUrlTitle(Tab tab) {
    291         super.setUrlTitle(tab);
    292         mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
    293     }
    294 
    295     // Set the favicon in the title bar.
    296     @Override
    297     public void setFavicon(Tab tab) {
    298         super.setFavicon(tab);
    299         mTabBar.onFavicon(tab, tab.getFavicon());
    300     }
    301 
    302     @Override
    303     public void onHideCustomView() {
    304         super.onHideCustomView();
    305         if (mUseQuickControls) {
    306             checkTabCount();
    307         }
    308     }
    309 
    310     @Override
    311     public boolean dispatchKey(int code, KeyEvent event) {
    312         if (mActiveTab != null) {
    313             WebView web = mActiveTab.getWebView();
    314             if (event.getAction() == KeyEvent.ACTION_DOWN) {
    315                 switch (code) {
    316                     case KeyEvent.KEYCODE_TAB:
    317                     case KeyEvent.KEYCODE_DPAD_UP:
    318                     case KeyEvent.KEYCODE_DPAD_LEFT:
    319                         if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
    320                             editUrl(false);
    321                             return true;
    322                         }
    323                 }
    324                 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
    325                 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
    326                     editUrl(true);
    327                     return mContentView.dispatchKeyEvent(event);
    328                 }
    329             }
    330         }
    331         return false;
    332     }
    333 
    334     private boolean isTypingKey(KeyEvent evt) {
    335         return evt.getUnicodeChar() > 0;
    336     }
    337 
    338     TabBar getTabBar() {
    339         return mTabBar;
    340     }
    341 
    342     @Override
    343     public boolean shouldCaptureThumbnails() {
    344         return mUseQuickControls;
    345     }
    346 
    347     private Drawable getFaviconBackground() {
    348         if (mFaviconBackground == null) {
    349             mFaviconBackground = new PaintDrawable();
    350             Resources res = mActivity.getResources();
    351             mFaviconBackground.getPaint().setColor(
    352                     res.getColor(R.color.tabFaviconBackground));
    353             mFaviconBackground.setCornerRadius(
    354                     res.getDimension(R.dimen.tab_favicon_corner_radius));
    355         }
    356         return mFaviconBackground;
    357     }
    358 
    359     @Override
    360     public Drawable getFaviconDrawable(Bitmap icon) {
    361         Drawable[] array = new Drawable[2];
    362         array[0] = getFaviconBackground();
    363         if (icon == null) {
    364             array[1] = mGenericFavicon;
    365         } else {
    366             array[1] = new BitmapDrawable(mActivity.getResources(), icon);
    367         }
    368         LayerDrawable d = new LayerDrawable(array);
    369         d.setLayerInset(1, 2, 2, 2, 2);
    370         return d;
    371     }
    372 
    373 }
    374