blob: c6c88cd3f0dea611177612d4c1f1e031fb129d0c [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;
Pankaj Garg32e1b942015-06-03 18:13:24 -070028import android.os.Bundle;
Michael Kolb1ce78132010-09-23 15:50:53 -070029import android.text.TextUtils;
John Reck0f602f32011-07-07 15:38:43 -070030import android.util.AttributeSet;
Leon Scroggins571b3762010-05-26 10:25:01 -040031import android.view.View;
Michael Kolb5a72f182011-01-13 20:35:06 -080032import android.widget.ImageButton;
Leon Scroggins571b3762010-05-26 10:25:01 -040033import android.widget.ImageView;
Leon Scroggins571b3762010-05-26 10:25:01 -040034
Bijan Amirzada41242f22014-03-21 12:12:18 -070035import com.android.browser.UI.ComboViews;
36import com.android.browser.UrlInputView.StateListener;
Pankaj Garg32e1b942015-06-03 18:13:24 -070037import com.android.browser.preferences.SiteSpecificPreferencesFragment;
38
39import org.codeaurora.swe.WebRefiner;
40import org.codeaurora.swe.WebView;
41
42import java.io.ByteArrayOutputStream;
Michael Kolb315d5022011-10-13 12:47:11 -070043
Michael Kolb0b129122012-06-04 16:31:58 -070044public class NavigationBarTablet extends NavigationBarBase implements StateListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070045
Michael Kolba2b2ba82010-08-04 17:54:03 -070046 private Drawable mStopDrawable;
47 private Drawable mReloadDrawable;
Michael Kolb30adae62011-07-29 13:37:05 -070048 private String mStopDescription;
49 private String mRefreshDescription;
Michael Kolbc7485ae2010-09-03 10:10:58 -070050
Michael Kolb11d19782011-03-20 10:17:40 -070051 private View mUrlContainer;
Michael Kolb5a72f182011-01-13 20:35:06 -080052 private ImageButton mBackButton;
53 private ImageButton mForwardButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080054 private ImageView mStar;
Michael Kolbe3524d82011-03-02 14:53:15 -080055 private ImageView mUrlIcon;
56 private ImageView mSearchButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070057 private ImageView mStopButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070058 private View mAllButton;
Michael Kolbb7b115e2010-09-25 16:59:37 -070059 private View mClearButton;
Michael Kolb0b129122012-06-04 16:31:58 -070060 private View mVoiceButton;
Michael Kolbde463762011-07-14 15:25:45 -070061 private View mNavButtons;
Michael Kolbde463762011-07-14 15:25:45 -070062 private boolean mHideNavButtons;
John Reck034637c2011-08-11 11:34:44 -070063 private Drawable mFaviconDrawable;
Michael Kolb81b6f832010-12-12 12:44:27 -080064
John Reck0f602f32011-07-07 15:38:43 -070065 public NavigationBarTablet(Context context) {
66 super(context);
67 init(context);
68 }
69
70 public NavigationBarTablet(Context context, AttributeSet attrs) {
71 super(context, attrs);
72 init(context);
73 }
74
75 public NavigationBarTablet(Context context, AttributeSet attrs, int defStyle) {
76 super(context, attrs, defStyle);
77 init(context);
78 }
79
80 private void init(Context context) {
81 Resources resources = context.getResources();
Enrico Ros1f5a0952014-11-18 20:15:48 -080082 mStopDrawable = resources.getDrawable(R.drawable.ic_action_stop);
83 mReloadDrawable = resources.getDrawable(R.drawable.ic_action_reload);
Michael Kolb30adae62011-07-29 13:37:05 -070084 mStopDescription = resources.getString(R.string.accessibility_button_stop);
85 mRefreshDescription = resources.getString(R.string.accessibility_button_refresh);
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);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700115 mUrlIcon.setOnClickListener(this);
John Reck0f602f32011-07-07 15:38:43 -0700116 }
117
Michael Kolbde463762011-07-14 15:25:45 -0700118 public void onConfigurationChanged(Configuration config) {
119 super.onConfigurationChanged(config);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800120 Resources res = getContext().getResources();
Michael Kolbde463762011-07-14 15:25:45 -0700121 mHideNavButtons = res.getBoolean(R.bool.hide_nav_buttons);
122 if (mUrlInput.hasFocus()) {
123 if (mHideNavButtons && (mNavButtons.getVisibility() == View.VISIBLE)) {
124 int aw = mNavButtons.getMeasuredWidth();
125 mNavButtons.setVisibility(View.GONE);
126 mNavButtons.setAlpha(0f);
127 mNavButtons.setTranslationX(-aw);
128 } else if (!mHideNavButtons && (mNavButtons.getVisibility() == View.GONE)) {
129 mNavButtons.setVisibility(View.VISIBLE);
130 mNavButtons.setAlpha(1f);
131 mNavButtons.setTranslationX(0);
132 }
133 }
134 }
135
John Reck0f602f32011-07-07 15:38:43 -0700136 @Override
137 public void setTitleBar(TitleBar titleBar) {
138 super.setTitleBar(titleBar);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800139 setFocusState(false);
Michael Kolb31d469b2010-12-09 20:49:54 -0800140 }
141
Michael Kolb5a72f182011-01-13 20:35:06 -0800142 void updateNavigationState(Tab tab) {
John Reckef654f12011-07-12 16:42:08 -0700143 if (tab != null) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800144 mBackButton.setEnabled(tab.canGoBack());
Pankaj Garg5ae52a02014-12-12 11:06:17 -0800145 mForwardButton.setEnabled(tab.canGoForward());
Michael Kolb5a72f182011-01-13 20:35:06 -0800146 }
John Reckb8b2af82011-05-20 15:58:33 -0700147 updateUrlIcon();
Michael Kolb5a72f182011-01-13 20:35:06 -0800148 }
149
Michael Kolb31d469b2010-12-09 20:49:54 -0800150 @Override
George Mount387d45d2011-10-07 15:57:53 -0700151 public void onTabDataChanged(Tab tab) {
152 super.onTabDataChanged(tab);
153 showHideStar(tab);
154 }
155
156 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500157 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800158 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500159 }
160
Michael Kolba2b2ba82010-08-04 17:54:03 -0700161 @Override
162 public void onClick(View v) {
Michael Kolbc832e5e2012-03-09 15:08:23 -0800163 if ((mBackButton == v) && (mUiController.getCurrentTab() != null)) {
John Reckef654f12011-07-12 16:42:08 -0700164 mUiController.getCurrentTab().goBack();
Michael Kolbc832e5e2012-03-09 15:08:23 -0800165 } else if ((mForwardButton == v) && (mUiController.getCurrentTab() != null)) {
John Reckef654f12011-07-12 16:42:08 -0700166 mUiController.getCurrentTab().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700167 } else if (mStar == v) {
John Recka60fffa2011-09-06 16:30:29 -0700168 Intent intent = mUiController.createBookmarkCurrentPageIntent(true);
169 if (intent != null) {
170 getContext().startActivity(intent);
171 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700172 } else if (mAllButton == v) {
Michael Kolb315d5022011-10-13 12:47:11 -0700173 mUiController.bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700174 } else if (mSearchButton == v) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700175 mBaseUi.editUrl(true, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700176 } else if (mStopButton == v) {
177 stopOrRefresh();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700178 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800179 clearOrClose();
Michael Kolb0b129122012-06-04 16:31:58 -0700180 } else if (mVoiceButton == v) {
181 mUiController.startVoiceRecognizer();
Pankaj Garg32e1b942015-06-03 18:13:24 -0700182 } else if (mUrlIcon == v) {
183 showSiteSpecificSettings();
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 Kamathf3174a52011-11-17 14:43:32 +0000190 if (TextUtils.isEmpty(mUrlInput.getText())) {
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) {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700201 super.setFavicon(icon);
John Reck034637c2011-08-11 11:34:44 -0700202 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()) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800208 mUrlIcon.setImageResource(R.drawable.ic_action_search_normal);
John Reck034637c2011-08-11 11:34:44 -0700209 } 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);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800226 mUrlIcon.setImageResource(R.drawable.ic_action_search_normal);
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());
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700232 mSearchButton.setVisibility(View.VISIBLE);
John Reckb8b2af82011-05-20 15:58:33 -0700233 updateUrlIcon();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700234 }
235 }
236
Michael Kolba2b2ba82010-08-04 17:54:03 -0700237 private void stopOrRefresh() {
Michael Kolb1392b832011-09-07 13:02:59 -0700238 if (mUiController == null) return;
John Reck0f602f32011-07-07 15:38:43 -0700239 if (mTitleBar.isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700241 } else {
Michael Kolb66450842011-12-15 15:01:19 -0800242 if (mUiController.getCurrentTopWebView() != null) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800243 Tab currTab = mUiController.getTabControl().getCurrentTab();
Michael Kolb66450842011-12-15 15:01:19 -0800244 mUiController.getCurrentTopWebView().reload();
245 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700246 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400247 }
248
Michael Kolbfe251992010-07-08 15:41:55 -0700249 @Override
John Reck0f602f32011-07-07 15:38:43 -0700250 public void onProgressStarted() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700251 mStopButton.setImageDrawable(mStopDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700252 mStopButton.setContentDescription(mStopDescription);
Michael Kolb46f987e2011-04-05 10:39:10 -0700253 }
254
255 @Override
John Reck0f602f32011-07-07 15:38:43 -0700256 public void onProgressStopped() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700257 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolb30adae62011-07-29 13:37:05 -0700258 mStopButton.setContentDescription(mRefreshDescription);
Michael Kolbfe251992010-07-08 15:41:55 -0700259 }
260
Michael Kolbbae0cb22012-05-29 11:15:27 -0700261 private AnimatorSet mAnimation;
262
Michael Kolbde463762011-07-14 15:25:45 -0700263 private void hideNavButtons() {
Michael Kolbbae0cb22012-05-29 11:15:27 -0700264 if (mBaseUi.blockFocusAnimations()) {
265 mNavButtons.setVisibility(View.GONE);
266 return;
267 }
Michael Kolbde463762011-07-14 15:25:45 -0700268 int awidth = mNavButtons.getMeasuredWidth();
269 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, 0, - awidth);
270 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", mUrlContainer.getLeft(),
271 mUrlContainer.getPaddingLeft());
272 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 1f, 0f);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700273 mAnimation = new AnimatorSet();
274 mAnimation.playTogether(anim1, anim2, anim3);
275 mAnimation.addListener(new AnimatorListenerAdapter() {
Michael Kolbde463762011-07-14 15:25:45 -0700276 @Override
277 public void onAnimationEnd(Animator animation) {
278 mNavButtons.setVisibility(View.GONE);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700279 mAnimation = null;
Michael Kolbde463762011-07-14 15:25:45 -0700280 }
281 });
Michael Kolbbae0cb22012-05-29 11:15:27 -0700282 mAnimation.setDuration(150);
283 mAnimation.start();
Michael Kolbde463762011-07-14 15:25:45 -0700284 }
285
286 private void showNavButtons() {
Michael Kolbbae0cb22012-05-29 11:15:27 -0700287 if (mAnimation != null) {
288 mAnimation.cancel();
289 }
Michael Kolbde463762011-07-14 15:25:45 -0700290 mNavButtons.setVisibility(View.VISIBLE);
Michael Kolb0b129122012-06-04 16:31:58 -0700291 mNavButtons.setTranslationX(0);
Michael Kolbbae0cb22012-05-29 11:15:27 -0700292 if (!mBaseUi.blockFocusAnimations()) {
293 int awidth = mNavButtons.getMeasuredWidth();
294 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons,
295 View.TRANSLATION_X, -awidth, 0);
296 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", 0,
297 awidth);
298 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA,
299 0f, 1f);
300 AnimatorSet combo = new AnimatorSet();
301 combo.playTogether(anim1, anim2, anim3);
302 combo.setDuration(150);
303 combo.start();
304 }
Michael Kolbde463762011-07-14 15:25:45 -0700305 }
306
George Mount387d45d2011-10-07 15:57:53 -0700307 private void showHideStar(Tab tab) {
308 // hide the bookmark star for data URLs
309 if (tab != null && tab.inForeground()) {
310 int starVisibility = View.VISIBLE;
311 String url = tab.getUrl();
312 if (DataUri.isDataUri(url)) {
313 starVisibility = View.GONE;
314 }
315 mStar.setVisibility(starVisibility);
316 }
317 }
318
Michael Kolb0b129122012-06-04 16:31:58 -0700319 @Override
320 public void onStateChanged(int state) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800321 super.onStateChanged(state);
Michael Kolbe721cc32012-06-26 15:26:59 -0700322 mVoiceButton.setVisibility(View.GONE);
Michael Kolb0b129122012-06-04 16:31:58 -0700323 switch(state) {
324 case STATE_NORMAL:
325 mClearButton.setVisibility(View.GONE);
Michael Kolb0b129122012-06-04 16:31:58 -0700326 break;
327 case STATE_HIGHLIGHTED:
328 mClearButton.setVisibility(View.GONE);
Michael Kolbe721cc32012-06-26 15:26:59 -0700329 if ((mUiController != null) && mUiController.supportsVoice()) {
330 mVoiceButton.setVisibility(View.VISIBLE);
331 }
Michael Kolb0b129122012-06-04 16:31:58 -0700332 break;
333 case STATE_EDITED:
334 mClearButton.setVisibility(View.VISIBLE);
Michael Kolb0b129122012-06-04 16:31:58 -0700335 break;
336 }
337 }
338
Leon Scroggins571b3762010-05-26 10:25:01 -0400339}