blob: e3059ea955e08cf38870e26855cbf553b847d113 [file] [log] [blame]
Leon Scroggins571b3762010-05-26 10:25:01 -04001/*
John Reck0f602f32011-07-07 15:38:43 -07002 * Copyright (C) 2011 The Android Open Source Project
Leon Scroggins571b3762010-05-26 10:25:01 -04003 *
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 */
Leon Scroggins571b3762010-05-26 10:25:01 -040016package com.android.browser;
17
Michael Kolbde463762011-07-14 15:25:45 -070018import android.animation.Animator;
19import android.animation.AnimatorListenerAdapter;
20import android.animation.AnimatorSet;
21import android.animation.ObjectAnimator;
Leon Scroggins571b3762010-05-26 10:25:01 -040022import android.content.Context;
Michael Kolbde463762011-07-14 15:25:45 -070023import android.content.res.Configuration;
Leon Scroggins571b3762010-05-26 10:25:01 -040024import android.content.res.Resources;
Leon Scroggins571b3762010-05-26 10:25:01 -040025import android.graphics.drawable.Drawable;
Michael Kolb1ce78132010-09-23 15:50:53 -070026import android.text.TextUtils;
John Reck0f602f32011-07-07 15:38:43 -070027import android.util.AttributeSet;
Leon Scroggins571b3762010-05-26 10:25:01 -040028import android.view.View;
Michael Kolb5a72f182011-01-13 20:35:06 -080029import android.widget.ImageButton;
Leon Scroggins571b3762010-05-26 10:25:01 -040030import android.widget.ImageView;
Leon Scroggins571b3762010-05-26 10:25:01 -040031
Michael Kolbcfa3af52010-12-14 10:36:11 -080032import java.util.List;
33
John Reck0f602f32011-07-07 15:38:43 -070034public class NavigationBarTablet extends NavigationBarBase {
Michael Kolb8233fac2010-10-26 16:08:53 -070035
Michael Kolba2b2ba82010-08-04 17:54:03 -070036 private Drawable mStopDrawable;
37 private Drawable mReloadDrawable;
Michael Kolb30adae62011-07-29 13:37:05 -070038 private String mStopDescription;
39 private String mRefreshDescription;
Michael Kolbc7485ae2010-09-03 10:10:58 -070040
Michael Kolb11d19782011-03-20 10:17:40 -070041 private View mUrlContainer;
Michael Kolb5a72f182011-01-13 20:35:06 -080042 private ImageButton mBackButton;
43 private ImageButton mForwardButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080044 private ImageView mStar;
Michael Kolbe3524d82011-03-02 14:53:15 -080045 private ImageView mUrlIcon;
46 private ImageView mSearchButton;
Michael Kolb513286f2010-09-09 12:55:12 -070047 private View mGoButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070048 private ImageView mStopButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070049 private View mAllButton;
Michael Kolbb7b115e2010-09-25 16:59:37 -070050 private View mClearButton;
Michael Kolbe3524d82011-03-02 14:53:15 -080051 private ImageView mVoiceSearch;
Michael Kolbde463762011-07-14 15:25:45 -070052 private View mNavButtons;
Michael Kolbcfa3af52010-12-14 10:36:11 -080053 private Drawable mFocusDrawable;
54 private Drawable mUnfocusDrawable;
Michael Kolbde463762011-07-14 15:25:45 -070055 private boolean mHideNavButtons;
Michael Kolb81b6f832010-12-12 12:44:27 -080056
John Reck0f602f32011-07-07 15:38:43 -070057 public NavigationBarTablet(Context context) {
58 super(context);
59 init(context);
60 }
61
62 public NavigationBarTablet(Context context, AttributeSet attrs) {
63 super(context, attrs);
64 init(context);
65 }
66
67 public NavigationBarTablet(Context context, AttributeSet attrs, int defStyle) {
68 super(context, attrs, defStyle);
69 init(context);
70 }
71
72 private void init(Context context) {
73 Resources resources = context.getResources();
Michael Kolb5a72f182011-01-13 20:35:06 -080074 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_holo_dark);
75 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb30adae62011-07-29 13:37:05 -070076 mStopDescription = resources.getString(R.string.accessibility_button_stop);
77 mRefreshDescription = resources.getString(R.string.accessibility_button_refresh);
Michael Kolbcfa3af52010-12-14 10:36:11 -080078 mFocusDrawable = resources.getDrawable(
79 R.drawable.textfield_active_holo_dark);
80 mUnfocusDrawable = resources.getDrawable(
81 R.drawable.textfield_default_holo_dark);
Michael Kolbde463762011-07-14 15:25:45 -070082 mHideNavButtons = resources.getBoolean(R.bool.hide_nav_buttons);
Michael Kolbfe251992010-07-08 15:41:55 -070083 }
Leon Scroggins571b3762010-05-26 10:25:01 -040084
Michael Kolb7cdc4902011-02-03 17:54:40 -080085 @Override
John Reck0f602f32011-07-07 15:38:43 -070086 protected void onFinishInflate() {
87 super.onFinishInflate();
Michael Kolbfe251992010-07-08 15:41:55 -070088 mAllButton = findViewById(R.id.all_btn);
89 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040090 // back/forward. Probably should be done inside onPageStarted.
Michael Kolbde463762011-07-14 15:25:45 -070091 mNavButtons = findViewById(R.id.navbuttons);
Michael Kolb5a72f182011-01-13 20:35:06 -080092 mBackButton = (ImageButton) findViewById(R.id.back);
93 mForwardButton = (ImageButton) findViewById(R.id.forward);
Michael Kolbe3524d82011-03-02 14:53:15 -080094 mUrlIcon = (ImageView) findViewById(R.id.url_icon);
Michael Kolb31d469b2010-12-09 20:49:54 -080095 mStar = (ImageView) findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -070096 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolbe3524d82011-03-02 14:53:15 -080097 mSearchButton = (ImageView) findViewById(R.id.search);
Michael Kolb513286f2010-09-09 12:55:12 -070098 mGoButton = findViewById(R.id.go);
Michael Kolbb7b115e2010-09-25 16:59:37 -070099 mClearButton = findViewById(R.id.clear);
Michael Kolbe3524d82011-03-02 14:53:15 -0800100 mVoiceSearch = (ImageView) findViewById(R.id.voicesearch);
Michael Kolb31d469b2010-12-09 20:49:54 -0800101 mUrlContainer = findViewById(R.id.urlbar_focused);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700102 mBackButton.setOnClickListener(this);
103 mForwardButton.setOnClickListener(this);
104 mStar.setOnClickListener(this);
105 mAllButton.setOnClickListener(this);
106 mStopButton.setOnClickListener(this);
107 mSearchButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700108 mGoButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700109 mClearButton.setOnClickListener(this);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800110 mVoiceSearch.setOnClickListener(this);
John Reck46500332011-06-07 14:36:10 -0700111 setUaSwitcher(mUrlIcon);
Michael Kolb31d469b2010-12-09 20:49:54 -0800112 mUrlInput.setContainer(mUrlContainer);
John Reck0f602f32011-07-07 15:38:43 -0700113 }
114
Michael Kolbde463762011-07-14 15:25:45 -0700115 public void onConfigurationChanged(Configuration config) {
116 super.onConfigurationChanged(config);
117 Resources res = mContext.getResources();
118 mHideNavButtons = res.getBoolean(R.bool.hide_nav_buttons);
119 if (mUrlInput.hasFocus()) {
120 if (mHideNavButtons && (mNavButtons.getVisibility() == View.VISIBLE)) {
121 int aw = mNavButtons.getMeasuredWidth();
122 mNavButtons.setVisibility(View.GONE);
123 mNavButtons.setAlpha(0f);
124 mNavButtons.setTranslationX(-aw);
125 } else if (!mHideNavButtons && (mNavButtons.getVisibility() == View.GONE)) {
126 mNavButtons.setVisibility(View.VISIBLE);
127 mNavButtons.setAlpha(1f);
128 mNavButtons.setTranslationX(0);
129 }
130 }
131 }
132
John Reck0f602f32011-07-07 15:38:43 -0700133 @Override
134 public void setTitleBar(TitleBar titleBar) {
135 super.setTitleBar(titleBar);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800136 setFocusState(false);
Michael Kolb31d469b2010-12-09 20:49:54 -0800137 }
138
Michael Kolb5a72f182011-01-13 20:35:06 -0800139 void updateNavigationState(Tab tab) {
John Reckef654f12011-07-12 16:42:08 -0700140 if (tab != null) {
141 mBackButton.setImageResource(tab.canGoBack()
Michael Kolb5a72f182011-01-13 20:35:06 -0800142 ? R.drawable.ic_back_holo_dark
143 : R.drawable.ic_back_disabled_holo_dark);
John Reckef654f12011-07-12 16:42:08 -0700144 mForwardButton.setImageResource(tab.canGoForward()
Michael Kolb5a72f182011-01-13 20:35:06 -0800145 ? R.drawable.ic_forward_holo_dark
146 : R.drawable.ic_forward_disabled_holo_dark);
147 }
John Reckb8b2af82011-05-20 15:58:33 -0700148 updateUrlIcon();
Michael Kolb5a72f182011-01-13 20:35:06 -0800149 }
150
Michael Kolb31d469b2010-12-09 20:49:54 -0800151 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500152 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800153 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500154 }
155
Michael Kolba2b2ba82010-08-04 17:54:03 -0700156 @Override
157 public void onClick(View v) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800158 if (mBackButton == v) {
John Reckef654f12011-07-12 16:42:08 -0700159 mUiController.getCurrentTab().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700160 } else if (mForwardButton == v) {
John Reckef654f12011-07-12 16:42:08 -0700161 mUiController.getCurrentTab().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700162 } else if (mStar == v) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700163 getContext().startActivity(mUiController.createBookmarkCurrentPageIntent(true));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700164 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700165 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700166 } else if (mSearchButton == v) {
John Reck0f602f32011-07-07 15:38:43 -0700167 mBaseUi.editUrl(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700168 } else if (mStopButton == v) {
169 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700170 } else if (mGoButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800171 if (!TextUtils.isEmpty(mUrlInput.getText())) {
172 onAction(mUrlInput.getText().toString(), null,
Michael Kolb257cc2c2010-12-09 09:45:52 -0800173 UrlInputView.TYPED);
Michael Kolb1ce78132010-09-23 15:50:53 -0700174 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700175 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800176 clearOrClose();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800177 } else if (mVoiceSearch == v) {
178 mUiController.startVoiceSearch();
Michael Kolb11d19782011-03-20 10:17:40 -0700179 } else {
180 super.onClick(v);
Michael Kolbfe251992010-07-08 15:41:55 -0700181 }
182 }
183
Michael Kolb31d469b2010-12-09 20:49:54 -0800184 private void clearOrClose() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000185 if (TextUtils.isEmpty(mUrlInput.getUserText())) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800186 // close
Michael Kolb7cdc4902011-02-03 17:54:40 -0800187 mUrlInput.clearFocus();
Michael Kolb31d469b2010-12-09 20:49:54 -0800188 } else {
189 // clear
190 mUrlInput.setText("");
191 }
192 }
193
John Reckb8b2af82011-05-20 15:58:33 -0700194 void updateUrlIcon() {
John Reck46500332011-06-07 14:36:10 -0700195 mUrlIcon.setImageResource(mInVoiceMode ?
196 R.drawable.ic_search_holo_dark
197 : R.drawable.ic_web_holo_dark);
John Reckb8b2af82011-05-20 15:58:33 -0700198 }
199
Michael Kolb11d19782011-03-20 10:17:40 -0700200 @Override
201 protected void setFocusState(boolean focus) {
202 super.setFocusState(focus);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800203 if (focus) {
Michael Kolbde463762011-07-14 15:25:45 -0700204 if (mHideNavButtons) {
205 hideNavButtons();
206 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700207 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800208 mStar.setVisibility(View.GONE);
209 mClearButton.setVisibility(View.VISIBLE);
Michael Kolbe3524d82011-03-02 14:53:15 -0800210 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
Michael Kolb60a68f52011-02-24 11:02:52 -0800211 updateSearchMode(false);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700212 } else {
Michael Kolbde463762011-07-14 15:25:45 -0700213 if (mHideNavButtons) {
214 showNavButtons();
215 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700216 mGoButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800217 mVoiceSearch.setVisibility(View.GONE);
218 mStar.setVisibility(View.VISIBLE);
219 mClearButton.setVisibility(View.GONE);
John Reck0f602f32011-07-07 15:38:43 -0700220 if (mTitleBar.useQuickControls()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800221 mSearchButton.setVisibility(View.GONE);
222 } else {
223 mSearchButton.setVisibility(View.VISIBLE);
224 }
John Reckb8b2af82011-05-20 15:58:33 -0700225 updateUrlIcon();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700226 }
John Reck7e5b8b52011-06-22 13:46:25 -0700227 mUrlContainer.setBackgroundDrawable(focus
228 ? mFocusDrawable : mUnfocusDrawable);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700229 }
230
Michael Kolba2b2ba82010-08-04 17:54:03 -0700231 private void stopOrRefresh() {
John Reck0f602f32011-07-07 15:38:43 -0700232 if (mTitleBar.isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700233 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700234 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700235 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700236 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400237 }
238
Michael Kolbfe251992010-07-08 15:41:55 -0700239 @Override
John Reck0f602f32011-07-07 15:38:43 -0700240 public void onProgressStarted() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700241 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700242 mStopButton.setContentDescription(mStopDescription);
Michael Kolb46f987e2011-04-05 10:39:10 -0700243 }
244
245 @Override
John Reck0f602f32011-07-07 15:38:43 -0700246 public void onProgressStopped() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700247 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700248 mStopButton.setContentDescription(mRefreshDescription);
Michael Kolbfe251992010-07-08 15:41:55 -0700249 }
250
Michael Kolb11d19782011-03-20 10:17:40 -0700251 protected void updateSearchMode(boolean userEdited) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000252 setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText()));
Michael Kolb31d469b2010-12-09 20:49:54 -0800253 }
254
Michael Kolb11d19782011-03-20 10:17:40 -0700255 @Override
256 protected void setSearchMode(boolean voiceSearchEnabled) {
Michael Kolb793e05e2011-01-11 15:17:31 -0800257 boolean showvoicebutton = voiceSearchEnabled &&
Michael Kolb736990c2011-03-20 10:01:20 -0700258 mUiController.supportsVoiceSearch();
Michael Kolb793e05e2011-01-11 15:17:31 -0800259 mVoiceSearch.setVisibility(showvoicebutton ? View.VISIBLE :
Michael Kolb31d469b2010-12-09 20:49:54 -0800260 View.GONE);
261 mGoButton.setVisibility(voiceSearchEnabled ? View.GONE :
262 View.VISIBLE);
263 }
264
Michael Kolbfe251992010-07-08 15:41:55 -0700265 @Override
Michael Kolbcfa3af52010-12-14 10:36:11 -0800266 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
Michael Kolb11d19782011-03-20 10:17:40 -0700267 super.setInVoiceMode(voicemode, voiceResults);
Michael Kolb3aaef252011-03-09 18:13:56 -0800268 if (voicemode) {
269 mUrlIcon.setImageDrawable(mSearchButton.getDrawable());
270 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800271 }
272
Michael Kolbde463762011-07-14 15:25:45 -0700273 private void hideNavButtons() {
274 int awidth = mNavButtons.getMeasuredWidth();
275 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, 0, - awidth);
276 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", mUrlContainer.getLeft(),
277 mUrlContainer.getPaddingLeft());
278 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 1f, 0f);
279 AnimatorSet combo = new AnimatorSet();
280 combo.playTogether(anim1, anim2, anim3);
281 combo.addListener(new AnimatorListenerAdapter() {
282 @Override
283 public void onAnimationEnd(Animator animation) {
284 mNavButtons.setVisibility(View.GONE);
285 }
286 });
287 combo.setDuration(150);
288 combo.start();
289 }
290
291 private void showNavButtons() {
292 int awidth = mNavButtons.getMeasuredWidth();
293 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, -awidth, 0);
294 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", 0, awidth);
295 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 0f, 1f);
296 AnimatorSet combo = new AnimatorSet();
297 combo.playTogether(anim1, anim2, anim3);
298 mNavButtons.setVisibility(View.VISIBLE);
299 combo.setDuration(150);
300 combo.start();
301 }
302
Leon Scroggins571b3762010-05-26 10:25:01 -0400303}