blob: 31edce903f5b0c6968d86eb2e279777a4a099d7b [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 */
Bijan Amirzada41242f22014-03-21 12:12:18 -070016package com.android.browser;
Leon Scroggins571b3762010-05-26 10:25:01 -040017
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
Bijan Amirzada41242f22014-03-21 12:12:18 -070034import com.android.browser.UI.ComboViews;
35import 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 Kolbde463762011-07-14 15:25:45 -070055 private boolean mHideNavButtons;
John Reck034637c2011-08-11 11:34:44 -070056 private Drawable mFaviconDrawable;
Michael Kolb81b6f832010-12-12 12:44:27 -080057
John Reck0f602f32011-07-07 15:38:43 -070058 public NavigationBarTablet(Context context) {
59 super(context);
60 init(context);
61 }
62
63 public NavigationBarTablet(Context context, AttributeSet attrs) {
64 super(context, attrs);
65 init(context);
66 }
67
68 public NavigationBarTablet(Context context, AttributeSet attrs, int defStyle) {
69 super(context, attrs, defStyle);
70 init(context);
71 }
72
73 private void init(Context context) {
74 Resources resources = context.getResources();
Enrico Ros1f5a0952014-11-18 20:15:48 -080075 mStopDrawable = resources.getDrawable(R.drawable.ic_action_stop);
76 mReloadDrawable = resources.getDrawable(R.drawable.ic_action_reload);
Michael Kolb30adae62011-07-29 13:37:05 -070077 mStopDescription = resources.getString(R.string.accessibility_button_stop);
78 mRefreshDescription = resources.getString(R.string.accessibility_button_refresh);
Michael Kolbde463762011-07-14 15:25:45 -070079 mHideNavButtons = resources.getBoolean(R.bool.hide_nav_buttons);
Michael Kolbfe251992010-07-08 15:41:55 -070080 }
Leon Scroggins571b3762010-05-26 10:25:01 -040081
Michael Kolb7cdc4902011-02-03 17:54:40 -080082 @Override
John Reck0f602f32011-07-07 15:38:43 -070083 protected void onFinishInflate() {
84 super.onFinishInflate();
Michael Kolbfe251992010-07-08 15:41:55 -070085 mAllButton = findViewById(R.id.all_btn);
86 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040087 // back/forward. Probably should be done inside onPageStarted.
Michael Kolbde463762011-07-14 15:25:45 -070088 mNavButtons = findViewById(R.id.navbuttons);
Michael Kolb5a72f182011-01-13 20:35:06 -080089 mBackButton = (ImageButton) findViewById(R.id.back);
90 mForwardButton = (ImageButton) findViewById(R.id.forward);
Michael Kolbe3524d82011-03-02 14:53:15 -080091 mUrlIcon = (ImageView) findViewById(R.id.url_icon);
Michael Kolb31d469b2010-12-09 20:49:54 -080092 mStar = (ImageView) findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -070093 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolbe3524d82011-03-02 14:53:15 -080094 mSearchButton = (ImageView) findViewById(R.id.search);
Michael Kolbb7b115e2010-09-25 16:59:37 -070095 mClearButton = findViewById(R.id.clear);
Michael Kolb0b129122012-06-04 16:31:58 -070096 mVoiceButton = findViewById(R.id.voice);
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 Kolbb7b115e2010-09-25 16:59:37 -0700104 mClearButton.setOnClickListener(this);
Michael Kolb0b129122012-06-04 16:31:58 -0700105 mVoiceButton.setOnClickListener(this);
Michael Kolb31d469b2010-12-09 20:49:54 -0800106 mUrlInput.setContainer(mUrlContainer);
Michael Kolb0b129122012-06-04 16:31:58 -0700107 mUrlInput.setStateListener(this);
John Reck0f602f32011-07-07 15:38:43 -0700108 }
109
Michael Kolbde463762011-07-14 15:25:45 -0700110 public void onConfigurationChanged(Configuration config) {
111 super.onConfigurationChanged(config);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800112 Resources res = getContext().getResources();
Michael Kolbde463762011-07-14 15:25:45 -0700113 mHideNavButtons = res.getBoolean(R.bool.hide_nav_buttons);
114 if (mUrlInput.hasFocus()) {
115 if (mHideNavButtons && (mNavButtons.getVisibility() == View.VISIBLE)) {
116 int aw = mNavButtons.getMeasuredWidth();
117 mNavButtons.setVisibility(View.GONE);
118 mNavButtons.setAlpha(0f);
119 mNavButtons.setTranslationX(-aw);
120 } else if (!mHideNavButtons && (mNavButtons.getVisibility() == View.GONE)) {
121 mNavButtons.setVisibility(View.VISIBLE);
122 mNavButtons.setAlpha(1f);
123 mNavButtons.setTranslationX(0);
124 }
125 }
126 }
127
John Reck0f602f32011-07-07 15:38:43 -0700128 @Override
129 public void setTitleBar(TitleBar titleBar) {
130 super.setTitleBar(titleBar);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800131 setFocusState(false);
Michael Kolb31d469b2010-12-09 20:49:54 -0800132 }
133
Michael Kolb5a72f182011-01-13 20:35:06 -0800134 void updateNavigationState(Tab tab) {
John Reckef654f12011-07-12 16:42:08 -0700135 if (tab != null) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800136 mBackButton.setEnabled(tab.canGoBack());
Pankaj Garg5ae52a02014-12-12 11:06:17 -0800137 mForwardButton.setEnabled(tab.canGoForward());
Michael Kolb5a72f182011-01-13 20:35:06 -0800138 }
John Reckb8b2af82011-05-20 15:58:33 -0700139 updateUrlIcon();
Michael Kolb5a72f182011-01-13 20:35:06 -0800140 }
141
Michael Kolb31d469b2010-12-09 20:49:54 -0800142 @Override
George Mount387d45d2011-10-07 15:57:53 -0700143 public void onTabDataChanged(Tab tab) {
144 super.onTabDataChanged(tab);
145 showHideStar(tab);
146 }
147
148 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500149 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800150 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500151 }
152
Michael Kolba2b2ba82010-08-04 17:54:03 -0700153 @Override
154 public void onClick(View v) {
Michael Kolbc832e5e2012-03-09 15:08:23 -0800155 if ((mBackButton == v) && (mUiController.getCurrentTab() != null)) {
John Reckef654f12011-07-12 16:42:08 -0700156 mUiController.getCurrentTab().goBack();
Michael Kolbc832e5e2012-03-09 15:08:23 -0800157 } else if ((mForwardButton == v) && (mUiController.getCurrentTab() != null)) {
John Reckef654f12011-07-12 16:42:08 -0700158 mUiController.getCurrentTab().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700159 } else if (mStar == v) {
John Recka60fffa2011-09-06 16:30:29 -0700160 Intent intent = mUiController.createBookmarkCurrentPageIntent(true);
161 if (intent != null) {
162 getContext().startActivity(intent);
163 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700164 } else if (mAllButton == v) {
Michael Kolb315d5022011-10-13 12:47:11 -0700165 mUiController.bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700166 } else if (mSearchButton == v) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700167 mBaseUi.editUrl(true, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700168 } else if (mStopButton == v) {
169 stopOrRefresh();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700170 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800171 clearOrClose();
Michael Kolb0b129122012-06-04 16:31:58 -0700172 } else if (mVoiceButton == v) {
173 mUiController.startVoiceRecognizer();
Michael Kolb11d19782011-03-20 10:17:40 -0700174 } else {
175 super.onClick(v);
Michael Kolbfe251992010-07-08 15:41:55 -0700176 }
177 }
178
Michael Kolb31d469b2010-12-09 20:49:54 -0800179 private void clearOrClose() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000180 if (TextUtils.isEmpty(mUrlInput.getText())) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800181 // close
Michael Kolb7cdc4902011-02-03 17:54:40 -0800182 mUrlInput.clearFocus();
Michael Kolb31d469b2010-12-09 20:49:54 -0800183 } else {
184 // clear
185 mUrlInput.setText("");
186 }
187 }
188
John Reck034637c2011-08-11 11:34:44 -0700189 @Override
190 public void setFavicon(Bitmap icon) {
191 mFaviconDrawable = mBaseUi.getFaviconDrawable(icon);
192 updateUrlIcon();
193 }
194
John Reckb8b2af82011-05-20 15:58:33 -0700195 void updateUrlIcon() {
John Reck034637c2011-08-11 11:34:44 -0700196 if (mUrlInput.hasFocus()) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800197 mUrlIcon.setImageResource(R.drawable.ic_action_search_normal);
John Reck034637c2011-08-11 11:34:44 -0700198 } else {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700199 if (mFaviconDrawable == null) {
200 mFaviconDrawable = mBaseUi.getFaviconDrawable(null);
John Reck034637c2011-08-11 11:34:44 -0700201 }
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700202 mUrlIcon.setImageDrawable(mFaviconDrawable);
John Reck034637c2011-08-11 11:34:44 -0700203 }
John Reckb8b2af82011-05-20 15:58:33 -0700204 }
205
Michael Kolb11d19782011-03-20 10:17:40 -0700206 @Override
207 protected void setFocusState(boolean focus) {
208 super.setFocusState(focus);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800209 if (focus) {
Michael Kolbde463762011-07-14 15:25:45 -0700210 if (mHideNavButtons) {
211 hideNavButtons();
212 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700213 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800214 mStar.setVisibility(View.GONE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800215 mUrlIcon.setImageResource(R.drawable.ic_action_search_normal);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700216 } else {
Michael Kolbde463762011-07-14 15:25:45 -0700217 if (mHideNavButtons) {
218 showNavButtons();
219 }
George Mount387d45d2011-10-07 15:57:53 -0700220 showHideStar(mUiController.getCurrentTab());
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700221 mSearchButton.setVisibility(View.VISIBLE);
John Reckb8b2af82011-05-20 15:58:33 -0700222 updateUrlIcon();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700223 }
224 }
225
Michael Kolba2b2ba82010-08-04 17:54:03 -0700226 private void stopOrRefresh() {
Michael Kolb1392b832011-09-07 13:02:59 -0700227 if (mUiController == null) return;
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 Kolb66450842011-12-15 15:01:19 -0800231 if (mUiController.getCurrentTopWebView() != null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800232 Tab currTab = mUiController.getTabControl().getCurrentTab();
Michael Kolb66450842011-12-15 15:01:19 -0800233 mUiController.getCurrentTopWebView().reload();
234 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700235 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400236 }
237
Michael Kolbfe251992010-07-08 15:41:55 -0700238 @Override
John Reck0f602f32011-07-07 15:38:43 -0700239 public void onProgressStarted() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700240 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700241 mStopButton.setContentDescription(mStopDescription);
Michael Kolb46f987e2011-04-05 10:39:10 -0700242 }
243
244 @Override
John Reck0f602f32011-07-07 15:38:43 -0700245 public void onProgressStopped() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700246 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700247 mStopButton.setContentDescription(mRefreshDescription);
Michael Kolbfe251992010-07-08 15:41:55 -0700248 }
249
Michael Kolbbae0cb22012-05-29 11:15:27 -0700250 private AnimatorSet mAnimation;
251
Michael Kolbde463762011-07-14 15:25:45 -0700252 private void hideNavButtons() {
Michael Kolbbae0cb22012-05-29 11:15:27 -0700253 if (mBaseUi.blockFocusAnimations()) {
254 mNavButtons.setVisibility(View.GONE);
255 return;
256 }
Michael Kolbde463762011-07-14 15:25:45 -0700257 int awidth = mNavButtons.getMeasuredWidth();
258 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, 0, - awidth);
259 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", mUrlContainer.getLeft(),
260 mUrlContainer.getPaddingLeft());
261 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 1f, 0f);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700262 mAnimation = new AnimatorSet();
263 mAnimation.playTogether(anim1, anim2, anim3);
264 mAnimation.addListener(new AnimatorListenerAdapter() {
Michael Kolbde463762011-07-14 15:25:45 -0700265 @Override
266 public void onAnimationEnd(Animator animation) {
267 mNavButtons.setVisibility(View.GONE);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700268 mAnimation = null;
Michael Kolbde463762011-07-14 15:25:45 -0700269 }
270 });
Michael Kolbbae0cb22012-05-29 11:15:27 -0700271 mAnimation.setDuration(150);
272 mAnimation.start();
Michael Kolbde463762011-07-14 15:25:45 -0700273 }
274
275 private void showNavButtons() {
Michael Kolbbae0cb22012-05-29 11:15:27 -0700276 if (mAnimation != null) {
277 mAnimation.cancel();
278 }
Michael Kolbde463762011-07-14 15:25:45 -0700279 mNavButtons.setVisibility(View.VISIBLE);
Michael Kolb0b129122012-06-04 16:31:58 -0700280 mNavButtons.setTranslationX(0);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700281 if (!mBaseUi.blockFocusAnimations()) {
282 int awidth = mNavButtons.getMeasuredWidth();
283 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons,
284 View.TRANSLATION_X, -awidth, 0);
285 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", 0,
286 awidth);
287 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA,
288 0f, 1f);
289 AnimatorSet combo = new AnimatorSet();
290 combo.playTogether(anim1, anim2, anim3);
291 combo.setDuration(150);
292 combo.start();
293 }
Michael Kolbde463762011-07-14 15:25:45 -0700294 }
295
George Mount387d45d2011-10-07 15:57:53 -0700296 private void showHideStar(Tab tab) {
297 // hide the bookmark star for data URLs
298 if (tab != null && tab.inForeground()) {
299 int starVisibility = View.VISIBLE;
300 String url = tab.getUrl();
301 if (DataUri.isDataUri(url)) {
302 starVisibility = View.GONE;
303 }
304 mStar.setVisibility(starVisibility);
305 }
306 }
307
Michael Kolb0b129122012-06-04 16:31:58 -0700308 @Override
309 public void onStateChanged(int state) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800310 super.onStateChanged(state);
Michael Kolbe721cc32012-06-26 15:26:59 -0700311 mVoiceButton.setVisibility(View.GONE);
Michael Kolb0b129122012-06-04 16:31:58 -0700312 switch(state) {
313 case STATE_NORMAL:
314 mClearButton.setVisibility(View.GONE);
Michael Kolb0b129122012-06-04 16:31:58 -0700315 break;
316 case STATE_HIGHLIGHTED:
317 mClearButton.setVisibility(View.GONE);
Michael Kolbe721cc32012-06-26 15:26:59 -0700318 if ((mUiController != null) && mUiController.supportsVoice()) {
319 mVoiceButton.setVisibility(View.VISIBLE);
320 }
Michael Kolb0b129122012-06-04 16:31:58 -0700321 break;
322 case STATE_EDITED:
323 mClearButton.setVisibility(View.VISIBLE);
Michael Kolb0b129122012-06-04 16:31:58 -0700324 break;
325 }
326 }
327
Leon Scroggins571b3762010-05-26 10:25:01 -0400328}