blob: adfd5e79927a8ad125dd33678bcf87c1733060e9 [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;
John Recka60fffa2011-09-06 16:30:29 -070023import android.content.Intent;
Michael Kolbde463762011-07-14 15:25:45 -070024import android.content.res.Configuration;
Leon Scroggins571b3762010-05-26 10:25:01 -040025import android.content.res.Resources;
John Reck034637c2011-08-11 11:34:44 -070026import android.graphics.Bitmap;
Leon Scroggins571b3762010-05-26 10:25:01 -040027import android.graphics.drawable.Drawable;
Michael Kolb1ce78132010-09-23 15:50:53 -070028import android.text.TextUtils;
John Reck0f602f32011-07-07 15:38:43 -070029import android.util.AttributeSet;
Leon Scroggins571b3762010-05-26 10:25:01 -040030import android.view.View;
Michael Kolb5a72f182011-01-13 20:35:06 -080031import android.widget.ImageButton;
Leon Scroggins571b3762010-05-26 10:25:01 -040032import android.widget.ImageView;
Leon Scroggins571b3762010-05-26 10:25:01 -040033
Michael Kolb315d5022011-10-13 12:47:11 -070034import com.android.browser.UI.ComboViews;
Michael Kolb0b129122012-06-04 16:31:58 -070035import com.android.browser.UrlInputView.StateListener;
Michael Kolb315d5022011-10-13 12:47:11 -070036
Michael Kolb0b129122012-06-04 16:31:58 -070037public class NavigationBarTablet extends NavigationBarBase implements StateListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070038
Michael Kolba2b2ba82010-08-04 17:54:03 -070039 private Drawable mStopDrawable;
40 private Drawable mReloadDrawable;
Michael Kolb30adae62011-07-29 13:37:05 -070041 private String mStopDescription;
42 private String mRefreshDescription;
Michael Kolbc7485ae2010-09-03 10:10:58 -070043
Michael Kolb11d19782011-03-20 10:17:40 -070044 private View mUrlContainer;
Michael Kolb5a72f182011-01-13 20:35:06 -080045 private ImageButton mBackButton;
46 private ImageButton mForwardButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080047 private ImageView mStar;
Michael Kolbe3524d82011-03-02 14:53:15 -080048 private ImageView mUrlIcon;
49 private ImageView mSearchButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070050 private ImageView mStopButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070051 private View mAllButton;
Michael Kolbb7b115e2010-09-25 16:59:37 -070052 private View mClearButton;
Michael Kolb0b129122012-06-04 16:31:58 -070053 private View mVoiceButton;
Michael Kolbde463762011-07-14 15:25:45 -070054 private View mNavButtons;
Michael Kolbcfa3af52010-12-14 10:36:11 -080055 private Drawable mFocusDrawable;
56 private Drawable mUnfocusDrawable;
Michael Kolbde463762011-07-14 15:25:45 -070057 private boolean mHideNavButtons;
John Reck034637c2011-08-11 11:34:44 -070058 private Drawable mFaviconDrawable;
Michael Kolb81b6f832010-12-12 12:44:27 -080059
John Reck0f602f32011-07-07 15:38:43 -070060 public NavigationBarTablet(Context context) {
61 super(context);
62 init(context);
63 }
64
65 public NavigationBarTablet(Context context, AttributeSet attrs) {
66 super(context, attrs);
67 init(context);
68 }
69
70 public NavigationBarTablet(Context context, AttributeSet attrs, int defStyle) {
71 super(context, attrs, defStyle);
72 init(context);
73 }
74
75 private void init(Context context) {
76 Resources resources = context.getResources();
Michael Kolb5a72f182011-01-13 20:35:06 -080077 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_holo_dark);
78 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb30adae62011-07-29 13:37:05 -070079 mStopDescription = resources.getString(R.string.accessibility_button_stop);
80 mRefreshDescription = resources.getString(R.string.accessibility_button_refresh);
Michael Kolbcfa3af52010-12-14 10:36:11 -080081 mFocusDrawable = resources.getDrawable(
82 R.drawable.textfield_active_holo_dark);
83 mUnfocusDrawable = resources.getDrawable(
84 R.drawable.textfield_default_holo_dark);
Michael Kolbde463762011-07-14 15:25:45 -070085 mHideNavButtons = resources.getBoolean(R.bool.hide_nav_buttons);
Michael Kolbfe251992010-07-08 15:41:55 -070086 }
Leon Scroggins571b3762010-05-26 10:25:01 -040087
Michael Kolb7cdc4902011-02-03 17:54:40 -080088 @Override
John Reck0f602f32011-07-07 15:38:43 -070089 protected void onFinishInflate() {
90 super.onFinishInflate();
Michael Kolbfe251992010-07-08 15:41:55 -070091 mAllButton = findViewById(R.id.all_btn);
92 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040093 // back/forward. Probably should be done inside onPageStarted.
Michael Kolbde463762011-07-14 15:25:45 -070094 mNavButtons = findViewById(R.id.navbuttons);
Michael Kolb5a72f182011-01-13 20:35:06 -080095 mBackButton = (ImageButton) findViewById(R.id.back);
96 mForwardButton = (ImageButton) findViewById(R.id.forward);
Michael Kolbe3524d82011-03-02 14:53:15 -080097 mUrlIcon = (ImageView) findViewById(R.id.url_icon);
Michael Kolb31d469b2010-12-09 20:49:54 -080098 mStar = (ImageView) findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -070099 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolbe3524d82011-03-02 14:53:15 -0800100 mSearchButton = (ImageView) findViewById(R.id.search);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700101 mClearButton = findViewById(R.id.clear);
Michael Kolb0b129122012-06-04 16:31:58 -0700102 mVoiceButton = findViewById(R.id.voice);
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 Kolbb7b115e2010-09-25 16:59:37 -0700110 mClearButton.setOnClickListener(this);
Michael Kolb0b129122012-06-04 16:31:58 -0700111 mVoiceButton.setOnClickListener(this);
Michael Kolb31d469b2010-12-09 20:49:54 -0800112 mUrlInput.setContainer(mUrlContainer);
Michael Kolb0b129122012-06-04 16:31:58 -0700113 mUrlInput.setStateListener(this);
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
George Mount387d45d2011-10-07 15:57:53 -0700153 public void onTabDataChanged(Tab tab) {
154 super.onTabDataChanged(tab);
155 showHideStar(tab);
156 }
157
158 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500159 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800160 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500161 }
162
Michael Kolba2b2ba82010-08-04 17:54:03 -0700163 @Override
164 public void onClick(View v) {
Michael Kolbc832e5e2012-03-09 15:08:23 -0800165 if ((mBackButton == v) && (mUiController.getCurrentTab() != null)) {
John Reckef654f12011-07-12 16:42:08 -0700166 mUiController.getCurrentTab().goBack();
Michael Kolbc832e5e2012-03-09 15:08:23 -0800167 } else if ((mForwardButton == v) && (mUiController.getCurrentTab() != null)) {
John Reckef654f12011-07-12 16:42:08 -0700168 mUiController.getCurrentTab().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700169 } else if (mStar == v) {
John Recka60fffa2011-09-06 16:30:29 -0700170 Intent intent = mUiController.createBookmarkCurrentPageIntent(true);
171 if (intent != null) {
172 getContext().startActivity(intent);
173 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700174 } else if (mAllButton == v) {
Michael Kolb315d5022011-10-13 12:47:11 -0700175 mUiController.bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700176 } else if (mSearchButton == v) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700177 mBaseUi.editUrl(true, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700178 } else if (mStopButton == v) {
179 stopOrRefresh();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700180 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800181 clearOrClose();
Michael Kolb0b129122012-06-04 16:31:58 -0700182 } else if (mVoiceButton == v) {
183 mUiController.startVoiceRecognizer();
Michael Kolb11d19782011-03-20 10:17:40 -0700184 } else {
185 super.onClick(v);
Michael Kolbfe251992010-07-08 15:41:55 -0700186 }
187 }
188
Michael Kolb31d469b2010-12-09 20:49:54 -0800189 private void clearOrClose() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000190 if (TextUtils.isEmpty(mUrlInput.getText())) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800191 // close
Michael Kolb7cdc4902011-02-03 17:54:40 -0800192 mUrlInput.clearFocus();
Michael Kolb31d469b2010-12-09 20:49:54 -0800193 } else {
194 // clear
195 mUrlInput.setText("");
196 }
197 }
198
John Reck034637c2011-08-11 11:34:44 -0700199 @Override
200 public void setFavicon(Bitmap icon) {
201 mFaviconDrawable = mBaseUi.getFaviconDrawable(icon);
202 updateUrlIcon();
203 }
204
John Reckb8b2af82011-05-20 15:58:33 -0700205 void updateUrlIcon() {
John Reck034637c2011-08-11 11:34:44 -0700206 if (mUrlInput.hasFocus()) {
207 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
208 } else {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700209 if (mFaviconDrawable == null) {
210 mFaviconDrawable = mBaseUi.getFaviconDrawable(null);
John Reck034637c2011-08-11 11:34:44 -0700211 }
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700212 mUrlIcon.setImageDrawable(mFaviconDrawable);
John Reck034637c2011-08-11 11:34:44 -0700213 }
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);
Michael Kolbe3524d82011-03-02 14:53:15 -0800225 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700226 } else {
Michael Kolbde463762011-07-14 15:25:45 -0700227 if (mHideNavButtons) {
228 showNavButtons();
229 }
George Mount387d45d2011-10-07 15:57:53 -0700230 showHideStar(mUiController.getCurrentTab());
John Reck0f602f32011-07-07 15:38:43 -0700231 if (mTitleBar.useQuickControls()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800232 mSearchButton.setVisibility(View.GONE);
233 } else {
234 mSearchButton.setVisibility(View.VISIBLE);
235 }
John Reckb8b2af82011-05-20 15:58:33 -0700236 updateUrlIcon();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700237 }
John Reck7e5b8b52011-06-22 13:46:25 -0700238 mUrlContainer.setBackgroundDrawable(focus
239 ? mFocusDrawable : mUnfocusDrawable);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700240 }
241
Michael Kolba2b2ba82010-08-04 17:54:03 -0700242 private void stopOrRefresh() {
Michael Kolb1392b832011-09-07 13:02:59 -0700243 if (mUiController == null) return;
John Reck0f602f32011-07-07 15:38:43 -0700244 if (mTitleBar.isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700245 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700246 } else {
Michael Kolb66450842011-12-15 15:01:19 -0800247 if (mUiController.getCurrentTopWebView() != null) {
248 mUiController.getCurrentTopWebView().reload();
249 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700250 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400251 }
252
Michael Kolbfe251992010-07-08 15:41:55 -0700253 @Override
John Reck0f602f32011-07-07 15:38:43 -0700254 public void onProgressStarted() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700255 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700256 mStopButton.setContentDescription(mStopDescription);
Michael Kolb46f987e2011-04-05 10:39:10 -0700257 }
258
259 @Override
John Reck0f602f32011-07-07 15:38:43 -0700260 public void onProgressStopped() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700261 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700262 mStopButton.setContentDescription(mRefreshDescription);
Michael Kolbfe251992010-07-08 15:41:55 -0700263 }
264
Michael Kolbbae0cb22012-05-29 11:15:27 -0700265 private AnimatorSet mAnimation;
266
Michael Kolbde463762011-07-14 15:25:45 -0700267 private void hideNavButtons() {
Michael Kolbbae0cb22012-05-29 11:15:27 -0700268 if (mBaseUi.blockFocusAnimations()) {
269 mNavButtons.setVisibility(View.GONE);
270 return;
271 }
Michael Kolbde463762011-07-14 15:25:45 -0700272 int awidth = mNavButtons.getMeasuredWidth();
273 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, 0, - awidth);
274 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", mUrlContainer.getLeft(),
275 mUrlContainer.getPaddingLeft());
276 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 1f, 0f);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700277 mAnimation = new AnimatorSet();
278 mAnimation.playTogether(anim1, anim2, anim3);
279 mAnimation.addListener(new AnimatorListenerAdapter() {
Michael Kolbde463762011-07-14 15:25:45 -0700280 @Override
281 public void onAnimationEnd(Animator animation) {
282 mNavButtons.setVisibility(View.GONE);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700283 mAnimation = null;
Michael Kolbde463762011-07-14 15:25:45 -0700284 }
285 });
Michael Kolbbae0cb22012-05-29 11:15:27 -0700286 mAnimation.setDuration(150);
287 mAnimation.start();
Michael Kolbde463762011-07-14 15:25:45 -0700288 }
289
290 private void showNavButtons() {
Michael Kolbbae0cb22012-05-29 11:15:27 -0700291 if (mAnimation != null) {
292 mAnimation.cancel();
293 }
Michael Kolbde463762011-07-14 15:25:45 -0700294 mNavButtons.setVisibility(View.VISIBLE);
Michael Kolb0b129122012-06-04 16:31:58 -0700295 mNavButtons.setTranslationX(0);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700296 if (!mBaseUi.blockFocusAnimations()) {
297 int awidth = mNavButtons.getMeasuredWidth();
298 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons,
299 View.TRANSLATION_X, -awidth, 0);
300 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", 0,
301 awidth);
302 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA,
303 0f, 1f);
304 AnimatorSet combo = new AnimatorSet();
305 combo.playTogether(anim1, anim2, anim3);
306 combo.setDuration(150);
307 combo.start();
308 }
Michael Kolbde463762011-07-14 15:25:45 -0700309 }
310
George Mount387d45d2011-10-07 15:57:53 -0700311 private void showHideStar(Tab tab) {
312 // hide the bookmark star for data URLs
313 if (tab != null && tab.inForeground()) {
314 int starVisibility = View.VISIBLE;
315 String url = tab.getUrl();
316 if (DataUri.isDataUri(url)) {
317 starVisibility = View.GONE;
318 }
319 mStar.setVisibility(starVisibility);
320 }
321 }
322
Michael Kolb0b129122012-06-04 16:31:58 -0700323 @Override
324 public void onStateChanged(int state) {
Michael Kolbe721cc32012-06-26 15:26:59 -0700325 mVoiceButton.setVisibility(View.GONE);
Michael Kolb0b129122012-06-04 16:31:58 -0700326 switch(state) {
327 case STATE_NORMAL:
328 mClearButton.setVisibility(View.GONE);
Michael Kolb0b129122012-06-04 16:31:58 -0700329 break;
330 case STATE_HIGHLIGHTED:
331 mClearButton.setVisibility(View.GONE);
Michael Kolbe721cc32012-06-26 15:26:59 -0700332 if ((mUiController != null) && mUiController.supportsVoice()) {
333 mVoiceButton.setVisibility(View.VISIBLE);
334 }
Michael Kolb0b129122012-06-04 16:31:58 -0700335 break;
336 case STATE_EDITED:
337 mClearButton.setVisibility(View.VISIBLE);
Michael Kolb0b129122012-06-04 16:31:58 -0700338 break;
339 }
340 }
341
Leon Scroggins571b3762010-05-26 10:25:01 -0400342}