blob: 17f7398e3a5deefa06c3d3e7c7540ae3c03f8ce2 [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());
137 mForwardButton.setEnabled(tab.canGoBack());
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();
233 if (currTab.hasCrashed) {
234 currTab.replaceCrashView(mUiController.getCurrentTopWebView(),
235 currTab.getViewContainer());
236 }
Michael Kolb66450842011-12-15 15:01:19 -0800237 mUiController.getCurrentTopWebView().reload();
238 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700239 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400240 }
241
Michael Kolbfe251992010-07-08 15:41:55 -0700242 @Override
John Reck0f602f32011-07-07 15:38:43 -0700243 public void onProgressStarted() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700244 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700245 mStopButton.setContentDescription(mStopDescription);
Michael Kolb46f987e2011-04-05 10:39:10 -0700246 }
247
248 @Override
John Reck0f602f32011-07-07 15:38:43 -0700249 public void onProgressStopped() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700250 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700251 mStopButton.setContentDescription(mRefreshDescription);
Michael Kolbfe251992010-07-08 15:41:55 -0700252 }
253
Michael Kolbbae0cb22012-05-29 11:15:27 -0700254 private AnimatorSet mAnimation;
255
Michael Kolbde463762011-07-14 15:25:45 -0700256 private void hideNavButtons() {
Michael Kolbbae0cb22012-05-29 11:15:27 -0700257 if (mBaseUi.blockFocusAnimations()) {
258 mNavButtons.setVisibility(View.GONE);
259 return;
260 }
Michael Kolbde463762011-07-14 15:25:45 -0700261 int awidth = mNavButtons.getMeasuredWidth();
262 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, 0, - awidth);
263 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", mUrlContainer.getLeft(),
264 mUrlContainer.getPaddingLeft());
265 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 1f, 0f);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700266 mAnimation = new AnimatorSet();
267 mAnimation.playTogether(anim1, anim2, anim3);
268 mAnimation.addListener(new AnimatorListenerAdapter() {
Michael Kolbde463762011-07-14 15:25:45 -0700269 @Override
270 public void onAnimationEnd(Animator animation) {
271 mNavButtons.setVisibility(View.GONE);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700272 mAnimation = null;
Michael Kolbde463762011-07-14 15:25:45 -0700273 }
274 });
Michael Kolbbae0cb22012-05-29 11:15:27 -0700275 mAnimation.setDuration(150);
276 mAnimation.start();
Michael Kolbde463762011-07-14 15:25:45 -0700277 }
278
279 private void showNavButtons() {
Michael Kolbbae0cb22012-05-29 11:15:27 -0700280 if (mAnimation != null) {
281 mAnimation.cancel();
282 }
Michael Kolbde463762011-07-14 15:25:45 -0700283 mNavButtons.setVisibility(View.VISIBLE);
Michael Kolb0b129122012-06-04 16:31:58 -0700284 mNavButtons.setTranslationX(0);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700285 if (!mBaseUi.blockFocusAnimations()) {
286 int awidth = mNavButtons.getMeasuredWidth();
287 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons,
288 View.TRANSLATION_X, -awidth, 0);
289 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", 0,
290 awidth);
291 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA,
292 0f, 1f);
293 AnimatorSet combo = new AnimatorSet();
294 combo.playTogether(anim1, anim2, anim3);
295 combo.setDuration(150);
296 combo.start();
297 }
Michael Kolbde463762011-07-14 15:25:45 -0700298 }
299
George Mount387d45d2011-10-07 15:57:53 -0700300 private void showHideStar(Tab tab) {
301 // hide the bookmark star for data URLs
302 if (tab != null && tab.inForeground()) {
303 int starVisibility = View.VISIBLE;
304 String url = tab.getUrl();
305 if (DataUri.isDataUri(url)) {
306 starVisibility = View.GONE;
307 }
308 mStar.setVisibility(starVisibility);
309 }
310 }
311
Michael Kolb0b129122012-06-04 16:31:58 -0700312 @Override
313 public void onStateChanged(int state) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800314 super.onStateChanged(state);
Michael Kolbe721cc32012-06-26 15:26:59 -0700315 mVoiceButton.setVisibility(View.GONE);
Michael Kolb0b129122012-06-04 16:31:58 -0700316 switch(state) {
317 case STATE_NORMAL:
318 mClearButton.setVisibility(View.GONE);
Michael Kolb0b129122012-06-04 16:31:58 -0700319 break;
320 case STATE_HIGHLIGHTED:
321 mClearButton.setVisibility(View.GONE);
Michael Kolbe721cc32012-06-26 15:26:59 -0700322 if ((mUiController != null) && mUiController.supportsVoice()) {
323 mVoiceButton.setVisibility(View.VISIBLE);
324 }
Michael Kolb0b129122012-06-04 16:31:58 -0700325 break;
326 case STATE_EDITED:
327 mClearButton.setVisibility(View.VISIBLE);
Michael Kolb0b129122012-06-04 16:31:58 -0700328 break;
329 }
330 }
331
Leon Scroggins571b3762010-05-26 10:25:01 -0400332}