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