OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.toolbar; |
| 6 |
| 7 import android.content.res.Resources; |
| 8 import android.graphics.Bitmap; |
| 9 import android.graphics.BitmapFactory; |
| 10 import android.graphics.Canvas; |
| 11 import android.graphics.Paint.Align; |
| 12 import android.graphics.Rect; |
| 13 import android.graphics.Typeface; |
| 14 import android.text.TextPaint; |
| 15 |
| 16 import com.google.android.apps.chrome.R; |
| 17 |
| 18 import org.chromium.base.VisibleForTesting; |
| 19 import org.chromium.chrome.browser.widget.TintedDrawable; |
| 20 |
| 21 import java.util.Locale; |
| 22 |
| 23 /** |
| 24 * A drawable for the tab switcher icon. |
| 25 */ |
| 26 public class TabSwitcherDrawable extends TintedDrawable { |
| 27 private final float mSingleDigitTextSize; |
| 28 private final float mDoubleDigitTextSize; |
| 29 |
| 30 private final Rect mTextBounds = new Rect(); |
| 31 private final TextPaint mTextPaint; |
| 32 |
| 33 // Tab Count Label |
| 34 private int mTabCount; |
| 35 private boolean mIncognito; |
| 36 |
| 37 /** |
| 38 * Creates a {@link TabSwitcherDrawable}. |
| 39 * @param resources A {@link Resources} instance. |
| 40 * @param useLight Whether or not to use light or dark textures and text co
lors. |
| 41 * @return A {@link TabSwitcherDrawable} instance. |
| 42 */ |
| 43 public static TabSwitcherDrawable createTabSwitcherDrawable( |
| 44 Resources resources, boolean useLight) { |
| 45 Bitmap icon = BitmapFactory.decodeResource(resources, R.drawable.btn_tab
switcher); |
| 46 return new TabSwitcherDrawable(resources, useLight, icon); |
| 47 } |
| 48 |
| 49 private TabSwitcherDrawable(Resources resources, boolean useLight, Bitmap bi
tmap) { |
| 50 super(resources, bitmap); |
| 51 setTint(resources.getColorStateList(useLight ? R.color.light_mode_tint |
| 52 : R.color.dark_mode_tint)); |
| 53 mSingleDigitTextSize = |
| 54 resources.getDimension(R.dimen.toolbar_tab_count_text_size_1_dig
it); |
| 55 mDoubleDigitTextSize = |
| 56 resources.getDimension(R.dimen.toolbar_tab_count_text_size_2_dig
it); |
| 57 |
| 58 mTextPaint = new TextPaint(); |
| 59 mTextPaint.setAntiAlias(true); |
| 60 mTextPaint.setTextAlign(Align.CENTER); |
| 61 mTextPaint.setTypeface(Typeface.create("sans-serif-condensed", Typeface.
BOLD)); |
| 62 mTextPaint.setColor(getColorForState()); |
| 63 } |
| 64 |
| 65 @Override |
| 66 protected boolean onStateChange(int[] state) { |
| 67 boolean retVal = super.onStateChange(state); |
| 68 if (retVal) mTextPaint.setColor(getColorForState()); |
| 69 return retVal; |
| 70 } |
| 71 |
| 72 @Override |
| 73 public void draw(Canvas canvas) { |
| 74 super.draw(canvas); |
| 75 |
| 76 String textString = getTabCountString(); |
| 77 if (!textString.isEmpty()) { |
| 78 mTextPaint.getTextBounds(textString, 0, textString.length(), mTextBo
unds); |
| 79 |
| 80 Rect drawableBounds = getBounds(); |
| 81 int textX = drawableBounds.width() / 2; |
| 82 int textY = drawableBounds.height() / 2 + (mTextBounds.bottom - mTex
tBounds.top) / 2 |
| 83 - mTextBounds.bottom; |
| 84 |
| 85 canvas.drawText(textString, textX, textY, mTextPaint); |
| 86 } |
| 87 } |
| 88 |
| 89 /** |
| 90 * @return The current tab count this drawable is displaying. |
| 91 */ |
| 92 @VisibleForTesting |
| 93 public int getTabCount() { |
| 94 return mTabCount; |
| 95 } |
| 96 |
| 97 /** |
| 98 * Update the visual state based on the number of tabs present. |
| 99 * @param tabCount The number of tabs. |
| 100 */ |
| 101 public void updateForTabCount(int tabCount, boolean incognito) { |
| 102 if (tabCount == mTabCount && incognito == mIncognito) return; |
| 103 mTabCount = tabCount; |
| 104 mIncognito = incognito; |
| 105 float textSizePx = mTabCount > 9 ? mDoubleDigitTextSize : mSingleDigitTe
xtSize; |
| 106 mTextPaint.setTextSize(textSizePx); |
| 107 invalidateSelf(); |
| 108 } |
| 109 |
| 110 private String getTabCountString() { |
| 111 if (mTabCount <= 0) { |
| 112 return ""; |
| 113 } else if (mTabCount > 99) { |
| 114 return mIncognito ? ";)" : ":D"; |
| 115 } else { |
| 116 return String.format(Locale.getDefault(), "%d", mTabCount); |
| 117 } |
| 118 } |
| 119 |
| 120 private int getColorForState() { |
| 121 return mTint.getColorForState(getState(), 0); |
| 122 } |
| 123 } |
OLD | NEW |