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