blob: ebe40eaa4212fde30d795e278c786ec1b7b125b3 [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.R;
35import com.android.browser.UI.ComboViews;
36import com.android.browser.UrlInputView.StateListener;
Michael Kolb315d5022011-10-13 12:47:11 -070037
Michael Kolb0b129122012-06-04 16:31:58 -070038public class NavigationBarTablet extends NavigationBarBase implements StateListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070039
Michael Kolba2b2ba82010-08-04 17:54:03 -070040 private Drawable mStopDrawable;
41 private Drawable mReloadDrawable;
Michael Kolb30adae62011-07-29 13:37:05 -070042 private String mStopDescription;
43 private String mRefreshDescription;
Michael Kolbc7485ae2010-09-03 10:10:58 -070044
Michael Kolb11d19782011-03-20 10:17:40 -070045 private View mUrlContainer;
Michael Kolb5a72f182011-01-13 20:35:06 -080046 private ImageButton mBackButton;
47 private ImageButton mForwardButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080048 private ImageView mStar;
Michael Kolbe3524d82011-03-02 14:53:15 -080049 private ImageView mUrlIcon;
50 private ImageView mSearchButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070051 private ImageView mStopButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070052 private View mAllButton;
Michael Kolbb7b115e2010-09-25 16:59:37 -070053 private View mClearButton;
Michael Kolb0b129122012-06-04 16:31:58 -070054 private View mVoiceButton;
Michael Kolbde463762011-07-14 15:25:45 -070055 private View mNavButtons;
Michael Kolbcfa3af52010-12-14 10:36:11 -080056 private Drawable mFocusDrawable;
57 private Drawable mUnfocusDrawable;
Michael Kolbde463762011-07-14 15:25:45 -070058 private boolean mHideNavButtons;
John Reck034637c2011-08-11 11:34:44 -070059 private Drawable mFaviconDrawable;
Michael Kolb81b6f832010-12-12 12:44:27 -080060
John Reck0f602f32011-07-07 15:38:43 -070061 public NavigationBarTablet(Context context) {
62 super(context);
63 init(context);
64 }
65
66 public NavigationBarTablet(Context context, AttributeSet attrs) {
67 super(context, attrs);
68 init(context);
69 }
70
71 public NavigationBarTablet(Context context, AttributeSet attrs, int defStyle) {
72 super(context, attrs, defStyle);
73 init(context);
74 }
75
76 private void init(Context context) {
77 Resources resources = context.getResources();
Michael Kolb5a72f182011-01-13 20:35:06 -080078 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_holo_dark);
79 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb30adae62011-07-29 13:37:05 -070080 mStopDescription = resources.getString(R.string.accessibility_button_stop);
81 mRefreshDescription = resources.getString(R.string.accessibility_button_refresh);
Michael Kolbcfa3af52010-12-14 10:36:11 -080082 mFocusDrawable = resources.getDrawable(
83 R.drawable.textfield_active_holo_dark);
84 mUnfocusDrawable = resources.getDrawable(
85 R.drawable.textfield_default_holo_dark);
Michael Kolbde463762011-07-14 15:25:45 -070086 mHideNavButtons = resources.getBoolean(R.bool.hide_nav_buttons);
Michael Kolbfe251992010-07-08 15:41:55 -070087 }
Leon Scroggins571b3762010-05-26 10:25:01 -040088
Michael Kolb7cdc4902011-02-03 17:54:40 -080089 @Override
John Reck0f602f32011-07-07 15:38:43 -070090 protected void onFinishInflate() {
91 super.onFinishInflate();
Michael Kolbfe251992010-07-08 15:41:55 -070092 mAllButton = findViewById(R.id.all_btn);
93 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040094 // back/forward. Probably should be done inside onPageStarted.
Michael Kolbde463762011-07-14 15:25:45 -070095 mNavButtons = findViewById(R.id.navbuttons);
Michael Kolb5a72f182011-01-13 20:35:06 -080096 mBackButton = (ImageButton) findViewById(R.id.back);
97 mForwardButton = (ImageButton) findViewById(R.id.forward);
Michael Kolbe3524d82011-03-02 14:53:15 -080098 mUrlIcon = (ImageView) findViewById(R.id.url_icon);
Michael Kolb31d469b2010-12-09 20:49:54 -080099 mStar = (ImageView) findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700100 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolbe3524d82011-03-02 14:53:15 -0800101 mSearchButton = (ImageView) findViewById(R.id.search);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700102 mClearButton = findViewById(R.id.clear);
Michael Kolb0b129122012-06-04 16:31:58 -0700103 mVoiceButton = findViewById(R.id.voice);
Michael Kolb31d469b2010-12-09 20:49:54 -0800104 mUrlContainer = findViewById(R.id.urlbar_focused);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700105 mBackButton.setOnClickListener(this);
106 mForwardButton.setOnClickListener(this);
107 mStar.setOnClickListener(this);
108 mAllButton.setOnClickListener(this);
109 mStopButton.setOnClickListener(this);
110 mSearchButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700111 mClearButton.setOnClickListener(this);
Michael Kolb0b129122012-06-04 16:31:58 -0700112 mVoiceButton.setOnClickListener(this);
Michael Kolb31d469b2010-12-09 20:49:54 -0800113 mUrlInput.setContainer(mUrlContainer);
Michael Kolb0b129122012-06-04 16:31:58 -0700114 mUrlInput.setStateListener(this);
John Reck0f602f32011-07-07 15:38:43 -0700115 }
116
Michael Kolbde463762011-07-14 15:25:45 -0700117 public void onConfigurationChanged(Configuration config) {
118 super.onConfigurationChanged(config);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800119 Resources res = getContext().getResources();
Michael Kolbde463762011-07-14 15:25:45 -0700120 mHideNavButtons = res.getBoolean(R.bool.hide_nav_buttons);
121 if (mUrlInput.hasFocus()) {
122 if (mHideNavButtons && (mNavButtons.getVisibility() == View.VISIBLE)) {
123 int aw = mNavButtons.getMeasuredWidth();
124 mNavButtons.setVisibility(View.GONE);
125 mNavButtons.setAlpha(0f);
126 mNavButtons.setTranslationX(-aw);
127 } else if (!mHideNavButtons && (mNavButtons.getVisibility() == View.GONE)) {
128 mNavButtons.setVisibility(View.VISIBLE);
129 mNavButtons.setAlpha(1f);
130 mNavButtons.setTranslationX(0);
131 }
132 }
133 }
134
John Reck0f602f32011-07-07 15:38:43 -0700135 @Override
136 public void setTitleBar(TitleBar titleBar) {
137 super.setTitleBar(titleBar);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800138 setFocusState(false);
Michael Kolb31d469b2010-12-09 20:49:54 -0800139 }
140
Michael Kolb5a72f182011-01-13 20:35:06 -0800141 void updateNavigationState(Tab tab) {
John Reckef654f12011-07-12 16:42:08 -0700142 if (tab != null) {
143 mBackButton.setImageResource(tab.canGoBack()
Michael Kolb5a72f182011-01-13 20:35:06 -0800144 ? R.drawable.ic_back_holo_dark
145 : R.drawable.ic_back_disabled_holo_dark);
John Reckef654f12011-07-12 16:42:08 -0700146 mForwardButton.setImageResource(tab.canGoForward()
Michael Kolb5a72f182011-01-13 20:35:06 -0800147 ? R.drawable.ic_forward_holo_dark
148 : R.drawable.ic_forward_disabled_holo_dark);
149 }
John Reckb8b2af82011-05-20 15:58:33 -0700150 updateUrlIcon();
Michael Kolb5a72f182011-01-13 20:35:06 -0800151 }
152
Michael Kolb31d469b2010-12-09 20:49:54 -0800153 @Override
George Mount387d45d2011-10-07 15:57:53 -0700154 public void onTabDataChanged(Tab tab) {
155 super.onTabDataChanged(tab);
156 showHideStar(tab);
157 }
158
159 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500160 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800161 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500162 }
163
Michael Kolba2b2ba82010-08-04 17:54:03 -0700164 @Override
165 public void onClick(View v) {
Michael Kolbc832e5e2012-03-09 15:08:23 -0800166 if ((mBackButton == v) && (mUiController.getCurrentTab() != null)) {
John Reckef654f12011-07-12 16:42:08 -0700167 mUiController.getCurrentTab().goBack();
Michael Kolbc832e5e2012-03-09 15:08:23 -0800168 } else if ((mForwardButton == v) && (mUiController.getCurrentTab() != null)) {
John Reckef654f12011-07-12 16:42:08 -0700169 mUiController.getCurrentTab().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700170 } else if (mStar == v) {
John Recka60fffa2011-09-06 16:30:29 -0700171 Intent intent = mUiController.createBookmarkCurrentPageIntent(true);
172 if (intent != null) {
173 getContext().startActivity(intent);
174 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700175 } else if (mAllButton == v) {
Michael Kolb315d5022011-10-13 12:47:11 -0700176 mUiController.bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700177 } else if (mSearchButton == v) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700178 mBaseUi.editUrl(true, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700179 } else if (mStopButton == v) {
180 stopOrRefresh();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700181 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800182 clearOrClose();
Michael Kolb0b129122012-06-04 16:31:58 -0700183 } else if (mVoiceButton == v) {
184 mUiController.startVoiceRecognizer();
Michael Kolb11d19782011-03-20 10:17:40 -0700185 } else {
186 super.onClick(v);
Michael Kolbfe251992010-07-08 15:41:55 -0700187 }
188 }
189
Michael Kolb31d469b2010-12-09 20:49:54 -0800190 private void clearOrClose() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000191 if (TextUtils.isEmpty(mUrlInput.getText())) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800192 // close
Michael Kolb7cdc4902011-02-03 17:54:40 -0800193 mUrlInput.clearFocus();
Michael Kolb31d469b2010-12-09 20:49:54 -0800194 } else {
195 // clear
196 mUrlInput.setText("");
197 }
198 }
199
John Reck034637c2011-08-11 11:34:44 -0700200 @Override
201 public void setFavicon(Bitmap icon) {
202 mFaviconDrawable = mBaseUi.getFaviconDrawable(icon);
203 updateUrlIcon();
204 }
205
John Reckb8b2af82011-05-20 15:58:33 -0700206 void updateUrlIcon() {
John Reck034637c2011-08-11 11:34:44 -0700207 if (mUrlInput.hasFocus()) {
208 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
209 } else {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700210 if (mFaviconDrawable == null) {
211 mFaviconDrawable = mBaseUi.getFaviconDrawable(null);
John Reck034637c2011-08-11 11:34:44 -0700212 }
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700213 mUrlIcon.setImageDrawable(mFaviconDrawable);
John Reck034637c2011-08-11 11:34:44 -0700214 }
John Reckb8b2af82011-05-20 15:58:33 -0700215 }
216
Michael Kolb11d19782011-03-20 10:17:40 -0700217 @Override
218 protected void setFocusState(boolean focus) {
219 super.setFocusState(focus);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800220 if (focus) {
Michael Kolbde463762011-07-14 15:25:45 -0700221 if (mHideNavButtons) {
222 hideNavButtons();
223 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700224 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800225 mStar.setVisibility(View.GONE);
Michael Kolbe3524d82011-03-02 14:53:15 -0800226 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700227 } else {
Michael Kolbde463762011-07-14 15:25:45 -0700228 if (mHideNavButtons) {
229 showNavButtons();
230 }
George Mount387d45d2011-10-07 15:57:53 -0700231 showHideStar(mUiController.getCurrentTab());
John Reck0f602f32011-07-07 15:38:43 -0700232 if (mTitleBar.useQuickControls()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800233 mSearchButton.setVisibility(View.GONE);
234 } else {
235 mSearchButton.setVisibility(View.VISIBLE);
236 }
John Reckb8b2af82011-05-20 15:58:33 -0700237 updateUrlIcon();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700238 }
John Reck7e5b8b52011-06-22 13:46:25 -0700239 mUrlContainer.setBackgroundDrawable(focus
240 ? mFocusDrawable : mUnfocusDrawable);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700241 }
242
Michael Kolba2b2ba82010-08-04 17:54:03 -0700243 private void stopOrRefresh() {
Michael Kolb1392b832011-09-07 13:02:59 -0700244 if (mUiController == null) return;
John Reck0f602f32011-07-07 15:38:43 -0700245 if (mTitleBar.isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700246 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700247 } else {
Michael Kolb66450842011-12-15 15:01:19 -0800248 if (mUiController.getCurrentTopWebView() != null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800249 Tab currTab = mUiController.getTabControl().getCurrentTab();
250 if (currTab.hasCrashed) {
251 currTab.replaceCrashView(mUiController.getCurrentTopWebView(),
252 currTab.getViewContainer());
253 }
Michael Kolb66450842011-12-15 15:01:19 -0800254 mUiController.getCurrentTopWebView().reload();
255 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700256 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400257 }
258
Michael Kolbfe251992010-07-08 15:41:55 -0700259 @Override
John Reck0f602f32011-07-07 15:38:43 -0700260 public void onProgressStarted() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700261 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700262 mStopButton.setContentDescription(mStopDescription);
Michael Kolb46f987e2011-04-05 10:39:10 -0700263 }
264
265 @Override
John Reck0f602f32011-07-07 15:38:43 -0700266 public void onProgressStopped() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700267 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700268 mStopButton.setContentDescription(mRefreshDescription);
Michael Kolbfe251992010-07-08 15:41:55 -0700269 }
270
Michael Kolbbae0cb22012-05-29 11:15:27 -0700271 private AnimatorSet mAnimation;
272
Michael Kolbde463762011-07-14 15:25:45 -0700273 private void hideNavButtons() {
Michael Kolbbae0cb22012-05-29 11:15:27 -0700274 if (mBaseUi.blockFocusAnimations()) {
275 mNavButtons.setVisibility(View.GONE);
276 return;
277 }
Michael Kolbde463762011-07-14 15:25:45 -0700278 int awidth = mNavButtons.getMeasuredWidth();
279 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, 0, - awidth);
280 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", mUrlContainer.getLeft(),
281 mUrlContainer.getPaddingLeft());
282 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 1f, 0f);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700283 mAnimation = new AnimatorSet();
284 mAnimation.playTogether(anim1, anim2, anim3);
285 mAnimation.addListener(new AnimatorListenerAdapter() {
Michael Kolbde463762011-07-14 15:25:45 -0700286 @Override
287 public void onAnimationEnd(Animator animation) {
288 mNavButtons.setVisibility(View.GONE);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700289 mAnimation = null;
Michael Kolbde463762011-07-14 15:25:45 -0700290 }
291 });
Michael Kolbbae0cb22012-05-29 11:15:27 -0700292 mAnimation.setDuration(150);
293 mAnimation.start();
Michael Kolbde463762011-07-14 15:25:45 -0700294 }
295
296 private void showNavButtons() {
Michael Kolbbae0cb22012-05-29 11:15:27 -0700297 if (mAnimation != null) {
298 mAnimation.cancel();
299 }
Michael Kolbde463762011-07-14 15:25:45 -0700300 mNavButtons.setVisibility(View.VISIBLE);
Michael Kolb0b129122012-06-04 16:31:58 -0700301 mNavButtons.setTranslationX(0);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700302 if (!mBaseUi.blockFocusAnimations()) {
303 int awidth = mNavButtons.getMeasuredWidth();
304 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons,
305 View.TRANSLATION_X, -awidth, 0);
306 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", 0,
307 awidth);
308 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA,
309 0f, 1f);
310 AnimatorSet combo = new AnimatorSet();
311 combo.playTogether(anim1, anim2, anim3);
312 combo.setDuration(150);
313 combo.start();
314 }
Michael Kolbde463762011-07-14 15:25:45 -0700315 }
316
George Mount387d45d2011-10-07 15:57:53 -0700317 private void showHideStar(Tab tab) {
318 // hide the bookmark star for data URLs
319 if (tab != null && tab.inForeground()) {
320 int starVisibility = View.VISIBLE;
321 String url = tab.getUrl();
322 if (DataUri.isDataUri(url)) {
323 starVisibility = View.GONE;
324 }
325 mStar.setVisibility(starVisibility);
326 }
327 }
328
Michael Kolb0b129122012-06-04 16:31:58 -0700329 @Override
330 public void onStateChanged(int state) {
Michael Kolbe721cc32012-06-26 15:26:59 -0700331 mVoiceButton.setVisibility(View.GONE);
Michael Kolb0b129122012-06-04 16:31:58 -0700332 switch(state) {
333 case STATE_NORMAL:
334 mClearButton.setVisibility(View.GONE);
Michael Kolb0b129122012-06-04 16:31:58 -0700335 break;
336 case STATE_HIGHLIGHTED:
337 mClearButton.setVisibility(View.GONE);
Michael Kolbe721cc32012-06-26 15:26:59 -0700338 if ((mUiController != null) && mUiController.supportsVoice()) {
339 mVoiceButton.setVisibility(View.VISIBLE);
340 }
Michael Kolb0b129122012-06-04 16:31:58 -0700341 break;
342 case STATE_EDITED:
343 mClearButton.setVisibility(View.VISIBLE);
Michael Kolb0b129122012-06-04 16:31:58 -0700344 break;
345 }
346 }
347
Leon Scroggins571b3762010-05-26 10:25:01 -0400348}