blob: 2517d624fa1bd817788899a3d59f12988d496b9c [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 Kolb2814a362011-05-19 15:49:41 -070041 private Drawable mStopDrawable;
42 private Drawable mRefreshDrawable;
Michael Kolb30adae62011-07-29 13:37:05 -070043 private String mStopDescription;
44 private String mRefreshDescription;
John Reck8ac42902011-06-29 16:14:34 -070045 private View mTabSwitcher;
Michael Kolb305b1c52011-06-21 16:16:22 -070046 private View mComboIcon;
47 private View mTitleContainer;
Michael Kolb017ffab2011-07-11 15:26:47 -070048 private View mMore;
Michael Kolb305b1c52011-06-21 16:16:22 -070049 private Drawable mTextfieldBgDrawable;
Michael Kolb8441d4b2011-07-18 14:50:21 -070050 private PopupMenu mPopupMenu;
John Reck58891902011-08-11 17:48:53 -070051 private boolean mOverflowMenuShowing;
Michael Kolb017ffab2011-07-11 15:26:47 -070052 private boolean mNeedsMenu;
John Reck419f6b42011-08-16 16:10:51 -070053 private View mIncognitoIcon;
Michael Kolb11d19782011-03-20 10:17:40 -070054
John Reck0f602f32011-07-07 15:38:43 -070055 public NavigationBarPhone(Context context) {
56 super(context);
57 }
58
59 public NavigationBarPhone(Context context, AttributeSet attrs) {
60 super(context, attrs);
61 }
62
63 public NavigationBarPhone(Context context, AttributeSet attrs, int defStyle) {
64 super(context, attrs, defStyle);
Michael Kolb11d19782011-03-20 10:17:40 -070065 }
66
67 @Override
John Reck0f602f32011-07-07 15:38:43 -070068 protected void onFinishInflate() {
69 super.onFinishInflate();
Michael Kolbc16c5952011-03-29 15:37:03 -070070 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolb11d19782011-03-20 10:17:40 -070071 mStopButton.setOnClickListener(this);
Michael Kolb94ec5272011-08-31 16:23:57 -070072 mClearButton = (ImageView) findViewById(R.id.clear);
73 mClearButton.setOnClickListener(this);
74 mMagnify = (ImageView) findViewById(R.id.magnify);
John Reck8ac42902011-06-29 16:14:34 -070075 mTabSwitcher = findViewById(R.id.tab_switcher);
76 mTabSwitcher.setOnClickListener(this);
Michael Kolb017ffab2011-07-11 15:26:47 -070077 mMore = findViewById(R.id.more);
78 mMore.setOnClickListener(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070079 mComboIcon = findViewById(R.id.iconcombo);
Michael Kolb315d5022011-10-13 12:47:11 -070080 mComboIcon.setOnClickListener(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070081 mTitleContainer = findViewById(R.id.title_bg);
Michael Kolb11d19782011-03-20 10:17:40 -070082 setFocusState(false);
John Reck0f602f32011-07-07 15:38:43 -070083 Resources res = getContext().getResources();
Michael Kolb2814a362011-05-19 15:49:41 -070084 mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
85 mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb30adae62011-07-29 13:37:05 -070086 mStopDescription = res.getString(R.string.accessibility_button_stop);
87 mRefreshDescription = res.getString(R.string.accessibility_button_refresh);
Michael Kolb305b1c52011-06-21 16:16:22 -070088 mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark);
John Reck67f33632011-06-17 16:23:42 -070089 mUrlInput.setContainer(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070090 mUrlInput.setStateListener(this);
John Reck0f602f32011-07-07 15:38:43 -070091 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
John Reck419f6b42011-08-16 16:10:51 -070092 mIncognitoIcon = findViewById(R.id.incognito_icon);
Michael Kolb11d19782011-03-20 10:17:40 -070093 }
94
95 @Override
John Reck0f602f32011-07-07 15:38:43 -070096 public void onProgressStarted() {
97 super.onProgressStarted();
98 if (mStopButton.getDrawable() != mStopDrawable) {
99 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700100 mStopButton.setContentDescription(mStopDescription);
John Reck0f602f32011-07-07 15:38:43 -0700101 if (mStopButton.getVisibility() != View.VISIBLE) {
102 mComboIcon.setVisibility(View.GONE);
103 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700104 }
John Reck0f602f32011-07-07 15:38:43 -0700105 }
106 }
107
108 @Override
109 public void onProgressStopped() {
110 super.onProgressStopped();
John Reck0f602f32011-07-07 15:38:43 -0700111 mStopButton.setImageDrawable(mRefreshDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700112 mStopButton.setContentDescription(mRefreshDescription);
John Reck0f602f32011-07-07 15:38:43 -0700113 if (!isEditingUrl()) {
114 mComboIcon.setVisibility(View.VISIBLE);
Michael Kolb2814a362011-05-19 15:49:41 -0700115 }
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700116 onStateChanged(mUrlInput.getState());
Michael Kolb2814a362011-05-19 15:49:41 -0700117 }
118
Michael Kolb11d19782011-03-20 10:17:40 -0700119 /**
120 * Update the text displayed in the title bar.
121 * @param title String to display. If null, the new tab string will be
122 * shown.
123 */
124 @Override
125 void setDisplayTitle(String title) {
John Reck434e9f82011-08-10 18:16:52 -0700126 mUrlInput.setTag(title);
John Reck67f33632011-06-17 16:23:42 -0700127 if (!isEditingUrl()) {
128 if (title == null) {
129 mUrlInput.setText(R.string.new_tab);
130 } else {
John Reck434e9f82011-08-10 18:16:52 -0700131 mUrlInput.setText(UrlUtils.stripUrl(title), false);
John Reck67f33632011-06-17 16:23:42 -0700132 }
133 mUrlInput.setSelection(0);
Michael Kolb11d19782011-03-20 10:17:40 -0700134 }
135 }
136
137 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700138 public void onClick(View v) {
139 if (v == mStopButton) {
John Reck0f602f32011-07-07 15:38:43 -0700140 if (mTitleBar.isInLoad()) {
Michael Kolb2814a362011-05-19 15:49:41 -0700141 mUiController.stopLoading();
142 } else {
143 WebView web = mBaseUi.getWebView();
144 if (web != null) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700145 stopEditingUrl();
Michael Kolb2814a362011-05-19 15:49:41 -0700146 web.reload();
147 }
148 }
John Reck8ac42902011-06-29 16:14:34 -0700149 } else if (v == mTabSwitcher) {
Michael Kolb20be26d2011-07-18 16:38:02 -0700150 ((PhoneUi) mBaseUi).toggleNavScreen();
Michael Kolb017ffab2011-07-11 15:26:47 -0700151 } else if (mMore == v) {
John Reckef654f12011-07-12 16:42:08 -0700152 showMenu(mMore);
Michael Kolb94ec5272011-08-31 16:23:57 -0700153 } else if (mClearButton == v) {
154 mUrlInput.setText("");
Michael Kolb315d5022011-10-13 12:47:11 -0700155 } else if (mComboIcon == v) {
156 mUiController.showPageInfo();
Michael Kolb11d19782011-03-20 10:17:40 -0700157 } else {
158 super.onClick(v);
159 }
160 }
161
John Reck58891902011-08-11 17:48:53 -0700162 @Override
Michael Kolb017ffab2011-07-11 15:26:47 -0700163 public boolean isMenuShowing() {
John Reck58891902011-08-11 17:48:53 -0700164 return super.isMenuShowing() || mOverflowMenuShowing;
Michael Kolb8441d4b2011-07-18 14:50:21 -0700165 }
166
John Reckef654f12011-07-12 16:42:08 -0700167 void showMenu(View anchor) {
John Recke1a03a32011-09-14 17:04:16 -0700168 Activity activity = mUiController.getActivity();
169 if (mPopupMenu == null) {
170 mPopupMenu = new PopupMenu(mContext, anchor);
171 mPopupMenu.setOnMenuItemClickListener(this);
172 mPopupMenu.setOnDismissListener(this);
173 if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
174 mPopupMenu = null;
175 return;
176 }
177 }
Michael Kolb8441d4b2011-07-18 14:50:21 -0700178 Menu menu = mPopupMenu.getMenu();
John Recke1a03a32011-09-14 17:04:16 -0700179 if (activity.onPrepareOptionsMenu(menu)) {
180 mOverflowMenuShowing = true;
181 mPopupMenu.show();
182 }
Michael Kolb8441d4b2011-07-18 14:50:21 -0700183 }
184
Michael Kolb017ffab2011-07-11 15:26:47 -0700185 @Override
186 public void onDismiss(PopupMenu menu) {
John Reck58891902011-08-11 17:48:53 -0700187 if (menu == mPopupMenu) {
188 onMenuHidden();
189 }
Michael Kolb017ffab2011-07-11 15:26:47 -0700190 }
191
Michael Kolb017ffab2011-07-11 15:26:47 -0700192 private void onMenuHidden() {
John Reck58891902011-08-11 17:48:53 -0700193 mOverflowMenuShowing = false;
Michael Kolb017ffab2011-07-11 15:26:47 -0700194 mBaseUi.showTitleBarForDuration();
195 }
196
Michael Kolb305b1c52011-06-21 16:16:22 -0700197 @Override
John Reck434e9f82011-08-10 18:16:52 -0700198 public void onFocusChange(View view, boolean hasFocus) {
199 if (view == mUrlInput) {
Michael Kolb2fc7c162011-08-31 13:38:24 -0700200 if (hasFocus && !mUrlInput.getText().toString().equals(mUrlInput.getTag())) {
201 // only change text if different
John Reck434e9f82011-08-10 18:16:52 -0700202 mUrlInput.setText((String) mUrlInput.getTag(), false);
John Reck2edc80c2011-11-18 09:27:21 -0800203 mUrlInput.selectAll();
John Reck434e9f82011-08-10 18:16:52 -0700204 } else {
205 setDisplayTitle(mUrlInput.getText().toString());
206 }
207 }
208 super.onFocusChange(view, hasFocus);
209 }
210
211 @Override
Michael Kolb305b1c52011-06-21 16:16:22 -0700212 public void onStateChanged(int state) {
213 switch(state) {
214 case StateListener.STATE_NORMAL:
215 mComboIcon.setVisibility(View.VISIBLE);
216 mStopButton.setVisibility(View.GONE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700217 mClearButton.setVisibility(View.GONE);
218 mMagnify.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700219 mTabSwitcher.setVisibility(View.VISIBLE);
220 mTitleContainer.setBackgroundDrawable(null);
Michael Kolb017ffab2011-07-11 15:26:47 -0700221 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700222 break;
223 case StateListener.STATE_HIGHLIGHTED:
224 mComboIcon.setVisibility(View.GONE);
225 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700226 mClearButton.setVisibility(View.GONE);
227 mMagnify.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700228 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700229 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700230 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
231 break;
232 case StateListener.STATE_EDITED:
233 mComboIcon.setVisibility(View.GONE);
234 mStopButton.setVisibility(View.GONE);
Michael Kolb94ec5272011-08-31 16:23:57 -0700235 mClearButton.setVisibility(View.VISIBLE);
236 mMagnify.setVisibility(View.VISIBLE);
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;
Michael Kolb2814a362011-05-19 15:49:41 -0700241 }
242 }
John Reck419f6b42011-08-16 16:10:51 -0700243
244 @Override
245 public void onTabDataChanged(Tab tab) {
246 super.onTabDataChanged(tab);
247 mIncognitoIcon.setVisibility(tab.isPrivateBrowsingEnabled()
248 ? View.VISIBLE : View.GONE);
249 }
250
John Reck42229bc2011-08-19 13:26:43 -0700251 @Override
252 public boolean onMenuItemClick(MenuItem item) {
253 return mUiController.onOptionsItemSelected(item);
254 }
255
Michael Kolb11d19782011-03-20 10:17:40 -0700256}