blob: 2030169af4a9ba0aefa1da93b1d04bc9fe8b84cb [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;
John Reck8ac42902011-06-29 16:14:34 -070043 private View mTabSwitcher;
Michael Kolb305b1c52011-06-21 16:16:22 -070044 private View mComboIcon;
45 private View mTitleContainer;
Michael Kolb017ffab2011-07-11 15:26:47 -070046 private View mMore;
Michael Kolb305b1c52011-06-21 16:16:22 -070047 private Drawable mTextfieldBgDrawable;
Michael Kolb8441d4b2011-07-18 14:50:21 -070048 private PopupMenu mPopupMenu;
Michael Kolb20be26d2011-07-18 16:38:02 -070049 private boolean mMenuShowing;
Michael Kolb017ffab2011-07-11 15:26:47 -070050 private boolean mNeedsMenu;
Michael Kolb11d19782011-03-20 10:17:40 -070051
John Reck0f602f32011-07-07 15:38:43 -070052 public NavigationBarPhone(Context context) {
53 super(context);
54 }
55
56 public NavigationBarPhone(Context context, AttributeSet attrs) {
57 super(context, attrs);
58 }
59
60 public NavigationBarPhone(Context context, AttributeSet attrs, int defStyle) {
61 super(context, attrs, defStyle);
Michael Kolb11d19782011-03-20 10:17:40 -070062 }
63
64 @Override
John Reck0f602f32011-07-07 15:38:43 -070065 protected void onFinishInflate() {
66 super.onFinishInflate();
Michael Kolbc16c5952011-03-29 15:37:03 -070067 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolb11d19782011-03-20 10:17:40 -070068 mStopButton.setOnClickListener(this);
69 mVoiceButton = (ImageView) findViewById(R.id.voice);
70 mVoiceButton.setOnClickListener(this);
John Reck8ac42902011-06-29 16:14:34 -070071 mTabSwitcher = findViewById(R.id.tab_switcher);
72 mTabSwitcher.setOnClickListener(this);
Michael Kolb017ffab2011-07-11 15:26:47 -070073 mMore = findViewById(R.id.more);
74 mMore.setOnClickListener(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070075 mComboIcon = findViewById(R.id.iconcombo);
76 mTitleContainer = findViewById(R.id.title_bg);
Michael Kolb11d19782011-03-20 10:17:40 -070077 setFocusState(false);
John Reck0f602f32011-07-07 15:38:43 -070078 Resources res = getContext().getResources();
Michael Kolb2814a362011-05-19 15:49:41 -070079 mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
80 mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb305b1c52011-06-21 16:16:22 -070081 mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark);
82 setUaSwitcher(mComboIcon);
John Reck67f33632011-06-17 16:23:42 -070083 mUrlInput.setContainer(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070084 mUrlInput.setStateListener(this);
John Reck0f602f32011-07-07 15:38:43 -070085 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
Michael Kolb11d19782011-03-20 10:17:40 -070086 }
87
88 @Override
Michael Kolb11d19782011-03-20 10:17:40 -070089 public void createContextMenu(ContextMenu menu) {
John Reck0f602f32011-07-07 15:38:43 -070090 Activity activity = mBaseUi.getActivity();
91 MenuInflater inflater = activity.getMenuInflater();
Michael Kolb11d19782011-03-20 10:17:40 -070092 inflater.inflate(R.menu.title_context, menu);
John Reck0f602f32011-07-07 15:38:43 -070093 activity.onCreateContextMenu(menu, this, null);
Michael Kolb11d19782011-03-20 10:17:40 -070094 }
95
96 @Override
97 protected void setSearchMode(boolean voiceSearchEnabled) {
98 boolean showvoicebutton = voiceSearchEnabled &&
99 mUiController.supportsVoiceSearch();
100 mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE :
101 View.GONE);
102 }
103
104 @Override
John Reck0f602f32011-07-07 15:38:43 -0700105 public void onProgressStarted() {
106 super.onProgressStarted();
107 if (mStopButton.getDrawable() != mStopDrawable) {
108 mStopButton.setImageDrawable(mStopDrawable);
109 if (mStopButton.getVisibility() != View.VISIBLE) {
110 mComboIcon.setVisibility(View.GONE);
111 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700112 }
John Reck0f602f32011-07-07 15:38:43 -0700113 }
114 }
115
116 @Override
117 public void onProgressStopped() {
118 super.onProgressStopped();
John Reck0f602f32011-07-07 15:38:43 -0700119 mStopButton.setImageDrawable(mRefreshDrawable);
120 if (!isEditingUrl()) {
121 mComboIcon.setVisibility(View.VISIBLE);
Michael Kolb2814a362011-05-19 15:49:41 -0700122 }
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700123 onStateChanged(mUrlInput.getState());
Michael Kolb2814a362011-05-19 15:49:41 -0700124 }
125
Michael Kolb11d19782011-03-20 10:17:40 -0700126 /**
127 * Update the text displayed in the title bar.
128 * @param title String to display. If null, the new tab string will be
129 * shown.
130 */
131 @Override
132 void setDisplayTitle(String title) {
John Reck67f33632011-06-17 16:23:42 -0700133 if (!isEditingUrl()) {
134 if (title == null) {
135 mUrlInput.setText(R.string.new_tab);
136 } else {
137 mUrlInput.setText(title);
138 }
139 mUrlInput.setSelection(0);
Michael Kolb11d19782011-03-20 10:17:40 -0700140 }
141 }
142
143 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700144 public void onClick(View v) {
145 if (v == mStopButton) {
John Reck0f602f32011-07-07 15:38:43 -0700146 if (mTitleBar.isInLoad()) {
Michael Kolb2814a362011-05-19 15:49:41 -0700147 mUiController.stopLoading();
148 } else {
149 WebView web = mBaseUi.getWebView();
150 if (web != null) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700151 stopEditingUrl();
Michael Kolb2814a362011-05-19 15:49:41 -0700152 web.reload();
153 }
154 }
Michael Kolb11d19782011-03-20 10:17:40 -0700155 } else if (v == mVoiceButton) {
156 mUiController.startVoiceSearch();
John Reck8ac42902011-06-29 16:14:34 -0700157 } else if (v == mTabSwitcher) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700158 ((PhoneUi) mBaseUi).toggleNavScreen();
Michael Kolb017ffab2011-07-11 15:26:47 -0700159 } else if (mMore == v) {
John Reckef654f12011-07-12 16:42:08 -0700160 showMenu(mMore);
Michael Kolb11d19782011-03-20 10:17:40 -0700161 } else {
162 super.onClick(v);
163 }
164 }
165
Michael Kolb017ffab2011-07-11 15:26:47 -0700166 public boolean isMenuShowing() {
Michael Kolb20be26d2011-07-18 16:38:02 -0700167 return mMenuShowing;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700168 }
169
170 void showMenu() {
171 // called from menu key, use tab switcher as anchor
Michael Kolb20be26d2011-07-18 16:38:02 -0700172 mMenuShowing = true;
173 if (isEditingUrl()) {
174 stopEditingUrl();
175 post(new Runnable() {
176 @Override
177 public void run() {
178 showMenu();
179 }
180 });
181 } else {
182 mBaseUi.getTitleBar().setSkipTitleBarAnimations(true);
183 mBaseUi.showTitleBar();
184 mBaseUi.getTitleBar().setSkipTitleBarAnimations(false);
185 post(new Runnable() {
186 @Override
187 public void run() {
188 showMenu(mTabSwitcher);
189 }
190 });
191 }
Michael Kolb017ffab2011-07-11 15:26:47 -0700192 }
193
John Reckef654f12011-07-12 16:42:08 -0700194 void showMenu(View anchor) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700195 mMenuShowing = true;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700196 mPopupMenu = new PopupMenu(mContext, anchor);
197 Menu menu = mPopupMenu.getMenu();
198 mPopupMenu.getMenuInflater().inflate(R.menu.browser, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -0700199 mUiController.updateMenuState(mBaseUi.getActiveTab(), menu);
Michael Kolb8441d4b2011-07-18 14:50:21 -0700200 mPopupMenu.setOnMenuItemClickListener(this);
201 mPopupMenu.setOnDismissListener(this);
202 mPopupMenu.show();
203 }
204
205 void dismissMenu() {
206 if (mPopupMenu != null) {
207 mPopupMenu.dismiss();
Michael Kolb20be26d2011-07-18 16:38:02 -0700208 mBaseUi.suggestHideTitleBar();
Michael Kolb8441d4b2011-07-18 14:50:21 -0700209 }
Michael Kolb017ffab2011-07-11 15:26:47 -0700210 }
211
212 @Override
213 public void onDismiss(PopupMenu menu) {
214 onMenuHidden();
215 }
216
217 @Override
218 public boolean onMenuItemClick(MenuItem item) {
219 onMenuHidden();
220 boolean res = mUiController.onOptionsItemSelected(item);
221 if (!res) {
222 return super.onMenuItemClick(item);
223 }
224 return res;
225 }
226
227 private void onMenuHidden() {
Michael Kolb20be26d2011-07-18 16:38:02 -0700228 mMenuShowing = false;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700229 mPopupMenu = null;
Michael Kolb017ffab2011-07-11 15:26:47 -0700230 mBaseUi.showTitleBarForDuration();
231 }
232
Michael Kolb305b1c52011-06-21 16:16:22 -0700233 @Override
234 public void onStateChanged(int state) {
235 switch(state) {
236 case StateListener.STATE_NORMAL:
237 mComboIcon.setVisibility(View.VISIBLE);
238 mStopButton.setVisibility(View.GONE);
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700239 setSearchMode(mInVoiceMode);
Michael Kolb305b1c52011-06-21 16:16:22 -0700240 mTabSwitcher.setVisibility(View.VISIBLE);
241 mTitleContainer.setBackgroundDrawable(null);
Michael Kolb017ffab2011-07-11 15:26:47 -0700242 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700243 break;
244 case StateListener.STATE_HIGHLIGHTED:
245 mComboIcon.setVisibility(View.GONE);
246 mStopButton.setVisibility(View.VISIBLE);
247 setSearchMode(true);
248 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700249 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700250 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
251 break;
252 case StateListener.STATE_EDITED:
253 mComboIcon.setVisibility(View.GONE);
254 mStopButton.setVisibility(View.GONE);
255 setSearchMode(false);
256 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700257 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700258 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
259 break;
Michael Kolb2814a362011-05-19 15:49:41 -0700260 }
261 }
Michael Kolb11d19782011-03-20 10:17:40 -0700262}