Home | History | Annotate | Download | only in browser
      1 /*
      2  * Copyright (C) 2011 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 package com.android.browser;
     17 
     18 import android.app.Activity;
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.graphics.drawable.Drawable;
     22 import android.util.AttributeSet;
     23 import android.view.Menu;
     24 import android.view.MenuItem;
     25 import android.view.View;
     26 import android.view.ViewConfiguration;
     27 import android.webkit.WebView;
     28 import android.widget.ImageView;
     29 import android.widget.PopupMenu;
     30 import android.widget.PopupMenu.OnDismissListener;
     31 import android.widget.PopupMenu.OnMenuItemClickListener;
     32 
     33 import com.android.browser.UrlInputView.StateListener;
     34 
     35 public class NavigationBarPhone extends NavigationBarBase implements
     36         StateListener, OnMenuItemClickListener, OnDismissListener {
     37 
     38     private ImageView mStopButton;
     39     private ImageView mMagnify;
     40     private ImageView mClearButton;
     41     private ImageView mVoiceButton;
     42     private Drawable mStopDrawable;
     43     private Drawable mRefreshDrawable;
     44     private String mStopDescription;
     45     private String mRefreshDescription;
     46     private View mTabSwitcher;
     47     private View mComboIcon;
     48     private View mTitleContainer;
     49     private View mMore;
     50     private Drawable mTextfieldBgDrawable;
     51     private PopupMenu mPopupMenu;
     52     private boolean mOverflowMenuShowing;
     53     private boolean mNeedsMenu;
     54     private View mIncognitoIcon;
     55 
     56     public NavigationBarPhone(Context context) {
     57         super(context);
     58     }
     59 
     60     public NavigationBarPhone(Context context, AttributeSet attrs) {
     61         super(context, attrs);
     62     }
     63 
     64     public NavigationBarPhone(Context context, AttributeSet attrs, int defStyle) {
     65         super(context, attrs, defStyle);
     66     }
     67 
     68     @Override
     69     protected void onFinishInflate() {
     70         super.onFinishInflate();
     71         mStopButton = (ImageView) findViewById(R.id.stop);
     72         mStopButton.setOnClickListener(this);
     73         mClearButton = (ImageView) findViewById(R.id.clear);
     74         mClearButton.setOnClickListener(this);
     75         mVoiceButton = (ImageView) findViewById(R.id.voice);
     76         mVoiceButton.setOnClickListener(this);
     77         mMagnify = (ImageView) findViewById(R.id.magnify);
     78         mTabSwitcher = findViewById(R.id.tab_switcher);
     79         mTabSwitcher.setOnClickListener(this);
     80         mMore = findViewById(R.id.more);
     81         mMore.setOnClickListener(this);
     82         mComboIcon = findViewById(R.id.iconcombo);
     83         mComboIcon.setOnClickListener(this);
     84         mTitleContainer = findViewById(R.id.title_bg);
     85         setFocusState(false);
     86         Resources res = getContext().getResources();
     87         mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
     88         mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
     89         mStopDescription = res.getString(R.string.accessibility_button_stop);
     90         mRefreshDescription = res.getString(R.string.accessibility_button_refresh);
     91         mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark);
     92         mUrlInput.setContainer(this);
     93         mUrlInput.setStateListener(this);
     94         mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
     95         mIncognitoIcon = findViewById(R.id.incognito_icon);
     96     }
     97 
     98     @Override
     99     public void onProgressStarted() {
    100         super.onProgressStarted();
    101         if (mStopButton.getDrawable() != mStopDrawable) {
    102             mStopButton.setImageDrawable(mStopDrawable);
    103             mStopButton.setContentDescription(mStopDescription);
    104             if (mStopButton.getVisibility() != View.VISIBLE) {
    105                 mComboIcon.setVisibility(View.GONE);
    106                 mStopButton.setVisibility(View.VISIBLE);
    107             }
    108         }
    109     }
    110 
    111     @Override
    112     public void onProgressStopped() {
    113         super.onProgressStopped();
    114         mStopButton.setImageDrawable(mRefreshDrawable);
    115         mStopButton.setContentDescription(mRefreshDescription);
    116         if (!isEditingUrl()) {
    117             mComboIcon.setVisibility(View.VISIBLE);
    118         }
    119         onStateChanged(mUrlInput.getState());
    120     }
    121 
    122     /**
    123      * Update the text displayed in the title bar.
    124      * @param title String to display.  If null, the new tab string will be
    125      *      shown.
    126      */
    127     @Override
    128     void setDisplayTitle(String title) {
    129         mUrlInput.setTag(title);
    130         if (!isEditingUrl()) {
    131             if (title == null) {
    132                 mUrlInput.setText(R.string.new_tab);
    133             } else {
    134                 mUrlInput.setText(UrlUtils.stripUrl(title), false);
    135             }
    136             mUrlInput.setSelection(0);
    137         }
    138     }
    139 
    140     @Override
    141     public void onClick(View v) {
    142         if (v == mStopButton) {
    143             if (mTitleBar.isInLoad()) {
    144                 mUiController.stopLoading();
    145             } else {
    146                 WebView web = mBaseUi.getWebView();
    147                 if (web != null) {
    148                     stopEditingUrl();
    149                     web.reload();
    150                 }
    151             }
    152         } else if (v == mTabSwitcher) {
    153             ((PhoneUi) mBaseUi).toggleNavScreen();
    154         } else if (mMore == v) {
    155             showMenu(mMore);
    156         } else if (mClearButton == v) {
    157             mUrlInput.setText("");
    158         } else if (mComboIcon == v) {
    159             mUiController.showPageInfo();
    160         } else if (mVoiceButton == v) {
    161             mUiController.startVoiceRecognizer();
    162         } else {
    163             super.onClick(v);
    164         }
    165     }
    166 
    167     @Override
    168     public boolean isMenuShowing() {
    169         return super.isMenuShowing() || mOverflowMenuShowing;
    170     }
    171 
    172     void showMenu(View anchor) {
    173         Activity activity = mUiController.getActivity();
    174         if (mPopupMenu == null) {
    175             mPopupMenu = new PopupMenu(mContext, anchor);
    176             mPopupMenu.setOnMenuItemClickListener(this);
    177             mPopupMenu.setOnDismissListener(this);
    178             if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
    179                 mPopupMenu = null;
    180                 return;
    181             }
    182         }
    183         Menu menu = mPopupMenu.getMenu();
    184         if (activity.onPrepareOptionsMenu(menu)) {
    185             mOverflowMenuShowing = true;
    186             mPopupMenu.show();
    187         }
    188     }
    189 
    190     @Override
    191     public void onDismiss(PopupMenu menu) {
    192         if (menu == mPopupMenu) {
    193             onMenuHidden();
    194         }
    195     }
    196 
    197     private void onMenuHidden() {
    198         mOverflowMenuShowing = false;
    199         mBaseUi.showTitleBarForDuration();
    200     }
    201 
    202     @Override
    203     public void onFocusChange(View view, boolean hasFocus) {
    204         if (view == mUrlInput) {
    205             if (hasFocus && !mUrlInput.getText().toString().equals(mUrlInput.getTag())) {
    206                 // only change text if different
    207                 mUrlInput.setText((String) mUrlInput.getTag(), false);
    208                 mUrlInput.selectAll();
    209             } else {
    210                 setDisplayTitle(mUrlInput.getText().toString());
    211             }
    212         }
    213         super.onFocusChange(view, hasFocus);
    214     }
    215 
    216     @Override
    217     public void onStateChanged(int state) {
    218         mVoiceButton.setVisibility(View.GONE);
    219         switch(state) {
    220         case StateListener.STATE_NORMAL:
    221             mComboIcon.setVisibility(View.VISIBLE);
    222             mStopButton.setVisibility(View.GONE);
    223             mClearButton.setVisibility(View.GONE);
    224             mMagnify.setVisibility(View.GONE);
    225             mTabSwitcher.setVisibility(View.VISIBLE);
    226             mTitleContainer.setBackgroundDrawable(null);
    227             mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
    228             break;
    229         case StateListener.STATE_HIGHLIGHTED:
    230             mComboIcon.setVisibility(View.GONE);
    231             mStopButton.setVisibility(View.VISIBLE);
    232             mClearButton.setVisibility(View.GONE);
    233             if ((mUiController != null) && mUiController.supportsVoice()) {
    234                 mVoiceButton.setVisibility(View.VISIBLE);
    235             }
    236             mMagnify.setVisibility(View.GONE);
    237             mTabSwitcher.setVisibility(View.GONE);
    238             mMore.setVisibility(View.GONE);
    239             mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
    240             break;
    241         case StateListener.STATE_EDITED:
    242             mComboIcon.setVisibility(View.GONE);
    243             mStopButton.setVisibility(View.GONE);
    244             mClearButton.setVisibility(View.VISIBLE);
    245             mMagnify.setVisibility(View.VISIBLE);
    246             mTabSwitcher.setVisibility(View.GONE);
    247             mMore.setVisibility(View.GONE);
    248             mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
    249             break;
    250         }
    251     }
    252 
    253     @Override
    254     public void onTabDataChanged(Tab tab) {
    255         super.onTabDataChanged(tab);
    256         mIncognitoIcon.setVisibility(tab.isPrivateBrowsingEnabled()
    257                 ? View.VISIBLE : View.GONE);
    258     }
    259 
    260     @Override
    261     public boolean onMenuItemClick(MenuItem item) {
    262         return mUiController.onOptionsItemSelected(item);
    263     }
    264 
    265 }
    266