blob: 3403fd7e698e753d6d3c8cf8f36981479cfaa840 [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 Kolb513286f2010-09-09 12:55:12 -070051 private View mGoButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070052 private ImageView mStopButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070053 private View mAllButton;
Michael Kolbb7b115e2010-09-25 16:59:37 -070054 private View mClearButton;
Michael Kolbe3524d82011-03-02 14:53:15 -080055 private ImageView mVoiceSearch;
Michael Kolbde463762011-07-14 15:25:45 -070056 private View mNavButtons;
Michael Kolbcfa3af52010-12-14 10:36:11 -080057 private Drawable mFocusDrawable;
58 private Drawable mUnfocusDrawable;
Michael Kolbde463762011-07-14 15:25:45 -070059 private boolean mHideNavButtons;
John Reck034637c2011-08-11 11:34:44 -070060 private Drawable mFaviconDrawable;
Michael Kolb81b6f832010-12-12 12:44:27 -080061
John Reck0f602f32011-07-07 15:38:43 -070062 public NavigationBarTablet(Context context) {
63 super(context);
64 init(context);
65 }
66
67 public NavigationBarTablet(Context context, AttributeSet attrs) {
68 super(context, attrs);
69 init(context);
70 }
71
72 public NavigationBarTablet(Context context, AttributeSet attrs, int defStyle) {
73 super(context, attrs, defStyle);
74 init(context);
75 }
76
77 private void init(Context context) {
78 Resources resources = context.getResources();
Michael Kolb5a72f182011-01-13 20:35:06 -080079 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_holo_dark);
80 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb30adae62011-07-29 13:37:05 -070081 mStopDescription = resources.getString(R.string.accessibility_button_stop);
82 mRefreshDescription = resources.getString(R.string.accessibility_button_refresh);
Michael Kolbcfa3af52010-12-14 10:36:11 -080083 mFocusDrawable = resources.getDrawable(
84 R.drawable.textfield_active_holo_dark);
85 mUnfocusDrawable = resources.getDrawable(
86 R.drawable.textfield_default_holo_dark);
Michael Kolbde463762011-07-14 15:25:45 -070087 mHideNavButtons = resources.getBoolean(R.bool.hide_nav_buttons);
Michael Kolbfe251992010-07-08 15:41:55 -070088 }
Leon Scroggins571b3762010-05-26 10:25:01 -040089
Michael Kolb7cdc4902011-02-03 17:54:40 -080090 @Override
John Reck0f602f32011-07-07 15:38:43 -070091 protected void onFinishInflate() {
92 super.onFinishInflate();
Michael Kolbfe251992010-07-08 15:41:55 -070093 mAllButton = findViewById(R.id.all_btn);
94 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040095 // back/forward. Probably should be done inside onPageStarted.
Michael Kolbde463762011-07-14 15:25:45 -070096 mNavButtons = findViewById(R.id.navbuttons);
Michael Kolb5a72f182011-01-13 20:35:06 -080097 mBackButton = (ImageButton) findViewById(R.id.back);
98 mForwardButton = (ImageButton) findViewById(R.id.forward);
Michael Kolbe3524d82011-03-02 14:53:15 -080099 mUrlIcon = (ImageView) findViewById(R.id.url_icon);
Michael Kolb31d469b2010-12-09 20:49:54 -0800100 mStar = (ImageView) findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700101 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolbe3524d82011-03-02 14:53:15 -0800102 mSearchButton = (ImageView) findViewById(R.id.search);
Michael Kolb513286f2010-09-09 12:55:12 -0700103 mGoButton = findViewById(R.id.go);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700104 mClearButton = findViewById(R.id.clear);
Michael Kolbe3524d82011-03-02 14:53:15 -0800105 mVoiceSearch = (ImageView) findViewById(R.id.voicesearch);
Michael Kolb31d469b2010-12-09 20:49:54 -0800106 mUrlContainer = findViewById(R.id.urlbar_focused);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700107 mBackButton.setOnClickListener(this);
108 mForwardButton.setOnClickListener(this);
109 mStar.setOnClickListener(this);
110 mAllButton.setOnClickListener(this);
111 mStopButton.setOnClickListener(this);
112 mSearchButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700113 mGoButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700114 mClearButton.setOnClickListener(this);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800115 mVoiceSearch.setOnClickListener(this);
Michael Kolb31d469b2010-12-09 20:49:54 -0800116 mUrlInput.setContainer(mUrlContainer);
John Reck0f602f32011-07-07 15:38:43 -0700117 }
118
Michael Kolbde463762011-07-14 15:25:45 -0700119 public void onConfigurationChanged(Configuration config) {
120 super.onConfigurationChanged(config);
121 Resources res = mContext.getResources();
122 mHideNavButtons = res.getBoolean(R.bool.hide_nav_buttons);
123 if (mUrlInput.hasFocus()) {
124 if (mHideNavButtons && (mNavButtons.getVisibility() == View.VISIBLE)) {
125 int aw = mNavButtons.getMeasuredWidth();
126 mNavButtons.setVisibility(View.GONE);
127 mNavButtons.setAlpha(0f);
128 mNavButtons.setTranslationX(-aw);
129 } else if (!mHideNavButtons && (mNavButtons.getVisibility() == View.GONE)) {
130 mNavButtons.setVisibility(View.VISIBLE);
131 mNavButtons.setAlpha(1f);
132 mNavButtons.setTranslationX(0);
133 }
134 }
135 }
136
John Reck0f602f32011-07-07 15:38:43 -0700137 @Override
138 public void setTitleBar(TitleBar titleBar) {
139 super.setTitleBar(titleBar);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800140 setFocusState(false);
Michael Kolb31d469b2010-12-09 20:49:54 -0800141 }
142
Michael Kolb5a72f182011-01-13 20:35:06 -0800143 void updateNavigationState(Tab tab) {
John Reckef654f12011-07-12 16:42:08 -0700144 if (tab != null) {
145 mBackButton.setImageResource(tab.canGoBack()
Michael Kolb5a72f182011-01-13 20:35:06 -0800146 ? R.drawable.ic_back_holo_dark
147 : R.drawable.ic_back_disabled_holo_dark);
John Reckef654f12011-07-12 16:42:08 -0700148 mForwardButton.setImageResource(tab.canGoForward()
Michael Kolb5a72f182011-01-13 20:35:06 -0800149 ? R.drawable.ic_forward_holo_dark
150 : R.drawable.ic_forward_disabled_holo_dark);
151 }
John Reckb8b2af82011-05-20 15:58:33 -0700152 updateUrlIcon();
Michael Kolb5a72f182011-01-13 20:35:06 -0800153 }
154
Michael Kolb31d469b2010-12-09 20:49:54 -0800155 @Override
George Mount387d45d2011-10-07 15:57:53 -0700156 public void onTabDataChanged(Tab tab) {
157 super.onTabDataChanged(tab);
158 showHideStar(tab);
159 }
160
161 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500162 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800163 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500164 }
165
Michael Kolba2b2ba82010-08-04 17:54:03 -0700166 @Override
167 public void onClick(View v) {
Michael Kolbc832e5e2012-03-09 15:08:23 -0800168 if ((mBackButton == v) && (mUiController.getCurrentTab() != null)) {
John Reckef654f12011-07-12 16:42:08 -0700169 mUiController.getCurrentTab().goBack();
Michael Kolbc832e5e2012-03-09 15:08:23 -0800170 } else if ((mForwardButton == v) && (mUiController.getCurrentTab() != null)) {
John Reckef654f12011-07-12 16:42:08 -0700171 mUiController.getCurrentTab().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700172 } else if (mStar == v) {
John Recka60fffa2011-09-06 16:30:29 -0700173 Intent intent = mUiController.createBookmarkCurrentPageIntent(true);
174 if (intent != null) {
175 getContext().startActivity(intent);
176 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700177 } else if (mAllButton == v) {
Michael Kolb315d5022011-10-13 12:47:11 -0700178 mUiController.bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700179 } else if (mSearchButton == v) {
John Reck0f602f32011-07-07 15:38:43 -0700180 mBaseUi.editUrl(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700181 } else if (mStopButton == v) {
182 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700183 } else if (mGoButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800184 if (!TextUtils.isEmpty(mUrlInput.getText())) {
185 onAction(mUrlInput.getText().toString(), null,
Michael Kolb257cc2c2010-12-09 09:45:52 -0800186 UrlInputView.TYPED);
Michael Kolb1ce78132010-09-23 15:50:53 -0700187 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700188 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800189 clearOrClose();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800190 } else if (mVoiceSearch == v) {
191 mUiController.startVoiceSearch();
Michael Kolb11d19782011-03-20 10:17:40 -0700192 } else {
193 super.onClick(v);
Michael Kolbfe251992010-07-08 15:41:55 -0700194 }
195 }
196
Michael Kolb31d469b2010-12-09 20:49:54 -0800197 private void clearOrClose() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000198 if (TextUtils.isEmpty(mUrlInput.getText())) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800199 // close
Michael Kolb7cdc4902011-02-03 17:54:40 -0800200 mUrlInput.clearFocus();
Michael Kolb31d469b2010-12-09 20:49:54 -0800201 } else {
202 // clear
203 mUrlInput.setText("");
204 }
205 }
206
John Reck034637c2011-08-11 11:34:44 -0700207 @Override
208 public void setFavicon(Bitmap icon) {
209 mFaviconDrawable = mBaseUi.getFaviconDrawable(icon);
210 updateUrlIcon();
211 }
212
John Reckb8b2af82011-05-20 15:58:33 -0700213 void updateUrlIcon() {
John Reck034637c2011-08-11 11:34:44 -0700214 if (mUrlInput.hasFocus()) {
215 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
216 } else {
217 if (mInVoiceMode) {
218 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
219 } else {
220 if (mFaviconDrawable == null) {
221 mFaviconDrawable = mBaseUi.getFaviconDrawable(null);
222 }
223 mUrlIcon.setImageDrawable(mFaviconDrawable);
224 }
225 }
John Reckb8b2af82011-05-20 15:58:33 -0700226 }
227
Michael Kolb11d19782011-03-20 10:17:40 -0700228 @Override
229 protected void setFocusState(boolean focus) {
230 super.setFocusState(focus);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800231 if (focus) {
Michael Kolbde463762011-07-14 15:25:45 -0700232 if (mHideNavButtons) {
233 hideNavButtons();
234 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700235 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800236 mStar.setVisibility(View.GONE);
237 mClearButton.setVisibility(View.VISIBLE);
Michael Kolbe3524d82011-03-02 14:53:15 -0800238 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
Michael Kolb60a68f52011-02-24 11:02:52 -0800239 updateSearchMode(false);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700240 } else {
Michael Kolbde463762011-07-14 15:25:45 -0700241 if (mHideNavButtons) {
242 showNavButtons();
243 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700244 mGoButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800245 mVoiceSearch.setVisibility(View.GONE);
George Mount387d45d2011-10-07 15:57:53 -0700246 showHideStar(mUiController.getCurrentTab());
Michael Kolb31d469b2010-12-09 20:49:54 -0800247 mClearButton.setVisibility(View.GONE);
John Reck0f602f32011-07-07 15:38:43 -0700248 if (mTitleBar.useQuickControls()) {
Michael Kolb376b5412010-12-15 11:52:57 -0800249 mSearchButton.setVisibility(View.GONE);
250 } else {
251 mSearchButton.setVisibility(View.VISIBLE);
252 }
John Reckb8b2af82011-05-20 15:58:33 -0700253 updateUrlIcon();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700254 }
John Reck7e5b8b52011-06-22 13:46:25 -0700255 mUrlContainer.setBackgroundDrawable(focus
256 ? mFocusDrawable : mUnfocusDrawable);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700257 }
258
Michael Kolba2b2ba82010-08-04 17:54:03 -0700259 private void stopOrRefresh() {
Michael Kolb1392b832011-09-07 13:02:59 -0700260 if (mUiController == null) return;
John Reck0f602f32011-07-07 15:38:43 -0700261 if (mTitleBar.isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700263 } else {
Michael Kolb66450842011-12-15 15:01:19 -0800264 if (mUiController.getCurrentTopWebView() != null) {
265 mUiController.getCurrentTopWebView().reload();
266 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700267 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400268 }
269
Michael Kolbfe251992010-07-08 15:41:55 -0700270 @Override
John Reck0f602f32011-07-07 15:38:43 -0700271 public void onProgressStarted() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700272 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700273 mStopButton.setContentDescription(mStopDescription);
Michael Kolb46f987e2011-04-05 10:39:10 -0700274 }
275
276 @Override
John Reck0f602f32011-07-07 15:38:43 -0700277 public void onProgressStopped() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700278 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700279 mStopButton.setContentDescription(mRefreshDescription);
Michael Kolbfe251992010-07-08 15:41:55 -0700280 }
281
Michael Kolb11d19782011-03-20 10:17:40 -0700282 protected void updateSearchMode(boolean userEdited) {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000283 setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getText()));
Michael Kolb31d469b2010-12-09 20:49:54 -0800284 }
285
Michael Kolb11d19782011-03-20 10:17:40 -0700286 @Override
287 protected void setSearchMode(boolean voiceSearchEnabled) {
Michael Kolb793e05e2011-01-11 15:17:31 -0800288 boolean showvoicebutton = voiceSearchEnabled &&
Michael Kolb736990c2011-03-20 10:01:20 -0700289 mUiController.supportsVoiceSearch();
Michael Kolb793e05e2011-01-11 15:17:31 -0800290 mVoiceSearch.setVisibility(showvoicebutton ? View.VISIBLE :
Michael Kolb31d469b2010-12-09 20:49:54 -0800291 View.GONE);
292 mGoButton.setVisibility(voiceSearchEnabled ? View.GONE :
293 View.VISIBLE);
294 }
295
Michael Kolbfe251992010-07-08 15:41:55 -0700296 @Override
Michael Kolbcfa3af52010-12-14 10:36:11 -0800297 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
Michael Kolb11d19782011-03-20 10:17:40 -0700298 super.setInVoiceMode(voicemode, voiceResults);
Michael Kolb3aaef252011-03-09 18:13:56 -0800299 if (voicemode) {
300 mUrlIcon.setImageDrawable(mSearchButton.getDrawable());
301 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800302 }
303
Michael Kolbde463762011-07-14 15:25:45 -0700304 private void hideNavButtons() {
305 int awidth = mNavButtons.getMeasuredWidth();
306 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, 0, - awidth);
307 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", mUrlContainer.getLeft(),
308 mUrlContainer.getPaddingLeft());
309 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 1f, 0f);
310 AnimatorSet combo = new AnimatorSet();
311 combo.playTogether(anim1, anim2, anim3);
312 combo.addListener(new AnimatorListenerAdapter() {
313 @Override
314 public void onAnimationEnd(Animator animation) {
315 mNavButtons.setVisibility(View.GONE);
316 }
317 });
318 combo.setDuration(150);
319 combo.start();
320 }
321
322 private void showNavButtons() {
323 int awidth = mNavButtons.getMeasuredWidth();
324 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, -awidth, 0);
325 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", 0, awidth);
326 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 0f, 1f);
327 AnimatorSet combo = new AnimatorSet();
328 combo.playTogether(anim1, anim2, anim3);
329 mNavButtons.setVisibility(View.VISIBLE);
330 combo.setDuration(150);
331 combo.start();
332 }
333
George Mount387d45d2011-10-07 15:57:53 -0700334 private void showHideStar(Tab tab) {
335 // hide the bookmark star for data URLs
336 if (tab != null && tab.inForeground()) {
337 int starVisibility = View.VISIBLE;
338 String url = tab.getUrl();
339 if (DataUri.isDataUri(url)) {
340 starVisibility = View.GONE;
341 }
342 mStar.setVisibility(starVisibility);
343 }
344 }
345
Leon Scroggins571b3762010-05-26 10:25:01 -0400346}