blob: fee9604ff731e41b1499e33676e8116141f09abd [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;
John Reck42229bc2011-08-19 13:26:43 -070030import android.widget.PopupMenu.OnMenuItemClickListener;
Michael Kolb11d19782011-03-20 10:17:40 -070031
Michael Kolb305b1c52011-06-21 16:16:22 -070032import com.android.browser.UrlInputView.StateListener;
Michael Kolb305b1c52011-06-21 16:16:22 -070033
John Reck0f602f32011-07-07 15:38:43 -070034public class NavigationBarPhone extends NavigationBarBase implements
John Reck42229bc2011-08-19 13:26:43 -070035 StateListener, OnMenuItemClickListener, OnDismissListener {
Michael Kolb11d19782011-03-20 10:17:40 -070036
Michael Kolbc16c5952011-03-29 15:37:03 -070037 private ImageView mStopButton;
Michael Kolb11d19782011-03-20 10:17:40 -070038 private ImageView mVoiceButton;
Michael Kolb2814a362011-05-19 15:49:41 -070039 private Drawable mStopDrawable;
40 private Drawable mRefreshDrawable;
Michael Kolb30adae62011-07-29 13:37:05 -070041 private String mStopDescription;
42 private String mRefreshDescription;
John Reck8ac42902011-06-29 16:14:34 -070043 private View mTabSwitcher;
Michael Kolb305b1c52011-06-21 16:16:22 -070044 private View mComboIcon;
45 private View mTitleContainer;
Michael Kolb017ffab2011-07-11 15:26:47 -070046 private View mMore;
Michael Kolb305b1c52011-06-21 16:16:22 -070047 private Drawable mTextfieldBgDrawable;
Michael Kolb8441d4b2011-07-18 14:50:21 -070048 private PopupMenu mPopupMenu;
John Reck58891902011-08-11 17:48:53 -070049 private boolean mOverflowMenuShowing;
Michael Kolb017ffab2011-07-11 15:26:47 -070050 private boolean mNeedsMenu;
John Reck419f6b42011-08-16 16:10:51 -070051 private View mIncognitoIcon;
Michael Kolb11d19782011-03-20 10:17:40 -070052
John Reck0f602f32011-07-07 15:38:43 -070053 public NavigationBarPhone(Context context) {
54 super(context);
55 }
56
57 public NavigationBarPhone(Context context, AttributeSet attrs) {
58 super(context, attrs);
59 }
60
61 public NavigationBarPhone(Context context, AttributeSet attrs, int defStyle) {
62 super(context, attrs, defStyle);
Michael Kolb11d19782011-03-20 10:17:40 -070063 }
64
65 @Override
John Reck0f602f32011-07-07 15:38:43 -070066 protected void onFinishInflate() {
67 super.onFinishInflate();
Michael Kolbc16c5952011-03-29 15:37:03 -070068 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolb11d19782011-03-20 10:17:40 -070069 mStopButton.setOnClickListener(this);
70 mVoiceButton = (ImageView) findViewById(R.id.voice);
71 mVoiceButton.setOnClickListener(this);
John Reck8ac42902011-06-29 16:14:34 -070072 mTabSwitcher = findViewById(R.id.tab_switcher);
73 mTabSwitcher.setOnClickListener(this);
Michael Kolb017ffab2011-07-11 15:26:47 -070074 mMore = findViewById(R.id.more);
75 mMore.setOnClickListener(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070076 mComboIcon = findViewById(R.id.iconcombo);
77 mTitleContainer = findViewById(R.id.title_bg);
Michael Kolb11d19782011-03-20 10:17:40 -070078 setFocusState(false);
John Reck0f602f32011-07-07 15:38:43 -070079 Resources res = getContext().getResources();
Michael Kolb2814a362011-05-19 15:49:41 -070080 mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
81 mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb30adae62011-07-29 13:37:05 -070082 mStopDescription = res.getString(R.string.accessibility_button_stop);
83 mRefreshDescription = res.getString(R.string.accessibility_button_refresh);
Michael Kolb305b1c52011-06-21 16:16:22 -070084 mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark);
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 }
Michael Kolb017ffab2011-07-11 15:26:47 -0700185 }
186
Michael Kolb017ffab2011-07-11 15:26:47 -0700187 private void onMenuHidden() {
John Reck58891902011-08-11 17:48:53 -0700188 mOverflowMenuShowing = false;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700189 mPopupMenu = null;
Michael Kolb017ffab2011-07-11 15:26:47 -0700190 mBaseUi.showTitleBarForDuration();
191 }
192
Michael Kolb305b1c52011-06-21 16:16:22 -0700193 @Override
John Reck434e9f82011-08-10 18:16:52 -0700194 public void onFocusChange(View view, boolean hasFocus) {
195 if (view == mUrlInput) {
Michael Kolb2fc7c162011-08-31 13:38:24 -0700196 if (hasFocus && !mUrlInput.getText().toString().equals(mUrlInput.getTag())) {
197 // only change text if different
John Reck434e9f82011-08-10 18:16:52 -0700198 mUrlInput.setText((String) mUrlInput.getTag(), false);
199 } else {
200 setDisplayTitle(mUrlInput.getText().toString());
201 }
202 }
203 super.onFocusChange(view, hasFocus);
204 }
205
206 @Override
Michael Kolb305b1c52011-06-21 16:16:22 -0700207 public void onStateChanged(int state) {
208 switch(state) {
209 case StateListener.STATE_NORMAL:
210 mComboIcon.setVisibility(View.VISIBLE);
211 mStopButton.setVisibility(View.GONE);
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700212 setSearchMode(mInVoiceMode);
Michael Kolb305b1c52011-06-21 16:16:22 -0700213 mTabSwitcher.setVisibility(View.VISIBLE);
214 mTitleContainer.setBackgroundDrawable(null);
Michael Kolb017ffab2011-07-11 15:26:47 -0700215 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700216 break;
217 case StateListener.STATE_HIGHLIGHTED:
218 mComboIcon.setVisibility(View.GONE);
219 mStopButton.setVisibility(View.VISIBLE);
220 setSearchMode(true);
221 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700222 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700223 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
224 break;
225 case StateListener.STATE_EDITED:
226 mComboIcon.setVisibility(View.GONE);
227 mStopButton.setVisibility(View.GONE);
228 setSearchMode(false);
229 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700230 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700231 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
232 break;
Michael Kolb2814a362011-05-19 15:49:41 -0700233 }
234 }
John Reck419f6b42011-08-16 16:10:51 -0700235
236 @Override
237 public void onTabDataChanged(Tab tab) {
238 super.onTabDataChanged(tab);
239 mIncognitoIcon.setVisibility(tab.isPrivateBrowsingEnabled()
240 ? View.VISIBLE : View.GONE);
241 }
242
John Reck42229bc2011-08-19 13:26:43 -0700243 @Override
244 public boolean onMenuItemClick(MenuItem item) {
245 return mUiController.onOptionsItemSelected(item);
246 }
247
Michael Kolb11d19782011-03-20 10:17:40 -0700248}