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 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 19 | import android.annotation.TargetApi; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 21 | import android.content.res.ColorStateList; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 22 | import android.content.res.Resources; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 23 | import android.content.res.Resources.Theme; |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 24 | import android.content.res.TypedArray; |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 25 | import android.graphics.Bitmap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | import android.graphics.Canvas; |
Michael Jurka | 137142e | 2011-01-05 20:57:04 -0800 | [diff] [blame] | 27 | import android.graphics.Region; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 29 | import android.os.Build; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 30 | import android.util.AttributeSet; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 31 | import android.util.SparseArray; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 32 | import android.util.TypedValue; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 33 | import android.view.KeyEvent; |
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; |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 36 | import android.view.ViewParent; |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 37 | import android.widget.TextView; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 38 | import com.android.launcher3.IconCache.IconLoadRequest; |
Hyunyoung Song | 2bd3d7d | 2015-05-21 13:04:53 -0700 | [diff] [blame] | 39 | import com.android.launcher3.model.PackageItemInfo; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 40 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | /** |
| 42 | * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan |
| 43 | * because we want to make the bubble taller than the text and TextView's clip is |
| 44 | * too aggressive. |
| 45 | */ |
Michael Jurka | 08ee770 | 2011-08-11 16:53:35 -0700 | [diff] [blame] | 46 | public class BubbleTextView extends TextView { |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 47 | |
Sameer Padala | 8fd7483 | 2014-09-08 16:00:29 -0700 | [diff] [blame] | 48 | private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 49 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 50 | private static final float SHADOW_LARGE_RADIUS = 4.0f; |
| 51 | private static final float SHADOW_SMALL_RADIUS = 1.75f; |
| 52 | private static final float SHADOW_Y_OFFSET = 2.0f; |
| 53 | private static final int SHADOW_LARGE_COLOUR = 0xDD000000; |
| 54 | private static final int SHADOW_SMALL_COLOUR = 0xCC000000; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 55 | static final float PADDING_V = 3.0f; |
| 56 | |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 57 | private static final int DISPLAY_WORKSPACE = 0; |
| 58 | private static final int DISPLAY_ALL_APPS = 1; |
| 59 | |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 60 | private final Launcher mLauncher; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 61 | private Drawable mIcon; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 62 | private final Drawable mBackground; |
| 63 | private final CheckLongPressHelper mLongPressHelper; |
| 64 | private final HolographicOutlineHelper mOutlineHelper; |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 65 | private final StylusEventHelper mStylusEventHelper; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 66 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 67 | private boolean mBackgroundSizeChanged; |
| 68 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 69 | private Bitmap mPressedBackground; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 70 | |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 71 | private float mSlop; |
| 72 | |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 73 | private final boolean mDeferShadowGenerationOnTouch; |
Sunny Goyal | 15872da | 2014-07-08 15:43:54 -0700 | [diff] [blame] | 74 | private final boolean mCustomShadowsEnabled; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 75 | private final boolean mLayoutHorizontal; |
| 76 | private final int mIconSize; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 77 | private int mTextColor; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 78 | |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 79 | private boolean mStayPressed; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 80 | private boolean mIgnorePressedStateChange; |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 81 | private boolean mDisableRelayout = false; |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 82 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 83 | private IconLoadRequest mIconLoadRequest; |
| 84 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 85 | public BubbleTextView(Context context) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 86 | this(context, null, 0); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | public BubbleTextView(Context context, AttributeSet attrs) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 90 | this(context, attrs, 0); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 94 | super(context, attrs, defStyle); |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 95 | mLauncher = (Launcher) context; |
| 96 | DeviceProfile grid = mLauncher.getDeviceProfile(); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 97 | |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 98 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 99 | R.styleable.BubbleTextView, defStyle, 0); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 100 | mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 101 | mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false); |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 102 | mDeferShadowGenerationOnTouch = |
| 103 | a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 104 | |
| 105 | int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE); |
| 106 | int defaultIconSize = grid.iconSizePx; |
| 107 | if (display == DISPLAY_WORKSPACE) { |
Winson Chung | 44d0aac | 2015-05-11 18:02:55 -0700 | [diff] [blame] | 108 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 109 | } else if (display == DISPLAY_ALL_APPS) { |
Winson Chung | 44d0aac | 2015-05-11 18:02:55 -0700 | [diff] [blame] | 110 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 111 | defaultIconSize = grid.allAppsIconSizePx; |
| 112 | } |
| 113 | |
| 114 | mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride, |
| 115 | defaultIconSize); |
| 116 | |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 117 | a.recycle(); |
| 118 | |
Sunny Goyal | 15872da | 2014-07-08 15:43:54 -0700 | [diff] [blame] | 119 | if (mCustomShadowsEnabled) { |
| 120 | // Draw the background itself as the parent is drawn twice. |
| 121 | mBackground = getBackground(); |
| 122 | setBackground(null); |
| 123 | } else { |
| 124 | mBackground = null; |
| 125 | } |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 126 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 127 | mLongPressHelper = new CheckLongPressHelper(this); |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 128 | mStylusEventHelper = new StylusEventHelper(this); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 129 | |
| 130 | mOutlineHelper = HolographicOutlineHelper.obtain(getContext()); |
| 131 | if (mCustomShadowsEnabled) { |
| 132 | setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR); |
| 133 | } |
| 134 | |
| 135 | setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 138 | public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) { |
| 139 | applyFromShortcutInfo(info, iconCache, false); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 142 | public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache, |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 143 | boolean promiseStateChanged) { |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 144 | Bitmap b = info.getIcon(iconCache); |
| 145 | |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 146 | FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(b); |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 147 | iconDrawable.setGhostModeEnabled(info.isDisabled != 0); |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 148 | |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 149 | setIcon(iconDrawable, mIconSize); |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 150 | if (info.contentDescription != null) { |
| 151 | setContentDescription(info.contentDescription); |
| 152 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 153 | setText(info.title); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 154 | setTag(info); |
Sunny Goyal | 3484638 | 2014-07-09 00:09:28 -0700 | [diff] [blame] | 155 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 156 | if (promiseStateChanged || info.isPromise()) { |
| 157 | applyState(promiseStateChanged); |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 158 | } |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 161 | public void applyFromApplicationInfo(AppInfo info) { |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 162 | setIcon(mLauncher.createIconDrawable(info.iconBitmap), mIconSize); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 163 | setText(info.title); |
| 164 | if (info.contentDescription != null) { |
| 165 | setContentDescription(info.contentDescription); |
| 166 | } |
Winson Chung | 888b3a1 | 2015-03-13 11:14:16 -0700 | [diff] [blame] | 167 | // We don't need to check the info since it's not a ShortcutInfo |
| 168 | super.setTag(info); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 169 | |
| 170 | // Verify high res immediately |
| 171 | verifyHighRes(); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 174 | public void applyFromPackageItemInfo(PackageItemInfo info) { |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 175 | setIcon(mLauncher.createIconDrawable(info.iconBitmap), mIconSize); |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 176 | setText(info.title); |
| 177 | if (info.contentDescription != null) { |
| 178 | setContentDescription(info.contentDescription); |
| 179 | } |
| 180 | // We don't need to check the info since it's not a ShortcutInfo |
| 181 | super.setTag(info); |
| 182 | |
| 183 | // Verify high res immediately |
| 184 | verifyHighRes(); |
| 185 | } |
| 186 | |
| 187 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 188 | @Override |
| 189 | protected boolean setFrame(int left, int top, int right, int bottom) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 190 | if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 191 | mBackgroundSizeChanged = true; |
| 192 | } |
| 193 | return super.setFrame(left, top, right, bottom); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | protected boolean verifyDrawable(Drawable who) { |
| 198 | return who == mBackground || super.verifyDrawable(who); |
| 199 | } |
| 200 | |
| 201 | @Override |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 202 | public void setTag(Object tag) { |
| 203 | if (tag != null) { |
| 204 | LauncherModel.checkItemInfo((ItemInfo) tag); |
| 205 | } |
| 206 | super.setTag(tag); |
| 207 | } |
| 208 | |
| 209 | @Override |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 210 | public void setPressed(boolean pressed) { |
| 211 | super.setPressed(pressed); |
| 212 | |
| 213 | if (!mIgnorePressedStateChange) { |
| 214 | updateIconState(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 215 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 216 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 217 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 218 | /** Returns the icon for this view. */ |
| 219 | public Drawable getIcon() { |
| 220 | return mIcon; |
| 221 | } |
| 222 | |
| 223 | /** Returns whether the layout is horizontal. */ |
| 224 | public boolean isLayoutHorizontal() { |
| 225 | return mLayoutHorizontal; |
| 226 | } |
| 227 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 228 | private void updateIconState() { |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 229 | if (mIcon instanceof FastBitmapDrawable) { |
| 230 | ((FastBitmapDrawable) mIcon).setPressed(isPressed() || mStayPressed); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 231 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public boolean onTouchEvent(MotionEvent event) { |
| 236 | // Call the superclass onTouchEvent first, because sometimes it changes the state to |
| 237 | // isPressed() on an ACTION_UP |
| 238 | boolean result = super.onTouchEvent(event); |
| 239 | |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 240 | // Check for a stylus button press, if it occurs cancel any long press checks. |
| 241 | if (mStylusEventHelper.checkAndPerformStylusEvent(event)) { |
| 242 | mLongPressHelper.cancelLongPress(); |
| 243 | result = true; |
| 244 | } |
| 245 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 246 | switch (event.getAction()) { |
| 247 | case MotionEvent.ACTION_DOWN: |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 248 | // So that the pressed outline is visible immediately on setStayPressed(), |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 249 | // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time |
| 250 | // to create it) |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 251 | if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 252 | mPressedBackground = mOutlineHelper.createMediumDropShadow(this); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 253 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 254 | |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 255 | // If we're in a stylus button press, don't check for long press. |
| 256 | if (!mStylusEventHelper.inStylusButtonPressed()) { |
| 257 | mLongPressHelper.postCheckForLongPress(); |
| 258 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 259 | break; |
| 260 | case MotionEvent.ACTION_CANCEL: |
| 261 | case MotionEvent.ACTION_UP: |
| 262 | // If we've touched down and up on an item, and it's still not "pressed", then |
| 263 | // destroy the pressed outline |
| 264 | if (!isPressed()) { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 265 | mPressedBackground = null; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 266 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 267 | |
| 268 | mLongPressHelper.cancelLongPress(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 269 | break; |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 270 | case MotionEvent.ACTION_MOVE: |
| 271 | if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) { |
| 272 | mLongPressHelper.cancelLongPress(); |
| 273 | } |
| 274 | break; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 275 | } |
| 276 | return result; |
| 277 | } |
| 278 | |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 279 | void setStayPressed(boolean stayPressed) { |
| 280 | mStayPressed = stayPressed; |
| 281 | if (!stayPressed) { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 282 | mPressedBackground = null; |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 283 | } else { |
| 284 | if (mPressedBackground == null) { |
| 285 | mPressedBackground = mOutlineHelper.createMediumDropShadow(this); |
| 286 | } |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 287 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 288 | |
| 289 | // Only show the shadow effect when persistent pressed state is set. |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 290 | ViewParent parent = getParent(); |
| 291 | if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) { |
| 292 | ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon( |
| 293 | this, mPressedBackground); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | updateIconState(); |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 299 | void clearPressedBackground() { |
| 300 | setPressed(false); |
| 301 | setStayPressed(false); |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 306 | if (super.onKeyDown(keyCode, event)) { |
| 307 | // Pre-create shadow so show immediately on click. |
| 308 | if (mPressedBackground == null) { |
| 309 | mPressedBackground = mOutlineHelper.createMediumDropShadow(this); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 310 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 311 | return true; |
Patrick Dubroy | d69e113 | 2011-03-15 10:29:01 -0700 | [diff] [blame] | 312 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 313 | return false; |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 316 | @Override |
| 317 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 318 | // Unlike touch events, keypress event propagate pressed state change immediately, |
| 319 | // without waiting for onClickHandler to execute. Disable pressed state changes here |
| 320 | // to avoid flickering. |
| 321 | mIgnorePressedStateChange = true; |
| 322 | boolean result = super.onKeyUp(keyCode, event); |
Winson Chung | 1e9cbfe | 2011-09-30 16:52:26 -0700 | [diff] [blame] | 323 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 324 | mPressedBackground = null; |
| 325 | mIgnorePressedStateChange = false; |
| 326 | updateIconState(); |
| 327 | return result; |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 328 | } |
Patrick Dubroy | a017c03 | 2011-03-09 15:58:32 -0800 | [diff] [blame] | 329 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 330 | @Override |
| 331 | public void draw(Canvas canvas) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 332 | if (!mCustomShadowsEnabled) { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 333 | super.draw(canvas); |
| 334 | return; |
| 335 | } |
| 336 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 337 | final Drawable background = mBackground; |
| 338 | if (background != null) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 339 | final int scrollX = getScrollX(); |
| 340 | final int scrollY = getScrollY(); |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 341 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 342 | if (mBackgroundSizeChanged) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 343 | background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop()); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 344 | mBackgroundSizeChanged = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 345 | } |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 346 | |
| 347 | if ((scrollX | scrollY) == 0) { |
| 348 | background.draw(canvas); |
| 349 | } else { |
| 350 | canvas.translate(scrollX, scrollY); |
| 351 | background.draw(canvas); |
| 352 | canvas.translate(-scrollX, -scrollY); |
| 353 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 354 | } |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 355 | |
| 356 | // If text is transparent, don't draw any shadow |
Andrew Flynn | bc239a1 | 2012-03-06 11:39:49 -0800 | [diff] [blame] | 357 | if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) { |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 358 | getPaint().clearShadowLayer(); |
| 359 | super.draw(canvas); |
| 360 | return; |
| 361 | } |
| 362 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 363 | // We enhance the shadow by drawing the shadow twice |
Michael Jurka | e7e3f6c | 2011-02-01 21:08:29 -0800 | [diff] [blame] | 364 | 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] | 365 | super.draw(canvas); |
| 366 | canvas.save(Canvas.CLIP_SAVE_FLAG); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 367 | canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(), |
| 368 | getScrollX() + getWidth(), |
| 369 | getScrollY() + getHeight(), Region.Op.INTERSECT); |
Michael Jurka | e7e3f6c | 2011-02-01 21:08:29 -0800 | [diff] [blame] | 370 | getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 371 | super.draw(canvas); |
| 372 | canvas.restore(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 373 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 374 | |
| 375 | @Override |
| 376 | protected void onAttachedToWindow() { |
| 377 | super.onAttachedToWindow(); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 378 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 379 | if (mBackground != null) mBackground.setCallback(this); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 380 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 381 | if (mIcon instanceof PreloadIconDrawable) { |
| 382 | ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme()); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 383 | } |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 384 | mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | @Override |
| 388 | protected void onDetachedFromWindow() { |
| 389 | super.onDetachedFromWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 390 | if (mBackground != null) mBackground.setCallback(null); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 391 | } |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 392 | |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 393 | @Override |
| 394 | public void setTextColor(int color) { |
| 395 | mTextColor = color; |
| 396 | super.setTextColor(color); |
| 397 | } |
| 398 | |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 399 | @Override |
| 400 | public void setTextColor(ColorStateList colors) { |
| 401 | mTextColor = colors.getDefaultColor(); |
| 402 | super.setTextColor(colors); |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 405 | public void setTextVisibility(boolean visible) { |
| 406 | Resources res = getResources(); |
| 407 | if (visible) { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 408 | super.setTextColor(mTextColor); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 409 | } else { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 410 | super.setTextColor(res.getColor(android.R.color.transparent)); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 411 | } |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 414 | @Override |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 415 | public void cancelLongPress() { |
| 416 | super.cancelLongPress(); |
| 417 | |
| 418 | mLongPressHelper.cancelLongPress(); |
| 419 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 420 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 421 | public void applyState(boolean promiseStateChanged) { |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 422 | if (getTag() instanceof ShortcutInfo) { |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 423 | ShortcutInfo info = (ShortcutInfo) getTag(); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 424 | final boolean isPromise = info.isPromise(); |
| 425 | final int progressLevel = isPromise ? |
| 426 | ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ? |
| 427 | info.getInstallProgress() : 0)) : 100; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 428 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 429 | if (mIcon != null) { |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 430 | final PreloadIconDrawable preloadDrawable; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 431 | if (mIcon instanceof PreloadIconDrawable) { |
| 432 | preloadDrawable = (PreloadIconDrawable) mIcon; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 433 | } else { |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 434 | preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme()); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 435 | setIcon(preloadDrawable, mIconSize); |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | preloadDrawable.setLevel(progressLevel); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 439 | if (promiseStateChanged) { |
| 440 | preloadDrawable.maybePerformFinishedAnimation(); |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 441 | } |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 442 | } |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 443 | } |
| 444 | } |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 445 | |
| 446 | private Theme getPreloaderTheme() { |
| 447 | Object tag = getTag(); |
| 448 | int style = ((tag != null) && (tag instanceof ShortcutInfo) && |
| 449 | (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder |
| 450 | : R.style.PreloadIcon; |
| 451 | Theme theme = sPreloaderThemes.get(style); |
| 452 | if (theme == null) { |
| 453 | theme = getResources().newTheme(); |
| 454 | theme.applyStyle(style, true); |
| 455 | sPreloaderThemes.put(style, theme); |
| 456 | } |
| 457 | return theme; |
| 458 | } |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 459 | |
| 460 | /** |
| 461 | * Sets the icon for this view based on the layout direction. |
| 462 | */ |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 463 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 464 | private Drawable setIcon(Drawable icon, int iconSize) { |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 465 | mIcon = icon; |
| 466 | if (iconSize != -1) { |
| 467 | mIcon.setBounds(0, 0, iconSize, iconSize); |
| 468 | } |
| 469 | if (mLayoutHorizontal) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 470 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { |
| 471 | setCompoundDrawablesRelative(mIcon, null, null, null); |
| 472 | } else { |
| 473 | setCompoundDrawables(mIcon, null, null, null); |
| 474 | } |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 475 | } else { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 476 | setCompoundDrawables(null, mIcon, null, null); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 477 | } |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 478 | return icon; |
| 479 | } |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 480 | |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 481 | @Override |
| 482 | public void requestLayout() { |
| 483 | if (!mDisableRelayout) { |
| 484 | super.requestLayout(); |
| 485 | } |
| 486 | } |
| 487 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 488 | /** |
| 489 | * Applies the item info if it is same as what the view is pointing to currently. |
| 490 | */ |
| 491 | public void reapplyItemInfo(final ItemInfo info) { |
| 492 | if (getTag() == info) { |
| 493 | mIconLoadRequest = null; |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 494 | mDisableRelayout = true; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 495 | if (info instanceof AppInfo) { |
| 496 | applyFromApplicationInfo((AppInfo) info); |
| 497 | } else if (info instanceof ShortcutInfo) { |
| 498 | applyFromShortcutInfo((ShortcutInfo) info, |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 499 | LauncherAppState.getInstance().getIconCache()); |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 500 | } else if (info instanceof PackageItemInfo) { |
| 501 | applyFromPackageItemInfo((PackageItemInfo) info); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 502 | } |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 503 | mDisableRelayout = false; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Verifies that the current icon is high-res otherwise posts a request to load the icon. |
| 509 | */ |
| 510 | public void verifyHighRes() { |
| 511 | if (mIconLoadRequest != null) { |
| 512 | mIconLoadRequest.cancel(); |
| 513 | mIconLoadRequest = null; |
| 514 | } |
| 515 | if (getTag() instanceof AppInfo) { |
| 516 | AppInfo info = (AppInfo) getTag(); |
| 517 | if (info.usingLowResIcon) { |
| 518 | mIconLoadRequest = LauncherAppState.getInstance().getIconCache() |
| 519 | .updateIconInBackground(BubbleTextView.this, info); |
| 520 | } |
| 521 | } else if (getTag() instanceof ShortcutInfo) { |
| 522 | ShortcutInfo info = (ShortcutInfo) getTag(); |
| 523 | if (info.usingLowResIcon) { |
| 524 | mIconLoadRequest = LauncherAppState.getInstance().getIconCache() |
| 525 | .updateIconInBackground(BubbleTextView.this, info); |
| 526 | } |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 527 | } else if (getTag() instanceof PackageItemInfo) { |
| 528 | PackageItemInfo info = (PackageItemInfo) getTag(); |
| 529 | if (info.usingLowResIcon) { |
| 530 | mIconLoadRequest = LauncherAppState.getInstance().getIconCache() |
| 531 | .updateIconInBackground(BubbleTextView.this, info); |
| 532 | } |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 533 | } |
| 534 | } |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 535 | |
| 536 | /** |
| 537 | * Interface to be implemented by the grand parent to allow click shadow effect. |
| 538 | */ |
| 539 | public static interface BubbleTextShadowHandler { |
| 540 | void setPressedIcon(BubbleTextView icon, Bitmap background); |
| 541 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 542 | } |