blob: f3eaa2a45688c4cf7957d1947f4c387e7744e97a [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 {
John Reck434e9f82011-08-10 18:16:52 -0700134 mUrlInput.setText(UrlUtils.stripUrl(title), false);
John Reck67f33632011-06-17 16:23:42 -0700135 }
136 mUrlInput.setSelection(0);
Michael Kolb11d19782011-03-20 10:17:40 -0700137 }
138 }
139
140 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700141 public void onClick(View v) {
142 if (v == mStopButton) {
John Reck0f602f32011-07-07 15:38:43 -0700143 if (mTitleBar.isInLoad()) {
Michael Kolb2814a362011-05-19 15:49:41 -0700144 mUiController.stopLoading();
145 } else {
146 WebView web = mBaseUi.getWebView();
147 if (web != null) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700148 stopEditingUrl();
Michael Kolb2814a362011-05-19 15:49:41 -0700149 web.reload();
150 }
151 }
John Reck8ac42902011-06-29 16:14:34 -0700152 } else if (v == mTabSwitcher) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700153 ((PhoneUi) mBaseUi).toggleNavScreen();
Michael Kolb017ffab2011-07-11 15:26:47 -0700154 } else if (mMore == v) {
John Reckef654f12011-07-12 16:42:08 -0700155 showMenu(mMore);
Michael Kolb94ec5272011-08-31 16:23:57 -0700156 } else if (mClearButton == v) {
157 mUrlInput.setText("");
Michael Kolb315d5022011-10-13 12:47:11 -0700158 } else if (mComboIcon == v) {
159 mUiController.showPageInfo();
Michael Kolb0b129122012-06-04 16:31:58 -0700160 } else if (mVoiceButton == v) {
161 mUiController.startVoiceRecognizer();
Michael Kolb11d19782011-03-20 10:17:40 -0700162 } else {
163 super.onClick(v);
164 }
165 }
166
John Reck58891902011-08-11 17:48:53 -0700167 @Override
Michael Kolb017ffab2011-07-11 15:26:47 -0700168 public boolean isMenuShowing() {
John Reck58891902011-08-11 17:48:53 -0700169 return super.isMenuShowing() || mOverflowMenuShowing;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700170 }
171
John Reckef654f12011-07-12 16:42:08 -0700172 void showMenu(View anchor) {
John Recke1a03a32011-09-14 17:04:16 -0700173 Activity activity = mUiController.getActivity();
174 if (mPopupMenu == null) {
175 mPopupMenu = new PopupMenu(mContext, anchor);
176 mPopupMenu.setOnMenuItemClickListener(this);
177 mPopupMenu.setOnDismissListener(this);
178 if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
179 mPopupMenu = null;
180 return;
181 }
182 }
Michael Kolb8441d4b2011-07-18 14:50:21 -0700183 Menu menu = mPopupMenu.getMenu();
John Recke1a03a32011-09-14 17:04:16 -0700184 if (activity.onPrepareOptionsMenu(menu)) {
185 mOverflowMenuShowing = true;
186 mPopupMenu.show();
187 }
Michael Kolb8441d4b2011-07-18 14:50:21 -0700188 }
189
Michael Kolb017ffab2011-07-11 15:26:47 -0700190 @Override
191 public void onDismiss(PopupMenu menu) {
John Reck58891902011-08-11 17:48:53 -0700192 if (menu == mPopupMenu) {
193 onMenuHidden();
194 }
Michael Kolb017ffab2011-07-11 15:26:47 -0700195 }
196
Michael Kolb017ffab2011-07-11 15:26:47 -0700197 private void onMenuHidden() {
John Reck58891902011-08-11 17:48:53 -0700198 mOverflowMenuShowing = false;
Michael Kolb017ffab2011-07-11 15:26:47 -0700199 mBaseUi.showTitleBarForDuration();
200 }
201
Michael Kolb305b1c52011-06-21 16:16:22 -0700202 @Override
John Reck434e9f82011-08-10 18:16:52 -0700203 public void onFocusChange(View view, boolean hasFocus) {
204 if (view == mUrlInput) {
Michael Kolb2fc7c162011-08-31 13:38:24 -0700205 if (hasFocus && !mUrlInput.getText().toString().equals(mUrlInput.getTag())) {
206 // only change text if different
John Reck434e9f82011-08-10 18:16:52 -0700207 mUrlInput.setText((String) mUrlInput.getTag(), false);
John Reck2edc80c2011-11-18 09:27:21 -0800208 mUrlInput.selectAll();
John Reck434e9f82011-08-10 18:16:52 -0700209 } else {
210 setDisplayTitle(mUrlInput.getText().toString());
211 }
212 }
213 super.onFocusChange(view, hasFocus);
214 }
215
216 @Override
Michael Kolb305b1c52011-06-21 16:16:22 -0700217 public void onStateChanged(int state) {
Michael Kolbe721cc32012-06-26 15:26:59 -0700218 mVoiceButton.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700219 switch(state) {
220 case StateListener.STATE_NORMAL:
221 mComboIcon.setVisibility(View.VISIBLE);
222 mStopButton.setVisibility(View.GONE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700223 mClearButton.setVisibility(View.GONE);
224 mMagnify.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700225 mTabSwitcher.setVisibility(View.VISIBLE);
226 mTitleContainer.setBackgroundDrawable(null);
Michael Kolb017ffab2011-07-11 15:26:47 -0700227 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700228 break;
229 case StateListener.STATE_HIGHLIGHTED:
230 mComboIcon.setVisibility(View.GONE);
231 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700232 mClearButton.setVisibility(View.GONE);
Michael Kolbe721cc32012-06-26 15:26:59 -0700233 if ((mUiController != null) && mUiController.supportsVoice()) {
234 mVoiceButton.setVisibility(View.VISIBLE);
235 }
Michael Kolb94ec5272011-08-31 16:23:57 -0700236 mMagnify.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700237 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700238 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700239 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
240 break;
241 case StateListener.STATE_EDITED:
242 mComboIcon.setVisibility(View.GONE);
243 mStopButton.setVisibility(View.GONE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700244 mClearButton.setVisibility(View.VISIBLE);
245 mMagnify.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700246 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700247 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700248 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
249 break;
Michael Kolb2814a362011-05-19 15:49:41 -0700250 }
251 }
John Reck419f6b42011-08-16 16:10:51 -0700252
253 @Override
254 public void onTabDataChanged(Tab tab) {
255 super.onTabDataChanged(tab);
256 mIncognitoIcon.setVisibility(tab.isPrivateBrowsingEnabled()
257 ? View.VISIBLE : View.GONE);
258 }
259
John Reck42229bc2011-08-19 13:26:43 -0700260 @Override
261 public boolean onMenuItemClick(MenuItem item) {
262 return mUiController.onOptionsItemSelected(item);
263 }
264
Michael Kolb11d19782011-03-20 10:17:40 -0700265}