blob: b9c86e3f9282c0ec4dce9a147d0372275092ba89 [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;
35
Michael Kolbcfa3af52010-12-14 10:36:11 -080036import java.util.List;
37
John Reck0f602f32011-07-07 15:38:43 -070038public class NavigationBarTablet extends NavigationBarBase {
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 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 Kolb31d469b2010-12-09 20:49:54 -0800102 mUrlContainer = findViewById(R.id.urlbar_focused);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700103 mBackButton.setOnClickListener(this);
104 mForwardButton.setOnClickListener(this);
105 mStar.setOnClickListener(this);
106 mAllButton.setOnClickListener(this);
107 mStopButton.setOnClickListener(this);
108 mSearchButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700109 mClearButton.setOnClickListener(this);
Michael Kolb31d469b2010-12-09 20:49:54 -0800110 mUrlInput.setContainer(mUrlContainer);
John Reck0f602f32011-07-07 15:38:43 -0700111 }
112
Michael Kolbde463762011-07-14 15:25:45 -0700113 public void onConfigurationChanged(Configuration config) {
114 super.onConfigurationChanged(config);
115 Resources res = mContext.getResources();
116 mHideNavButtons = res.getBoolean(R.bool.hide_nav_buttons);
117 if (mUrlInput.hasFocus()) {
118 if (mHideNavButtons && (mNavButtons.getVisibility() == View.VISIBLE)) {
119 int aw = mNavButtons.getMeasuredWidth();
120 mNavButtons.setVisibility(View.GONE);
121 mNavButtons.setAlpha(0f);
122 mNavButtons.setTranslationX(-aw);
123 } else if (!mHideNavButtons && (mNavButtons.getVisibility() == View.GONE)) {
124 mNavButtons.setVisibility(View.VISIBLE);
125 mNavButtons.setAlpha(1f);
126 mNavButtons.setTranslationX(0);
127 }
128 }
129 }
130
John Reck0f602f32011-07-07 15:38:43 -0700131 @Override
132 public void setTitleBar(TitleBar titleBar) {
133 super.setTitleBar(titleBar);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800134 setFocusState(false);
Michael Kolb31d469b2010-12-09 20:49:54 -0800135 }
136
Michael Kolb5a72f182011-01-13 20:35:06 -0800137 void updateNavigationState(Tab tab) {
John Reckef654f12011-07-12 16:42:08 -0700138 if (tab != null) {
139 mBackButton.setImageResource(tab.canGoBack()
Michael Kolb5a72f182011-01-13 20:35:06 -0800140 ? R.drawable.ic_back_holo_dark
141 : R.drawable.ic_back_disabled_holo_dark);
John Reckef654f12011-07-12 16:42:08 -0700142 mForwardButton.setImageResource(tab.canGoForward()
Michael Kolb5a72f182011-01-13 20:35:06 -0800143 ? R.drawable.ic_forward_holo_dark
144 : R.drawable.ic_forward_disabled_holo_dark);
145 }
John Reckb8b2af82011-05-20 15:58:33 -0700146 updateUrlIcon();
Michael Kolb5a72f182011-01-13 20:35:06 -0800147 }
148
Michael Kolb31d469b2010-12-09 20:49:54 -0800149 @Override
George Mount387d45d2011-10-07 15:57:53 -0700150 public void onTabDataChanged(Tab tab) {
151 super.onTabDataChanged(tab);
152 showHideStar(tab);
153 }
154
155 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500156 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800157 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500158 }
159
Michael Kolba2b2ba82010-08-04 17:54:03 -0700160 @Override
161 public void onClick(View v) {
Michael Kolbc832e5e2012-03-09 15:08:23 -0800162 if ((mBackButton == v) && (mUiController.getCurrentTab() != null)) {
John Reckef654f12011-07-12 16:42:08 -0700163 mUiController.getCurrentTab().goBack();
Michael Kolbc832e5e2012-03-09 15:08:23 -0800164 } else if ((mForwardButton == v) && (mUiController.getCurrentTab() != null)) {
John Reckef654f12011-07-12 16:42:08 -0700165 mUiController.getCurrentTab().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700166 } else if (mStar == v) {
John Recka60fffa2011-09-06 16:30:29 -0700167 Intent intent = mUiController.createBookmarkCurrentPageIntent(true);
168 if (intent != null) {
169 getContext().startActivity(intent);
170 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700171 } else if (mAllButton == v) {
Michael Kolb315d5022011-10-13 12:47:11 -0700172 mUiController.bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700173 } else if (mSearchButton == v) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700174 mBaseUi.editUrl(true, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700175 } else if (mStopButton == v) {
176 stopOrRefresh();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700177 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800178 clearOrClose();
Michael Kolb11d19782011-03-20 10:17:40 -0700179 } else {
180 super.onClick(v);
Michael Kolbfe251992010-07-08 15:41:55 -0700181 }
182 }
183
Michael Kolb31d469b2010-12-09 20:49:54 -0800184 private void clearOrClose() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000185 if (TextUtils.isEmpty(mUrlInput.getText())) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800186 // close
Michael Kolb7cdc4902011-02-03 17:54:40 -0800187 mUrlInput.clearFocus();
Michael Kolb31d469b2010-12-09 20:49:54 -0800188 } else {
189 // clear
190 mUrlInput.setText("");
191 }
192 }
193
John Reck034637c2011-08-11 11:34:44 -0700194 @Override
195 public void setFavicon(Bitmap icon) {
196 mFaviconDrawable = mBaseUi.getFaviconDrawable(icon);
197 updateUrlIcon();
198 }
199
John Reckb8b2af82011-05-20 15:58:33 -0700200 void updateUrlIcon() {
John Reck034637c2011-08-11 11:34:44 -0700201 if (mUrlInput.hasFocus()) {
202 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
203 } else {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700204 if (mFaviconDrawable == null) {
205 mFaviconDrawable = mBaseUi.getFaviconDrawable(null);
John Reck034637c2011-08-11 11:34:44 -0700206 }
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700207 mUrlIcon.setImageDrawable(mFaviconDrawable);
John Reck034637c2011-08-11 11:34:44 -0700208 }
John Reckb8b2af82011-05-20 15:58:33 -0700209 }
210
Michael Kolb11d19782011-03-20 10:17:40 -0700211 @Override
212 protected void setFocusState(boolean focus) {
213 super.setFocusState(focus);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800214 if (focus) {
Michael Kolbde463762011-07-14 15:25:45 -0700215 if (mHideNavButtons) {
216 hideNavButtons();
217 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700218 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800219 mStar.setVisibility(View.GONE);
220 mClearButton.setVisibility(View.VISIBLE);
Michael Kolbe3524d82011-03-02 14:53:15 -0800221 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700222 } else {
Michael Kolbde463762011-07-14 15:25:45 -0700223 if (mHideNavButtons) {
224 showNavButtons();
225 }
George Mount387d45d2011-10-07 15:57:53 -0700226 showHideStar(mUiController.getCurrentTab());
Michael Kolb31d469b2010-12-09 20:49:54 -0800227 mClearButton.setVisibility(View.GONE);
John Reck0f602f32011-07-07 15:38:43 -0700228 if (mTitleBar.useQuickControls()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800229 mSearchButton.setVisibility(View.GONE);
230 } else {
231 mSearchButton.setVisibility(View.VISIBLE);
232 }
John Reckb8b2af82011-05-20 15:58:33 -0700233 updateUrlIcon();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700234 }
John Reck7e5b8b52011-06-22 13:46:25 -0700235 mUrlContainer.setBackgroundDrawable(focus
236 ? mFocusDrawable : mUnfocusDrawable);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700237 }
238
Michael Kolba2b2ba82010-08-04 17:54:03 -0700239 private void stopOrRefresh() {
Michael Kolb1392b832011-09-07 13:02:59 -0700240 if (mUiController == null) return;
John Reck0f602f32011-07-07 15:38:43 -0700241 if (mTitleBar.isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700242 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700243 } else {
Michael Kolb66450842011-12-15 15:01:19 -0800244 if (mUiController.getCurrentTopWebView() != null) {
245 mUiController.getCurrentTopWebView().reload();
246 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700247 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400248 }
249
Michael Kolbfe251992010-07-08 15:41:55 -0700250 @Override
John Reck0f602f32011-07-07 15:38:43 -0700251 public void onProgressStarted() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700252 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700253 mStopButton.setContentDescription(mStopDescription);
Michael Kolb46f987e2011-04-05 10:39:10 -0700254 }
255
256 @Override
John Reck0f602f32011-07-07 15:38:43 -0700257 public void onProgressStopped() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700258 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700259 mStopButton.setContentDescription(mRefreshDescription);
Michael Kolbfe251992010-07-08 15:41:55 -0700260 }
261
Michael Kolbde463762011-07-14 15:25:45 -0700262 private void hideNavButtons() {
263 int awidth = mNavButtons.getMeasuredWidth();
264 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, 0, - awidth);
265 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", mUrlContainer.getLeft(),
266 mUrlContainer.getPaddingLeft());
267 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 1f, 0f);
268 AnimatorSet combo = new AnimatorSet();
269 combo.playTogether(anim1, anim2, anim3);
270 combo.addListener(new AnimatorListenerAdapter() {
271 @Override
272 public void onAnimationEnd(Animator animation) {
273 mNavButtons.setVisibility(View.GONE);
274 }
275 });
276 combo.setDuration(150);
277 combo.start();
278 }
279
280 private void showNavButtons() {
281 int awidth = mNavButtons.getMeasuredWidth();
282 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, -awidth, 0);
283 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", 0, awidth);
284 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 0f, 1f);
285 AnimatorSet combo = new AnimatorSet();
286 combo.playTogether(anim1, anim2, anim3);
287 mNavButtons.setVisibility(View.VISIBLE);
288 combo.setDuration(150);
289 combo.start();
290 }
291
George Mount387d45d2011-10-07 15:57:53 -0700292 private void showHideStar(Tab tab) {
293 // hide the bookmark star for data URLs
294 if (tab != null && tab.inForeground()) {
295 int starVisibility = View.VISIBLE;
296 String url = tab.getUrl();
297 if (DataUri.isDataUri(url)) {
298 starVisibility = View.GONE;
299 }
300 mStar.setVisibility(starVisibility);
301 }
302 }
303
Leon Scroggins571b3762010-05-26 10:25:01 -0400304}