blob: 23176589296c2f501e3f19d7d5b429a0cc7c8b55 [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 Kolb94ec5272011-08-31 16:23:57 -070039 private ImageView mMagnify;
40 private ImageView mClearButton;
Michael Kolb0b129122012-06-04 16:31:58 -070041 private ImageView mVoiceButton;
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);
Michael Kolb94ec5272011-08-31 16:23:57 -070073 mClearButton = (ImageView) findViewById(R.id.clear);
74 mClearButton.setOnClickListener(this);
Michael Kolb0b129122012-06-04 16:31:58 -070075 mVoiceButton = (ImageView) findViewById(R.id.voice);
76 mVoiceButton.setOnClickListener(this);
Michael Kolb94ec5272011-08-31 16:23:57 -070077 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
John Reck0f602f32011-07-07 15:38:43 -070099 public void onProgressStarted() {
100 super.onProgressStarted();
101 if (mStopButton.getDrawable() != mStopDrawable) {
102 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700103 mStopButton.setContentDescription(mStopDescription);
John Reck0f602f32011-07-07 15:38:43 -0700104 if (mStopButton.getVisibility() != View.VISIBLE) {
105 mComboIcon.setVisibility(View.GONE);
106 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700107 }
John Reck0f602f32011-07-07 15:38:43 -0700108 }
109 }
110
111 @Override
112 public void onProgressStopped() {
113 super.onProgressStopped();
John Reck0f602f32011-07-07 15:38:43 -0700114 mStopButton.setImageDrawable(mRefreshDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700115 mStopButton.setContentDescription(mRefreshDescription);
John Reck0f602f32011-07-07 15:38:43 -0700116 if (!isEditingUrl()) {
117 mComboIcon.setVisibility(View.VISIBLE);
Michael Kolb2814a362011-05-19 15:49:41 -0700118 }
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700119 onStateChanged(mUrlInput.getState());
Michael Kolb2814a362011-05-19 15:49:41 -0700120 }
121
Michael Kolb11d19782011-03-20 10:17:40 -0700122 /**
123 * Update the text displayed in the title bar.
124 * @param title String to display. If null, the new tab string will be
125 * shown.
126 */
127 @Override
128 void setDisplayTitle(String title) {
John Reck434e9f82011-08-10 18:16:52 -0700129 mUrlInput.setTag(title);
John Reck67f33632011-06-17 16:23:42 -0700130 if (!isEditingUrl()) {
131 if (title == null) {
132 mUrlInput.setText(R.string.new_tab);
133 } else {
kayiz8b54fce2013-12-05 10:27:29 +0800134 Tab tab = mUiController.getTabControl().getCurrentTab();
135 if (tab != null && tab.getUrl() != null &&
136 tab.getUrl().startsWith("http://218.206.177.209:8080/waptest/browser15")) {
137 //for cmcc test case, display website title for specified cmcc website,
138 //not url address.
139 mUrlInput.setText(tab.getTitle(), false);
140 } else {
141 mUrlInput.setText(UrlUtils.stripUrl(title), false);
142 }
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 }
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 Kolb315d5022011-10-13 12:47:11 -0700166 } else if (mComboIcon == v) {
167 mUiController.showPageInfo();
Michael Kolb0b129122012-06-04 16:31:58 -0700168 } else if (mVoiceButton == v) {
169 mUiController.startVoiceRecognizer();
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);
John Reck2edc80c2011-11-18 09:27:21 -0800216 mUrlInput.selectAll();
John Reck434e9f82011-08-10 18:16:52 -0700217 } else {
218 setDisplayTitle(mUrlInput.getText().toString());
219 }
220 }
221 super.onFocusChange(view, hasFocus);
222 }
223
224 @Override
Michael Kolb305b1c52011-06-21 16:16:22 -0700225 public void onStateChanged(int state) {
Michael Kolbe721cc32012-06-26 15:26:59 -0700226 mVoiceButton.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700227 switch(state) {
228 case StateListener.STATE_NORMAL:
229 mComboIcon.setVisibility(View.VISIBLE);
230 mStopButton.setVisibility(View.GONE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700231 mClearButton.setVisibility(View.GONE);
232 mMagnify.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700233 mTabSwitcher.setVisibility(View.VISIBLE);
234 mTitleContainer.setBackgroundDrawable(null);
Michael Kolb017ffab2011-07-11 15:26:47 -0700235 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700236 break;
237 case StateListener.STATE_HIGHLIGHTED:
238 mComboIcon.setVisibility(View.GONE);
239 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700240 mClearButton.setVisibility(View.GONE);
Michael Kolbe721cc32012-06-26 15:26:59 -0700241 if ((mUiController != null) && mUiController.supportsVoice()) {
242 mVoiceButton.setVisibility(View.VISIBLE);
243 }
Michael Kolb94ec5272011-08-31 16:23:57 -0700244 mMagnify.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700245 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700246 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700247 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
248 break;
249 case StateListener.STATE_EDITED:
250 mComboIcon.setVisibility(View.GONE);
251 mStopButton.setVisibility(View.GONE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700252 mClearButton.setVisibility(View.VISIBLE);
253 mMagnify.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700254 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700255 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700256 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
257 break;
Michael Kolb2814a362011-05-19 15:49:41 -0700258 }
259 }
John Reck419f6b42011-08-16 16:10:51 -0700260
261 @Override
262 public void onTabDataChanged(Tab tab) {
263 super.onTabDataChanged(tab);
264 mIncognitoIcon.setVisibility(tab.isPrivateBrowsingEnabled()
265 ? View.VISIBLE : View.GONE);
266 }
267
John Reck42229bc2011-08-19 13:26:43 -0700268 @Override
269 public boolean onMenuItemClick(MenuItem item) {
270 return mUiController.onOptionsItemSelected(item);
271 }
272
Michael Kolb11d19782011-03-20 10:17:40 -0700273}