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