blob: 50ddea2cdec3cea1e6384f423f55d86c1dab3f33 [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 */
Bijan Amirzada41242f22014-03-21 12:12:18 -070016package com.android.browser;
Michael Kolb11d19782011-03-20 10:17:40 -070017
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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080027import org.codeaurora.swe.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
Bijan Amirzada41242f22014-03-21 12:12:18 -070033import com.android.browser.R;
34import com.android.browser.UrlInputView.StateListener;
Michael Kolb305b1c52011-06-21 16:16:22 -070035
John Reck0f602f32011-07-07 15:38:43 -070036public class NavigationBarPhone extends NavigationBarBase implements
John Reck42229bc2011-08-19 13:26:43 -070037 StateListener, OnMenuItemClickListener, OnDismissListener {
Michael Kolb11d19782011-03-20 10:17:40 -070038
Michael Kolbc16c5952011-03-29 15:37:03 -070039 private ImageView mStopButton;
Michael Kolb94ec5272011-08-31 16:23:57 -070040 private ImageView mMagnify;
41 private ImageView mClearButton;
Michael Kolb0b129122012-06-04 16:31:58 -070042 private ImageView mVoiceButton;
Michael Kolb2814a362011-05-19 15:49:41 -070043 private Drawable mStopDrawable;
44 private Drawable mRefreshDrawable;
Michael Kolb30adae62011-07-29 13:37:05 -070045 private String mStopDescription;
46 private String mRefreshDescription;
John Reck8ac42902011-06-29 16:14:34 -070047 private View mTabSwitcher;
Michael Kolb305b1c52011-06-21 16:16:22 -070048 private View mComboIcon;
49 private View mTitleContainer;
Michael Kolb017ffab2011-07-11 15:26:47 -070050 private View mMore;
Michael Kolb305b1c52011-06-21 16:16:22 -070051 private Drawable mTextfieldBgDrawable;
Michael Kolb8441d4b2011-07-18 14:50:21 -070052 private PopupMenu mPopupMenu;
John Reck58891902011-08-11 17:48:53 -070053 private boolean mOverflowMenuShowing;
Michael Kolb017ffab2011-07-11 15:26:47 -070054 private boolean mNeedsMenu;
John Reck419f6b42011-08-16 16:10:51 -070055 private View mIncognitoIcon;
Michael Kolb11d19782011-03-20 10:17:40 -070056
John Reck0f602f32011-07-07 15:38:43 -070057 public NavigationBarPhone(Context context) {
58 super(context);
59 }
60
61 public NavigationBarPhone(Context context, AttributeSet attrs) {
62 super(context, attrs);
63 }
64
65 public NavigationBarPhone(Context context, AttributeSet attrs, int defStyle) {
66 super(context, attrs, defStyle);
Michael Kolb11d19782011-03-20 10:17:40 -070067 }
68
69 @Override
John Reck0f602f32011-07-07 15:38:43 -070070 protected void onFinishInflate() {
71 super.onFinishInflate();
Michael Kolbc16c5952011-03-29 15:37:03 -070072 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolb11d19782011-03-20 10:17:40 -070073 mStopButton.setOnClickListener(this);
Michael Kolb94ec5272011-08-31 16:23:57 -070074 mClearButton = (ImageView) findViewById(R.id.clear);
75 mClearButton.setOnClickListener(this);
Michael Kolb0b129122012-06-04 16:31:58 -070076 mVoiceButton = (ImageView) findViewById(R.id.voice);
77 mVoiceButton.setOnClickListener(this);
Michael Kolb94ec5272011-08-31 16:23:57 -070078 mMagnify = (ImageView) findViewById(R.id.magnify);
John Reck8ac42902011-06-29 16:14:34 -070079 mTabSwitcher = findViewById(R.id.tab_switcher);
80 mTabSwitcher.setOnClickListener(this);
Michael Kolb017ffab2011-07-11 15:26:47 -070081 mMore = findViewById(R.id.more);
82 mMore.setOnClickListener(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070083 mComboIcon = findViewById(R.id.iconcombo);
Michael Kolb315d5022011-10-13 12:47:11 -070084 mComboIcon.setOnClickListener(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070085 mTitleContainer = findViewById(R.id.title_bg);
Michael Kolb11d19782011-03-20 10:17:40 -070086 setFocusState(false);
John Reck0f602f32011-07-07 15:38:43 -070087 Resources res = getContext().getResources();
Michael Kolb2814a362011-05-19 15:49:41 -070088 mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
89 mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb30adae62011-07-29 13:37:05 -070090 mStopDescription = res.getString(R.string.accessibility_button_stop);
91 mRefreshDescription = res.getString(R.string.accessibility_button_refresh);
Michael Kolb305b1c52011-06-21 16:16:22 -070092 mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark);
John Reck67f33632011-06-17 16:23:42 -070093 mUrlInput.setContainer(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070094 mUrlInput.setStateListener(this);
John Reck0f602f32011-07-07 15:38:43 -070095 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
John Reck419f6b42011-08-16 16:10:51 -070096 mIncognitoIcon = findViewById(R.id.incognito_icon);
Michael Kolb11d19782011-03-20 10:17:40 -070097 }
98
99 @Override
John Reck0f602f32011-07-07 15:38:43 -0700100 public void onProgressStarted() {
101 super.onProgressStarted();
102 if (mStopButton.getDrawable() != mStopDrawable) {
103 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700104 mStopButton.setContentDescription(mStopDescription);
John Reck0f602f32011-07-07 15:38:43 -0700105 if (mStopButton.getVisibility() != View.VISIBLE) {
106 mComboIcon.setVisibility(View.GONE);
107 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700108 }
John Reck0f602f32011-07-07 15:38:43 -0700109 }
110 }
111
112 @Override
113 public void onProgressStopped() {
114 super.onProgressStopped();
John Reck0f602f32011-07-07 15:38:43 -0700115 mStopButton.setImageDrawable(mRefreshDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700116 mStopButton.setContentDescription(mRefreshDescription);
John Reck0f602f32011-07-07 15:38:43 -0700117 if (!isEditingUrl()) {
118 mComboIcon.setVisibility(View.VISIBLE);
Michael Kolb2814a362011-05-19 15:49:41 -0700119 }
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700120 onStateChanged(mUrlInput.getState());
Michael Kolb2814a362011-05-19 15:49:41 -0700121 }
122
Michael Kolb11d19782011-03-20 10:17:40 -0700123 /**
124 * Update the text displayed in the title bar.
125 * @param title String to display. If null, the new tab string will be
126 * shown.
127 */
128 @Override
129 void setDisplayTitle(String title) {
John Reck434e9f82011-08-10 18:16:52 -0700130 mUrlInput.setTag(title);
John Reck67f33632011-06-17 16:23:42 -0700131 if (!isEditingUrl()) {
132 if (title == null) {
133 mUrlInput.setText(R.string.new_tab);
134 } else {
kayiz8b54fce2013-12-05 10:27:29 +0800135 Tab tab = mUiController.getTabControl().getCurrentTab();
136 if (tab != null && tab.getUrl() != null &&
137 tab.getUrl().startsWith("http://218.206.177.209:8080/waptest/browser15")) {
138 //for cmcc test case, display website title for specified cmcc website,
139 //not url address.
140 mUrlInput.setText(tab.getTitle(), false);
141 } else {
142 mUrlInput.setText(UrlUtils.stripUrl(title), false);
143 }
John Reck67f33632011-06-17 16:23:42 -0700144 }
145 mUrlInput.setSelection(0);
Michael Kolb11d19782011-03-20 10:17:40 -0700146 }
147 }
148
149 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700150 public void onClick(View v) {
151 if (v == mStopButton) {
John Reck0f602f32011-07-07 15:38:43 -0700152 if (mTitleBar.isInLoad()) {
Michael Kolb2814a362011-05-19 15:49:41 -0700153 mUiController.stopLoading();
154 } else {
155 WebView web = mBaseUi.getWebView();
156 if (web != null) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700157 stopEditingUrl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800158 Tab currentTab = mUiController.getTabControl().getCurrentTab();
159 if (currentTab.hasCrashed) {
160 currentTab.replaceCrashView(web, currentTab.getViewContainer());
161 }
Michael Kolb2814a362011-05-19 15:49:41 -0700162 web.reload();
163 }
164 }
John Reck8ac42902011-06-29 16:14:34 -0700165 } else if (v == mTabSwitcher) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700166 ((PhoneUi) mBaseUi).toggleNavScreen();
Michael Kolb017ffab2011-07-11 15:26:47 -0700167 } else if (mMore == v) {
John Reckef654f12011-07-12 16:42:08 -0700168 showMenu(mMore);
Michael Kolb94ec5272011-08-31 16:23:57 -0700169 } else if (mClearButton == v) {
170 mUrlInput.setText("");
Michael Kolb315d5022011-10-13 12:47:11 -0700171 } else if (mComboIcon == v) {
172 mUiController.showPageInfo();
Michael Kolb0b129122012-06-04 16:31:58 -0700173 } else if (mVoiceButton == v) {
174 mUiController.startVoiceRecognizer();
Michael Kolb11d19782011-03-20 10:17:40 -0700175 } else {
176 super.onClick(v);
177 }
178 }
179
John Reck58891902011-08-11 17:48:53 -0700180 @Override
Michael Kolb017ffab2011-07-11 15:26:47 -0700181 public boolean isMenuShowing() {
John Reck58891902011-08-11 17:48:53 -0700182 return super.isMenuShowing() || mOverflowMenuShowing;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700183 }
184
John Reckef654f12011-07-12 16:42:08 -0700185 void showMenu(View anchor) {
John Recke1a03a32011-09-14 17:04:16 -0700186 Activity activity = mUiController.getActivity();
187 if (mPopupMenu == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800188 mPopupMenu = new PopupMenu(getContext(), anchor);
John Recke1a03a32011-09-14 17:04:16 -0700189 mPopupMenu.setOnMenuItemClickListener(this);
190 mPopupMenu.setOnDismissListener(this);
191 if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
192 mPopupMenu = null;
193 return;
194 }
195 }
Michael Kolb8441d4b2011-07-18 14:50:21 -0700196 Menu menu = mPopupMenu.getMenu();
John Recke1a03a32011-09-14 17:04:16 -0700197 if (activity.onPrepareOptionsMenu(menu)) {
198 mOverflowMenuShowing = true;
199 mPopupMenu.show();
200 }
Michael Kolb8441d4b2011-07-18 14:50:21 -0700201 }
202
Michael Kolb017ffab2011-07-11 15:26:47 -0700203 @Override
204 public void onDismiss(PopupMenu menu) {
John Reck58891902011-08-11 17:48:53 -0700205 if (menu == mPopupMenu) {
206 onMenuHidden();
207 }
Michael Kolb017ffab2011-07-11 15:26:47 -0700208 }
209
Michael Kolb017ffab2011-07-11 15:26:47 -0700210 private void onMenuHidden() {
John Reck58891902011-08-11 17:48:53 -0700211 mOverflowMenuShowing = false;
Michael Kolb017ffab2011-07-11 15:26:47 -0700212 mBaseUi.showTitleBarForDuration();
213 }
214
Michael Kolb305b1c52011-06-21 16:16:22 -0700215 @Override
John Reck434e9f82011-08-10 18:16:52 -0700216 public void onFocusChange(View view, boolean hasFocus) {
217 if (view == mUrlInput) {
Michael Kolb2fc7c162011-08-31 13:38:24 -0700218 if (hasFocus && !mUrlInput.getText().toString().equals(mUrlInput.getTag())) {
219 // only change text if different
John Reck434e9f82011-08-10 18:16:52 -0700220 mUrlInput.setText((String) mUrlInput.getTag(), false);
John Reck2edc80c2011-11-18 09:27:21 -0800221 mUrlInput.selectAll();
John Reck434e9f82011-08-10 18:16:52 -0700222 } else {
223 setDisplayTitle(mUrlInput.getText().toString());
224 }
225 }
226 super.onFocusChange(view, hasFocus);
227 }
228
229 @Override
Michael Kolb305b1c52011-06-21 16:16:22 -0700230 public void onStateChanged(int state) {
Michael Kolbe721cc32012-06-26 15:26:59 -0700231 mVoiceButton.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700232 switch(state) {
233 case StateListener.STATE_NORMAL:
234 mComboIcon.setVisibility(View.VISIBLE);
235 mStopButton.setVisibility(View.GONE);
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 mTabSwitcher.setVisibility(View.VISIBLE);
239 mTitleContainer.setBackgroundDrawable(null);
Michael Kolb017ffab2011-07-11 15:26:47 -0700240 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700241 break;
242 case StateListener.STATE_HIGHLIGHTED:
243 mComboIcon.setVisibility(View.GONE);
244 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700245 mClearButton.setVisibility(View.GONE);
Michael Kolbe721cc32012-06-26 15:26:59 -0700246 if ((mUiController != null) && mUiController.supportsVoice()) {
247 mVoiceButton.setVisibility(View.VISIBLE);
248 }
Michael Kolb94ec5272011-08-31 16:23:57 -0700249 mMagnify.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700250 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700251 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700252 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
253 break;
254 case StateListener.STATE_EDITED:
255 mComboIcon.setVisibility(View.GONE);
256 mStopButton.setVisibility(View.GONE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700257 mClearButton.setVisibility(View.VISIBLE);
258 mMagnify.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700259 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700260 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700261 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
262 break;
Michael Kolb2814a362011-05-19 15:49:41 -0700263 }
264 }
John Reck419f6b42011-08-16 16:10:51 -0700265
266 @Override
267 public void onTabDataChanged(Tab tab) {
268 super.onTabDataChanged(tab);
269 mIncognitoIcon.setVisibility(tab.isPrivateBrowsingEnabled()
270 ? View.VISIBLE : View.GONE);
271 }
272
John Reck42229bc2011-08-19 13:26:43 -0700273 @Override
274 public boolean onMenuItemClick(MenuItem item) {
275 return mUiController.onOptionsItemSelected(item);
276 }
277
Michael Kolb11d19782011-03-20 10:17:40 -0700278}