TabBar.java revision b9a051bd9ed974c4d3d29a549bb7e00d8a95c33c
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.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 // add some monkey protection 479 if ((mActiveShader != null) && (mInactiveShader != null)) { 480 int state = canvas.save(); 481 getLocationInWindow(mWindowPos); 482 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint; 483 drawClipped(canvas, paint, mPath, mWindowPos[0]); 484 canvas.restoreToCount(state); 485 } 486 super.dispatchDraw(canvas); 487 } 488 489 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) { 490 // TODO: We should change the matrix/shader only when needed 491 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix; 492 matrix.setTranslate(-left, 0.0f); 493 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix); 494 canvas.drawPath(clipPath, paint); 495 if (isFocused()) { 496 canvas.drawPath(mFocusPath, mFocusPaint); 497 } 498 } 499 500 private void setTabPath(Path path, int l, int t, int r, int b) { 501 path.reset(); 502 path.moveTo(l, b); 503 path.lineTo(l, t); 504 path.lineTo(r - mTabSliceWidth, t); 505 path.lineTo(r, b); 506 path.close(); 507 } 508 509 private void setFocusPath(Path path, int l, int t, int r, int b) { 510 path.reset(); 511 path.moveTo(l, b); 512 path.lineTo(l, t); 513 path.lineTo(r - mTabSliceWidth, t); 514 path.lineTo(r, b); 515 } 516 517 } 518 519 static Drawable createFaviconBackground(Context context) { 520 PaintDrawable faviconBackground = new PaintDrawable(); 521 Resources res = context.getResources(); 522 faviconBackground.getPaint().setColor(context.getResources() 523 .getColor(R.color.tabFaviconBackground)); 524 faviconBackground.setCornerRadius( 525 res.getDimension(R.dimen.tab_favicon_corner_radius)); 526 return faviconBackground; 527 } 528 529 private Drawable renderFavicon(Bitmap icon) { 530 Drawable[] array = new Drawable[2]; 531 array[0] = createFaviconBackground(getContext()); 532 if (icon == null) { 533 array[1] = mGenericFavicon; 534 } else { 535 array[1] = new BitmapDrawable(icon); 536 } 537 LayerDrawable d = new LayerDrawable(array); 538 d.setLayerInset(1, 2, 2, 2, 2); 539 return d; 540 } 541 542 private void animateTabOut(final Tab tab, final TabView tv) { 543 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f); 544 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f); 545 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f); 546 AnimatorSet animator = new AnimatorSet(); 547 animator.playTogether(scalex, scaley, alpha); 548 animator.setDuration(150); 549 animator.addListener(new AnimatorListener() { 550 551 @Override 552 public void onAnimationCancel(Animator animation) { 553 } 554 555 @Override 556 public void onAnimationEnd(Animator animation) { 557 mTabs.removeTab(tv); 558 mTabMap.remove(tab); 559 mUi.onRemoveTabCompleted(tab); 560 } 561 562 @Override 563 public void onAnimationRepeat(Animator animation) { 564 } 565 566 @Override 567 public void onAnimationStart(Animator animation) { 568 } 569 570 }); 571 animator.start(); 572 } 573 574 private void animateTabIn(final Tab tab, final TabView tv) { 575 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f); 576 scalex.setDuration(150); 577 scalex.addListener(new AnimatorListener() { 578 579 @Override 580 public void onAnimationCancel(Animator animation) { 581 } 582 583 @Override 584 public void onAnimationEnd(Animator animation) { 585 mUi.onAddTabCompleted(tab); 586 } 587 588 @Override 589 public void onAnimationRepeat(Animator animation) { 590 } 591 592 @Override 593 public void onAnimationStart(Animator animation) { 594 mTabs.addTab(tv); 595 } 596 597 }); 598 scalex.start(); 599 } 600 601 // TabChangeListener implementation 602 603 public void onSetActiveTab(Tab tab) { 604 mTabs.setSelectedTab(mTabControl.getTabIndex(tab)); 605 TabView tv = mTabMap.get(tab); 606 if (tv != null) { 607 tv.setProgress(tv.mTab.getLoadProgress()); 608 // update the scroll state 609 WebView webview = tab.getWebView(); 610 if (webview != null) { 611 int h = webview.getVisibleTitleHeight(); 612 onScroll(h, true); 613 } 614 } 615 } 616 617 public void onFavicon(Tab tab, Bitmap favicon) { 618 TabView tv = mTabMap.get(tab); 619 if (tv != null) { 620 tv.setFavicon(renderFavicon(favicon)); 621 } 622 } 623 624 public void onNewTab(Tab tab) { 625 TabView tv = buildTabView(tab); 626 animateTabIn(tab, tv); 627 } 628 629 public void onProgress(Tab tab, int progress) { 630 TabView tv = mTabMap.get(tab); 631 if (tv != null) { 632 tv.setProgress(progress); 633 } 634 } 635 636 public void onRemoveTab(Tab tab) { 637 TabView tv = mTabMap.get(tab); 638 if (tv != null) { 639 animateTabOut(tab, tv); 640 } else { 641 mTabMap.remove(tab); 642 } 643 } 644 645 public void onUrlAndTitle(Tab tab, String url, String title) { 646 TabView tv = mTabMap.get(tab); 647 if (tv != null) { 648 if (title != null) { 649 tv.setDisplayTitle(title); 650 } else if (url != null) { 651 tv.setDisplayTitle(UrlUtils.stripUrl(url)); 652 } 653 } 654 } 655 656 private boolean isLoading() { 657 TabView tv = mTabMap.get(mTabControl.getCurrentTab()); 658 if (tv != null) { 659 return tv.mInLoad; 660 } else { 661 return false; 662 } 663 } 664 665} 666