blob: 0cb0764b871e3734603aa2a27b5d6d6c9fd7bae4 [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.content.Context;
Michael Kolb2814a362011-05-19 15:49:41 -070019import android.content.res.Resources;
20import android.graphics.drawable.Drawable;
John Reck0f602f32011-07-07 15:38:43 -070021import android.util.AttributeSet;
Michael Kolb017ffab2011-07-11 15:26:47 -070022import android.view.Menu;
Michael Kolb017ffab2011-07-11 15:26:47 -070023import android.view.MenuItem;
Michael Kolb11d19782011-03-20 10:17:40 -070024import android.view.View;
Michael Kolb017ffab2011-07-11 15:26:47 -070025import android.view.ViewConfiguration;
Michael Kolb2814a362011-05-19 15:49:41 -070026import android.webkit.WebView;
Michael Kolb11d19782011-03-20 10:17:40 -070027import android.widget.ImageView;
Michael Kolb017ffab2011-07-11 15:26:47 -070028import android.widget.PopupMenu;
29import android.widget.PopupMenu.OnDismissListener;
Michael Kolb11d19782011-03-20 10:17:40 -070030
Michael Kolb305b1c52011-06-21 16:16:22 -070031import com.android.browser.UrlInputView.StateListener;
Michael Kolb305b1c52011-06-21 16:16:22 -070032
John Reck0f602f32011-07-07 15:38:43 -070033public class NavigationBarPhone extends NavigationBarBase implements
John Reck58891902011-08-11 17:48:53 -070034 StateListener {
Michael Kolb11d19782011-03-20 10:17:40 -070035
Michael Kolbc16c5952011-03-29 15:37:03 -070036 private ImageView mStopButton;
Michael Kolb11d19782011-03-20 10:17:40 -070037 private ImageView mVoiceButton;
Michael Kolb2814a362011-05-19 15:49:41 -070038 private Drawable mStopDrawable;
39 private Drawable mRefreshDrawable;
Michael Kolb30adae62011-07-29 13:37:05 -070040 private String mStopDescription;
41 private String mRefreshDescription;
John Reck8ac42902011-06-29 16:14:34 -070042 private View mTabSwitcher;
Michael Kolb305b1c52011-06-21 16:16:22 -070043 private View mComboIcon;
44 private View mTitleContainer;
Michael Kolb017ffab2011-07-11 15:26:47 -070045 private View mMore;
Michael Kolb305b1c52011-06-21 16:16:22 -070046 private Drawable mTextfieldBgDrawable;
Michael Kolb8441d4b2011-07-18 14:50:21 -070047 private PopupMenu mPopupMenu;
John Reck58891902011-08-11 17:48:53 -070048 private boolean mOverflowMenuShowing;
Michael Kolb017ffab2011-07-11 15:26:47 -070049 private boolean mNeedsMenu;
Michael Kolb11d19782011-03-20 10:17:40 -070050
John Reck0f602f32011-07-07 15:38:43 -070051 public NavigationBarPhone(Context context) {
52 super(context);
53 }
54
55 public NavigationBarPhone(Context context, AttributeSet attrs) {
56 super(context, attrs);
57 }
58
59 public NavigationBarPhone(Context context, AttributeSet attrs, int defStyle) {
60 super(context, attrs, defStyle);
Michael Kolb11d19782011-03-20 10:17:40 -070061 }
62
63 @Override
John Reck0f602f32011-07-07 15:38:43 -070064 protected void onFinishInflate() {
65 super.onFinishInflate();
Michael Kolbc16c5952011-03-29 15:37:03 -070066 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolb11d19782011-03-20 10:17:40 -070067 mStopButton.setOnClickListener(this);
68 mVoiceButton = (ImageView) findViewById(R.id.voice);
69 mVoiceButton.setOnClickListener(this);
John Reck8ac42902011-06-29 16:14:34 -070070 mTabSwitcher = findViewById(R.id.tab_switcher);
71 mTabSwitcher.setOnClickListener(this);
Michael Kolb017ffab2011-07-11 15:26:47 -070072 mMore = findViewById(R.id.more);
73 mMore.setOnClickListener(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070074 mComboIcon = findViewById(R.id.iconcombo);
75 mTitleContainer = findViewById(R.id.title_bg);
Michael Kolb11d19782011-03-20 10:17:40 -070076 setFocusState(false);
John Reck0f602f32011-07-07 15:38:43 -070077 Resources res = getContext().getResources();
Michael Kolb2814a362011-05-19 15:49:41 -070078 mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
79 mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb30adae62011-07-29 13:37:05 -070080 mStopDescription = res.getString(R.string.accessibility_button_stop);
81 mRefreshDescription = res.getString(R.string.accessibility_button_refresh);
Michael Kolb305b1c52011-06-21 16:16:22 -070082 mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark);
83 setUaSwitcher(mComboIcon);
John Reck67f33632011-06-17 16:23:42 -070084 mUrlInput.setContainer(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070085 mUrlInput.setStateListener(this);
John Reck0f602f32011-07-07 15:38:43 -070086 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
Michael Kolb11d19782011-03-20 10:17:40 -070087 }
88
89 @Override
Michael Kolb11d19782011-03-20 10:17:40 -070090 protected void setSearchMode(boolean voiceSearchEnabled) {
91 boolean showvoicebutton = voiceSearchEnabled &&
92 mUiController.supportsVoiceSearch();
93 mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE :
94 View.GONE);
95 }
96
97 @Override
John Reck0f602f32011-07-07 15:38:43 -070098 public void onProgressStarted() {
99 super.onProgressStarted();
100 if (mStopButton.getDrawable() != mStopDrawable) {
101 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700102 mStopButton.setContentDescription(mStopDescription);
John Reck0f602f32011-07-07 15:38:43 -0700103 if (mStopButton.getVisibility() != View.VISIBLE) {
104 mComboIcon.setVisibility(View.GONE);
105 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700106 }
John Reck0f602f32011-07-07 15:38:43 -0700107 }
108 }
109
110 @Override
111 public void onProgressStopped() {
112 super.onProgressStopped();
John Reck0f602f32011-07-07 15:38:43 -0700113 mStopButton.setImageDrawable(mRefreshDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700114 mStopButton.setContentDescription(mRefreshDescription);
John Reck0f602f32011-07-07 15:38:43 -0700115 if (!isEditingUrl()) {
116 mComboIcon.setVisibility(View.VISIBLE);
Michael Kolb2814a362011-05-19 15:49:41 -0700117 }
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700118 onStateChanged(mUrlInput.getState());
Michael Kolb2814a362011-05-19 15:49:41 -0700119 }
120
Michael Kolb11d19782011-03-20 10:17:40 -0700121 /**
122 * Update the text displayed in the title bar.
123 * @param title String to display. If null, the new tab string will be
124 * shown.
125 */
126 @Override
127 void setDisplayTitle(String title) {
John Reck434e9f82011-08-10 18:16:52 -0700128 mUrlInput.setTag(title);
John Reck67f33632011-06-17 16:23:42 -0700129 if (!isEditingUrl()) {
130 if (title == null) {
131 mUrlInput.setText(R.string.new_tab);
132 } else {
John Reck434e9f82011-08-10 18:16:52 -0700133 mUrlInput.setText(UrlUtils.stripUrl(title), false);
John Reck67f33632011-06-17 16:23:42 -0700134 }
135 mUrlInput.setSelection(0);
Michael Kolb11d19782011-03-20 10:17:40 -0700136 }
137 }
138
139 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700140 public void onClick(View v) {
141 if (v == mStopButton) {
John Reck0f602f32011-07-07 15:38:43 -0700142 if (mTitleBar.isInLoad()) {
Michael Kolb2814a362011-05-19 15:49:41 -0700143 mUiController.stopLoading();
144 } else {
145 WebView web = mBaseUi.getWebView();
146 if (web != null) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700147 stopEditingUrl();
Michael Kolb2814a362011-05-19 15:49:41 -0700148 web.reload();
149 }
150 }
Michael Kolb11d19782011-03-20 10:17:40 -0700151 } else if (v == mVoiceButton) {
152 mUiController.startVoiceSearch();
John Reck8ac42902011-06-29 16:14:34 -0700153 } else if (v == mTabSwitcher) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700154 ((PhoneUi) mBaseUi).toggleNavScreen();
Michael Kolb017ffab2011-07-11 15:26:47 -0700155 } else if (mMore == v) {
John Reckef654f12011-07-12 16:42:08 -0700156 showMenu(mMore);
Michael Kolb11d19782011-03-20 10:17:40 -0700157 } else {
158 super.onClick(v);
159 }
160 }
161
John Reck58891902011-08-11 17:48:53 -0700162 @Override
Michael Kolb017ffab2011-07-11 15:26:47 -0700163 public boolean isMenuShowing() {
John Reck58891902011-08-11 17:48:53 -0700164 return super.isMenuShowing() || mOverflowMenuShowing;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700165 }
166
John Reckef654f12011-07-12 16:42:08 -0700167 void showMenu(View anchor) {
John Reck58891902011-08-11 17:48:53 -0700168 mOverflowMenuShowing = true;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700169 mPopupMenu = new PopupMenu(mContext, anchor);
170 Menu menu = mPopupMenu.getMenu();
171 mPopupMenu.getMenuInflater().inflate(R.menu.browser, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -0700172 mUiController.updateMenuState(mBaseUi.getActiveTab(), menu);
Michael Kolb8441d4b2011-07-18 14:50:21 -0700173 mPopupMenu.setOnMenuItemClickListener(this);
174 mPopupMenu.setOnDismissListener(this);
175 mPopupMenu.show();
176 }
177
Michael Kolb017ffab2011-07-11 15:26:47 -0700178 @Override
179 public void onDismiss(PopupMenu menu) {
John Reck58891902011-08-11 17:48:53 -0700180 if (menu == mPopupMenu) {
181 onMenuHidden();
182 }
183 super.onDismiss(menu);
Michael Kolb017ffab2011-07-11 15:26:47 -0700184 }
185
186 @Override
187 public boolean onMenuItemClick(MenuItem item) {
188 onMenuHidden();
189 boolean res = mUiController.onOptionsItemSelected(item);
190 if (!res) {
191 return super.onMenuItemClick(item);
192 }
193 return res;
194 }
195
196 private void onMenuHidden() {
John Reck58891902011-08-11 17:48:53 -0700197 mOverflowMenuShowing = false;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700198 mPopupMenu = null;
Michael Kolb017ffab2011-07-11 15:26:47 -0700199 mBaseUi.showTitleBarForDuration();
200 }
201
Michael Kolb305b1c52011-06-21 16:16:22 -0700202 @Override
John Reck434e9f82011-08-10 18:16:52 -0700203 public void onFocusChange(View view, boolean hasFocus) {
204 if (view == mUrlInput) {
205 if (hasFocus) {
206 mUrlInput.setText((String) mUrlInput.getTag(), false);
207 } else {
208 setDisplayTitle(mUrlInput.getText().toString());
209 }
210 }
211 super.onFocusChange(view, hasFocus);
212 }
213
214 @Override
Michael Kolb305b1c52011-06-21 16:16:22 -0700215 public void onStateChanged(int state) {
216 switch(state) {
217 case StateListener.STATE_NORMAL:
218 mComboIcon.setVisibility(View.VISIBLE);
219 mStopButton.setVisibility(View.GONE);
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700220 setSearchMode(mInVoiceMode);
Michael Kolb305b1c52011-06-21 16:16:22 -0700221 mTabSwitcher.setVisibility(View.VISIBLE);
222 mTitleContainer.setBackgroundDrawable(null);
Michael Kolb017ffab2011-07-11 15:26:47 -0700223 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700224 break;
225 case StateListener.STATE_HIGHLIGHTED:
226 mComboIcon.setVisibility(View.GONE);
227 mStopButton.setVisibility(View.VISIBLE);
228 setSearchMode(true);
229 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700230 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700231 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
232 break;
233 case StateListener.STATE_EDITED:
234 mComboIcon.setVisibility(View.GONE);
235 mStopButton.setVisibility(View.GONE);
236 setSearchMode(false);
237 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700238 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700239 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
240 break;
Michael Kolb2814a362011-05-19 15:49:41 -0700241 }
242 }
Michael Kolb11d19782011-03-20 10:17:40 -0700243}