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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
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; |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 20 | import android.content.res.ColorStateList; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 21 | import android.content.res.Resources; |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 22 | import android.content.res.TypedArray; |
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; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 25 | import android.graphics.Rect; |
Michael Jurka | 137142e | 2011-01-05 20:57:04 -0800 | [diff] [blame] | 26 | import android.graphics.Region; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 27 | import android.graphics.Region.Op; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 30 | import android.util.Log; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 31 | import android.util.TypedValue; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 32 | import android.view.MotionEvent; |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 33 | import android.view.ViewConfiguration; |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 34 | import android.widget.TextView; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 35 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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 | */ |
Michael Jurka | 08ee770 | 2011-08-11 16:53:35 -0700 | [diff] [blame] | 41 | public class BubbleTextView extends TextView { |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 42 | static final float SHADOW_LARGE_RADIUS = 4.0f; |
| 43 | static final float SHADOW_SMALL_RADIUS = 1.75f; |
| 44 | static final float SHADOW_Y_OFFSET = 2.0f; |
Winson Chung | c7aef8c | 2011-09-15 18:53:04 -0700 | [diff] [blame] | 45 | static final int SHADOW_LARGE_COLOUR = 0xDD000000; |
| 46 | static final int SHADOW_SMALL_COLOUR = 0xCC000000; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 47 | static final float PADDING_H = 8.0f; |
| 48 | static final float PADDING_V = 3.0f; |
| 49 | |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 50 | private static final String TAG = "BubbleTextView"; |
| 51 | |
| 52 | private static final boolean DEBUG = false; |
| 53 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 54 | private HolographicOutlineHelper mOutlineHelper; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 55 | private final Canvas mTempCanvas = new Canvas(); |
| 56 | private final Rect mTempRect = new Rect(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 57 | private boolean mDidInvalidateForPressedState; |
| 58 | private Bitmap mPressedOrFocusedBackground; |
| 59 | private int mFocusedOutlineColor; |
| 60 | private int mFocusedGlowColor; |
| 61 | private int mPressedOutlineColor; |
| 62 | private int mPressedGlowColor; |
| 63 | |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 64 | private float mSlop; |
| 65 | |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 66 | private int mTextColor; |
Sunny Goyal | 15872da | 2014-07-08 15:43:54 -0700 | [diff] [blame] | 67 | private final boolean mCustomShadowsEnabled; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 68 | private boolean mIsTextVisible; |
| 69 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 70 | private boolean mBackgroundSizeChanged; |
Sunny Goyal | 15872da | 2014-07-08 15:43:54 -0700 | [diff] [blame] | 71 | private final Drawable mBackground; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 72 | |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 73 | private boolean mStayPressed; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 74 | private CheckLongPressHelper mLongPressHelper; |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 75 | |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 76 | private CharSequence mDefaultText = ""; |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 77 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 78 | public BubbleTextView(Context context) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 79 | this(context, null, 0); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | public BubbleTextView(Context context, AttributeSet attrs) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 83 | this(context, attrs, 0); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 87 | super(context, attrs, defStyle); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 88 | |
| 89 | Resources res = context.getResources(); |
| 90 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 91 | R.styleable.BubbleTextView, defStyle, 0); |
| 92 | setGlowColor(a.getColor(R.styleable.BubbleTextView_glowColor, |
| 93 | res.getColor(R.color.outline_color))); |
| 94 | mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true); |
| 95 | a.recycle(); |
| 96 | |
Sunny Goyal | 15872da | 2014-07-08 15:43:54 -0700 | [diff] [blame] | 97 | if (mCustomShadowsEnabled) { |
| 98 | // Draw the background itself as the parent is drawn twice. |
| 99 | mBackground = getBackground(); |
| 100 | setBackground(null); |
| 101 | } else { |
| 102 | mBackground = null; |
| 103 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 104 | init(); |
| 105 | } |
| 106 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 107 | public void onFinishInflate() { |
| 108 | super.onFinishInflate(); |
| 109 | |
| 110 | // Ensure we are using the right text size |
| 111 | LauncherAppState app = LauncherAppState.getInstance(); |
| 112 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 113 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 114 | } |
| 115 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 116 | private void init() { |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 117 | mLongPressHelper = new CheckLongPressHelper(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 118 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 119 | mOutlineHelper = HolographicOutlineHelper.obtain(getContext()); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 120 | if (mCustomShadowsEnabled) { |
| 121 | setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR); |
| 122 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 125 | public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache, |
| 126 | boolean setDefaultPadding) { |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 127 | Bitmap b = info.getIcon(iconCache); |
Winson Chung | cdef044 | 2013-09-19 17:32:58 -0700 | [diff] [blame] | 128 | LauncherAppState app = LauncherAppState.getInstance(); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 129 | |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 130 | FastBitmapDrawable iconDrawable = Utilities.createIconDrawable(b); |
| 131 | if (info.isDisabled) { |
| 132 | iconDrawable.setSaturation(0); |
| 133 | iconDrawable.setBrightness(20); |
| 134 | } |
| 135 | |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 136 | setCompoundDrawables(null, iconDrawable, null, null); |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 137 | if (setDefaultPadding) { |
| 138 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 139 | setCompoundDrawablePadding(grid.iconDrawablePaddingPx); |
| 140 | } |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 141 | if (info.contentDescription != null) { |
| 142 | setContentDescription(info.contentDescription); |
| 143 | } |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 144 | setTag(info); |
Sunny Goyal | 3484638 | 2014-07-09 00:09:28 -0700 | [diff] [blame] | 145 | |
| 146 | if (info.wasPromise) { |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 147 | applyState(); |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 148 | } |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 149 | } |
| 150 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 151 | @Override |
| 152 | protected boolean setFrame(int left, int top, int right, int bottom) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 153 | if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 154 | mBackgroundSizeChanged = true; |
| 155 | } |
| 156 | return super.setFrame(left, top, right, bottom); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | protected boolean verifyDrawable(Drawable who) { |
| 161 | return who == mBackground || super.verifyDrawable(who); |
| 162 | } |
| 163 | |
| 164 | @Override |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 165 | public void setTag(Object tag) { |
| 166 | if (tag != null) { |
| 167 | LauncherModel.checkItemInfo((ItemInfo) tag); |
| 168 | } |
| 169 | super.setTag(tag); |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 170 | if (tag instanceof ShortcutInfo) { |
| 171 | final ShortcutInfo info = (ShortcutInfo) tag; |
| 172 | mDefaultText = info.title; |
| 173 | setText(mDefaultText); |
| 174 | } |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 178 | protected void drawableStateChanged() { |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 179 | if (isPressed()) { |
| 180 | // In this case, we have already created the pressed outline on ACTION_DOWN, |
| 181 | // so we just need to do an invalidate to trigger draw |
| 182 | if (!mDidInvalidateForPressedState) { |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 183 | setCellLayoutPressedOrFocusedIcon(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 184 | } |
| 185 | } else { |
| 186 | // Otherwise, either clear the pressed/focused background, or create a background |
| 187 | // for the focused state |
| 188 | final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null; |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 189 | if (!mStayPressed) { |
| 190 | mPressedOrFocusedBackground = null; |
| 191 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 192 | if (isFocused()) { |
Gilles Debunne | c786952 | 2011-11-28 18:03:56 -0800 | [diff] [blame] | 193 | if (getLayout() == null) { |
Patrick Dubroy | a017c03 | 2011-03-09 15:58:32 -0800 | [diff] [blame] | 194 | // In some cases, we get focus before we have been layed out. Set the |
| 195 | // background to null so that it will get created when the view is drawn. |
| 196 | mPressedOrFocusedBackground = null; |
| 197 | } else { |
| 198 | mPressedOrFocusedBackground = createGlowingOutline( |
| 199 | mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor); |
| 200 | } |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 201 | mStayPressed = false; |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 202 | setCellLayoutPressedOrFocusedIcon(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 203 | } |
| 204 | final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null; |
| 205 | if (!backgroundEmptyBefore && backgroundEmptyNow) { |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 206 | setCellLayoutPressedOrFocusedIcon(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 210 | Drawable d = mBackground; |
| 211 | if (d != null && d.isStateful()) { |
| 212 | d.setState(getDrawableState()); |
| 213 | } |
| 214 | super.drawableStateChanged(); |
| 215 | } |
| 216 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 217 | /** |
Patrick Dubroy | cd95371 | 2011-02-28 15:16:42 -0800 | [diff] [blame] | 218 | * Draw this BubbleTextView into the given Canvas. |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 219 | * |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 220 | * @param destCanvas the canvas to draw on |
| 221 | * @param padding the horizontal and vertical padding to use when drawing |
| 222 | */ |
| 223 | private void drawWithPadding(Canvas destCanvas, int padding) { |
| 224 | final Rect clipRect = mTempRect; |
| 225 | getDrawingRect(clipRect); |
| 226 | |
| 227 | // adjust the clip rect so that we don't include the text label |
| 228 | clipRect.bottom = |
| 229 | getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0); |
| 230 | |
| 231 | // Draw the View into the bitmap. |
| 232 | // The translate of scrollX and scrollY is necessary when drawing TextViews, because |
| 233 | // they set scrollX and scrollY to large values to achieve centered text |
| 234 | destCanvas.save(); |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 235 | destCanvas.scale(getScaleX(), getScaleY(), |
| 236 | (getWidth() + padding) / 2, (getHeight() + padding) / 2); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 237 | destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2); |
| 238 | destCanvas.clipRect(clipRect, Op.REPLACE); |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 239 | draw(destCanvas); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 240 | destCanvas.restore(); |
| 241 | } |
| 242 | |
Adam Cohen | 7397e62 | 2013-10-24 12:11:34 -0700 | [diff] [blame] | 243 | public void setGlowColor(int color) { |
| 244 | mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor = color; |
| 245 | } |
| 246 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 247 | /** |
| 248 | * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. |
| 249 | * Responsibility for the bitmap is transferred to the caller. |
| 250 | */ |
| 251 | private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 252 | final int padding = mOutlineHelper.mMaxOuterBlurRadius; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 253 | final Bitmap b = Bitmap.createBitmap( |
| 254 | getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888); |
| 255 | |
| 256 | canvas.setBitmap(b); |
| 257 | drawWithPadding(canvas, padding); |
| 258 | mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 259 | canvas.setBitmap(null); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 260 | |
| 261 | return b; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public boolean onTouchEvent(MotionEvent event) { |
| 266 | // Call the superclass onTouchEvent first, because sometimes it changes the state to |
| 267 | // isPressed() on an ACTION_UP |
| 268 | boolean result = super.onTouchEvent(event); |
| 269 | |
| 270 | switch (event.getAction()) { |
| 271 | case MotionEvent.ACTION_DOWN: |
| 272 | // So that the pressed outline is visible immediately when isPressed() is true, |
| 273 | // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time |
| 274 | // to create it) |
| 275 | if (mPressedOrFocusedBackground == null) { |
| 276 | mPressedOrFocusedBackground = createGlowingOutline( |
| 277 | mTempCanvas, mPressedGlowColor, mPressedOutlineColor); |
| 278 | } |
| 279 | // Invalidate so the pressed state is visible, or set a flag so we know that we |
| 280 | // have to call invalidate as soon as the state is "pressed" |
| 281 | if (isPressed()) { |
| 282 | mDidInvalidateForPressedState = true; |
Michael Jurka | e6235dd | 2011-10-04 15:02:05 -0700 | [diff] [blame] | 283 | setCellLayoutPressedOrFocusedIcon(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 284 | } else { |
| 285 | mDidInvalidateForPressedState = false; |
| 286 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 287 | |
| 288 | mLongPressHelper.postCheckForLongPress(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 289 | break; |
| 290 | case MotionEvent.ACTION_CANCEL: |
| 291 | case MotionEvent.ACTION_UP: |
| 292 | // If we've touched down and up on an item, and it's still not "pressed", then |
| 293 | // destroy the pressed outline |
| 294 | if (!isPressed()) { |
| 295 | mPressedOrFocusedBackground = null; |
| 296 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 297 | |
| 298 | mLongPressHelper.cancelLongPress(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 299 | break; |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 300 | case MotionEvent.ACTION_MOVE: |
| 301 | if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) { |
| 302 | mLongPressHelper.cancelLongPress(); |
| 303 | } |
| 304 | break; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 305 | } |
| 306 | return result; |
| 307 | } |
| 308 | |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 309 | void setStayPressed(boolean stayPressed) { |
| 310 | mStayPressed = stayPressed; |
| 311 | if (!stayPressed) { |
| 312 | mPressedOrFocusedBackground = null; |
| 313 | } |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 314 | setCellLayoutPressedOrFocusedIcon(); |
| 315 | } |
| 316 | |
| 317 | void setCellLayoutPressedOrFocusedIcon() { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 318 | if (getParent() instanceof ShortcutAndWidgetContainer) { |
| 319 | ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent(); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 320 | if (parent != null) { |
| 321 | CellLayout layout = (CellLayout) parent.getParent(); |
| 322 | layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null); |
| 323 | } |
Patrick Dubroy | d69e113 | 2011-03-15 10:29:01 -0700 | [diff] [blame] | 324 | } |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Winson Chung | 1e9cbfe | 2011-09-30 16:52:26 -0700 | [diff] [blame] | 327 | void clearPressedOrFocusedBackground() { |
| 328 | mPressedOrFocusedBackground = null; |
| 329 | setCellLayoutPressedOrFocusedIcon(); |
| 330 | } |
| 331 | |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 332 | Bitmap getPressedOrFocusedBackground() { |
| 333 | return mPressedOrFocusedBackground; |
| 334 | } |
| 335 | |
| 336 | int getPressedOrFocusedBackgroundPadding() { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 337 | return mOutlineHelper.mMaxOuterBlurRadius / 2; |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 338 | } |
Patrick Dubroy | a017c03 | 2011-03-09 15:58:32 -0800 | [diff] [blame] | 339 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 340 | @Override |
| 341 | public void draw(Canvas canvas) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 342 | if (!mCustomShadowsEnabled) { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 343 | super.draw(canvas); |
| 344 | return; |
| 345 | } |
| 346 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 347 | final Drawable background = mBackground; |
| 348 | if (background != null) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 349 | final int scrollX = getScrollX(); |
| 350 | final int scrollY = getScrollY(); |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 351 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 352 | if (mBackgroundSizeChanged) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 353 | background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop()); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 354 | mBackgroundSizeChanged = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 355 | } |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 356 | |
| 357 | if ((scrollX | scrollY) == 0) { |
| 358 | background.draw(canvas); |
| 359 | } else { |
| 360 | canvas.translate(scrollX, scrollY); |
| 361 | background.draw(canvas); |
| 362 | canvas.translate(-scrollX, -scrollY); |
| 363 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 364 | } |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 365 | |
| 366 | // If text is transparent, don't draw any shadow |
Andrew Flynn | bc239a1 | 2012-03-06 11:39:49 -0800 | [diff] [blame] | 367 | if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) { |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 368 | getPaint().clearShadowLayer(); |
| 369 | super.draw(canvas); |
| 370 | return; |
| 371 | } |
| 372 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 373 | // We enhance the shadow by drawing the shadow twice |
Michael Jurka | e7e3f6c | 2011-02-01 21:08:29 -0800 | [diff] [blame] | 374 | getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 375 | super.draw(canvas); |
| 376 | canvas.save(Canvas.CLIP_SAVE_FLAG); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 377 | canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(), |
| 378 | getScrollX() + getWidth(), |
| 379 | getScrollY() + getHeight(), Region.Op.INTERSECT); |
Michael Jurka | e7e3f6c | 2011-02-01 21:08:29 -0800 | [diff] [blame] | 380 | getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 381 | super.draw(canvas); |
| 382 | canvas.restore(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 383 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 384 | |
| 385 | @Override |
| 386 | protected void onAttachedToWindow() { |
| 387 | super.onAttachedToWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 388 | if (mBackground != null) mBackground.setCallback(this); |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 389 | mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | @Override |
| 393 | protected void onDetachedFromWindow() { |
| 394 | super.onDetachedFromWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 395 | if (mBackground != null) mBackground.setCallback(null); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 396 | } |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 397 | |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 398 | @Override |
| 399 | public void setTextColor(int color) { |
| 400 | mTextColor = color; |
| 401 | super.setTextColor(color); |
| 402 | } |
| 403 | |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 404 | @Override |
| 405 | public void setTextColor(ColorStateList colors) { |
| 406 | mTextColor = colors.getDefaultColor(); |
| 407 | super.setTextColor(colors); |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 410 | public void setTextVisibility(boolean visible) { |
| 411 | Resources res = getResources(); |
| 412 | if (visible) { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 413 | super.setTextColor(mTextColor); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 414 | } else { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 415 | super.setTextColor(res.getColor(android.R.color.transparent)); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 416 | } |
| 417 | mIsTextVisible = visible; |
| 418 | } |
| 419 | |
| 420 | public boolean isTextVisible() { |
| 421 | return mIsTextVisible; |
| 422 | } |
| 423 | |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 424 | @Override |
| 425 | protected boolean onSetAlpha(int alpha) { |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 426 | return true; |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 427 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 428 | |
| 429 | @Override |
| 430 | public void cancelLongPress() { |
| 431 | super.cancelLongPress(); |
| 432 | |
| 433 | mLongPressHelper.cancelLongPress(); |
| 434 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 435 | |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 436 | public void applyState() { |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame^] | 437 | if (getTag() instanceof ShortcutInfo) { |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 438 | ShortcutInfo info = (ShortcutInfo) getTag(); |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame^] | 439 | final int state = info.getState(); |
| 440 | |
| 441 | final int progressLevel; |
| 442 | if (DEBUG) Log.d(TAG, "applying icon state: " + state); |
| 443 | |
| 444 | switch(state) { |
| 445 | case ShortcutInfo.PACKAGE_STATE_DEFAULT: |
| 446 | progressLevel = 100; |
| 447 | break; |
| 448 | |
| 449 | case ShortcutInfo.PACKAGE_STATE_INSTALLING: |
| 450 | setText(R.string.package_state_installing); |
| 451 | progressLevel = info.getProgress(); |
| 452 | break; |
| 453 | |
| 454 | case ShortcutInfo.PACKAGE_STATE_ERROR: |
| 455 | case ShortcutInfo.PACKAGE_STATE_UNKNOWN: |
| 456 | default: |
| 457 | progressLevel = 0; |
| 458 | setText(R.string.package_state_unknown); |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | Drawable[] drawables = getCompoundDrawables(); |
| 463 | Drawable top = drawables[1]; |
| 464 | if (top != null) { |
| 465 | final PreloadIconDrawable preloadDrawable; |
| 466 | if (top instanceof PreloadIconDrawable) { |
| 467 | preloadDrawable = (PreloadIconDrawable) top; |
| 468 | } else { |
| 469 | preloadDrawable = new PreloadIconDrawable(top, getResources()); |
| 470 | setCompoundDrawables(drawables[0], preloadDrawable, drawables[2], drawables[3]); |
| 471 | } |
| 472 | |
| 473 | preloadDrawable.setLevel(progressLevel); |
| 474 | if (state == ShortcutInfo.PACKAGE_STATE_DEFAULT) { |
| 475 | preloadDrawable.maybePerformFinishedAnimation(); |
| 476 | } |
| 477 | |
| 478 | } |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 479 | } |
| 480 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 481 | } |