blob: 8c08dcf466db91226abd02c3c96ae14e996efaef [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;
John Reck034637c2011-08-11 11:34:44 -070025import android.graphics.Bitmap;
Leon Scroggins571b3762010-05-26 10:25:01 -040026import android.graphics.drawable.Drawable;
Michael Kolb1ce78132010-09-23 15:50:53 -070027import android.text.TextUtils;
John Reck0f602f32011-07-07 15:38:43 -070028import android.util.AttributeSet;
Leon Scroggins571b3762010-05-26 10:25:01 -040029import android.view.View;
Michael Kolb5a72f182011-01-13 20:35:06 -080030import android.widget.ImageButton;
Leon Scroggins571b3762010-05-26 10:25:01 -040031import android.widget.ImageView;
Leon Scroggins571b3762010-05-26 10:25:01 -040032
Michael Kolbcfa3af52010-12-14 10:36:11 -080033import java.util.List;
34
John Reck0f602f32011-07-07 15:38:43 -070035public class NavigationBarTablet extends NavigationBarBase {
Michael Kolb8233fac2010-10-26 16:08:53 -070036
Michael Kolba2b2ba82010-08-04 17:54:03 -070037 private Drawable mStopDrawable;
38 private Drawable mReloadDrawable;
Michael Kolb30adae62011-07-29 13:37:05 -070039 private String mStopDescription;
40 private String mRefreshDescription;
Michael Kolbc7485ae2010-09-03 10:10:58 -070041
Michael Kolb11d19782011-03-20 10:17:40 -070042 private View mUrlContainer;
Michael Kolb5a72f182011-01-13 20:35:06 -080043 private ImageButton mBackButton;
44 private ImageButton mForwardButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080045 private ImageView mStar;
Michael Kolbe3524d82011-03-02 14:53:15 -080046 private ImageView mUrlIcon;
47 private ImageView mSearchButton;
Michael Kolb513286f2010-09-09 12:55:12 -070048 private View mGoButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070049 private ImageView mStopButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070050 private View mAllButton;
Michael Kolbb7b115e2010-09-25 16:59:37 -070051 private View mClearButton;
Michael Kolbe3524d82011-03-02 14:53:15 -080052 private ImageView mVoiceSearch;
Michael Kolbde463762011-07-14 15:25:45 -070053 private View mNavButtons;
Michael Kolbcfa3af52010-12-14 10:36:11 -080054 private Drawable mFocusDrawable;
55 private Drawable mUnfocusDrawable;
Michael Kolbde463762011-07-14 15:25:45 -070056 private boolean mHideNavButtons;
John Reck034637c2011-08-11 11:34:44 -070057 private Drawable mFaviconDrawable;
Michael Kolb81b6f832010-12-12 12:44:27 -080058
John Reck0f602f32011-07-07 15:38:43 -070059 public NavigationBarTablet(Context context) {
60 super(context);
61 init(context);
62 }
63
64 public NavigationBarTablet(Context context, AttributeSet attrs) {
65 super(context, attrs);
66 init(context);
67 }
68
69 public NavigationBarTablet(Context context, AttributeSet attrs, int defStyle) {
70 super(context, attrs, defStyle);
71 init(context);
72 }
73
74 private void init(Context context) {
75 Resources resources = context.getResources();
Michael Kolb5a72f182011-01-13 20:35:06 -080076 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_holo_dark);
77 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb30adae62011-07-29 13:37:05 -070078 mStopDescription = resources.getString(R.string.accessibility_button_stop);
79 mRefreshDescription = resources.getString(R.string.accessibility_button_refresh);
Michael Kolbcfa3af52010-12-14 10:36:11 -080080 mFocusDrawable = resources.getDrawable(
81 R.drawable.textfield_active_holo_dark);
82 mUnfocusDrawable = resources.getDrawable(
83 R.drawable.textfield_default_holo_dark);
Michael Kolbde463762011-07-14 15:25:45 -070084 mHideNavButtons = resources.getBoolean(R.bool.hide_nav_buttons);
Michael Kolbfe251992010-07-08 15:41:55 -070085 }
Leon Scroggins571b3762010-05-26 10:25:01 -040086
Michael Kolb7cdc4902011-02-03 17:54:40 -080087 @Override
John Reck0f602f32011-07-07 15:38:43 -070088 protected void onFinishInflate() {
89 super.onFinishInflate();
Michael Kolbfe251992010-07-08 15:41:55 -070090 mAllButton = findViewById(R.id.all_btn);
91 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040092 // back/forward. Probably should be done inside onPageStarted.
Michael Kolbde463762011-07-14 15:25:45 -070093 mNavButtons = findViewById(R.id.navbuttons);
Michael Kolb5a72f182011-01-13 20:35:06 -080094 mBackButton = (ImageButton) findViewById(R.id.back);
95 mForwardButton = (ImageButton) findViewById(R.id.forward);
Michael Kolbe3524d82011-03-02 14:53:15 -080096 mUrlIcon = (ImageView) findViewById(R.id.url_icon);
Michael Kolb31d469b2010-12-09 20:49:54 -080097 mStar = (ImageView) findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -070098 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolbe3524d82011-03-02 14:53:15 -080099 mSearchButton = (ImageView) findViewById(R.id.search);
Michael Kolb513286f2010-09-09 12:55:12 -0700100 mGoButton = findViewById(R.id.go);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700101 mClearButton = findViewById(R.id.clear);
Michael Kolbe3524d82011-03-02 14:53:15 -0800102 mVoiceSearch = (ImageView) findViewById(R.id.voicesearch);
Michael Kolb31d469b2010-12-09 20:49:54 -0800103 mUrlContainer = findViewById(R.id.urlbar_focused);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700104 mBackButton.setOnClickListener(this);
105 mForwardButton.setOnClickListener(this);
106 mStar.setOnClickListener(this);
107 mAllButton.setOnClickListener(this);
108 mStopButton.setOnClickListener(this);
109 mSearchButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700110 mGoButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700111 mClearButton.setOnClickListener(this);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800112 mVoiceSearch.setOnClickListener(this);
Michael Kolb31d469b2010-12-09 20:49:54 -0800113 mUrlInput.setContainer(mUrlContainer);
John Reck0f602f32011-07-07 15:38:43 -0700114 }
115
Michael Kolbde463762011-07-14 15:25:45 -0700116 public void onConfigurationChanged(Configuration config) {
117 super.onConfigurationChanged(config);
118 Resources res = mContext.getResources();
119 mHideNavButtons = res.getBoolean(R.bool.hide_nav_buttons);
120 if (mUrlInput.hasFocus()) {
121 if (mHideNavButtons && (mNavButtons.getVisibility() == View.VISIBLE)) {
122 int aw = mNavButtons.getMeasuredWidth();
123 mNavButtons.setVisibility(View.GONE);
124 mNavButtons.setAlpha(0f);
125 mNavButtons.setTranslationX(-aw);
126 } else if (!mHideNavButtons && (mNavButtons.getVisibility() == View.GONE)) {
127 mNavButtons.setVisibility(View.VISIBLE);
128 mNavButtons.setAlpha(1f);
129 mNavButtons.setTranslationX(0);
130 }
131 }
132 }
133
John Reck0f602f32011-07-07 15:38:43 -0700134 @Override
135 public void setTitleBar(TitleBar titleBar) {
136 super.setTitleBar(titleBar);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800137 setFocusState(false);
Michael Kolb31d469b2010-12-09 20:49:54 -0800138 }
139
Michael Kolb5a72f182011-01-13 20:35:06 -0800140 void updateNavigationState(Tab tab) {
John Reckef654f12011-07-12 16:42:08 -0700141 if (tab != null) {
142 mBackButton.setImageResource(tab.canGoBack()
Michael Kolb5a72f182011-01-13 20:35:06 -0800143 ? R.drawable.ic_back_holo_dark
144 : R.drawable.ic_back_disabled_holo_dark);
John Reckef654f12011-07-12 16:42:08 -0700145 mForwardButton.setImageResource(tab.canGoForward()
Michael Kolb5a72f182011-01-13 20:35:06 -0800146 ? R.drawable.ic_forward_holo_dark
147 : R.drawable.ic_forward_disabled_holo_dark);
148 }
John Reckb8b2af82011-05-20 15:58:33 -0700149 updateUrlIcon();
Michael Kolb5a72f182011-01-13 20:35:06 -0800150 }
151
Michael Kolb31d469b2010-12-09 20:49:54 -0800152 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500153 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800154 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500155 }
156
Michael Kolba2b2ba82010-08-04 17:54:03 -0700157 @Override
158 public void onClick(View v) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800159 if (mBackButton == v) {
John Reckef654f12011-07-12 16:42:08 -0700160 mUiController.getCurrentTab().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700161 } else if (mForwardButton == v) {
John Reckef654f12011-07-12 16:42:08 -0700162 mUiController.getCurrentTab().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700163 } else if (mStar == v) {
John Reckd3e4d5b2011-07-13 15:48:43 -0700164 getContext().startActivity(mUiController.createBookmarkCurrentPageIntent(true));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700165 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700166 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700167 } else if (mSearchButton == v) {
John Reck0f602f32011-07-07 15:38:43 -0700168 mBaseUi.editUrl(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700169 } else if (mStopButton == v) {
170 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700171 } else if (mGoButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800172 if (!TextUtils.isEmpty(mUrlInput.getText())) {
173 onAction(mUrlInput.getText().toString(), null,
Michael Kolb257cc2c2010-12-09 09:45:52 -0800174 UrlInputView.TYPED);
Michael Kolb1ce78132010-09-23 15:50:53 -0700175 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700176 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800177 clearOrClose();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800178 } else if (mVoiceSearch == v) {
179 mUiController.startVoiceSearch();
Michael Kolb11d19782011-03-20 10:17:40 -0700180 } else {
181 super.onClick(v);
Michael Kolbfe251992010-07-08 15:41:55 -0700182 }
183 }
184
Michael Kolb31d469b2010-12-09 20:49:54 -0800185 private void clearOrClose() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000186 if (TextUtils.isEmpty(mUrlInput.getUserText())) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800187 // close
Michael Kolb7cdc4902011-02-03 17:54:40 -0800188 mUrlInput.clearFocus();
Michael Kolb31d469b2010-12-09 20:49:54 -0800189 } else {
190 // clear
191 mUrlInput.setText("");
192 }
193 }
194
John Reck034637c2011-08-11 11:34:44 -0700195 @Override
196 public void setFavicon(Bitmap icon) {
197 mFaviconDrawable = mBaseUi.getFaviconDrawable(icon);
198 updateUrlIcon();
199 }
200
John Reckb8b2af82011-05-20 15:58:33 -0700201 void updateUrlIcon() {
John Reck034637c2011-08-11 11:34:44 -0700202 if (mUrlInput.hasFocus()) {
203 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
204 } else {
205 if (mInVoiceMode) {
206 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
207 } else {
208 if (mFaviconDrawable == null) {
209 mFaviconDrawable = mBaseUi.getFaviconDrawable(null);
210 }
211 mUrlIcon.setImageDrawable(mFaviconDrawable);
212 }
213 }
John Reckb8b2af82011-05-20 15:58:33 -0700214 }
215
Michael Kolb11d19782011-03-20 10:17:40 -0700216 @Override
217 protected void setFocusState(boolean focus) {
218 super.setFocusState(focus);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800219 if (focus) {
Michael Kolbde463762011-07-14 15:25:45 -0700220 if (mHideNavButtons) {
221 hideNavButtons();
222 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700223 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800224 mStar.setVisibility(View.GONE);
225 mClearButton.setVisibility(View.VISIBLE);
Michael Kolbe3524d82011-03-02 14:53:15 -0800226 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
Michael Kolb60a68f52011-02-24 11:02:52 -0800227 updateSearchMode(false);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700228 } else {
Michael Kolbde463762011-07-14 15:25:45 -0700229 if (mHideNavButtons) {
230 showNavButtons();
231 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700232 mGoButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800233 mVoiceSearch.setVisibility(View.GONE);
234 mStar.setVisibility(View.VISIBLE);
235 mClearButton.setVisibility(View.GONE);
John Reck0f602f32011-07-07 15:38:43 -0700236 if (mTitleBar.useQuickControls()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800237 mSearchButton.setVisibility(View.GONE);
238 } else {
239 mSearchButton.setVisibility(View.VISIBLE);
240 }
John Reckb8b2af82011-05-20 15:58:33 -0700241 updateUrlIcon();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700242 }
John Reck7e5b8b52011-06-22 13:46:25 -0700243 mUrlContainer.setBackgroundDrawable(focus
244 ? mFocusDrawable : mUnfocusDrawable);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700245 }
246
Michael Kolba2b2ba82010-08-04 17:54:03 -0700247 private void stopOrRefresh() {
John Reck0f602f32011-07-07 15:38:43 -0700248 if (mTitleBar.isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700249 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700250 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700251 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700252 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400253 }
254
Michael Kolbfe251992010-07-08 15:41:55 -0700255 @Override
John Reck0f602f32011-07-07 15:38:43 -0700256 public void onProgressStarted() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700257 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700258 mStopButton.setContentDescription(mStopDescription);
Michael Kolb46f987e2011-04-05 10:39:10 -0700259 }
260
261 @Override
John Reck0f602f32011-07-07 15:38:43 -0700262 public void onProgressStopped() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700263 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700264 mStopButton.setContentDescription(mRefreshDescription);
Michael Kolbfe251992010-07-08 15:41:55 -0700265 }
266
Michael Kolb11d19782011-03-20 10:17:40 -0700267 protected void updateSearchMode(boolean userEdited) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000268 setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText()));
Michael Kolb31d469b2010-12-09 20:49:54 -0800269 }
270
Michael Kolb11d19782011-03-20 10:17:40 -0700271 @Override
272 protected void setSearchMode(boolean voiceSearchEnabled) {
Michael Kolb793e05e2011-01-11 15:17:31 -0800273 boolean showvoicebutton = voiceSearchEnabled &&
Michael Kolb736990c2011-03-20 10:01:20 -0700274 mUiController.supportsVoiceSearch();
Michael Kolb793e05e2011-01-11 15:17:31 -0800275 mVoiceSearch.setVisibility(showvoicebutton ? View.VISIBLE :
Michael Kolb31d469b2010-12-09 20:49:54 -0800276 View.GONE);
277 mGoButton.setVisibility(voiceSearchEnabled ? View.GONE :
278 View.VISIBLE);
279 }
280
Michael Kolbfe251992010-07-08 15:41:55 -0700281 @Override
Michael Kolbcfa3af52010-12-14 10:36:11 -0800282 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
Michael Kolb11d19782011-03-20 10:17:40 -0700283 super.setInVoiceMode(voicemode, voiceResults);
Michael Kolb3aaef252011-03-09 18:13:56 -0800284 if (voicemode) {
285 mUrlIcon.setImageDrawable(mSearchButton.getDrawable());
286 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800287 }
288
Michael Kolbde463762011-07-14 15:25:45 -0700289 private void hideNavButtons() {
290 int awidth = mNavButtons.getMeasuredWidth();
291 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, 0, - awidth);
292 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", mUrlContainer.getLeft(),
293 mUrlContainer.getPaddingLeft());
294 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 1f, 0f);
295 AnimatorSet combo = new AnimatorSet();
296 combo.playTogether(anim1, anim2, anim3);
297 combo.addListener(new AnimatorListenerAdapter() {
298 @Override
299 public void onAnimationEnd(Animator animation) {
300 mNavButtons.setVisibility(View.GONE);
301 }
302 });
303 combo.setDuration(150);
304 combo.start();
305 }
306
307 private void showNavButtons() {
308 int awidth = mNavButtons.getMeasuredWidth();
309 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, -awidth, 0);
310 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", 0, awidth);
311 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 0f, 1f);
312 AnimatorSet combo = new AnimatorSet();
313 combo.playTogether(anim1, anim2, anim3);
314 mNavButtons.setVisibility(View.VISIBLE);
315 combo.setDuration(150);
316 combo.start();
317 }
318
Leon Scroggins571b3762010-05-26 10:25:01 -0400319}