blob: 2e206fbd6da7e36a8befa6c499a94007b24dcb71 [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);
Michael Kolb315d5022011-10-13 12:47:11 -070083 mComboIcon.setOnClickListener(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070084 mTitleContainer = findViewById(R.id.title_bg);
Michael Kolb11d19782011-03-20 10:17:40 -070085 setFocusState(false);
John Reck0f602f32011-07-07 15:38:43 -070086 Resources res = getContext().getResources();
Michael Kolb2814a362011-05-19 15:49:41 -070087 mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
88 mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb30adae62011-07-29 13:37:05 -070089 mStopDescription = res.getString(R.string.accessibility_button_stop);
90 mRefreshDescription = res.getString(R.string.accessibility_button_refresh);
Michael Kolb305b1c52011-06-21 16:16:22 -070091 mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark);
John Reck67f33632011-06-17 16:23:42 -070092 mUrlInput.setContainer(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070093 mUrlInput.setStateListener(this);
John Reck0f602f32011-07-07 15:38:43 -070094 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
John Reck419f6b42011-08-16 16:10:51 -070095 mIncognitoIcon = findViewById(R.id.incognito_icon);
Michael Kolb11d19782011-03-20 10:17:40 -070096 }
97
98 @Override
Michael Kolb11d19782011-03-20 10:17:40 -070099 protected void setSearchMode(boolean voiceSearchEnabled) {
100 boolean showvoicebutton = voiceSearchEnabled &&
101 mUiController.supportsVoiceSearch();
102 mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE :
103 View.GONE);
104 }
105
106 @Override
John Reck0f602f32011-07-07 15:38:43 -0700107 public void onProgressStarted() {
108 super.onProgressStarted();
109 if (mStopButton.getDrawable() != mStopDrawable) {
110 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700111 mStopButton.setContentDescription(mStopDescription);
John Reck0f602f32011-07-07 15:38:43 -0700112 if (mStopButton.getVisibility() != View.VISIBLE) {
113 mComboIcon.setVisibility(View.GONE);
114 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700115 }
John Reck0f602f32011-07-07 15:38:43 -0700116 }
117 }
118
119 @Override
120 public void onProgressStopped() {
121 super.onProgressStopped();
John Reck0f602f32011-07-07 15:38:43 -0700122 mStopButton.setImageDrawable(mRefreshDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700123 mStopButton.setContentDescription(mRefreshDescription);
John Reck0f602f32011-07-07 15:38:43 -0700124 if (!isEditingUrl()) {
125 mComboIcon.setVisibility(View.VISIBLE);
Michael Kolb2814a362011-05-19 15:49:41 -0700126 }
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700127 onStateChanged(mUrlInput.getState());
Michael Kolb2814a362011-05-19 15:49:41 -0700128 }
129
Michael Kolb11d19782011-03-20 10:17:40 -0700130 /**
131 * Update the text displayed in the title bar.
132 * @param title String to display. If null, the new tab string will be
133 * shown.
134 */
135 @Override
136 void setDisplayTitle(String title) {
John Reck434e9f82011-08-10 18:16:52 -0700137 mUrlInput.setTag(title);
John Reck67f33632011-06-17 16:23:42 -0700138 if (!isEditingUrl()) {
139 if (title == null) {
140 mUrlInput.setText(R.string.new_tab);
141 } else {
John Reck434e9f82011-08-10 18:16:52 -0700142 mUrlInput.setText(UrlUtils.stripUrl(title), false);
John Reck67f33632011-06-17 16:23:42 -0700143 }
144 mUrlInput.setSelection(0);
Michael Kolb11d19782011-03-20 10:17:40 -0700145 }
146 }
147
148 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700149 public void onClick(View v) {
150 if (v == mStopButton) {
John Reck0f602f32011-07-07 15:38:43 -0700151 if (mTitleBar.isInLoad()) {
Michael Kolb2814a362011-05-19 15:49:41 -0700152 mUiController.stopLoading();
153 } else {
154 WebView web = mBaseUi.getWebView();
155 if (web != null) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700156 stopEditingUrl();
Michael Kolb2814a362011-05-19 15:49:41 -0700157 web.reload();
158 }
159 }
Michael Kolb11d19782011-03-20 10:17:40 -0700160 } else if (v == mVoiceButton) {
161 mUiController.startVoiceSearch();
John Reck8ac42902011-06-29 16:14:34 -0700162 } else if (v == mTabSwitcher) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700163 ((PhoneUi) mBaseUi).toggleNavScreen();
Michael Kolb017ffab2011-07-11 15:26:47 -0700164 } else if (mMore == v) {
John Reckef654f12011-07-12 16:42:08 -0700165 showMenu(mMore);
Michael Kolb94ec5272011-08-31 16:23:57 -0700166 } else if (mClearButton == v) {
167 mUrlInput.setText("");
Michael Kolb315d5022011-10-13 12:47:11 -0700168 } else if (mComboIcon == v) {
169 mUiController.showPageInfo();
Michael Kolb11d19782011-03-20 10:17:40 -0700170 } else {
171 super.onClick(v);
172 }
173 }
174
John Reck58891902011-08-11 17:48:53 -0700175 @Override
Michael Kolb017ffab2011-07-11 15:26:47 -0700176 public boolean isMenuShowing() {
John Reck58891902011-08-11 17:48:53 -0700177 return super.isMenuShowing() || mOverflowMenuShowing;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700178 }
179
John Reckef654f12011-07-12 16:42:08 -0700180 void showMenu(View anchor) {
John Recke1a03a32011-09-14 17:04:16 -0700181 Activity activity = mUiController.getActivity();
182 if (mPopupMenu == null) {
183 mPopupMenu = new PopupMenu(mContext, anchor);
184 mPopupMenu.setOnMenuItemClickListener(this);
185 mPopupMenu.setOnDismissListener(this);
186 if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
187 mPopupMenu = null;
188 return;
189 }
190 }
Michael Kolb8441d4b2011-07-18 14:50:21 -0700191 Menu menu = mPopupMenu.getMenu();
John Recke1a03a32011-09-14 17:04:16 -0700192 if (activity.onPrepareOptionsMenu(menu)) {
193 mOverflowMenuShowing = true;
194 mPopupMenu.show();
195 }
Michael Kolb8441d4b2011-07-18 14:50:21 -0700196 }
197
Michael Kolb017ffab2011-07-11 15:26:47 -0700198 @Override
199 public void onDismiss(PopupMenu menu) {
John Reck58891902011-08-11 17:48:53 -0700200 if (menu == mPopupMenu) {
201 onMenuHidden();
202 }
Michael Kolb017ffab2011-07-11 15:26:47 -0700203 }
204
Michael Kolb017ffab2011-07-11 15:26:47 -0700205 private void onMenuHidden() {
John Reck58891902011-08-11 17:48:53 -0700206 mOverflowMenuShowing = false;
Michael Kolb017ffab2011-07-11 15:26:47 -0700207 mBaseUi.showTitleBarForDuration();
208 }
209
Michael Kolb305b1c52011-06-21 16:16:22 -0700210 @Override
John Reck434e9f82011-08-10 18:16:52 -0700211 public void onFocusChange(View view, boolean hasFocus) {
212 if (view == mUrlInput) {
Michael Kolb2fc7c162011-08-31 13:38:24 -0700213 if (hasFocus && !mUrlInput.getText().toString().equals(mUrlInput.getTag())) {
214 // only change text if different
John Reck434e9f82011-08-10 18:16:52 -0700215 mUrlInput.setText((String) mUrlInput.getTag(), false);
216 } else {
217 setDisplayTitle(mUrlInput.getText().toString());
218 }
219 }
220 super.onFocusChange(view, hasFocus);
221 }
222
223 @Override
Michael Kolb305b1c52011-06-21 16:16:22 -0700224 public void onStateChanged(int state) {
225 switch(state) {
226 case StateListener.STATE_NORMAL:
227 mComboIcon.setVisibility(View.VISIBLE);
228 mStopButton.setVisibility(View.GONE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700229 mClearButton.setVisibility(View.GONE);
230 mMagnify.setVisibility(View.GONE);
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700231 setSearchMode(mInVoiceMode);
Michael Kolb305b1c52011-06-21 16:16:22 -0700232 mTabSwitcher.setVisibility(View.VISIBLE);
233 mTitleContainer.setBackgroundDrawable(null);
Michael Kolb017ffab2011-07-11 15:26:47 -0700234 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700235 break;
236 case StateListener.STATE_HIGHLIGHTED:
237 mComboIcon.setVisibility(View.GONE);
238 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700239 mClearButton.setVisibility(View.GONE);
240 mMagnify.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700241 setSearchMode(true);
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;
246 case StateListener.STATE_EDITED:
247 mComboIcon.setVisibility(View.GONE);
248 mStopButton.setVisibility(View.GONE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700249 mClearButton.setVisibility(View.VISIBLE);
250 mMagnify.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700251 setSearchMode(false);
252 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700253 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700254 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
255 break;
Michael Kolb2814a362011-05-19 15:49:41 -0700256 }
257 }
John Reck419f6b42011-08-16 16:10:51 -0700258
259 @Override
260 public void onTabDataChanged(Tab tab) {
261 super.onTabDataChanged(tab);
262 mIncognitoIcon.setVisibility(tab.isPrivateBrowsingEnabled()
263 ? View.VISIBLE : View.GONE);
264 }
265
John Reck42229bc2011-08-19 13:26:43 -0700266 @Override
267 public boolean onMenuItemClick(MenuItem item) {
268 return mUiController.onOptionsItemSelected(item);
269 }
270
Michael Kolb11d19782011-03-20 10:17:40 -0700271}