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