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