blob: be3a9aba86ee2f76d67faeb739ffdb9cc1a1ee8a [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
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 Kolb7cdc4902011-02-03 17:54:40 -0800166 if (mBackButton == v) {
John Reckef654f12011-07-12 16:42:08 -0700167 mUiController.getCurrentTab().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700168 } else if (mForwardButton == v) {
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 Kolb8233fac2010-10-26 16:08:53 -0700176 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700177 } else if (mSearchButton == v) {
John Reck0f602f32011-07-07 15:38:43 -0700178 mBaseUi.editUrl(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700179 } else if (mStopButton == v) {
180 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700181 } else if (mGoButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800182 if (!TextUtils.isEmpty(mUrlInput.getText())) {
183 onAction(mUrlInput.getText().toString(), null,
Michael Kolb257cc2c2010-12-09 09:45:52 -0800184 UrlInputView.TYPED);
Michael Kolb1ce78132010-09-23 15:50:53 -0700185 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700186 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800187 clearOrClose();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800188 } else if (mVoiceSearch == v) {
189 mUiController.startVoiceSearch();
Michael Kolb11d19782011-03-20 10:17:40 -0700190 } else {
191 super.onClick(v);
Michael Kolbfe251992010-07-08 15:41:55 -0700192 }
193 }
194
Michael Kolb31d469b2010-12-09 20:49:54 -0800195 private void clearOrClose() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000196 if (TextUtils.isEmpty(mUrlInput.getUserText())) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800197 // close
Michael Kolb7cdc4902011-02-03 17:54:40 -0800198 mUrlInput.clearFocus();
Michael Kolb31d469b2010-12-09 20:49:54 -0800199 } else {
200 // clear
201 mUrlInput.setText("");
202 }
203 }
204
John Reck034637c2011-08-11 11:34:44 -0700205 @Override
206 public void setFavicon(Bitmap icon) {
207 mFaviconDrawable = mBaseUi.getFaviconDrawable(icon);
208 updateUrlIcon();
209 }
210
John Reckb8b2af82011-05-20 15:58:33 -0700211 void updateUrlIcon() {
John Reck034637c2011-08-11 11:34:44 -0700212 if (mUrlInput.hasFocus()) {
213 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
214 } else {
215 if (mInVoiceMode) {
216 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
217 } else {
218 if (mFaviconDrawable == null) {
219 mFaviconDrawable = mBaseUi.getFaviconDrawable(null);
220 }
221 mUrlIcon.setImageDrawable(mFaviconDrawable);
222 }
223 }
John Reckb8b2af82011-05-20 15:58:33 -0700224 }
225
Michael Kolb11d19782011-03-20 10:17:40 -0700226 @Override
227 protected void setFocusState(boolean focus) {
228 super.setFocusState(focus);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800229 if (focus) {
Michael Kolbde463762011-07-14 15:25:45 -0700230 if (mHideNavButtons) {
231 hideNavButtons();
232 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700233 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800234 mStar.setVisibility(View.GONE);
235 mClearButton.setVisibility(View.VISIBLE);
Michael Kolbe3524d82011-03-02 14:53:15 -0800236 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
Michael Kolb60a68f52011-02-24 11:02:52 -0800237 updateSearchMode(false);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700238 } else {
Michael Kolbde463762011-07-14 15:25:45 -0700239 if (mHideNavButtons) {
240 showNavButtons();
241 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700242 mGoButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800243 mVoiceSearch.setVisibility(View.GONE);
George Mount387d45d2011-10-07 15:57:53 -0700244 showHideStar(mUiController.getCurrentTab());
Michael Kolb31d469b2010-12-09 20:49:54 -0800245 mClearButton.setVisibility(View.GONE);
John Reck0f602f32011-07-07 15:38:43 -0700246 if (mTitleBar.useQuickControls()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800247 mSearchButton.setVisibility(View.GONE);
248 } else {
249 mSearchButton.setVisibility(View.VISIBLE);
250 }
John Reckb8b2af82011-05-20 15:58:33 -0700251 updateUrlIcon();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700252 }
John Reck7e5b8b52011-06-22 13:46:25 -0700253 mUrlContainer.setBackgroundDrawable(focus
254 ? mFocusDrawable : mUnfocusDrawable);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700255 }
256
Michael Kolba2b2ba82010-08-04 17:54:03 -0700257 private void stopOrRefresh() {
Michael Kolb1392b832011-09-07 13:02:59 -0700258 if (mUiController == null) return;
John Reck0f602f32011-07-07 15:38:43 -0700259 if (mTitleBar.isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700261 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700263 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400264 }
265
Michael Kolbfe251992010-07-08 15:41:55 -0700266 @Override
John Reck0f602f32011-07-07 15:38:43 -0700267 public void onProgressStarted() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700268 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700269 mStopButton.setContentDescription(mStopDescription);
Michael Kolb46f987e2011-04-05 10:39:10 -0700270 }
271
272 @Override
John Reck0f602f32011-07-07 15:38:43 -0700273 public void onProgressStopped() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700274 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700275 mStopButton.setContentDescription(mRefreshDescription);
Michael Kolbfe251992010-07-08 15:41:55 -0700276 }
277
Michael Kolb11d19782011-03-20 10:17:40 -0700278 protected void updateSearchMode(boolean userEdited) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000279 setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText()));
Michael Kolb31d469b2010-12-09 20:49:54 -0800280 }
281
Michael Kolb11d19782011-03-20 10:17:40 -0700282 @Override
283 protected void setSearchMode(boolean voiceSearchEnabled) {
Michael Kolb793e05e2011-01-11 15:17:31 -0800284 boolean showvoicebutton = voiceSearchEnabled &&
Michael Kolb736990c2011-03-20 10:01:20 -0700285 mUiController.supportsVoiceSearch();
Michael Kolb793e05e2011-01-11 15:17:31 -0800286 mVoiceSearch.setVisibility(showvoicebutton ? View.VISIBLE :
Michael Kolb31d469b2010-12-09 20:49:54 -0800287 View.GONE);
288 mGoButton.setVisibility(voiceSearchEnabled ? View.GONE :
289 View.VISIBLE);
290 }
291
Michael Kolbfe251992010-07-08 15:41:55 -0700292 @Override
Michael Kolbcfa3af52010-12-14 10:36:11 -0800293 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
Michael Kolb11d19782011-03-20 10:17:40 -0700294 super.setInVoiceMode(voicemode, voiceResults);
Michael Kolb3aaef252011-03-09 18:13:56 -0800295 if (voicemode) {
296 mUrlIcon.setImageDrawable(mSearchButton.getDrawable());
297 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800298 }
299
Michael Kolbde463762011-07-14 15:25:45 -0700300 private void hideNavButtons() {
301 int awidth = mNavButtons.getMeasuredWidth();
302 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, 0, - awidth);
303 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", mUrlContainer.getLeft(),
304 mUrlContainer.getPaddingLeft());
305 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 1f, 0f);
306 AnimatorSet combo = new AnimatorSet();
307 combo.playTogether(anim1, anim2, anim3);
308 combo.addListener(new AnimatorListenerAdapter() {
309 @Override
310 public void onAnimationEnd(Animator animation) {
311 mNavButtons.setVisibility(View.GONE);
312 }
313 });
314 combo.setDuration(150);
315 combo.start();
316 }
317
318 private void showNavButtons() {
319 int awidth = mNavButtons.getMeasuredWidth();
320 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, -awidth, 0);
321 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", 0, awidth);
322 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 0f, 1f);
323 AnimatorSet combo = new AnimatorSet();
324 combo.playTogether(anim1, anim2, anim3);
325 mNavButtons.setVisibility(View.VISIBLE);
326 combo.setDuration(150);
327 combo.start();
328 }
329
George Mount387d45d2011-10-07 15:57:53 -0700330 private void showHideStar(Tab tab) {
331 // hide the bookmark star for data URLs
332 if (tab != null && tab.inForeground()) {
333 int starVisibility = View.VISIBLE;
334 String url = tab.getUrl();
335 if (DataUri.isDataUri(url)) {
336 starVisibility = View.GONE;
337 }
338 mStar.setVisibility(starVisibility);
339 }
340 }
341
Leon Scroggins571b3762010-05-26 10:25:01 -0400342}