blob: c22b1a0c5d9a5df37aff8772d4d1066a3da5b449 [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 Kolbcfa3af52010-12-14 10:36:11 -080034import java.util.List;
35
John Reck0f602f32011-07-07 15:38:43 -070036public class NavigationBarTablet extends NavigationBarBase {
Michael Kolb8233fac2010-10-26 16:08:53 -070037
Michael Kolba2b2ba82010-08-04 17:54:03 -070038 private Drawable mStopDrawable;
39 private Drawable mReloadDrawable;
Michael Kolb30adae62011-07-29 13:37:05 -070040 private String mStopDescription;
41 private String mRefreshDescription;
Michael Kolbc7485ae2010-09-03 10:10:58 -070042
Michael Kolb11d19782011-03-20 10:17:40 -070043 private View mUrlContainer;
Michael Kolb5a72f182011-01-13 20:35:06 -080044 private ImageButton mBackButton;
45 private ImageButton mForwardButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080046 private ImageView mStar;
Michael Kolbe3524d82011-03-02 14:53:15 -080047 private ImageView mUrlIcon;
48 private ImageView mSearchButton;
Michael Kolb513286f2010-09-09 12:55:12 -070049 private View mGoButton;
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 Kolbe3524d82011-03-02 14:53:15 -080053 private ImageView mVoiceSearch;
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 Kolb513286f2010-09-09 12:55:12 -0700101 mGoButton = findViewById(R.id.go);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700102 mClearButton = findViewById(R.id.clear);
Michael Kolbe3524d82011-03-02 14:53:15 -0800103 mVoiceSearch = (ImageView) findViewById(R.id.voicesearch);
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 Kolb513286f2010-09-09 12:55:12 -0700111 mGoButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700112 mClearButton.setOnClickListener(this);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800113 mVoiceSearch.setOnClickListener(this);
Michael Kolb31d469b2010-12-09 20:49:54 -0800114 mUrlInput.setContainer(mUrlContainer);
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);
119 Resources res = mContext.getResources();
120 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
Leon Scroggins4cd97792010-12-03 15:31:56 -0500154 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800155 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500156 }
157
Michael Kolba2b2ba82010-08-04 17:54:03 -0700158 @Override
159 public void onClick(View v) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800160 if (mBackButton == v) {
John Reckef654f12011-07-12 16:42:08 -0700161 mUiController.getCurrentTab().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700162 } else if (mForwardButton == v) {
John Reckef654f12011-07-12 16:42:08 -0700163 mUiController.getCurrentTab().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700164 } else if (mStar == v) {
John Recka60fffa2011-09-06 16:30:29 -0700165 Intent intent = mUiController.createBookmarkCurrentPageIntent(true);
166 if (intent != null) {
167 getContext().startActivity(intent);
168 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700169 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700170 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700171 } else if (mSearchButton == v) {
John Reck0f602f32011-07-07 15:38:43 -0700172 mBaseUi.editUrl(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700173 } else if (mStopButton == v) {
174 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700175 } else if (mGoButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800176 if (!TextUtils.isEmpty(mUrlInput.getText())) {
177 onAction(mUrlInput.getText().toString(), null,
Michael Kolb257cc2c2010-12-09 09:45:52 -0800178 UrlInputView.TYPED);
Michael Kolb1ce78132010-09-23 15:50:53 -0700179 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700180 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800181 clearOrClose();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800182 } else if (mVoiceSearch == v) {
183 mUiController.startVoiceSearch();
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 Kamath5119edd2011-02-23 15:49:17 +0000190 if (TextUtils.isEmpty(mUrlInput.getUserText())) {
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 {
209 if (mInVoiceMode) {
210 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
211 } else {
212 if (mFaviconDrawable == null) {
213 mFaviconDrawable = mBaseUi.getFaviconDrawable(null);
214 }
215 mUrlIcon.setImageDrawable(mFaviconDrawable);
216 }
217 }
John Reckb8b2af82011-05-20 15:58:33 -0700218 }
219
Michael Kolb11d19782011-03-20 10:17:40 -0700220 @Override
221 protected void setFocusState(boolean focus) {
222 super.setFocusState(focus);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800223 if (focus) {
Michael Kolbde463762011-07-14 15:25:45 -0700224 if (mHideNavButtons) {
225 hideNavButtons();
226 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700227 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800228 mStar.setVisibility(View.GONE);
229 mClearButton.setVisibility(View.VISIBLE);
Michael Kolbe3524d82011-03-02 14:53:15 -0800230 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
Michael Kolb60a68f52011-02-24 11:02:52 -0800231 updateSearchMode(false);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700232 } else {
Michael Kolbde463762011-07-14 15:25:45 -0700233 if (mHideNavButtons) {
234 showNavButtons();
235 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700236 mGoButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800237 mVoiceSearch.setVisibility(View.GONE);
238 mStar.setVisibility(View.VISIBLE);
239 mClearButton.setVisibility(View.GONE);
John Reck0f602f32011-07-07 15:38:43 -0700240 if (mTitleBar.useQuickControls()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800241 mSearchButton.setVisibility(View.GONE);
242 } else {
243 mSearchButton.setVisibility(View.VISIBLE);
244 }
John Reckb8b2af82011-05-20 15:58:33 -0700245 updateUrlIcon();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700246 }
John Reck7e5b8b52011-06-22 13:46:25 -0700247 mUrlContainer.setBackgroundDrawable(focus
248 ? mFocusDrawable : mUnfocusDrawable);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700249 }
250
Michael Kolba2b2ba82010-08-04 17:54:03 -0700251 private void stopOrRefresh() {
Michael Kolb1392b832011-09-07 13:02:59 -0700252 if (mUiController == null) return;
John Reck0f602f32011-07-07 15:38:43 -0700253 if (mTitleBar.isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700254 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700255 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700256 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700257 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400258 }
259
Michael Kolbfe251992010-07-08 15:41:55 -0700260 @Override
John Reck0f602f32011-07-07 15:38:43 -0700261 public void onProgressStarted() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700262 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700263 mStopButton.setContentDescription(mStopDescription);
Michael Kolb46f987e2011-04-05 10:39:10 -0700264 }
265
266 @Override
John Reck0f602f32011-07-07 15:38:43 -0700267 public void onProgressStopped() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700268 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700269 mStopButton.setContentDescription(mRefreshDescription);
Michael Kolbfe251992010-07-08 15:41:55 -0700270 }
271
Michael Kolb11d19782011-03-20 10:17:40 -0700272 protected void updateSearchMode(boolean userEdited) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000273 setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText()));
Michael Kolb31d469b2010-12-09 20:49:54 -0800274 }
275
Michael Kolb11d19782011-03-20 10:17:40 -0700276 @Override
277 protected void setSearchMode(boolean voiceSearchEnabled) {
Michael Kolb793e05e2011-01-11 15:17:31 -0800278 boolean showvoicebutton = voiceSearchEnabled &&
Michael Kolb736990c2011-03-20 10:01:20 -0700279 mUiController.supportsVoiceSearch();
Michael Kolb793e05e2011-01-11 15:17:31 -0800280 mVoiceSearch.setVisibility(showvoicebutton ? View.VISIBLE :
Michael Kolb31d469b2010-12-09 20:49:54 -0800281 View.GONE);
282 mGoButton.setVisibility(voiceSearchEnabled ? View.GONE :
283 View.VISIBLE);
284 }
285
Michael Kolbfe251992010-07-08 15:41:55 -0700286 @Override
Michael Kolbcfa3af52010-12-14 10:36:11 -0800287 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
Michael Kolb11d19782011-03-20 10:17:40 -0700288 super.setInVoiceMode(voicemode, voiceResults);
Michael Kolb3aaef252011-03-09 18:13:56 -0800289 if (voicemode) {
290 mUrlIcon.setImageDrawable(mSearchButton.getDrawable());
291 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800292 }
293
Michael Kolbde463762011-07-14 15:25:45 -0700294 private void hideNavButtons() {
295 int awidth = mNavButtons.getMeasuredWidth();
296 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, 0, - awidth);
297 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", mUrlContainer.getLeft(),
298 mUrlContainer.getPaddingLeft());
299 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 1f, 0f);
300 AnimatorSet combo = new AnimatorSet();
301 combo.playTogether(anim1, anim2, anim3);
302 combo.addListener(new AnimatorListenerAdapter() {
303 @Override
304 public void onAnimationEnd(Animator animation) {
305 mNavButtons.setVisibility(View.GONE);
306 }
307 });
308 combo.setDuration(150);
309 combo.start();
310 }
311
312 private void showNavButtons() {
313 int awidth = mNavButtons.getMeasuredWidth();
314 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, -awidth, 0);
315 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", 0, awidth);
316 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 0f, 1f);
317 AnimatorSet combo = new AnimatorSet();
318 combo.playTogether(anim1, anim2, anim3);
319 mNavButtons.setVisibility(View.VISIBLE);
320 combo.setDuration(150);
321 combo.start();
322 }
323
Leon Scroggins571b3762010-05-26 10:25:01 -0400324}