TabBar.java revision e755c575712cfee4aa558df7fcd5defe97927f2b
1/* 2 * Copyright (C) 2010 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 17package com.android.browser; 18 19import com.android.browser.BrowserWebView.ScrollListener; 20 21import android.animation.Animator; 22import android.animation.Animator.AnimatorListener; 23import android.animation.AnimatorSet; 24import android.animation.ObjectAnimator; 25import android.app.Activity; 26import android.content.Context; 27import android.content.res.Configuration; 28import android.content.res.Resources; 29import android.graphics.Bitmap; 30import android.graphics.BitmapShader; 31import android.graphics.Canvas; 32import android.graphics.Matrix; 33import android.graphics.Paint; 34import android.graphics.Path; 35import android.graphics.Shader; 36import android.graphics.drawable.BitmapDrawable; 37import android.graphics.drawable.Drawable; 38import android.graphics.drawable.LayerDrawable; 39import android.graphics.drawable.PaintDrawable; 40import android.view.ContextMenu; 41import android.view.Gravity; 42import android.view.LayoutInflater; 43import android.view.MenuInflater; 44import android.view.View; 45import android.view.View.OnClickListener; 46import android.webkit.WebView; 47import android.widget.ImageButton; 48import android.widget.ImageView; 49import android.widget.LinearLayout; 50import android.widget.TextView; 51 52import java.util.HashMap; 53import java.util.List; 54import java.util.Map; 55 56/** 57 * tabbed title bar for xlarge screen browser 58 */ 59public class TabBar extends LinearLayout 60 implements ScrollListener, OnClickListener { 61 62 private static final int PROGRESS_MAX = 100; 63 64 private Activity mActivity; 65 private UiController mUiController; 66 private TabControl mTabControl; 67 private XLargeUi mUi; 68 69 private int mTabWidthSelected; 70 private int mTabWidthUnselected; 71 72 private TabScrollView mTabs; 73 74 private ImageButton mNewTab; 75 private int mButtonWidth; 76 77 private Map<Tab, TabView> mTabMap; 78 79 private Drawable mGenericFavicon; 80 81 private int mCurrentTextureWidth = 0; 82 private int mCurrentTextureHeight = 0; 83 84 private Drawable mActiveDrawable; 85 private Drawable mInactiveDrawable; 86 87 private final Paint mActiveShaderPaint = new Paint(); 88 private final Paint mInactiveShaderPaint = new Paint(); 89 private final Paint mFocusPaint = new Paint(); 90 private final Matrix mActiveMatrix = new Matrix(); 91 private final Matrix mInactiveMatrix = new Matrix(); 92 93 private BitmapShader mActiveShader; 94 private BitmapShader mInactiveShader; 95 96 private int mTabOverlap; 97 private int mAddTabOverlap; 98 private int mTabSliceWidth; 99 private boolean mUseQuickControls; 100 101 public TabBar(Activity activity, UiController controller, XLargeUi ui) { 102 super(activity); 103 mActivity = activity; 104 mUiController = controller; 105 mTabControl = mUiController.getTabControl(); 106 mUi = ui; 107 Resources res = activity.getResources(); 108 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected); 109 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected); 110 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar); 111 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive); 112 113 mTabMap = new HashMap<Tab, TabView>(); 114 Resources resources = activity.getResources(); 115 LayoutInflater factory = LayoutInflater.from(activity); 116 factory.inflate(R.layout.tab_bar, this); 117 setPadding(0, (int) res.getDimension(R.dimen.tab_padding_top), 0, 0); 118 mTabs = (TabScrollView) findViewById(R.id.tabs); 119 mNewTab = (ImageButton) findViewById(R.id.newtab); 120 mNewTab.setOnClickListener(this); 121 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm); 122 123 updateTabs(mUiController.getTabs()); 124 mButtonWidth = -1; 125 // tab dimensions 126 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap); 127 mAddTabOverlap = (int) res.getDimension(R.dimen.tab_addoverlap); 128 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice); 129 130 mActiveShaderPaint.setStyle(Paint.Style.FILL); 131 mActiveShaderPaint.setAntiAlias(true); 132 133 mInactiveShaderPaint.setStyle(Paint.Style.FILL); 134 mInactiveShaderPaint.setAntiAlias(true); 135 136 mFocusPaint.setStyle(Paint.Style.STROKE); 137 mFocusPaint.setStrokeWidth(res.getDimension(R.dimen.tab_focus_stroke)); 138 mFocusPaint.setAntiAlias(true); 139 mFocusPaint.setColor(res.getColor(R.color.tabFocusHighlight)); 140 } 141 142 @Override 143 public void onConfigurationChanged(Configuration config) { 144 super.onConfigurationChanged(config); 145 Resources res = mActivity.getResources(); 146 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected); 147 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected); 148 // force update of tab bar 149 mTabs.updateLayout(); 150 } 151 152 void setUseQuickControls(boolean useQuickControls) { 153 mUseQuickControls = useQuickControls; 154 mNewTab.setVisibility(mUseQuickControls ? View.GONE 155 : View.VISIBLE); 156 } 157 158 int getTabCount() { 159 return mTabMap.size(); 160 } 161 162 void updateTabs(List<Tab> tabs) { 163 mTabs.clearTabs(); 164 mTabMap.clear(); 165 for (Tab tab : tabs) { 166 TabView tv = buildTabView(tab); 167 mTabs.addTab(tv); 168 } 169 mTabs.setSelectedTab(mTabControl.getCurrentPosition()); 170 } 171 172 @Override 173 protected void onMeasure(int hspec, int vspec) { 174 super.onMeasure(hspec, vspec); 175 int w = getMeasuredWidth(); 176 // adjust for new tab overlap 177 if (!mUseQuickControls) { 178 w -= mAddTabOverlap; 179 } 180 setMeasuredDimension(w, getMeasuredHeight()); 181 } 182 183 @Override 184 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 185 // use paddingLeft and paddingTop 186 int pl = getPaddingLeft(); 187 int pt = getPaddingTop(); 188 int sw = mTabs.getMeasuredWidth(); 189 int w = right - left - pl; 190 if (mUseQuickControls) { 191 mButtonWidth = 0; 192 } else { 193 mButtonWidth = mNewTab.getMeasuredWidth() - mAddTabOverlap; 194 if (w-sw < mButtonWidth) { 195 sw = w - mButtonWidth; 196 } 197 } 198 mTabs.layout(pl, pt, pl + sw, bottom - top); 199 // adjust for overlap 200 if (!mUseQuickControls) { 201 mNewTab.layout(pl + sw - mAddTabOverlap, pt, 202 pl + sw + mButtonWidth - mAddTabOverlap, bottom - top); 203 } 204 } 205 206 public void onClick(View view) { 207 if (mNewTab == view) { 208 mUiController.openTabToHomePage(); 209 } else if (mTabs.getSelectedTab() == view) { 210 if (mUseQuickControls) { 211 if (mUi.isTitleBarShowing() && !isLoading()) { 212 mUi.stopEditingUrl(); 213 mUi.hideTitleBar(); 214 } else { 215 mUi.stopWebViewScrolling(); 216 mUi.editUrl(false); 217 } 218 } else if (mUi.isTitleBarShowing() && !isLoading()) { 219 mUi.stopEditingUrl(); 220 mUi.hideTitleBar(); 221 } else { 222 showUrlBar(); 223 } 224 } else if (view instanceof TabView) { 225 final Tab tab = ((TabView) view).mTab; 226 int ix = mTabs.getChildIndex(view); 227 if (ix >= 0) { 228 mTabs.setSelectedTab(ix); 229 mUiController.switchToTab(tab); 230 } 231 } 232 } 233 234 private void showUrlBar() { 235 mUi.stopWebViewScrolling(); 236 mUi.showTitleBar(); 237 } 238 239 void showTitleBarIndicator(boolean show) { 240 Tab tab = mTabControl.getCurrentTab(); 241 if (tab != null) { 242 TabView tv = mTabMap.get(tab); 243 if (tv != null) { 244 tv.showIndicator(show); 245 } 246 } 247 } 248 249 boolean showsTitleBarIndicator() { 250 Tab tab = mTabControl.getCurrentTab(); 251 if (tab != null) { 252 TabView tv = mTabMap.get(tab); 253 if (tv != null) { 254 return tv.showsIndicator(); 255 } 256 } 257 return false; 258 } 259 260 // callback after fake titlebar is shown 261 void onShowTitleBar() { 262 showTitleBarIndicator(false); 263 } 264 265 // callback after fake titlebar is hidden 266 void onHideTitleBar() { 267 Tab tab = mTabControl.getCurrentTab(); 268 WebView w = tab.getWebView(); 269 if (w != null) { 270 showTitleBarIndicator(w.getVisibleTitleHeight() == 0); 271 } 272 } 273 274 // webview scroll listener 275 276 @Override 277 public void onScroll(int visibleTitleHeight, boolean userInitiated) { 278 if (mUseQuickControls) return; 279 // isLoading is using the current tab, which initially might not be set yet 280 if (mTabControl.getCurrentTab() != null 281 && !isLoading()) { 282 if (visibleTitleHeight == 0) { 283 if (!showsTitleBarIndicator() 284 && (!mUi.isEditingUrl() || userInitiated)) { 285 mUi.hideTitleBar(); 286 showTitleBarIndicator(true); 287 } 288 } else { 289 if (showsTitleBarIndicator()) { 290 showTitleBarIndicator(false); 291 } 292 } 293 } 294 } 295 296 @Override 297 public void createContextMenu(ContextMenu menu) { 298 MenuInflater inflater = mActivity.getMenuInflater(); 299 inflater.inflate(R.menu.title_context, menu); 300 mActivity.onCreateContextMenu(menu, this, null); 301 } 302 303 private TabView buildTabView(Tab tab) { 304 TabView tabview = new TabView(mActivity, tab); 305 mTabMap.put(tab, tabview); 306 tabview.setOnClickListener(this); 307 return tabview; 308 } 309 310 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) { 311 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 312 Canvas c = new Canvas(b); 313 drawable.setBounds(0, 0, width, height); 314 drawable.draw(c); 315 return b; 316 } 317 318 /** 319 * View used in the tab bar 320 */ 321 class TabView extends LinearLayout implements OnClickListener { 322 323 Tab mTab; 324 View mTabContent; 325 TextView mTitle; 326 View mIndicator; 327 View mIncognito; 328 ImageView mIconView; 329 ImageView mLock; 330 ImageView mClose; 331 boolean mSelected; 332 boolean mInLoad; 333 Path mPath; 334 Path mFocusPath; 335 int[] mWindowPos; 336 337 /** 338 * @param context 339 */ 340 public TabView(Context context, Tab tab) { 341 super(context); 342 setWillNotDraw(false); 343 mPath = new Path(); 344 mFocusPath = new Path(); 345 mWindowPos = new int[2]; 346 mTab = tab; 347 setGravity(Gravity.CENTER_VERTICAL); 348 setOrientation(LinearLayout.HORIZONTAL); 349 setPadding(mTabOverlap, 0, mTabSliceWidth, 0); 350 LayoutInflater inflater = LayoutInflater.from(getContext()); 351 mTabContent = inflater.inflate(R.layout.tab_title, this, true); 352 mTitle = (TextView) mTabContent.findViewById(R.id.title); 353 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon); 354 mLock = (ImageView) mTabContent.findViewById(R.id.lock); 355 mClose = (ImageView) mTabContent.findViewById(R.id.close); 356 mClose.setOnClickListener(this); 357 mIncognito = mTabContent.findViewById(R.id.incognito); 358 mIndicator = mTabContent.findViewById(R.id.chevron); 359 mSelected = false; 360 mInLoad = false; 361 // update the status 362 updateFromTab(); 363 } 364 365 void showIndicator(boolean show) { 366 if (mSelected) { 367 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE); 368 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams(); 369 if (show) { 370 lp.width = mTabWidthSelected + mIndicator.getWidth(); 371 } else { 372 lp.width = mTabWidthSelected; 373 } 374 lp.height = LayoutParams.MATCH_PARENT; 375 setLayoutParams(lp); 376 } else { 377 mIndicator.setVisibility(View.GONE); 378 } 379 } 380 381 boolean showsIndicator() { 382 return (mIndicator.getVisibility() == View.VISIBLE); 383 } 384 385 @Override 386 public void onClick(View v) { 387 if (v == mClose) { 388 closeTab(); 389 } 390 } 391 392 private void updateFromTab() { 393 String displayTitle = mTab.getTitle(); 394 if (displayTitle == null) { 395 displayTitle = mTab.getUrl(); 396 } 397 setDisplayTitle(displayTitle); 398 setProgress(mTab.getLoadProgress()); 399 if (mTab.getFavicon() != null) { 400 setFavicon(renderFavicon(mTab.getFavicon())); 401 } 402 if (mTab != null) { 403 mIncognito.setVisibility( 404 mTab.isPrivateBrowsingEnabled() ? 405 View.VISIBLE : View.GONE); 406 } 407 } 408 409 @Override 410 public void setActivated(boolean selected) { 411 mSelected = selected; 412 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE); 413 mIndicator.setVisibility(View.GONE); 414 mTitle.setTextAppearance(mActivity, mSelected ? 415 R.style.TabTitleSelected : R.style.TabTitleUnselected); 416 setHorizontalFadingEdgeEnabled(!mSelected); 417 super.setActivated(selected); 418 updateLayoutParams(); 419 setFocusable(!selected); 420 postInvalidate(); 421 } 422 423 public void updateLayoutParams() { 424 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams(); 425 lp.width = mSelected ? mTabWidthSelected : mTabWidthUnselected; 426 lp.height = LayoutParams.MATCH_PARENT; 427 setLayoutParams(lp); 428 } 429 430 void setDisplayTitle(String title) { 431 mTitle.setText(title); 432 } 433 434 void setFavicon(Drawable d) { 435 mIconView.setImageDrawable(d); 436 } 437 438 void setLock(Drawable d) { 439 if (null == d) { 440 mLock.setVisibility(View.GONE); 441 } else { 442 mLock.setImageDrawable(d); 443 mLock.setVisibility(View.VISIBLE); 444 } 445 } 446 447 void setProgress(int newProgress) { 448 if (newProgress >= PROGRESS_MAX) { 449 mInLoad = false; 450 } else { 451 if (!mInLoad && getWindowToken() != null) { 452 mInLoad = true; 453 } 454 } 455 } 456 457 private void closeTab() { 458 if (mTab == mTabControl.getCurrentTab()) { 459 mUiController.closeCurrentTab(); 460 } else { 461 mUiController.closeTab(mTab); 462 } 463 } 464 465 @Override 466 protected void onLayout(boolean changed, int l, int t, int r, int b) { 467 super.onLayout(changed, l, t, r, b); 468 setTabPath(mPath, 0, 0, r - l, b - t); 469 setFocusPath(mFocusPath, 0, 0, r - l, b - t); 470 } 471 472 @Override 473 protected void dispatchDraw(Canvas canvas) { 474 if (mCurrentTextureWidth != mUi.getContentWidth() || 475 mCurrentTextureHeight != getHeight()) { 476 mCurrentTextureWidth = mUi.getContentWidth(); 477 mCurrentTextureHeight = getHeight(); 478 479 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) { 480 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable, 481 mCurrentTextureWidth, mCurrentTextureHeight); 482 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable, 483 mCurrentTextureWidth, mCurrentTextureHeight); 484 485 mActiveShader = new BitmapShader(activeTexture, 486 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 487 mActiveShaderPaint.setShader(mActiveShader); 488 489 mInactiveShader = new BitmapShader(inactiveTexture, 490 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 491 mInactiveShaderPaint.setShader(mInactiveShader); 492 } 493 } 494 // add some monkey protection 495 if ((mActiveShader != null) && (mInactiveShader != null)) { 496 int state = canvas.save(); 497 getLocationInWindow(mWindowPos); 498 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint; 499 drawClipped(canvas, paint, mPath, mWindowPos[0]); 500 canvas.restoreToCount(state); 501 } 502 super.dispatchDraw(canvas); 503 } 504 505 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) { 506 // TODO: We should change the matrix/shader only when needed 507 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix; 508 matrix.setTranslate(-left, 0.0f); 509 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix); 510 canvas.drawPath(clipPath, paint); 511 if (isFocused()) { 512 canvas.drawPath(mFocusPath, mFocusPaint); 513 } 514 } 515 516 private void setTabPath(Path path, int l, int t, int r, int b) { 517 path.reset(); 518 path.moveTo(l, b); 519 path.lineTo(l, t); 520 path.lineTo(r - mTabSliceWidth, t); 521 path.lineTo(r, b); 522 path.close(); 523 } 524 525 private void setFocusPath(Path path, int l, int t, int r, int b) { 526 path.reset(); 527 path.moveTo(l, b); 528 path.lineTo(l, t); 529 path.lineTo(r - mTabSliceWidth, t); 530 path.lineTo(r, b); 531 } 532 533 } 534 535 static Drawable createFaviconBackground(Context context) { 536 PaintDrawable faviconBackground = new PaintDrawable(); 537 Resources res = context.getResources(); 538 faviconBackground.getPaint().setColor(context.getResources() 539 .getColor(R.color.tabFaviconBackground)); 540 faviconBackground.setCornerRadius( 541 res.getDimension(R.dimen.tab_favicon_corner_radius)); 542 return faviconBackground; 543 } 544 545 private Drawable renderFavicon(Bitmap icon) { 546 Drawable[] array = new Drawable[2]; 547 array[0] = createFaviconBackground(getContext()); 548 if (icon == null) { 549 array[1] = mGenericFavicon; 550 } else { 551 array[1] = new BitmapDrawable(icon); 552 } 553 LayerDrawable d = new LayerDrawable(array); 554 d.setLayerInset(1, 2, 2, 2, 2); 555 return d; 556 } 557 558 private void animateTabOut(final Tab tab, final TabView tv) { 559 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f); 560 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f); 561 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f); 562 AnimatorSet animator = new AnimatorSet(); 563 animator.playTogether(scalex, scaley, alpha); 564 animator.setDuration(150); 565 animator.addListener(new AnimatorListener() { 566 567 @Override 568 public void onAnimationCancel(Animator animation) { 569 } 570 571 @Override 572 public void onAnimationEnd(Animator animation) { 573 mTabs.removeTab(tv); 574 mTabMap.remove(tab); 575 mUi.onRemoveTabCompleted(tab); 576 } 577 578 @Override 579 public void onAnimationRepeat(Animator animation) { 580 } 581 582 @Override 583 public void onAnimationStart(Animator animation) { 584 } 585 586 }); 587 animator.start(); 588 } 589 590 private void animateTabIn(final Tab tab, final TabView tv) { 591 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f); 592 scalex.setDuration(150); 593 scalex.addListener(new AnimatorListener() { 594 595 @Override 596 public void onAnimationCancel(Animator animation) { 597 } 598 599 @Override 600 public void onAnimationEnd(Animator animation) { 601 mUi.onAddTabCompleted(tab); 602 } 603 604 @Override 605 public void onAnimationRepeat(Animator animation) { 606 } 607 608 @Override 609 public void onAnimationStart(Animator animation) { 610 mTabs.addTab(tv); 611 } 612 613 }); 614 scalex.start(); 615 } 616 617 // TabChangeListener implementation 618 619 public void onSetActiveTab(Tab tab) { 620 mTabs.setSelectedTab(mTabControl.getTabPosition(tab)); 621 TabView tv = mTabMap.get(tab); 622 if (tv != null) { 623 tv.setProgress(tv.mTab.getLoadProgress()); 624 // update the scroll state 625 WebView webview = tab.getWebView(); 626 if (webview != null) { 627 int h = webview.getVisibleTitleHeight(); 628 onScroll(h, true); 629 } 630 } 631 } 632 633 public void onFavicon(Tab tab, Bitmap favicon) { 634 TabView tv = mTabMap.get(tab); 635 if (tv != null) { 636 tv.setFavicon(renderFavicon(favicon)); 637 } 638 } 639 640 public void onNewTab(Tab tab) { 641 TabView tv = buildTabView(tab); 642 animateTabIn(tab, tv); 643 } 644 645 public void onProgress(Tab tab, int progress) { 646 TabView tv = mTabMap.get(tab); 647 if (tv != null) { 648 tv.setProgress(progress); 649 } 650 } 651 652 public void onRemoveTab(Tab tab) { 653 TabView tv = mTabMap.get(tab); 654 if (tv != null) { 655 animateTabOut(tab, tv); 656 } else { 657 mTabMap.remove(tab); 658 } 659 } 660 661 public void onUrlAndTitle(Tab tab, String url, String title) { 662 TabView tv = mTabMap.get(tab); 663 if (tv != null) { 664 if (title != null) { 665 tv.setDisplayTitle(title); 666 } else if (url != null) { 667 tv.setDisplayTitle(UrlUtils.stripUrl(url)); 668 } 669 } 670 } 671 672 private boolean isLoading() { 673 TabView tv = mTabMap.get(mTabControl.getCurrentTab()); 674 if (tv != null) { 675 return tv.mInLoad; 676 } else { 677 return false; 678 } 679 } 680 681} 682