blob: ff7dc7c13a9ce820a7362de369677928b99a0a4d [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
Michael Kolb11d19782011-03-20 10:17:40 -070018import android.content.Context;
Michael Kolb2814a362011-05-19 15:49:41 -070019import android.content.res.Resources;
20import android.graphics.drawable.Drawable;
John Reck0f602f32011-07-07 15:38:43 -070021import android.util.AttributeSet;
Michael Kolb017ffab2011-07-11 15:26:47 -070022import android.view.Menu;
Michael Kolb017ffab2011-07-11 15:26:47 -070023import android.view.MenuItem;
Michael Kolb11d19782011-03-20 10:17:40 -070024import android.view.View;
Michael Kolb017ffab2011-07-11 15:26:47 -070025import android.view.ViewConfiguration;
Michael Kolb2814a362011-05-19 15:49:41 -070026import android.webkit.WebView;
Michael Kolb11d19782011-03-20 10:17:40 -070027import android.widget.ImageView;
Michael Kolb017ffab2011-07-11 15:26:47 -070028import android.widget.PopupMenu;
29import android.widget.PopupMenu.OnDismissListener;
Michael Kolb11d19782011-03-20 10:17:40 -070030
Michael Kolb305b1c52011-06-21 16:16:22 -070031import com.android.browser.UrlInputView.StateListener;
Michael Kolb305b1c52011-06-21 16:16:22 -070032
John Reck0f602f32011-07-07 15:38:43 -070033public class NavigationBarPhone extends NavigationBarBase implements
John Reck58891902011-08-11 17:48:53 -070034 StateListener {
Michael Kolb11d19782011-03-20 10:17:40 -070035
Michael Kolbc16c5952011-03-29 15:37:03 -070036 private ImageView mStopButton;
Michael Kolb11d19782011-03-20 10:17:40 -070037 private ImageView mVoiceButton;
Michael Kolb2814a362011-05-19 15:49:41 -070038 private Drawable mStopDrawable;
39 private Drawable mRefreshDrawable;
Michael Kolb30adae62011-07-29 13:37:05 -070040 private String mStopDescription;
41 private String mRefreshDescription;
John Reck8ac42902011-06-29 16:14:34 -070042 private View mTabSwitcher;
Michael Kolb305b1c52011-06-21 16:16:22 -070043 private View mComboIcon;
44 private View mTitleContainer;
Michael Kolb017ffab2011-07-11 15:26:47 -070045 private View mMore;
Michael Kolb305b1c52011-06-21 16:16:22 -070046 private Drawable mTextfieldBgDrawable;
Michael Kolb8441d4b2011-07-18 14:50:21 -070047 private PopupMenu mPopupMenu;
John Reck58891902011-08-11 17:48:53 -070048 private boolean mOverflowMenuShowing;
Michael Kolb017ffab2011-07-11 15:26:47 -070049 private boolean mNeedsMenu;
John Reck419f6b42011-08-16 16:10:51 -070050 private View mIncognitoIcon;
Michael Kolb11d19782011-03-20 10:17:40 -070051
John Reck0f602f32011-07-07 15:38:43 -070052 public NavigationBarPhone(Context context) {
53 super(context);
54 }
55
56 public NavigationBarPhone(Context context, AttributeSet attrs) {
57 super(context, attrs);
58 }
59
60 public NavigationBarPhone(Context context, AttributeSet attrs, int defStyle) {
61 super(context, attrs, defStyle);
Michael Kolb11d19782011-03-20 10:17:40 -070062 }
63
64 @Override
John Reck0f602f32011-07-07 15:38:43 -070065 protected void onFinishInflate() {
66 super.onFinishInflate();
Michael Kolbc16c5952011-03-29 15:37:03 -070067 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolb11d19782011-03-20 10:17:40 -070068 mStopButton.setOnClickListener(this);
69 mVoiceButton = (ImageView) findViewById(R.id.voice);
70 mVoiceButton.setOnClickListener(this);
John Reck8ac42902011-06-29 16:14:34 -070071 mTabSwitcher = findViewById(R.id.tab_switcher);
72 mTabSwitcher.setOnClickListener(this);
Michael Kolb017ffab2011-07-11 15:26:47 -070073 mMore = findViewById(R.id.more);
74 mMore.setOnClickListener(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070075 mComboIcon = findViewById(R.id.iconcombo);
76 mTitleContainer = findViewById(R.id.title_bg);
Michael Kolb11d19782011-03-20 10:17:40 -070077 setFocusState(false);
John Reck0f602f32011-07-07 15:38:43 -070078 Resources res = getContext().getResources();
Michael Kolb2814a362011-05-19 15:49:41 -070079 mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
80 mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb30adae62011-07-29 13:37:05 -070081 mStopDescription = res.getString(R.string.accessibility_button_stop);
82 mRefreshDescription = res.getString(R.string.accessibility_button_refresh);
Michael Kolb305b1c52011-06-21 16:16:22 -070083 mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark);
84 setUaSwitcher(mComboIcon);
John Reck67f33632011-06-17 16:23:42 -070085 mUrlInput.setContainer(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070086 mUrlInput.setStateListener(this);
John Reck0f602f32011-07-07 15:38:43 -070087 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
John Reck419f6b42011-08-16 16:10:51 -070088 mIncognitoIcon = findViewById(R.id.incognito_icon);
Michael Kolb11d19782011-03-20 10:17:40 -070089 }
90
91 @Override
Michael Kolb11d19782011-03-20 10:17:40 -070092 protected void setSearchMode(boolean voiceSearchEnabled) {
93 boolean showvoicebutton = voiceSearchEnabled &&
94 mUiController.supportsVoiceSearch();
95 mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE :
96 View.GONE);
97 }
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 {
John Reck434e9f82011-08-10 18:16:52 -0700135 mUrlInput.setText(UrlUtils.stripUrl(title), false);
John Reck67f33632011-06-17 16:23:42 -0700136 }
137 mUrlInput.setSelection(0);
Michael Kolb11d19782011-03-20 10:17:40 -0700138 }
139 }
140
141 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700142 public void onClick(View v) {
143 if (v == mStopButton) {
John Reck0f602f32011-07-07 15:38:43 -0700144 if (mTitleBar.isInLoad()) {
Michael Kolb2814a362011-05-19 15:49:41 -0700145 mUiController.stopLoading();
146 } else {
147 WebView web = mBaseUi.getWebView();
148 if (web != null) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700149 stopEditingUrl();
Michael Kolb2814a362011-05-19 15:49:41 -0700150 web.reload();
151 }
152 }
Michael Kolb11d19782011-03-20 10:17:40 -0700153 } else if (v == mVoiceButton) {
154 mUiController.startVoiceSearch();
John Reck8ac42902011-06-29 16:14:34 -0700155 } else if (v == mTabSwitcher) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700156 ((PhoneUi) mBaseUi).toggleNavScreen();
Michael Kolb017ffab2011-07-11 15:26:47 -0700157 } else if (mMore == v) {
John Reckef654f12011-07-12 16:42:08 -0700158 showMenu(mMore);
Michael Kolb11d19782011-03-20 10:17:40 -0700159 } else {
160 super.onClick(v);
161 }
162 }
163
John Reck58891902011-08-11 17:48:53 -0700164 @Override
Michael Kolb017ffab2011-07-11 15:26:47 -0700165 public boolean isMenuShowing() {
John Reck58891902011-08-11 17:48:53 -0700166 return super.isMenuShowing() || mOverflowMenuShowing;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700167 }
168
John Reckef654f12011-07-12 16:42:08 -0700169 void showMenu(View anchor) {
John Reck58891902011-08-11 17:48:53 -0700170 mOverflowMenuShowing = true;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700171 mPopupMenu = new PopupMenu(mContext, anchor);
172 Menu menu = mPopupMenu.getMenu();
173 mPopupMenu.getMenuInflater().inflate(R.menu.browser, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -0700174 mUiController.updateMenuState(mBaseUi.getActiveTab(), menu);
Michael Kolb8441d4b2011-07-18 14:50:21 -0700175 mPopupMenu.setOnMenuItemClickListener(this);
176 mPopupMenu.setOnDismissListener(this);
177 mPopupMenu.show();
178 }
179
Michael Kolb017ffab2011-07-11 15:26:47 -0700180 @Override
181 public void onDismiss(PopupMenu menu) {
John Reck58891902011-08-11 17:48:53 -0700182 if (menu == mPopupMenu) {
183 onMenuHidden();
184 }
185 super.onDismiss(menu);
Michael Kolb017ffab2011-07-11 15:26:47 -0700186 }
187
188 @Override
189 public boolean onMenuItemClick(MenuItem item) {
190 onMenuHidden();
191 boolean res = mUiController.onOptionsItemSelected(item);
192 if (!res) {
193 return super.onMenuItemClick(item);
194 }
195 return res;
196 }
197
198 private void onMenuHidden() {
John Reck58891902011-08-11 17:48:53 -0700199 mOverflowMenuShowing = false;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700200 mPopupMenu = null;
Michael Kolb017ffab2011-07-11 15:26:47 -0700201 mBaseUi.showTitleBarForDuration();
202 }
203
Michael Kolb305b1c52011-06-21 16:16:22 -0700204 @Override
John Reck434e9f82011-08-10 18:16:52 -0700205 public void onFocusChange(View view, boolean hasFocus) {
206 if (view == mUrlInput) {
207 if (hasFocus) {
208 mUrlInput.setText((String) mUrlInput.getTag(), false);
209 } 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) {
218 switch(state) {
219 case StateListener.STATE_NORMAL:
220 mComboIcon.setVisibility(View.VISIBLE);
221 mStopButton.setVisibility(View.GONE);
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700222 setSearchMode(mInVoiceMode);
Michael Kolb305b1c52011-06-21 16:16:22 -0700223 mTabSwitcher.setVisibility(View.VISIBLE);
224 mTitleContainer.setBackgroundDrawable(null);
Michael Kolb017ffab2011-07-11 15:26:47 -0700225 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700226 break;
227 case StateListener.STATE_HIGHLIGHTED:
228 mComboIcon.setVisibility(View.GONE);
229 mStopButton.setVisibility(View.VISIBLE);
230 setSearchMode(true);
231 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700232 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700233 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
234 break;
235 case StateListener.STATE_EDITED:
236 mComboIcon.setVisibility(View.GONE);
237 mStopButton.setVisibility(View.GONE);
238 setSearchMode(false);
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;
Michael Kolb2814a362011-05-19 15:49:41 -0700243 }
244 }
John Reck419f6b42011-08-16 16:10:51 -0700245
246 @Override
247 public void onTabDataChanged(Tab tab) {
248 super.onTabDataChanged(tab);
249 mIncognitoIcon.setVisibility(tab.isPrivateBrowsingEnabled()
250 ? View.VISIBLE : View.GONE);
251 }
252
Michael Kolb11d19782011-03-20 10:17:40 -0700253}