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