blob: 817337718535e97043ecabe509dff71d8e3716a3 [file] [log] [blame]
Michael Kolb11d19782011-03-20 10:17:40 -07001/*
John Reck0f602f32011-07-07 15:38:43 -07002 * Copyright (C) 2011 The Android Open Source Project
Michael Kolb11d19782011-03-20 10:17:40 -07003 *
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 */
Michael Kolb11d19782011-03-20 10:17:40 -070016package com.android.browser;
17
Michael Kolb11d19782011-03-20 10:17:40 -070018import android.app.Activity;
19import android.content.Context;
Michael Kolb2814a362011-05-19 15:49:41 -070020import android.content.res.Resources;
21import android.graphics.drawable.Drawable;
John Reck0f602f32011-07-07 15:38:43 -070022import android.util.AttributeSet;
Michael Kolb11d19782011-03-20 10:17:40 -070023import android.view.ContextMenu;
Michael Kolb017ffab2011-07-11 15:26:47 -070024import android.view.Menu;
Michael Kolb11d19782011-03-20 10:17:40 -070025import android.view.MenuInflater;
Michael Kolb017ffab2011-07-11 15:26:47 -070026import android.view.MenuItem;
Michael Kolb11d19782011-03-20 10:17:40 -070027import android.view.View;
Michael Kolb017ffab2011-07-11 15:26:47 -070028import android.view.ViewConfiguration;
Michael Kolb2814a362011-05-19 15:49:41 -070029import android.webkit.WebView;
Michael Kolb11d19782011-03-20 10:17:40 -070030import android.widget.ImageView;
Michael Kolb017ffab2011-07-11 15:26:47 -070031import android.widget.PopupMenu;
32import android.widget.PopupMenu.OnDismissListener;
Michael Kolb11d19782011-03-20 10:17:40 -070033
Michael Kolb305b1c52011-06-21 16:16:22 -070034import com.android.browser.UrlInputView.StateListener;
Michael Kolb305b1c52011-06-21 16:16:22 -070035
John Reck0f602f32011-07-07 15:38:43 -070036public class NavigationBarPhone extends NavigationBarBase implements
37 StateListener, OnDismissListener {
Michael Kolb11d19782011-03-20 10:17:40 -070038
Michael Kolbc16c5952011-03-29 15:37:03 -070039 private ImageView mStopButton;
Michael Kolb11d19782011-03-20 10:17:40 -070040 private ImageView mVoiceButton;
Michael Kolb2814a362011-05-19 15:49:41 -070041 private Drawable mStopDrawable;
42 private Drawable mRefreshDrawable;
Michael Kolb30adae62011-07-29 13:37:05 -070043 private String mStopDescription;
44 private String mRefreshDescription;
John Reck8ac42902011-06-29 16:14:34 -070045 private View mTabSwitcher;
Michael Kolb305b1c52011-06-21 16:16:22 -070046 private View mComboIcon;
47 private View mTitleContainer;
Michael Kolb017ffab2011-07-11 15:26:47 -070048 private View mMore;
Michael Kolb305b1c52011-06-21 16:16:22 -070049 private Drawable mTextfieldBgDrawable;
Michael Kolb8441d4b2011-07-18 14:50:21 -070050 private PopupMenu mPopupMenu;
Michael Kolb20be26d2011-07-18 16:38:02 -070051 private boolean mMenuShowing;
Michael Kolb017ffab2011-07-11 15:26:47 -070052 private boolean mNeedsMenu;
Michael Kolb11d19782011-03-20 10:17:40 -070053
John Reck0f602f32011-07-07 15:38:43 -070054 public NavigationBarPhone(Context context) {
55 super(context);
56 }
57
58 public NavigationBarPhone(Context context, AttributeSet attrs) {
59 super(context, attrs);
60 }
61
62 public NavigationBarPhone(Context context, AttributeSet attrs, int defStyle) {
63 super(context, attrs, defStyle);
Michael Kolb11d19782011-03-20 10:17:40 -070064 }
65
66 @Override
John Reck0f602f32011-07-07 15:38:43 -070067 protected void onFinishInflate() {
68 super.onFinishInflate();
Michael Kolbc16c5952011-03-29 15:37:03 -070069 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolb11d19782011-03-20 10:17:40 -070070 mStopButton.setOnClickListener(this);
71 mVoiceButton = (ImageView) findViewById(R.id.voice);
72 mVoiceButton.setOnClickListener(this);
John Reck8ac42902011-06-29 16:14:34 -070073 mTabSwitcher = findViewById(R.id.tab_switcher);
74 mTabSwitcher.setOnClickListener(this);
Michael Kolb017ffab2011-07-11 15:26:47 -070075 mMore = findViewById(R.id.more);
76 mMore.setOnClickListener(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070077 mComboIcon = findViewById(R.id.iconcombo);
78 mTitleContainer = findViewById(R.id.title_bg);
Michael Kolb11d19782011-03-20 10:17:40 -070079 setFocusState(false);
John Reck0f602f32011-07-07 15:38:43 -070080 Resources res = getContext().getResources();
Michael Kolb2814a362011-05-19 15:49:41 -070081 mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
82 mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb30adae62011-07-29 13:37:05 -070083 mStopDescription = res.getString(R.string.accessibility_button_stop);
84 mRefreshDescription = res.getString(R.string.accessibility_button_refresh);
Michael Kolb305b1c52011-06-21 16:16:22 -070085 mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark);
86 setUaSwitcher(mComboIcon);
John Reck67f33632011-06-17 16:23:42 -070087 mUrlInput.setContainer(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070088 mUrlInput.setStateListener(this);
John Reck0f602f32011-07-07 15:38:43 -070089 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
Michael Kolb11d19782011-03-20 10:17:40 -070090 }
91
92 @Override
Michael Kolb11d19782011-03-20 10:17:40 -070093 protected void setSearchMode(boolean voiceSearchEnabled) {
94 boolean showvoicebutton = voiceSearchEnabled &&
95 mUiController.supportsVoiceSearch();
96 mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE :
97 View.GONE);
98 }
99
100 @Override
John Reck0f602f32011-07-07 15:38:43 -0700101 public void onProgressStarted() {
102 super.onProgressStarted();
103 if (mStopButton.getDrawable() != mStopDrawable) {
104 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700105 mStopButton.setContentDescription(mStopDescription);
John Reck0f602f32011-07-07 15:38:43 -0700106 if (mStopButton.getVisibility() != View.VISIBLE) {
107 mComboIcon.setVisibility(View.GONE);
108 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700109 }
John Reck0f602f32011-07-07 15:38:43 -0700110 }
111 }
112
113 @Override
114 public void onProgressStopped() {
115 super.onProgressStopped();
John Reck0f602f32011-07-07 15:38:43 -0700116 mStopButton.setImageDrawable(mRefreshDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700117 mStopButton.setContentDescription(mRefreshDescription);
John Reck0f602f32011-07-07 15:38:43 -0700118 if (!isEditingUrl()) {
119 mComboIcon.setVisibility(View.VISIBLE);
Michael Kolb2814a362011-05-19 15:49:41 -0700120 }
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700121 onStateChanged(mUrlInput.getState());
Michael Kolb2814a362011-05-19 15:49:41 -0700122 }
123
Michael Kolb11d19782011-03-20 10:17:40 -0700124 /**
125 * Update the text displayed in the title bar.
126 * @param title String to display. If null, the new tab string will be
127 * shown.
128 */
129 @Override
130 void setDisplayTitle(String title) {
John Reck67f33632011-06-17 16:23:42 -0700131 if (!isEditingUrl()) {
132 if (title == null) {
133 mUrlInput.setText(R.string.new_tab);
134 } else {
135 mUrlInput.setText(title);
136 }
137 mUrlInput.setSelection(0);
Michael Kolb11d19782011-03-20 10:17:40 -0700138 }
139 }
140
141 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700142 public void onClick(View v) {
143 if (v == mStopButton) {
John Reck0f602f32011-07-07 15:38:43 -0700144 if (mTitleBar.isInLoad()) {
Michael Kolb2814a362011-05-19 15:49:41 -0700145 mUiController.stopLoading();
146 } else {
147 WebView web = mBaseUi.getWebView();
148 if (web != null) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700149 stopEditingUrl();
Michael Kolb2814a362011-05-19 15:49:41 -0700150 web.reload();
151 }
152 }
Michael Kolb11d19782011-03-20 10:17:40 -0700153 } else if (v == mVoiceButton) {
154 mUiController.startVoiceSearch();
John Reck8ac42902011-06-29 16:14:34 -0700155 } else if (v == mTabSwitcher) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700156 ((PhoneUi) mBaseUi).toggleNavScreen();
Michael Kolb017ffab2011-07-11 15:26:47 -0700157 } else if (mMore == v) {
John Reckef654f12011-07-12 16:42:08 -0700158 showMenu(mMore);
Michael Kolb11d19782011-03-20 10:17:40 -0700159 } else {
160 super.onClick(v);
161 }
162 }
163
Michael Kolb017ffab2011-07-11 15:26:47 -0700164 public boolean isMenuShowing() {
Michael Kolb20be26d2011-07-18 16:38:02 -0700165 return mMenuShowing;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700166 }
167
John Reckef654f12011-07-12 16:42:08 -0700168 void showMenu(View anchor) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700169 mMenuShowing = true;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700170 mPopupMenu = new PopupMenu(mContext, anchor);
171 Menu menu = mPopupMenu.getMenu();
172 mPopupMenu.getMenuInflater().inflate(R.menu.browser, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -0700173 mUiController.updateMenuState(mBaseUi.getActiveTab(), menu);
Michael Kolb8441d4b2011-07-18 14:50:21 -0700174 mPopupMenu.setOnMenuItemClickListener(this);
175 mPopupMenu.setOnDismissListener(this);
176 mPopupMenu.show();
177 }
178
Michael Kolb017ffab2011-07-11 15:26:47 -0700179 @Override
180 public void onDismiss(PopupMenu menu) {
181 onMenuHidden();
182 }
183
184 @Override
185 public boolean onMenuItemClick(MenuItem item) {
186 onMenuHidden();
187 boolean res = mUiController.onOptionsItemSelected(item);
188 if (!res) {
189 return super.onMenuItemClick(item);
190 }
191 return res;
192 }
193
194 private void onMenuHidden() {
Michael Kolb20be26d2011-07-18 16:38:02 -0700195 mMenuShowing = false;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700196 mPopupMenu = null;
Michael Kolb017ffab2011-07-11 15:26:47 -0700197 mBaseUi.showTitleBarForDuration();
198 }
199
Michael Kolb305b1c52011-06-21 16:16:22 -0700200 @Override
201 public void onStateChanged(int state) {
202 switch(state) {
203 case StateListener.STATE_NORMAL:
204 mComboIcon.setVisibility(View.VISIBLE);
205 mStopButton.setVisibility(View.GONE);
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700206 setSearchMode(mInVoiceMode);
Michael Kolb305b1c52011-06-21 16:16:22 -0700207 mTabSwitcher.setVisibility(View.VISIBLE);
208 mTitleContainer.setBackgroundDrawable(null);
Michael Kolb017ffab2011-07-11 15:26:47 -0700209 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700210 break;
211 case StateListener.STATE_HIGHLIGHTED:
212 mComboIcon.setVisibility(View.GONE);
213 mStopButton.setVisibility(View.VISIBLE);
214 setSearchMode(true);
215 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700216 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700217 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
218 break;
219 case StateListener.STATE_EDITED:
220 mComboIcon.setVisibility(View.GONE);
221 mStopButton.setVisibility(View.GONE);
222 setSearchMode(false);
223 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700224 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700225 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
226 break;
Michael Kolb2814a362011-05-19 15:49:41 -0700227 }
228 }
Michael Kolb11d19782011-03-20 10:17:40 -0700229}