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