blob: 9cb8ff28644effafaa4b8022db3dfecf88a08ad7 [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 Kolbc7485ae2010-09-03 10:10:58 -070038
Michael Kolb11d19782011-03-20 10:17:40 -070039 private View mUrlContainer;
Michael Kolb5a72f182011-01-13 20:35:06 -080040 private ImageButton mBackButton;
41 private ImageButton mForwardButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080042 private ImageView mStar;
Michael Kolbe3524d82011-03-02 14:53:15 -080043 private ImageView mUrlIcon;
44 private ImageView mSearchButton;
Michael Kolb513286f2010-09-09 12:55:12 -070045 private View mGoButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070046 private ImageView mStopButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070047 private View mAllButton;
Michael Kolbb7b115e2010-09-25 16:59:37 -070048 private View mClearButton;
Michael Kolbe3524d82011-03-02 14:53:15 -080049 private ImageView mVoiceSearch;
Michael Kolbde463762011-07-14 15:25:45 -070050 private View mNavButtons;
Michael Kolbcfa3af52010-12-14 10:36:11 -080051 private Drawable mFocusDrawable;
52 private Drawable mUnfocusDrawable;
Michael Kolbde463762011-07-14 15:25:45 -070053 private boolean mHideNavButtons;
Michael Kolb81b6f832010-12-12 12:44:27 -080054
John Reck0f602f32011-07-07 15:38:43 -070055 public NavigationBarTablet(Context context) {
56 super(context);
57 init(context);
58 }
59
60 public NavigationBarTablet(Context context, AttributeSet attrs) {
61 super(context, attrs);
62 init(context);
63 }
64
65 public NavigationBarTablet(Context context, AttributeSet attrs, int defStyle) {
66 super(context, attrs, defStyle);
67 init(context);
68 }
69
70 private void init(Context context) {
71 Resources resources = context.getResources();
Michael Kolb5a72f182011-01-13 20:35:06 -080072 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_holo_dark);
73 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolbcfa3af52010-12-14 10:36:11 -080074 mFocusDrawable = resources.getDrawable(
75 R.drawable.textfield_active_holo_dark);
76 mUnfocusDrawable = resources.getDrawable(
77 R.drawable.textfield_default_holo_dark);
Michael Kolbde463762011-07-14 15:25:45 -070078 mHideNavButtons = resources.getBoolean(R.bool.hide_nav_buttons);
Michael Kolbfe251992010-07-08 15:41:55 -070079 }
Leon Scroggins571b3762010-05-26 10:25:01 -040080
Michael Kolb7cdc4902011-02-03 17:54:40 -080081 @Override
John Reck0f602f32011-07-07 15:38:43 -070082 protected void onFinishInflate() {
83 super.onFinishInflate();
Michael Kolbfe251992010-07-08 15:41:55 -070084 mAllButton = findViewById(R.id.all_btn);
85 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040086 // back/forward. Probably should be done inside onPageStarted.
Michael Kolbde463762011-07-14 15:25:45 -070087 mNavButtons = findViewById(R.id.navbuttons);
Michael Kolb5a72f182011-01-13 20:35:06 -080088 mBackButton = (ImageButton) findViewById(R.id.back);
89 mForwardButton = (ImageButton) findViewById(R.id.forward);
Michael Kolbe3524d82011-03-02 14:53:15 -080090 mUrlIcon = (ImageView) findViewById(R.id.url_icon);
Michael Kolb31d469b2010-12-09 20:49:54 -080091 mStar = (ImageView) findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -070092 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolbe3524d82011-03-02 14:53:15 -080093 mSearchButton = (ImageView) findViewById(R.id.search);
Michael Kolb513286f2010-09-09 12:55:12 -070094 mGoButton = findViewById(R.id.go);
Michael Kolbb7b115e2010-09-25 16:59:37 -070095 mClearButton = findViewById(R.id.clear);
Michael Kolbe3524d82011-03-02 14:53:15 -080096 mVoiceSearch = (ImageView) findViewById(R.id.voicesearch);
Michael Kolb31d469b2010-12-09 20:49:54 -080097 mUrlContainer = findViewById(R.id.urlbar_focused);
Michael Kolba2b2ba82010-08-04 17:54:03 -070098 mBackButton.setOnClickListener(this);
99 mForwardButton.setOnClickListener(this);
100 mStar.setOnClickListener(this);
101 mAllButton.setOnClickListener(this);
102 mStopButton.setOnClickListener(this);
103 mSearchButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700104 mGoButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700105 mClearButton.setOnClickListener(this);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800106 mVoiceSearch.setOnClickListener(this);
John Reck46500332011-06-07 14:36:10 -0700107 setUaSwitcher(mUrlIcon);
Michael Kolb31d469b2010-12-09 20:49:54 -0800108 mUrlInput.setContainer(mUrlContainer);
John Reck0f602f32011-07-07 15:38:43 -0700109 }
110
Michael Kolbde463762011-07-14 15:25:45 -0700111 public void onConfigurationChanged(Configuration config) {
112 super.onConfigurationChanged(config);
113 Resources res = mContext.getResources();
114 mHideNavButtons = res.getBoolean(R.bool.hide_nav_buttons);
115 if (mUrlInput.hasFocus()) {
116 if (mHideNavButtons && (mNavButtons.getVisibility() == View.VISIBLE)) {
117 int aw = mNavButtons.getMeasuredWidth();
118 mNavButtons.setVisibility(View.GONE);
119 mNavButtons.setAlpha(0f);
120 mNavButtons.setTranslationX(-aw);
121 } else if (!mHideNavButtons && (mNavButtons.getVisibility() == View.GONE)) {
122 mNavButtons.setVisibility(View.VISIBLE);
123 mNavButtons.setAlpha(1f);
124 mNavButtons.setTranslationX(0);
125 }
126 }
127 }
128
John Reck0f602f32011-07-07 15:38:43 -0700129 @Override
130 public void setTitleBar(TitleBar titleBar) {
131 super.setTitleBar(titleBar);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800132 setFocusState(false);
Michael Kolb31d469b2010-12-09 20:49:54 -0800133 }
134
Michael Kolb5a72f182011-01-13 20:35:06 -0800135 void updateNavigationState(Tab tab) {
John Reckef654f12011-07-12 16:42:08 -0700136 if (tab != null) {
137 mBackButton.setImageResource(tab.canGoBack()
Michael Kolb5a72f182011-01-13 20:35:06 -0800138 ? R.drawable.ic_back_holo_dark
139 : R.drawable.ic_back_disabled_holo_dark);
John Reckef654f12011-07-12 16:42:08 -0700140 mForwardButton.setImageResource(tab.canGoForward()
Michael Kolb5a72f182011-01-13 20:35:06 -0800141 ? R.drawable.ic_forward_holo_dark
142 : R.drawable.ic_forward_disabled_holo_dark);
143 }
John Reckb8b2af82011-05-20 15:58:33 -0700144 updateUrlIcon();
Michael Kolb5a72f182011-01-13 20:35:06 -0800145 }
146
Michael Kolb31d469b2010-12-09 20:49:54 -0800147 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500148 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800149 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500150 }
151
Michael Kolba2b2ba82010-08-04 17:54:03 -0700152 @Override
153 public void onClick(View v) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800154 if (mBackButton == v) {
John Reckef654f12011-07-12 16:42:08 -0700155 mUiController.getCurrentTab().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700156 } else if (mForwardButton == v) {
John Reckef654f12011-07-12 16:42:08 -0700157 mUiController.getCurrentTab().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700158 } else if (mStar == v) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700159 getContext().startActivity(mUiController.createBookmarkCurrentPageIntent(true));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700160 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700162 } else if (mSearchButton == v) {
John Reck0f602f32011-07-07 15:38:43 -0700163 mBaseUi.editUrl(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700164 } else if (mStopButton == v) {
165 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700166 } else if (mGoButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800167 if (!TextUtils.isEmpty(mUrlInput.getText())) {
168 onAction(mUrlInput.getText().toString(), null,
Michael Kolb257cc2c2010-12-09 09:45:52 -0800169 UrlInputView.TYPED);
Michael Kolb1ce78132010-09-23 15:50:53 -0700170 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700171 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800172 clearOrClose();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800173 } else if (mVoiceSearch == v) {
174 mUiController.startVoiceSearch();
Michael Kolb11d19782011-03-20 10:17:40 -0700175 } else {
176 super.onClick(v);
Michael Kolbfe251992010-07-08 15:41:55 -0700177 }
178 }
179
Michael Kolb31d469b2010-12-09 20:49:54 -0800180 private void clearOrClose() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000181 if (TextUtils.isEmpty(mUrlInput.getUserText())) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800182 // close
Michael Kolb7cdc4902011-02-03 17:54:40 -0800183 mUrlInput.clearFocus();
Michael Kolb31d469b2010-12-09 20:49:54 -0800184 } else {
185 // clear
186 mUrlInput.setText("");
187 }
188 }
189
John Reckb8b2af82011-05-20 15:58:33 -0700190 void updateUrlIcon() {
John Reck46500332011-06-07 14:36:10 -0700191 mUrlIcon.setImageResource(mInVoiceMode ?
192 R.drawable.ic_search_holo_dark
193 : R.drawable.ic_web_holo_dark);
John Reckb8b2af82011-05-20 15:58:33 -0700194 }
195
Michael Kolb11d19782011-03-20 10:17:40 -0700196 @Override
197 protected void setFocusState(boolean focus) {
198 super.setFocusState(focus);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800199 if (focus) {
Michael Kolbde463762011-07-14 15:25:45 -0700200 if (mHideNavButtons) {
201 hideNavButtons();
202 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700203 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800204 mStar.setVisibility(View.GONE);
205 mClearButton.setVisibility(View.VISIBLE);
Michael Kolbe3524d82011-03-02 14:53:15 -0800206 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
Michael Kolb60a68f52011-02-24 11:02:52 -0800207 updateSearchMode(false);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700208 } else {
Michael Kolbde463762011-07-14 15:25:45 -0700209 if (mHideNavButtons) {
210 showNavButtons();
211 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700212 mGoButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800213 mVoiceSearch.setVisibility(View.GONE);
214 mStar.setVisibility(View.VISIBLE);
215 mClearButton.setVisibility(View.GONE);
John Reck0f602f32011-07-07 15:38:43 -0700216 if (mTitleBar.useQuickControls()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800217 mSearchButton.setVisibility(View.GONE);
218 } else {
219 mSearchButton.setVisibility(View.VISIBLE);
220 }
John Reckb8b2af82011-05-20 15:58:33 -0700221 updateUrlIcon();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700222 }
John Reck7e5b8b52011-06-22 13:46:25 -0700223 mUrlContainer.setBackgroundDrawable(focus
224 ? mFocusDrawable : mUnfocusDrawable);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700225 }
226
Michael Kolba2b2ba82010-08-04 17:54:03 -0700227 private void stopOrRefresh() {
John Reck0f602f32011-07-07 15:38:43 -0700228 if (mTitleBar.isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700229 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700230 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700231 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700232 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400233 }
234
Michael Kolbfe251992010-07-08 15:41:55 -0700235 @Override
John Reck0f602f32011-07-07 15:38:43 -0700236 public void onProgressStarted() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700237 mStopButton.setImageDrawable(mStopDrawable);
238 }
239
240 @Override
John Reck0f602f32011-07-07 15:38:43 -0700241 public void onProgressStopped() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700242 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolbfe251992010-07-08 15:41:55 -0700243 }
244
Michael Kolb11d19782011-03-20 10:17:40 -0700245 protected void updateSearchMode(boolean userEdited) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000246 setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText()));
Michael Kolb31d469b2010-12-09 20:49:54 -0800247 }
248
Michael Kolb11d19782011-03-20 10:17:40 -0700249 @Override
250 protected void setSearchMode(boolean voiceSearchEnabled) {
Michael Kolb793e05e2011-01-11 15:17:31 -0800251 boolean showvoicebutton = voiceSearchEnabled &&
Michael Kolb736990c2011-03-20 10:01:20 -0700252 mUiController.supportsVoiceSearch();
Michael Kolb793e05e2011-01-11 15:17:31 -0800253 mVoiceSearch.setVisibility(showvoicebutton ? View.VISIBLE :
Michael Kolb31d469b2010-12-09 20:49:54 -0800254 View.GONE);
255 mGoButton.setVisibility(voiceSearchEnabled ? View.GONE :
256 View.VISIBLE);
257 }
258
Michael Kolbfe251992010-07-08 15:41:55 -0700259 @Override
Michael Kolbcfa3af52010-12-14 10:36:11 -0800260 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
Michael Kolb11d19782011-03-20 10:17:40 -0700261 super.setInVoiceMode(voicemode, voiceResults);
Michael Kolb3aaef252011-03-09 18:13:56 -0800262 if (voicemode) {
263 mUrlIcon.setImageDrawable(mSearchButton.getDrawable());
264 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800265 }
266
Michael Kolbde463762011-07-14 15:25:45 -0700267 private void hideNavButtons() {
268 int awidth = mNavButtons.getMeasuredWidth();
269 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, 0, - awidth);
270 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", mUrlContainer.getLeft(),
271 mUrlContainer.getPaddingLeft());
272 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 1f, 0f);
273 AnimatorSet combo = new AnimatorSet();
274 combo.playTogether(anim1, anim2, anim3);
275 combo.addListener(new AnimatorListenerAdapter() {
276 @Override
277 public void onAnimationEnd(Animator animation) {
278 mNavButtons.setVisibility(View.GONE);
279 }
280 });
281 combo.setDuration(150);
282 combo.start();
283 }
284
285 private void showNavButtons() {
286 int awidth = mNavButtons.getMeasuredWidth();
287 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, -awidth, 0);
288 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", 0, awidth);
289 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 0f, 1f);
290 AnimatorSet combo = new AnimatorSet();
291 combo.playTogether(anim1, anim2, anim3);
292 mNavButtons.setVisibility(View.VISIBLE);
293 combo.setDuration(150);
294 combo.start();
295 }
296
Leon Scroggins571b3762010-05-26 10:25:01 -0400297}