blob: 1bd9cad9a7e29fb7fd4708381851cbbc3a2c070c [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);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070081 mMore = findViewById(R.id.more_browser_settings);
Michael Kolb017ffab2011-07-11 15:26:47 -070082 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()) {
Vivek Sekhar60dca802014-06-27 14:48:30 -0700132 // add for carrier requirement - show title from native instead of url
133 Tab currentTab = mUiController.getTabControl().getCurrentTab();
134 if (currentTab != null && currentTab.getTitle() != null) {
135 mUrlInput.setText(currentTab.getTitle(), false);
136 } else if (title == null) {
John Reck67f33632011-06-17 16:23:42 -0700137 mUrlInput.setText(R.string.new_tab);
138 } else {
Bijan Amirzadae75909d2014-05-06 14:18:54 -0700139 mUrlInput.setText(UrlUtils.stripUrl(title), false);
John Reck67f33632011-06-17 16:23:42 -0700140 }
141 mUrlInput.setSelection(0);
Michael Kolb11d19782011-03-20 10:17:40 -0700142 }
143 }
144
145 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700146 public void onClick(View v) {
147 if (v == mStopButton) {
John Reck0f602f32011-07-07 15:38:43 -0700148 if (mTitleBar.isInLoad()) {
Michael Kolb2814a362011-05-19 15:49:41 -0700149 mUiController.stopLoading();
150 } else {
151 WebView web = mBaseUi.getWebView();
152 if (web != null) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700153 stopEditingUrl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800154 Tab currentTab = mUiController.getTabControl().getCurrentTab();
155 if (currentTab.hasCrashed) {
156 currentTab.replaceCrashView(web, currentTab.getViewContainer());
157 }
Michael Kolb2814a362011-05-19 15:49:41 -0700158 web.reload();
159 }
160 }
John Reck8ac42902011-06-29 16:14:34 -0700161 } else if (v == mTabSwitcher) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700162 ((PhoneUi) mBaseUi).toggleNavScreen();
Michael Kolb017ffab2011-07-11 15:26:47 -0700163 } else if (mMore == v) {
John Reckef654f12011-07-12 16:42:08 -0700164 showMenu(mMore);
Michael Kolb94ec5272011-08-31 16:23:57 -0700165 } else if (mClearButton == v) {
166 mUrlInput.setText("");
Michael Kolb315d5022011-10-13 12:47:11 -0700167 } else if (mComboIcon == v) {
168 mUiController.showPageInfo();
Michael Kolb0b129122012-06-04 16:31:58 -0700169 } else if (mVoiceButton == v) {
170 mUiController.startVoiceRecognizer();
Michael Kolb11d19782011-03-20 10:17:40 -0700171 } else {
172 super.onClick(v);
173 }
174 }
175
John Reck58891902011-08-11 17:48:53 -0700176 @Override
Michael Kolb017ffab2011-07-11 15:26:47 -0700177 public boolean isMenuShowing() {
John Reck58891902011-08-11 17:48:53 -0700178 return super.isMenuShowing() || mOverflowMenuShowing;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700179 }
180
John Reckef654f12011-07-12 16:42:08 -0700181 void showMenu(View anchor) {
John Recke1a03a32011-09-14 17:04:16 -0700182 Activity activity = mUiController.getActivity();
183 if (mPopupMenu == null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800184 mPopupMenu = new PopupMenu(getContext(), anchor);
John Recke1a03a32011-09-14 17:04:16 -0700185 mPopupMenu.setOnMenuItemClickListener(this);
186 mPopupMenu.setOnDismissListener(this);
187 if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
188 mPopupMenu = null;
189 return;
190 }
191 }
Michael Kolb8441d4b2011-07-18 14:50:21 -0700192 Menu menu = mPopupMenu.getMenu();
John Recke1a03a32011-09-14 17:04:16 -0700193 if (activity.onPrepareOptionsMenu(menu)) {
194 mOverflowMenuShowing = true;
195 mPopupMenu.show();
196 }
Michael Kolb8441d4b2011-07-18 14:50:21 -0700197 }
198
Michael Kolb017ffab2011-07-11 15:26:47 -0700199 @Override
200 public void onDismiss(PopupMenu menu) {
John Reck58891902011-08-11 17:48:53 -0700201 if (menu == mPopupMenu) {
202 onMenuHidden();
203 }
Michael Kolb017ffab2011-07-11 15:26:47 -0700204 }
205
Michael Kolb017ffab2011-07-11 15:26:47 -0700206 private void onMenuHidden() {
John Reck58891902011-08-11 17:48:53 -0700207 mOverflowMenuShowing = false;
Michael Kolb017ffab2011-07-11 15:26:47 -0700208 mBaseUi.showTitleBarForDuration();
209 }
210
Michael Kolb305b1c52011-06-21 16:16:22 -0700211 @Override
John Reck434e9f82011-08-10 18:16:52 -0700212 public void onFocusChange(View view, boolean hasFocus) {
213 if (view == mUrlInput) {
Michael Kolb2fc7c162011-08-31 13:38:24 -0700214 if (hasFocus && !mUrlInput.getText().toString().equals(mUrlInput.getTag())) {
215 // only change text if different
John Reck434e9f82011-08-10 18:16:52 -0700216 mUrlInput.setText((String) mUrlInput.getTag(), false);
John Reck2edc80c2011-11-18 09:27:21 -0800217 mUrlInput.selectAll();
John Reck434e9f82011-08-10 18:16:52 -0700218 } else {
219 setDisplayTitle(mUrlInput.getText().toString());
220 }
221 }
222 super.onFocusChange(view, hasFocus);
223 }
224
225 @Override
Michael Kolb305b1c52011-06-21 16:16:22 -0700226 public void onStateChanged(int state) {
Michael Kolbe721cc32012-06-26 15:26:59 -0700227 mVoiceButton.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700228 switch(state) {
229 case StateListener.STATE_NORMAL:
230 mComboIcon.setVisibility(View.VISIBLE);
231 mStopButton.setVisibility(View.GONE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700232 mClearButton.setVisibility(View.GONE);
233 mMagnify.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700234 mTabSwitcher.setVisibility(View.VISIBLE);
235 mTitleContainer.setBackgroundDrawable(null);
Michael Kolb017ffab2011-07-11 15:26:47 -0700236 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700237 break;
238 case StateListener.STATE_HIGHLIGHTED:
239 mComboIcon.setVisibility(View.GONE);
240 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700241 mClearButton.setVisibility(View.GONE);
Michael Kolbe721cc32012-06-26 15:26:59 -0700242 if ((mUiController != null) && mUiController.supportsVoice()) {
243 mVoiceButton.setVisibility(View.VISIBLE);
244 }
Michael Kolb94ec5272011-08-31 16:23:57 -0700245 mMagnify.setVisibility(View.GONE);
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;
250 case StateListener.STATE_EDITED:
251 mComboIcon.setVisibility(View.GONE);
252 mStopButton.setVisibility(View.GONE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700253 mClearButton.setVisibility(View.VISIBLE);
254 mMagnify.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700255 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700256 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700257 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
258 break;
Michael Kolb2814a362011-05-19 15:49:41 -0700259 }
260 }
John Reck419f6b42011-08-16 16:10:51 -0700261
262 @Override
263 public void onTabDataChanged(Tab tab) {
264 super.onTabDataChanged(tab);
265 mIncognitoIcon.setVisibility(tab.isPrivateBrowsingEnabled()
266 ? View.VISIBLE : View.GONE);
267 }
268
John Reck42229bc2011-08-19 13:26:43 -0700269 @Override
270 public boolean onMenuItemClick(MenuItem item) {
271 return mUiController.onOptionsItemSelected(item);
272 }
273
Michael Kolb11d19782011-03-20 10:17:40 -0700274}