1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 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 */ 16 package com.android.browser; 17 18 import android.animation.Animator; 19 import android.animation.AnimatorListenerAdapter; 20 import android.animation.AnimatorSet; 21 import android.animation.ObjectAnimator; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.res.Configuration; 25 import android.content.res.Resources; 26 import android.graphics.Bitmap; 27 import android.graphics.drawable.Drawable; 28 import android.text.TextUtils; 29 import android.util.AttributeSet; 30 import android.view.View; 31 import android.widget.ImageButton; 32 import android.widget.ImageView; 33 34 import com.android.browser.UI.ComboViews; 35 36 import java.util.List; 37 38 public class NavigationBarTablet extends NavigationBarBase { 39 40 private Drawable mStopDrawable; 41 private Drawable mReloadDrawable; 42 private String mStopDescription; 43 private String mRefreshDescription; 44 45 private View mUrlContainer; 46 private ImageButton mBackButton; 47 private ImageButton mForwardButton; 48 private ImageView mStar; 49 private ImageView mUrlIcon; 50 private ImageView mSearchButton; 51 private View mGoButton; 52 private ImageView mStopButton; 53 private View mAllButton; 54 private View mClearButton; 55 private ImageView mVoiceSearch; 56 private View mNavButtons; 57 private Drawable mFocusDrawable; 58 private Drawable mUnfocusDrawable; 59 private boolean mHideNavButtons; 60 private Drawable mFaviconDrawable; 61 NavigationBarTablet(Context context)62 public NavigationBarTablet(Context context) { 63 super(context); 64 init(context); 65 } 66 NavigationBarTablet(Context context, AttributeSet attrs)67 public NavigationBarTablet(Context context, AttributeSet attrs) { 68 super(context, attrs); 69 init(context); 70 } 71 NavigationBarTablet(Context context, AttributeSet attrs, int defStyle)72 public NavigationBarTablet(Context context, AttributeSet attrs, int defStyle) { 73 super(context, attrs, defStyle); 74 init(context); 75 } 76 init(Context context)77 private void init(Context context) { 78 Resources resources = context.getResources(); 79 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_holo_dark); 80 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_holo_dark); 81 mStopDescription = resources.getString(R.string.accessibility_button_stop); 82 mRefreshDescription = resources.getString(R.string.accessibility_button_refresh); 83 mFocusDrawable = resources.getDrawable( 84 R.drawable.textfield_active_holo_dark); 85 mUnfocusDrawable = resources.getDrawable( 86 R.drawable.textfield_default_holo_dark); 87 mHideNavButtons = resources.getBoolean(R.bool.hide_nav_buttons); 88 } 89 90 @Override onFinishInflate()91 protected void onFinishInflate() { 92 super.onFinishInflate(); 93 mAllButton = findViewById(R.id.all_btn); 94 // TODO: Change enabled states based on whether you can go 95 // back/forward. Probably should be done inside onPageStarted. 96 mNavButtons = findViewById(R.id.navbuttons); 97 mBackButton = (ImageButton) findViewById(R.id.back); 98 mForwardButton = (ImageButton) findViewById(R.id.forward); 99 mUrlIcon = (ImageView) findViewById(R.id.url_icon); 100 mStar = (ImageView) findViewById(R.id.star); 101 mStopButton = (ImageView) findViewById(R.id.stop); 102 mSearchButton = (ImageView) findViewById(R.id.search); 103 mGoButton = findViewById(R.id.go); 104 mClearButton = findViewById(R.id.clear); 105 mVoiceSearch = (ImageView) findViewById(R.id.voicesearch); 106 mUrlContainer = findViewById(R.id.urlbar_focused); 107 mBackButton.setOnClickListener(this); 108 mForwardButton.setOnClickListener(this); 109 mStar.setOnClickListener(this); 110 mAllButton.setOnClickListener(this); 111 mStopButton.setOnClickListener(this); 112 mSearchButton.setOnClickListener(this); 113 mGoButton.setOnClickListener(this); 114 mClearButton.setOnClickListener(this); 115 mVoiceSearch.setOnClickListener(this); 116 mUrlInput.setContainer(mUrlContainer); 117 } 118 onConfigurationChanged(Configuration config)119 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 137 @Override setTitleBar(TitleBar titleBar)138 public void setTitleBar(TitleBar titleBar) { 139 super.setTitleBar(titleBar); 140 setFocusState(false); 141 } 142 updateNavigationState(Tab tab)143 void updateNavigationState(Tab tab) { 144 if (tab != null) { 145 mBackButton.setImageResource(tab.canGoBack() 146 ? R.drawable.ic_back_holo_dark 147 : R.drawable.ic_back_disabled_holo_dark); 148 mForwardButton.setImageResource(tab.canGoForward() 149 ? R.drawable.ic_forward_holo_dark 150 : R.drawable.ic_forward_disabled_holo_dark); 151 } 152 updateUrlIcon(); 153 } 154 155 @Override onTabDataChanged(Tab tab)156 public void onTabDataChanged(Tab tab) { 157 super.onTabDataChanged(tab); 158 showHideStar(tab); 159 } 160 161 @Override setCurrentUrlIsBookmark(boolean isBookmark)162 public void setCurrentUrlIsBookmark(boolean isBookmark) { 163 mStar.setActivated(isBookmark); 164 } 165 166 @Override onClick(View v)167 public void onClick(View v) { 168 if (mBackButton == v) { 169 mUiController.getCurrentTab().goBack(); 170 } else if (mForwardButton == v) { 171 mUiController.getCurrentTab().goForward(); 172 } else if (mStar == v) { 173 Intent intent = mUiController.createBookmarkCurrentPageIntent(true); 174 if (intent != null) { 175 getContext().startActivity(intent); 176 } 177 } else if (mAllButton == v) { 178 mUiController.bookmarksOrHistoryPicker(ComboViews.Bookmarks); 179 } else if (mSearchButton == v) { 180 mBaseUi.editUrl(true); 181 } else if (mStopButton == v) { 182 stopOrRefresh(); 183 } else if (mGoButton == v) { 184 if (!TextUtils.isEmpty(mUrlInput.getText())) { 185 onAction(mUrlInput.getText().toString(), null, 186 UrlInputView.TYPED); 187 } 188 } else if (mClearButton == v) { 189 clearOrClose(); 190 } else if (mVoiceSearch == v) { 191 mUiController.startVoiceSearch(); 192 } else { 193 super.onClick(v); 194 } 195 } 196 clearOrClose()197 private void clearOrClose() { 198 if (TextUtils.isEmpty(mUrlInput.getUserText())) { 199 // close 200 mUrlInput.clearFocus(); 201 } else { 202 // clear 203 mUrlInput.setText(""); 204 } 205 } 206 207 @Override setFavicon(Bitmap icon)208 public void setFavicon(Bitmap icon) { 209 mFaviconDrawable = mBaseUi.getFaviconDrawable(icon); 210 updateUrlIcon(); 211 } 212 updateUrlIcon()213 void updateUrlIcon() { 214 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 } 226 } 227 228 @Override setFocusState(boolean focus)229 protected void setFocusState(boolean focus) { 230 super.setFocusState(focus); 231 if (focus) { 232 if (mHideNavButtons) { 233 hideNavButtons(); 234 } 235 mSearchButton.setVisibility(View.GONE); 236 mStar.setVisibility(View.GONE); 237 mClearButton.setVisibility(View.VISIBLE); 238 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark); 239 updateSearchMode(false); 240 } else { 241 if (mHideNavButtons) { 242 showNavButtons(); 243 } 244 mGoButton.setVisibility(View.GONE); 245 mVoiceSearch.setVisibility(View.GONE); 246 showHideStar(mUiController.getCurrentTab()); 247 mClearButton.setVisibility(View.GONE); 248 if (mTitleBar.useQuickControls()) { 249 mSearchButton.setVisibility(View.GONE); 250 } else { 251 mSearchButton.setVisibility(View.VISIBLE); 252 } 253 updateUrlIcon(); 254 } 255 mUrlContainer.setBackgroundDrawable(focus 256 ? mFocusDrawable : mUnfocusDrawable); 257 } 258 stopOrRefresh()259 private void stopOrRefresh() { 260 if (mUiController == null) return; 261 if (mTitleBar.isInLoad()) { 262 mUiController.stopLoading(); 263 } else { 264 mUiController.getCurrentTopWebView().reload(); 265 } 266 } 267 268 @Override onProgressStarted()269 public void onProgressStarted() { 270 mStopButton.setImageDrawable(mStopDrawable); 271 mStopButton.setContentDescription(mStopDescription); 272 } 273 274 @Override onProgressStopped()275 public void onProgressStopped() { 276 mStopButton.setImageDrawable(mReloadDrawable); 277 mStopButton.setContentDescription(mRefreshDescription); 278 } 279 updateSearchMode(boolean userEdited)280 protected void updateSearchMode(boolean userEdited) { 281 setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText())); 282 } 283 284 @Override setSearchMode(boolean voiceSearchEnabled)285 protected void setSearchMode(boolean voiceSearchEnabled) { 286 boolean showvoicebutton = voiceSearchEnabled && 287 mUiController.supportsVoiceSearch(); 288 mVoiceSearch.setVisibility(showvoicebutton ? View.VISIBLE : 289 View.GONE); 290 mGoButton.setVisibility(voiceSearchEnabled ? View.GONE : 291 View.VISIBLE); 292 } 293 294 @Override setInVoiceMode(boolean voicemode, List<String> voiceResults)295 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) { 296 super.setInVoiceMode(voicemode, voiceResults); 297 if (voicemode) { 298 mUrlIcon.setImageDrawable(mSearchButton.getDrawable()); 299 } 300 } 301 hideNavButtons()302 private void hideNavButtons() { 303 int awidth = mNavButtons.getMeasuredWidth(); 304 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, 0, - awidth); 305 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", mUrlContainer.getLeft(), 306 mUrlContainer.getPaddingLeft()); 307 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 1f, 0f); 308 AnimatorSet combo = new AnimatorSet(); 309 combo.playTogether(anim1, anim2, anim3); 310 combo.addListener(new AnimatorListenerAdapter() { 311 @Override 312 public void onAnimationEnd(Animator animation) { 313 mNavButtons.setVisibility(View.GONE); 314 } 315 }); 316 combo.setDuration(150); 317 combo.start(); 318 } 319 showNavButtons()320 private void showNavButtons() { 321 int awidth = mNavButtons.getMeasuredWidth(); 322 Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, -awidth, 0); 323 Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", 0, awidth); 324 Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 0f, 1f); 325 AnimatorSet combo = new AnimatorSet(); 326 combo.playTogether(anim1, anim2, anim3); 327 mNavButtons.setVisibility(View.VISIBLE); 328 combo.setDuration(150); 329 combo.start(); 330 } 331 showHideStar(Tab tab)332 private void showHideStar(Tab tab) { 333 // hide the bookmark star for data URLs 334 if (tab != null && tab.inForeground()) { 335 int starVisibility = View.VISIBLE; 336 String url = tab.getUrl(); 337 if (DataUri.isDataUri(url)) { 338 starVisibility = View.GONE; 339 } 340 mStar.setVisibility(starVisibility); 341 } 342 } 343 344 } 345