blob: a1e778d7bd12fc8ae032ead401145567661e6c1b [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
John Recke1a03a32011-09-14 17:04:16 -070018import android.app.Activity;
Michael Kolb11d19782011-03-20 10:17:40 -070019import 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 Kolb017ffab2011-07-11 15:26:47 -070023import android.view.Menu;
Michael Kolb017ffab2011-07-11 15:26:47 -070024import android.view.MenuItem;
Michael Kolb11d19782011-03-20 10:17:40 -070025import android.view.View;
Michael Kolb017ffab2011-07-11 15:26:47 -070026import android.view.ViewConfiguration;
Michael Kolb2814a362011-05-19 15:49:41 -070027import android.webkit.WebView;
Michael Kolb11d19782011-03-20 10:17:40 -070028import android.widget.ImageView;
Michael Kolb017ffab2011-07-11 15:26:47 -070029import android.widget.PopupMenu;
30import android.widget.PopupMenu.OnDismissListener;
John Reck42229bc2011-08-19 13:26:43 -070031import android.widget.PopupMenu.OnMenuItemClickListener;
Michael Kolb11d19782011-03-20 10:17:40 -070032
Michael Kolb305b1c52011-06-21 16:16:22 -070033import com.android.browser.UrlInputView.StateListener;
Michael Kolb305b1c52011-06-21 16:16:22 -070034
John Reck0f602f32011-07-07 15:38:43 -070035public class NavigationBarPhone extends NavigationBarBase implements
John Reck42229bc2011-08-19 13:26:43 -070036 StateListener, OnMenuItemClickListener, OnDismissListener {
Michael Kolb11d19782011-03-20 10:17:40 -070037
Michael Kolbc16c5952011-03-29 15:37:03 -070038 private ImageView mStopButton;
Michael Kolb11d19782011-03-20 10:17:40 -070039 private ImageView mVoiceButton;
Michael Kolb94ec5272011-08-31 16:23:57 -070040 private ImageView mMagnify;
41 private ImageView mClearButton;
Michael Kolb2814a362011-05-19 15:49:41 -070042 private Drawable mStopDrawable;
43 private Drawable mRefreshDrawable;
Michael Kolb30adae62011-07-29 13:37:05 -070044 private String mStopDescription;
45 private String mRefreshDescription;
John Reck8ac42902011-06-29 16:14:34 -070046 private View mTabSwitcher;
Michael Kolb305b1c52011-06-21 16:16:22 -070047 private View mComboIcon;
48 private View mTitleContainer;
Michael Kolb017ffab2011-07-11 15:26:47 -070049 private View mMore;
Michael Kolb305b1c52011-06-21 16:16:22 -070050 private Drawable mTextfieldBgDrawable;
Michael Kolb8441d4b2011-07-18 14:50:21 -070051 private PopupMenu mPopupMenu;
John Reck58891902011-08-11 17:48:53 -070052 private boolean mOverflowMenuShowing;
Michael Kolb017ffab2011-07-11 15:26:47 -070053 private boolean mNeedsMenu;
John Reck419f6b42011-08-16 16:10:51 -070054 private View mIncognitoIcon;
Michael Kolb11d19782011-03-20 10:17:40 -070055
John Reck0f602f32011-07-07 15:38:43 -070056 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);
Michael Kolb11d19782011-03-20 10:17:40 -070066 }
67
68 @Override
John Reck0f602f32011-07-07 15:38:43 -070069 protected void onFinishInflate() {
70 super.onFinishInflate();
Michael Kolbc16c5952011-03-29 15:37:03 -070071 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolb11d19782011-03-20 10:17:40 -070072 mStopButton.setOnClickListener(this);
73 mVoiceButton = (ImageView) findViewById(R.id.voice);
74 mVoiceButton.setOnClickListener(this);
Michael Kolb94ec5272011-08-31 16:23:57 -070075 mClearButton = (ImageView) findViewById(R.id.clear);
76 mClearButton.setOnClickListener(this);
77 mMagnify = (ImageView) findViewById(R.id.magnify);
John Reck8ac42902011-06-29 16:14:34 -070078 mTabSwitcher = findViewById(R.id.tab_switcher);
79 mTabSwitcher.setOnClickListener(this);
Michael Kolb017ffab2011-07-11 15:26:47 -070080 mMore = findViewById(R.id.more);
81 mMore.setOnClickListener(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070082 mComboIcon = findViewById(R.id.iconcombo);
83 mTitleContainer = findViewById(R.id.title_bg);
Michael Kolb11d19782011-03-20 10:17:40 -070084 setFocusState(false);
John Reck0f602f32011-07-07 15:38:43 -070085 Resources res = getContext().getResources();
Michael Kolb2814a362011-05-19 15:49:41 -070086 mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
87 mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb30adae62011-07-29 13:37:05 -070088 mStopDescription = res.getString(R.string.accessibility_button_stop);
89 mRefreshDescription = res.getString(R.string.accessibility_button_refresh);
Michael Kolb305b1c52011-06-21 16:16:22 -070090 mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark);
John Reck67f33632011-06-17 16:23:42 -070091 mUrlInput.setContainer(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070092 mUrlInput.setStateListener(this);
John Reck0f602f32011-07-07 15:38:43 -070093 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
John Reck419f6b42011-08-16 16:10:51 -070094 mIncognitoIcon = findViewById(R.id.incognito_icon);
Michael Kolb11d19782011-03-20 10:17:40 -070095 }
96
97 @Override
Michael Kolb11d19782011-03-20 10:17:40 -070098 protected void setSearchMode(boolean voiceSearchEnabled) {
99 boolean showvoicebutton = voiceSearchEnabled &&
100 mUiController.supportsVoiceSearch();
101 mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE :
102 View.GONE);
103 }
104
105 @Override
John Reck0f602f32011-07-07 15:38:43 -0700106 public void onProgressStarted() {
107 super.onProgressStarted();
108 if (mStopButton.getDrawable() != mStopDrawable) {
109 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700110 mStopButton.setContentDescription(mStopDescription);
John Reck0f602f32011-07-07 15:38:43 -0700111 if (mStopButton.getVisibility() != View.VISIBLE) {
112 mComboIcon.setVisibility(View.GONE);
113 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700114 }
John Reck0f602f32011-07-07 15:38:43 -0700115 }
116 }
117
118 @Override
119 public void onProgressStopped() {
120 super.onProgressStopped();
John Reck0f602f32011-07-07 15:38:43 -0700121 mStopButton.setImageDrawable(mRefreshDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700122 mStopButton.setContentDescription(mRefreshDescription);
John Reck0f602f32011-07-07 15:38:43 -0700123 if (!isEditingUrl()) {
124 mComboIcon.setVisibility(View.VISIBLE);
Michael Kolb2814a362011-05-19 15:49:41 -0700125 }
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700126 onStateChanged(mUrlInput.getState());
Michael Kolb2814a362011-05-19 15:49:41 -0700127 }
128
Michael Kolb11d19782011-03-20 10:17:40 -0700129 /**
130 * Update the text displayed in the title bar.
131 * @param title String to display. If null, the new tab string will be
132 * shown.
133 */
134 @Override
135 void setDisplayTitle(String title) {
John Reck434e9f82011-08-10 18:16:52 -0700136 mUrlInput.setTag(title);
John Reck67f33632011-06-17 16:23:42 -0700137 if (!isEditingUrl()) {
138 if (title == null) {
139 mUrlInput.setText(R.string.new_tab);
140 } else {
John Reck434e9f82011-08-10 18:16:52 -0700141 mUrlInput.setText(UrlUtils.stripUrl(title), false);
John Reck67f33632011-06-17 16:23:42 -0700142 }
143 mUrlInput.setSelection(0);
Michael Kolb11d19782011-03-20 10:17:40 -0700144 }
145 }
146
147 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700148 public void onClick(View v) {
149 if (v == mStopButton) {
John Reck0f602f32011-07-07 15:38:43 -0700150 if (mTitleBar.isInLoad()) {
Michael Kolb2814a362011-05-19 15:49:41 -0700151 mUiController.stopLoading();
152 } else {
153 WebView web = mBaseUi.getWebView();
154 if (web != null) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700155 stopEditingUrl();
Michael Kolb2814a362011-05-19 15:49:41 -0700156 web.reload();
157 }
158 }
Michael Kolb11d19782011-03-20 10:17:40 -0700159 } else if (v == mVoiceButton) {
160 mUiController.startVoiceSearch();
John Reck8ac42902011-06-29 16:14:34 -0700161 } else if (v == mTabSwitcher) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700162 ((PhoneUi) mBaseUi).toggleNavScreen();
Michael Kolb017ffab2011-07-11 15:26:47 -0700163 } else if (mMore == v) {
John Reckef654f12011-07-12 16:42:08 -0700164 showMenu(mMore);
Michael Kolb94ec5272011-08-31 16:23:57 -0700165 } else if (mClearButton == v) {
166 mUrlInput.setText("");
Michael Kolb11d19782011-03-20 10:17:40 -0700167 } else {
168 super.onClick(v);
169 }
170 }
171
John Reck58891902011-08-11 17:48:53 -0700172 @Override
Michael Kolb017ffab2011-07-11 15:26:47 -0700173 public boolean isMenuShowing() {
John Reck58891902011-08-11 17:48:53 -0700174 return super.isMenuShowing() || mOverflowMenuShowing;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700175 }
176
John Reckef654f12011-07-12 16:42:08 -0700177 void showMenu(View anchor) {
John Recke1a03a32011-09-14 17:04:16 -0700178 Activity activity = mUiController.getActivity();
179 if (mPopupMenu == null) {
180 mPopupMenu = new PopupMenu(mContext, anchor);
181 mPopupMenu.setOnMenuItemClickListener(this);
182 mPopupMenu.setOnDismissListener(this);
183 if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
184 mPopupMenu = null;
185 return;
186 }
187 }
Michael Kolb8441d4b2011-07-18 14:50:21 -0700188 Menu menu = mPopupMenu.getMenu();
John Recke1a03a32011-09-14 17:04:16 -0700189 if (activity.onPrepareOptionsMenu(menu)) {
190 mOverflowMenuShowing = true;
191 mPopupMenu.show();
192 }
Michael Kolb8441d4b2011-07-18 14:50:21 -0700193 }
194
Michael Kolb017ffab2011-07-11 15:26:47 -0700195 @Override
196 public void onDismiss(PopupMenu menu) {
John Reck58891902011-08-11 17:48:53 -0700197 if (menu == mPopupMenu) {
198 onMenuHidden();
199 }
Michael Kolb017ffab2011-07-11 15:26:47 -0700200 }
201
Michael Kolb017ffab2011-07-11 15:26:47 -0700202 private void onMenuHidden() {
John Reck58891902011-08-11 17:48:53 -0700203 mOverflowMenuShowing = false;
Michael Kolb017ffab2011-07-11 15:26:47 -0700204 mBaseUi.showTitleBarForDuration();
205 }
206
Michael Kolb305b1c52011-06-21 16:16:22 -0700207 @Override
John Reck434e9f82011-08-10 18:16:52 -0700208 public void onFocusChange(View view, boolean hasFocus) {
209 if (view == mUrlInput) {
Michael Kolb2fc7c162011-08-31 13:38:24 -0700210 if (hasFocus && !mUrlInput.getText().toString().equals(mUrlInput.getTag())) {
211 // only change text if different
John Reck434e9f82011-08-10 18:16:52 -0700212 mUrlInput.setText((String) mUrlInput.getTag(), false);
213 } else {
214 setDisplayTitle(mUrlInput.getText().toString());
215 }
216 }
217 super.onFocusChange(view, hasFocus);
218 }
219
220 @Override
Michael Kolb305b1c52011-06-21 16:16:22 -0700221 public void onStateChanged(int state) {
222 switch(state) {
223 case StateListener.STATE_NORMAL:
224 mComboIcon.setVisibility(View.VISIBLE);
225 mStopButton.setVisibility(View.GONE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700226 mClearButton.setVisibility(View.GONE);
227 mMagnify.setVisibility(View.GONE);
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700228 setSearchMode(mInVoiceMode);
Michael Kolb305b1c52011-06-21 16:16:22 -0700229 mTabSwitcher.setVisibility(View.VISIBLE);
230 mTitleContainer.setBackgroundDrawable(null);
Michael Kolb017ffab2011-07-11 15:26:47 -0700231 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700232 break;
233 case StateListener.STATE_HIGHLIGHTED:
234 mComboIcon.setVisibility(View.GONE);
235 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700236 mClearButton.setVisibility(View.GONE);
237 mMagnify.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700238 setSearchMode(true);
239 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700240 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700241 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
242 break;
243 case StateListener.STATE_EDITED:
244 mComboIcon.setVisibility(View.GONE);
245 mStopButton.setVisibility(View.GONE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700246 mClearButton.setVisibility(View.VISIBLE);
247 mMagnify.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700248 setSearchMode(false);
249 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700250 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700251 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
252 break;
Michael Kolb2814a362011-05-19 15:49:41 -0700253 }
254 }
John Reck419f6b42011-08-16 16:10:51 -0700255
256 @Override
257 public void onTabDataChanged(Tab tab) {
258 super.onTabDataChanged(tab);
259 mIncognitoIcon.setVisibility(tab.isPrivateBrowsingEnabled()
260 ? View.VISIBLE : View.GONE);
261 }
262
John Reck42229bc2011-08-19 13:26:43 -0700263 @Override
264 public boolean onMenuItemClick(MenuItem item) {
265 return mUiController.onOptionsItemSelected(item);
266 }
267
Michael Kolb11d19782011-03-20 10:17:40 -0700268}