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; |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame^] | 39 | static final float SHADOW_LARGE_RADIUS = 4.0f; |
| 40 | static final float SHADOW_SMALL_RADIUS = 1.75f; |
| 41 | static final float SHADOW_Y_OFFSET = 2.0f; |
| 42 | static final int SHADOW_LARGE_COLOUR = 0xCC000000; |
| 43 | static final int SHADOW_SMALL_COLOUR = 0xBB000000; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 44 | static final float PADDING_H = 8.0f; |
| 45 | static final float PADDING_V = 3.0f; |
| 46 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 47 | private final RectF mRect = new RectF(); |
| 48 | private Paint mPaint; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 49 | private float mBubbleColorAlpha; |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 50 | private int mPrevAlpha = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 51 | |
| 52 | private boolean mBackgroundSizeChanged; |
| 53 | private Drawable mBackground; |
| 54 | private float mCornerRadius; |
| 55 | private float mPaddingH; |
| 56 | private float mPaddingV; |
| 57 | |
| 58 | public BubbleTextView(Context context) { |
| 59 | super(context); |
| 60 | init(); |
| 61 | } |
| 62 | |
| 63 | public BubbleTextView(Context context, AttributeSet attrs) { |
| 64 | super(context, attrs); |
| 65 | init(); |
| 66 | } |
| 67 | |
| 68 | public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 69 | super(context, attrs, defStyle); |
| 70 | init(); |
| 71 | } |
| 72 | |
| 73 | private void init() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 74 | mBackground = getBackground(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 75 | setFocusable(true); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 76 | setBackgroundDrawable(null); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 77 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 78 | final Resources res = getContext().getResources(); |
| 79 | int bubbleColor = res.getColor(R.color.bubble_dark_background); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 80 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 81 | mPaint.setColor(bubbleColor); |
| 82 | mBubbleColorAlpha = Color.alpha(bubbleColor) / 255.0f; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 83 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 84 | final float scale = res.getDisplayMetrics().density; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 85 | mCornerRadius = CORNER_RADIUS * scale; |
| 86 | mPaddingH = PADDING_H * scale; |
| 87 | //noinspection PointlessArithmeticExpression |
| 88 | mPaddingV = PADDING_V * scale; |
| 89 | } |
| 90 | |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame^] | 91 | protected int getCacheTopPadding() { |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 92 | return (int) PADDING_V; |
| 93 | } |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame^] | 94 | protected int getCacheBottomPadding() { |
| 95 | return (int) (PADDING_V + SHADOW_LARGE_RADIUS + SHADOW_Y_OFFSET); |
| 96 | } |
| 97 | protected int getCacheLeftPadding() { |
| 98 | return (int) (PADDING_H + SHADOW_LARGE_RADIUS); |
| 99 | } |
| 100 | protected int getCacheRightPadding() { |
| 101 | return (int) (PADDING_H + SHADOW_LARGE_RADIUS); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 104 | public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) { |
| 105 | Bitmap b = info.getIcon(iconCache); |
| 106 | |
| 107 | setCompoundDrawablesWithIntrinsicBounds(null, |
| 108 | new FastBitmapDrawable(b), |
| 109 | null, null); |
| 110 | setText(info.title); |
| 111 | buildAndEnableCache(); |
| 112 | setTag(info); |
| 113 | |
| 114 | } |
| 115 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 116 | @Override |
| 117 | protected boolean setFrame(int left, int top, int right, int bottom) { |
| 118 | if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) { |
| 119 | mBackgroundSizeChanged = true; |
| 120 | } |
| 121 | return super.setFrame(left, top, right, bottom); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | protected boolean verifyDrawable(Drawable who) { |
| 126 | return who == mBackground || super.verifyDrawable(who); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | protected void drawableStateChanged() { |
| 131 | Drawable d = mBackground; |
| 132 | if (d != null && d.isStateful()) { |
| 133 | d.setState(getDrawableState()); |
| 134 | } |
| 135 | super.drawableStateChanged(); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void draw(Canvas canvas) { |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame^] | 140 | if (isBuildingCache()) { |
| 141 | // We enhance the shadow by drawing the shadow twice |
| 142 | this.setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR); |
| 143 | super.draw(canvas); |
| 144 | this.setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR); |
| 145 | super.draw(canvas); |
| 146 | } else { |
| 147 | final Drawable background = mBackground; |
| 148 | if (background != null) { |
| 149 | final int scrollX = mScrollX; |
| 150 | final int scrollY = mScrollY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 151 | |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame^] | 152 | if (mBackgroundSizeChanged) { |
| 153 | background.setBounds(0, 0, mRight - mLeft, mBottom - mTop); |
| 154 | mBackgroundSizeChanged = false; |
| 155 | } |
| 156 | |
| 157 | if ((scrollX | scrollY) == 0) { |
| 158 | background.draw(canvas); |
| 159 | } else { |
| 160 | canvas.translate(scrollX, scrollY); |
| 161 | background.draw(canvas); |
| 162 | canvas.translate(-scrollX, -scrollY); |
| 163 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame^] | 166 | super.draw(canvas); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 167 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 168 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 169 | |
| 170 | @Override |
| 171 | protected void onAttachedToWindow() { |
| 172 | super.onAttachedToWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 173 | if (mBackground != null) mBackground.setCallback(this); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
| 177 | protected void onDetachedFromWindow() { |
| 178 | super.onDetachedFromWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 179 | if (mBackground != null) mBackground.setCallback(null); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 180 | } |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 181 | |
| 182 | @Override |
| 183 | protected boolean onSetAlpha(int alpha) { |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 184 | if (mPrevAlpha != alpha) { |
| 185 | mPrevAlpha = alpha; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 186 | mPaint.setAlpha((int) (alpha * mBubbleColorAlpha)); |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 187 | super.onSetAlpha(alpha); |
| 188 | } |
| 189 | return true; |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 190 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 191 | } |