blob: 7e8695b96e15ec294bc2620f02eeecec15479ab4 [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;
John Reck42229bc2011-08-19 13:26:43 -070030import android.widget.PopupMenu.OnMenuItemClickListener;
Michael Kolb11d19782011-03-20 10:17:40 -070031
Michael Kolb305b1c52011-06-21 16:16:22 -070032import com.android.browser.UrlInputView.StateListener;
Michael Kolb305b1c52011-06-21 16:16:22 -070033
John Reck0f602f32011-07-07 15:38:43 -070034public class NavigationBarPhone extends NavigationBarBase implements
John Reck42229bc2011-08-19 13:26:43 -070035 StateListener, OnMenuItemClickListener, OnDismissListener {
Michael Kolb11d19782011-03-20 10:17:40 -070036
Michael Kolbc16c5952011-03-29 15:37:03 -070037 private ImageView mStopButton;
Michael Kolb11d19782011-03-20 10:17:40 -070038 private ImageView mVoiceButton;
Michael Kolb94ec5272011-08-31 16:23:57 -070039 private ImageView mMagnify;
40 private ImageView mClearButton;
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;
John Reck58891902011-08-11 17:48:53 -070051 private boolean mOverflowMenuShowing;
Michael Kolb017ffab2011-07-11 15:26:47 -070052 private boolean mNeedsMenu;
John Reck419f6b42011-08-16 16:10:51 -070053 private View mIncognitoIcon;
Michael Kolb11d19782011-03-20 10:17:40 -070054
John Reck0f602f32011-07-07 15:38:43 -070055 public NavigationBarPhone(Context context) {
56 super(context);
57 }
58
59 public NavigationBarPhone(Context context, AttributeSet attrs) {
60 super(context, attrs);
61 }
62
63 public NavigationBarPhone(Context context, AttributeSet attrs, int defStyle) {
64 super(context, attrs, defStyle);
Michael Kolb11d19782011-03-20 10:17:40 -070065 }
66
67 @Override
John Reck0f602f32011-07-07 15:38:43 -070068 protected void onFinishInflate() {
69 super.onFinishInflate();
Michael Kolbc16c5952011-03-29 15:37:03 -070070 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolb11d19782011-03-20 10:17:40 -070071 mStopButton.setOnClickListener(this);
72 mVoiceButton = (ImageView) findViewById(R.id.voice);
73 mVoiceButton.setOnClickListener(this);
Michael Kolb94ec5272011-08-31 16:23:57 -070074 mClearButton = (ImageView) findViewById(R.id.clear);
75 mClearButton.setOnClickListener(this);
76 mMagnify = (ImageView) findViewById(R.id.magnify);
John Reck8ac42902011-06-29 16:14:34 -070077 mTabSwitcher = findViewById(R.id.tab_switcher);
78 mTabSwitcher.setOnClickListener(this);
Michael Kolb017ffab2011-07-11 15:26:47 -070079 mMore = findViewById(R.id.more);
80 mMore.setOnClickListener(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070081 mComboIcon = findViewById(R.id.iconcombo);
82 mTitleContainer = findViewById(R.id.title_bg);
Michael Kolb11d19782011-03-20 10:17:40 -070083 setFocusState(false);
John Reck0f602f32011-07-07 15:38:43 -070084 Resources res = getContext().getResources();
Michael Kolb2814a362011-05-19 15:49:41 -070085 mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
86 mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb30adae62011-07-29 13:37:05 -070087 mStopDescription = res.getString(R.string.accessibility_button_stop);
88 mRefreshDescription = res.getString(R.string.accessibility_button_refresh);
Michael Kolb305b1c52011-06-21 16:16:22 -070089 mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark);
John Reck67f33632011-06-17 16:23:42 -070090 mUrlInput.setContainer(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070091 mUrlInput.setStateListener(this);
John Reck0f602f32011-07-07 15:38:43 -070092 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
John Reck419f6b42011-08-16 16:10:51 -070093 mIncognitoIcon = findViewById(R.id.incognito_icon);
Michael Kolb11d19782011-03-20 10:17:40 -070094 }
95
96 @Override
Michael Kolb11d19782011-03-20 10:17:40 -070097 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);
Michael Kolb30adae62011-07-29 13:37:05 -0700109 mStopButton.setContentDescription(mStopDescription);
John Reck0f602f32011-07-07 15:38:43 -0700110 if (mStopButton.getVisibility() != View.VISIBLE) {
111 mComboIcon.setVisibility(View.GONE);
112 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700113 }
John Reck0f602f32011-07-07 15:38:43 -0700114 }
115 }
116
117 @Override
118 public void onProgressStopped() {
119 super.onProgressStopped();
John Reck0f602f32011-07-07 15:38:43 -0700120 mStopButton.setImageDrawable(mRefreshDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700121 mStopButton.setContentDescription(mRefreshDescription);
John Reck0f602f32011-07-07 15:38:43 -0700122 if (!isEditingUrl()) {
123 mComboIcon.setVisibility(View.VISIBLE);
Michael Kolb2814a362011-05-19 15:49:41 -0700124 }
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700125 onStateChanged(mUrlInput.getState());
Michael Kolb2814a362011-05-19 15:49:41 -0700126 }
127
Michael Kolb11d19782011-03-20 10:17:40 -0700128 /**
129 * Update the text displayed in the title bar.
130 * @param title String to display. If null, the new tab string will be
131 * shown.
132 */
133 @Override
134 void setDisplayTitle(String title) {
John Reck434e9f82011-08-10 18:16:52 -0700135 mUrlInput.setTag(title);
John Reck67f33632011-06-17 16:23:42 -0700136 if (!isEditingUrl()) {
137 if (title == null) {
138 mUrlInput.setText(R.string.new_tab);
139 } else {
John Reck434e9f82011-08-10 18:16:52 -0700140 mUrlInput.setText(UrlUtils.stripUrl(title), false);
John Reck67f33632011-06-17 16:23:42 -0700141 }
142 mUrlInput.setSelection(0);
Michael Kolb11d19782011-03-20 10:17:40 -0700143 }
144 }
145
146 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700147 public void onClick(View v) {
148 if (v == mStopButton) {
John Reck0f602f32011-07-07 15:38:43 -0700149 if (mTitleBar.isInLoad()) {
Michael Kolb2814a362011-05-19 15:49:41 -0700150 mUiController.stopLoading();
151 } else {
152 WebView web = mBaseUi.getWebView();
153 if (web != null) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700154 stopEditingUrl();
Michael Kolb2814a362011-05-19 15:49:41 -0700155 web.reload();
156 }
157 }
Michael Kolb11d19782011-03-20 10:17:40 -0700158 } else if (v == mVoiceButton) {
159 mUiController.startVoiceSearch();
John Reck8ac42902011-06-29 16:14:34 -0700160 } else if (v == mTabSwitcher) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700161 ((PhoneUi) mBaseUi).toggleNavScreen();
Michael Kolb017ffab2011-07-11 15:26:47 -0700162 } else if (mMore == v) {
John Reckef654f12011-07-12 16:42:08 -0700163 showMenu(mMore);
Michael Kolb94ec5272011-08-31 16:23:57 -0700164 } else if (mClearButton == v) {
165 mUrlInput.setText("");
Michael Kolb11d19782011-03-20 10:17:40 -0700166 } else {
167 super.onClick(v);
168 }
169 }
170
John Reck58891902011-08-11 17:48:53 -0700171 @Override
Michael Kolb017ffab2011-07-11 15:26:47 -0700172 public boolean isMenuShowing() {
John Reck58891902011-08-11 17:48:53 -0700173 return super.isMenuShowing() || mOverflowMenuShowing;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700174 }
175
John Reckef654f12011-07-12 16:42:08 -0700176 void showMenu(View anchor) {
John Reck58891902011-08-11 17:48:53 -0700177 mOverflowMenuShowing = true;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700178 mPopupMenu = new PopupMenu(mContext, anchor);
179 Menu menu = mPopupMenu.getMenu();
180 mPopupMenu.getMenuInflater().inflate(R.menu.browser, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -0700181 mUiController.updateMenuState(mBaseUi.getActiveTab(), menu);
Michael Kolb8441d4b2011-07-18 14:50:21 -0700182 mPopupMenu.setOnMenuItemClickListener(this);
183 mPopupMenu.setOnDismissListener(this);
184 mPopupMenu.show();
185 }
186
Michael Kolb017ffab2011-07-11 15:26:47 -0700187 @Override
188 public void onDismiss(PopupMenu menu) {
John Reck58891902011-08-11 17:48:53 -0700189 if (menu == mPopupMenu) {
190 onMenuHidden();
191 }
Michael Kolb017ffab2011-07-11 15:26:47 -0700192 }
193
Michael Kolb017ffab2011-07-11 15:26:47 -0700194 private void onMenuHidden() {
John Reck58891902011-08-11 17:48:53 -0700195 mOverflowMenuShowing = 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
John Reck434e9f82011-08-10 18:16:52 -0700201 public void onFocusChange(View view, boolean hasFocus) {
202 if (view == mUrlInput) {
Michael Kolb2fc7c162011-08-31 13:38:24 -0700203 if (hasFocus && !mUrlInput.getText().toString().equals(mUrlInput.getTag())) {
204 // only change text if different
John Reck434e9f82011-08-10 18:16:52 -0700205 mUrlInput.setText((String) mUrlInput.getTag(), false);
206 } else {
207 setDisplayTitle(mUrlInput.getText().toString());
208 }
209 }
210 super.onFocusChange(view, hasFocus);
211 }
212
213 @Override
Michael Kolb305b1c52011-06-21 16:16:22 -0700214 public void onStateChanged(int state) {
215 switch(state) {
216 case StateListener.STATE_NORMAL:
217 mComboIcon.setVisibility(View.VISIBLE);
218 mStopButton.setVisibility(View.GONE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700219 mClearButton.setVisibility(View.GONE);
220 mMagnify.setVisibility(View.GONE);
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700221 setSearchMode(mInVoiceMode);
Michael Kolb305b1c52011-06-21 16:16:22 -0700222 mTabSwitcher.setVisibility(View.VISIBLE);
223 mTitleContainer.setBackgroundDrawable(null);
Michael Kolb017ffab2011-07-11 15:26:47 -0700224 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700225 break;
226 case StateListener.STATE_HIGHLIGHTED:
227 mComboIcon.setVisibility(View.GONE);
228 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700229 mClearButton.setVisibility(View.GONE);
230 mMagnify.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700231 setSearchMode(true);
232 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700233 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700234 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
235 break;
236 case StateListener.STATE_EDITED:
237 mComboIcon.setVisibility(View.GONE);
238 mStopButton.setVisibility(View.GONE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700239 mClearButton.setVisibility(View.VISIBLE);
240 mMagnify.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700241 setSearchMode(false);
242 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700243 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700244 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
245 break;
Michael Kolb2814a362011-05-19 15:49:41 -0700246 }
247 }
John Reck419f6b42011-08-16 16:10:51 -0700248
249 @Override
250 public void onTabDataChanged(Tab tab) {
251 super.onTabDataChanged(tab);
252 mIncognitoIcon.setVisibility(tab.isPrivateBrowsingEnabled()
253 ? View.VISIBLE : View.GONE);
254 }
255
John Reck42229bc2011-08-19 13:26:43 -0700256 @Override
257 public boolean onMenuItemClick(MenuItem item) {
258 return mUiController.onOptionsItemSelected(item);
259 }
260
Michael Kolb11d19782011-03-20 10:17:40 -0700261}