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 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame^] | 19 | import com.android.launcher.R; |
| 20 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 22 | import android.content.res.Resources; |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 24 | import android.graphics.Canvas; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 25 | import android.graphics.Color; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | import android.graphics.Paint; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame^] | 27 | import android.graphics.Rect; |
| 28 | import android.graphics.Region.Op; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 30 | import android.util.AttributeSet; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame^] | 31 | import android.view.MotionEvent; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 32 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 33 | /** |
| 34 | * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan |
| 35 | * because we want to make the bubble taller than the text and TextView's clip is |
| 36 | * too aggressive. |
| 37 | */ |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 38 | public class BubbleTextView extends CacheableTextView { |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 39 | static final float CORNER_RADIUS = 4.0f; |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 40 | static final float SHADOW_LARGE_RADIUS = 4.0f; |
| 41 | static final float SHADOW_SMALL_RADIUS = 1.75f; |
| 42 | static final float SHADOW_Y_OFFSET = 2.0f; |
| 43 | static final int SHADOW_LARGE_COLOUR = 0xCC000000; |
| 44 | static final int SHADOW_SMALL_COLOUR = 0xBB000000; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 45 | static final float PADDING_H = 8.0f; |
| 46 | static final float PADDING_V = 3.0f; |
| 47 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame^] | 52 | private final HolographicOutlineHelper mOutlineHelper = new HolographicOutlineHelper(); |
| 53 | private final Canvas mTempCanvas = new Canvas(); |
| 54 | private final Rect mTempRect = new Rect(); |
| 55 | private final Paint mTempPaint = new Paint(); |
| 56 | private boolean mDidInvalidateForPressedState; |
| 57 | private Bitmap mPressedOrFocusedBackground; |
| 58 | private int mFocusedOutlineColor; |
| 59 | private int mFocusedGlowColor; |
| 60 | private int mPressedOutlineColor; |
| 61 | private int mPressedGlowColor; |
| 62 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 63 | private boolean mBackgroundSizeChanged; |
| 64 | private Drawable mBackground; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 65 | |
| 66 | public BubbleTextView(Context context) { |
| 67 | super(context); |
| 68 | init(); |
| 69 | } |
| 70 | |
| 71 | public BubbleTextView(Context context, AttributeSet attrs) { |
| 72 | super(context, attrs); |
| 73 | init(); |
| 74 | } |
| 75 | |
| 76 | public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 77 | super(context, attrs, defStyle); |
| 78 | init(); |
| 79 | } |
| 80 | |
| 81 | private void init() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 82 | mBackground = getBackground(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 83 | setFocusable(true); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 84 | setBackgroundDrawable(null); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 85 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 86 | final Resources res = getContext().getResources(); |
| 87 | int bubbleColor = res.getColor(R.color.bubble_dark_background); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 88 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 89 | mPaint.setColor(bubbleColor); |
| 90 | mBubbleColorAlpha = Color.alpha(bubbleColor) / 255.0f; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame^] | 91 | mFocusedOutlineColor = |
| 92 | getResources().getColor(R.color.workspace_item_focused_outline_color); |
| 93 | mFocusedGlowColor = getResources().getColor(R.color.workspace_item_focused_glow_color); |
| 94 | mPressedOutlineColor = |
| 95 | getResources().getColor(R.color.workspace_item_pressed_outline_color); |
| 96 | mPressedGlowColor = |
| 97 | getResources().getColor(R.color.workspace_item_pressed_glow_color); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 100 | protected int getCacheTopPadding() { |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 101 | return (int) PADDING_V; |
| 102 | } |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 103 | protected int getCacheBottomPadding() { |
| 104 | return (int) (PADDING_V + SHADOW_LARGE_RADIUS + SHADOW_Y_OFFSET); |
| 105 | } |
| 106 | protected int getCacheLeftPadding() { |
| 107 | return (int) (PADDING_H + SHADOW_LARGE_RADIUS); |
| 108 | } |
| 109 | protected int getCacheRightPadding() { |
| 110 | return (int) (PADDING_H + SHADOW_LARGE_RADIUS); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 113 | public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) { |
| 114 | Bitmap b = info.getIcon(iconCache); |
| 115 | |
| 116 | setCompoundDrawablesWithIntrinsicBounds(null, |
| 117 | new FastBitmapDrawable(b), |
| 118 | null, null); |
| 119 | setText(info.title); |
| 120 | buildAndEnableCache(); |
| 121 | setTag(info); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 122 | } |
| 123 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 124 | @Override |
| 125 | protected boolean setFrame(int left, int top, int right, int bottom) { |
| 126 | if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) { |
| 127 | mBackgroundSizeChanged = true; |
| 128 | } |
| 129 | return super.setFrame(left, top, right, bottom); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | protected boolean verifyDrawable(Drawable who) { |
| 134 | return who == mBackground || super.verifyDrawable(who); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | protected void drawableStateChanged() { |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame^] | 139 | if (isPressed()) { |
| 140 | // In this case, we have already created the pressed outline on ACTION_DOWN, |
| 141 | // so we just need to do an invalidate to trigger draw |
| 142 | if (!mDidInvalidateForPressedState) { |
| 143 | invalidate(); |
| 144 | } |
| 145 | } else { |
| 146 | // Otherwise, either clear the pressed/focused background, or create a background |
| 147 | // for the focused state |
| 148 | final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null; |
| 149 | mPressedOrFocusedBackground = null; |
| 150 | if (isFocused()) { |
| 151 | mPressedOrFocusedBackground = createGlowingOutline( |
| 152 | mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor); |
| 153 | invalidate(); |
| 154 | } |
| 155 | final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null; |
| 156 | if (!backgroundEmptyBefore && backgroundEmptyNow) { |
| 157 | invalidate(); |
| 158 | } |
| 159 | } |
| 160 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 161 | Drawable d = mBackground; |
| 162 | if (d != null && d.isStateful()) { |
| 163 | d.setState(getDrawableState()); |
| 164 | } |
| 165 | super.drawableStateChanged(); |
| 166 | } |
| 167 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame^] | 168 | /** |
| 169 | * Draw the View v into the given Canvas. |
| 170 | * |
| 171 | * @param v the view to draw |
| 172 | * @param destCanvas the canvas to draw on |
| 173 | * @param padding the horizontal and vertical padding to use when drawing |
| 174 | */ |
| 175 | private void drawWithPadding(Canvas destCanvas, int padding) { |
| 176 | final Rect clipRect = mTempRect; |
| 177 | getDrawingRect(clipRect); |
| 178 | |
| 179 | // adjust the clip rect so that we don't include the text label |
| 180 | clipRect.bottom = |
| 181 | getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0); |
| 182 | |
| 183 | // Draw the View into the bitmap. |
| 184 | // The translate of scrollX and scrollY is necessary when drawing TextViews, because |
| 185 | // they set scrollX and scrollY to large values to achieve centered text |
| 186 | destCanvas.save(); |
| 187 | destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2); |
| 188 | destCanvas.clipRect(clipRect, Op.REPLACE); |
| 189 | draw(destCanvas); |
| 190 | destCanvas.restore(); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. |
| 195 | * Responsibility for the bitmap is transferred to the caller. |
| 196 | */ |
| 197 | private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) { |
| 198 | final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS; |
| 199 | final Bitmap b = Bitmap.createBitmap( |
| 200 | getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888); |
| 201 | |
| 202 | canvas.setBitmap(b); |
| 203 | drawWithPadding(canvas, padding); |
| 204 | mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor); |
| 205 | |
| 206 | return b; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public boolean onTouchEvent(MotionEvent event) { |
| 211 | // Call the superclass onTouchEvent first, because sometimes it changes the state to |
| 212 | // isPressed() on an ACTION_UP |
| 213 | boolean result = super.onTouchEvent(event); |
| 214 | |
| 215 | switch (event.getAction()) { |
| 216 | case MotionEvent.ACTION_DOWN: |
| 217 | // So that the pressed outline is visible immediately when isPressed() is true, |
| 218 | // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time |
| 219 | // to create it) |
| 220 | if (mPressedOrFocusedBackground == null) { |
| 221 | mPressedOrFocusedBackground = createGlowingOutline( |
| 222 | mTempCanvas, mPressedGlowColor, mPressedOutlineColor); |
| 223 | } |
| 224 | // Invalidate so the pressed state is visible, or set a flag so we know that we |
| 225 | // have to call invalidate as soon as the state is "pressed" |
| 226 | if (isPressed()) { |
| 227 | mDidInvalidateForPressedState = true; |
| 228 | invalidate(); |
| 229 | } else { |
| 230 | mDidInvalidateForPressedState = false; |
| 231 | } |
| 232 | break; |
| 233 | case MotionEvent.ACTION_CANCEL: |
| 234 | case MotionEvent.ACTION_UP: |
| 235 | // If we've touched down and up on an item, and it's still not "pressed", then |
| 236 | // destroy the pressed outline |
| 237 | if (!isPressed()) { |
| 238 | mPressedOrFocusedBackground = null; |
| 239 | } |
| 240 | break; |
| 241 | } |
| 242 | return result; |
| 243 | } |
| 244 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 245 | @Override |
| 246 | public void draw(Canvas canvas) { |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame^] | 247 | if (mPressedOrFocusedBackground != null && (isPressed() || isFocused())) { |
| 248 | canvas.drawBitmap(mPressedOrFocusedBackground, |
| 249 | mScrollX - HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2, |
| 250 | mScrollY - HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2, |
| 251 | mTempPaint); |
| 252 | } |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 253 | if (isBuildingCache()) { |
| 254 | // We enhance the shadow by drawing the shadow twice |
| 255 | this.setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR); |
| 256 | super.draw(canvas); |
| 257 | this.setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR); |
| 258 | super.draw(canvas); |
| 259 | } else { |
| 260 | final Drawable background = mBackground; |
| 261 | if (background != null) { |
| 262 | final int scrollX = mScrollX; |
| 263 | final int scrollY = mScrollY; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 264 | |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 265 | if (mBackgroundSizeChanged) { |
| 266 | background.setBounds(0, 0, mRight - mLeft, mBottom - mTop); |
| 267 | mBackgroundSizeChanged = false; |
| 268 | } |
| 269 | |
| 270 | if ((scrollX | scrollY) == 0) { |
| 271 | background.draw(canvas); |
| 272 | } else { |
| 273 | canvas.translate(scrollX, scrollY); |
| 274 | background.draw(canvas); |
| 275 | canvas.translate(-scrollX, -scrollY); |
| 276 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 277 | } |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 278 | super.draw(canvas); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 279 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 280 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 281 | |
| 282 | @Override |
| 283 | protected void onAttachedToWindow() { |
| 284 | super.onAttachedToWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 285 | if (mBackground != null) mBackground.setCallback(this); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | @Override |
| 289 | protected void onDetachedFromWindow() { |
| 290 | super.onDetachedFromWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 291 | if (mBackground != null) mBackground.setCallback(null); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 292 | } |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 293 | |
| 294 | @Override |
| 295 | protected boolean onSetAlpha(int alpha) { |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 296 | if (mPrevAlpha != alpha) { |
| 297 | mPrevAlpha = alpha; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 298 | mPaint.setAlpha((int) (alpha * mBubbleColorAlpha)); |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 299 | super.onSetAlpha(alpha); |
| 300 | } |
| 301 | return true; |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 302 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 303 | } |