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