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