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