The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 20 | import android.content.res.Resources; |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 22 | import android.graphics.Canvas; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 23 | import android.graphics.Color; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 24 | import android.graphics.Paint; |
| 25 | import android.graphics.RectF; |
| 26 | import android.graphics.drawable.Drawable; |
| 27 | import android.text.Layout; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 28 | import android.util.AttributeSet; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 29 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 30 | import com.android.launcher.R; |
| 31 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 32 | /** |
| 33 | * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan |
| 34 | * because we want to make the bubble taller than the text and TextView's clip is |
| 35 | * too aggressive. |
| 36 | */ |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 37 | public class BubbleTextView extends CacheableTextView { |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 38 | static final float CORNER_RADIUS = 4.0f; |
| 39 | static final float PADDING_H = 8.0f; |
| 40 | static final float PADDING_V = 3.0f; |
| 41 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 42 | private final RectF mRect = new RectF(); |
| 43 | private Paint mPaint; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 44 | private float mBubbleColorAlpha; |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 45 | private int mPrevAlpha = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 46 | |
| 47 | private boolean mBackgroundSizeChanged; |
| 48 | private Drawable mBackground; |
| 49 | private float mCornerRadius; |
| 50 | private float mPaddingH; |
| 51 | private float mPaddingV; |
| 52 | |
| 53 | public BubbleTextView(Context context) { |
| 54 | super(context); |
| 55 | init(); |
| 56 | } |
| 57 | |
| 58 | public BubbleTextView(Context context, AttributeSet attrs) { |
| 59 | super(context, attrs); |
| 60 | init(); |
| 61 | } |
| 62 | |
| 63 | public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 64 | super(context, attrs, defStyle); |
| 65 | init(); |
| 66 | } |
| 67 | |
| 68 | private void init() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 69 | mBackground = getBackground(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 70 | setFocusable(true); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 71 | setBackgroundDrawable(null); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 72 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 73 | final Resources res = getContext().getResources(); |
| 74 | int bubbleColor = res.getColor(R.color.bubble_dark_background); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 75 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 76 | mPaint.setColor(bubbleColor); |
| 77 | mBubbleColorAlpha = Color.alpha(bubbleColor) / 255.0f; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 78 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 79 | final float scale = res.getDisplayMetrics().density; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 80 | mCornerRadius = CORNER_RADIUS * scale; |
| 81 | mPaddingH = PADDING_H * scale; |
| 82 | //noinspection PointlessArithmeticExpression |
| 83 | mPaddingV = PADDING_V * scale; |
| 84 | } |
| 85 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 86 | protected int getVerticalPadding() { |
| 87 | return (int) PADDING_V; |
| 88 | } |
| 89 | protected int getHorizontalPadding() { |
| 90 | return (int) PADDING_H; |
| 91 | } |
| 92 | |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 93 | public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) { |
| 94 | Bitmap b = info.getIcon(iconCache); |
| 95 | |
| 96 | setCompoundDrawablesWithIntrinsicBounds(null, |
| 97 | new FastBitmapDrawable(b), |
| 98 | null, null); |
| 99 | setText(info.title); |
| 100 | buildAndEnableCache(); |
| 101 | setTag(info); |
| 102 | |
| 103 | } |
| 104 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 105 | @Override |
| 106 | protected boolean setFrame(int left, int top, int right, int bottom) { |
| 107 | if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) { |
| 108 | mBackgroundSizeChanged = true; |
| 109 | } |
| 110 | return super.setFrame(left, top, right, bottom); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | protected boolean verifyDrawable(Drawable who) { |
| 115 | return who == mBackground || super.verifyDrawable(who); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | protected void drawableStateChanged() { |
| 120 | Drawable d = mBackground; |
| 121 | if (d != null && d.isStateful()) { |
| 122 | d.setState(getDrawableState()); |
| 123 | } |
| 124 | super.drawableStateChanged(); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void draw(Canvas canvas) { |
| 129 | final Drawable background = mBackground; |
| 130 | if (background != null) { |
| 131 | final int scrollX = mScrollX; |
| 132 | final int scrollY = mScrollY; |
| 133 | |
| 134 | if (mBackgroundSizeChanged) { |
| 135 | background.setBounds(0, 0, mRight - mLeft, mBottom - mTop); |
| 136 | mBackgroundSizeChanged = false; |
| 137 | } |
| 138 | |
| 139 | if ((scrollX | scrollY) == 0) { |
| 140 | background.draw(canvas); |
| 141 | } else { |
| 142 | canvas.translate(scrollX, scrollY); |
| 143 | background.draw(canvas); |
| 144 | canvas.translate(-scrollX, -scrollY); |
| 145 | } |
| 146 | } |
| 147 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 148 | // Draw the hotdog bubble |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 149 | final Layout layout = getLayout(); |
Winson Chung | 07f2e81 | 2010-12-04 18:11:57 -0800 | [diff] [blame] | 150 | if (layout != null) { |
| 151 | final int offset = getExtendedPaddingTop(); |
| 152 | final int paddingLeft = getPaddingLeft(); |
| 153 | final int paddingRight = getPaddingRight(); |
| 154 | final float left = layout.getLineLeft(0) + paddingLeft; |
| 155 | final float right = Math.min(layout.getLineRight(0) + paddingRight, |
| 156 | left + getWidth() - paddingLeft - paddingRight); |
| 157 | mRect.set(left - mPaddingH, offset + (int) layout.getLineTop(0) - mPaddingV, |
| 158 | right + mPaddingH, offset + (int) layout.getLineBottom(0) + mPaddingV); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 159 | |
Winson Chung | 07f2e81 | 2010-12-04 18:11:57 -0800 | [diff] [blame] | 160 | canvas.drawRoundRect(mRect, mCornerRadius, mCornerRadius, mPaint); |
| 161 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 162 | |
| 163 | super.draw(canvas); |
| 164 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 165 | |
| 166 | @Override |
| 167 | protected void onAttachedToWindow() { |
| 168 | super.onAttachedToWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 169 | if (mBackground != null) mBackground.setCallback(this); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | protected void onDetachedFromWindow() { |
| 174 | super.onDetachedFromWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 175 | if (mBackground != null) mBackground.setCallback(null); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 176 | } |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 177 | |
| 178 | @Override |
| 179 | protected boolean onSetAlpha(int alpha) { |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 180 | if (mPrevAlpha != alpha) { |
| 181 | mPrevAlpha = alpha; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 182 | mPaint.setAlpha((int) (alpha * mBubbleColorAlpha)); |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 183 | super.onSetAlpha(alpha); |
| 184 | } |
| 185 | return true; |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 186 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 187 | } |