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 | 4043b05 | 2018-08-17 13:47:39 -0700 | [diff] [blame^] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 21 | import android.animation.ObjectAnimator; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 22 | import android.content.Context; |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 23 | import android.content.res.ColorStateList; |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 24 | import android.content.res.TypedArray; |
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; |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 30 | import android.graphics.drawable.ColorDrawable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 31 | import android.graphics.drawable.Drawable; |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 32 | import android.text.TextUtils.TruncateAt; |
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; |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 41 | import android.widget.TextView; |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 42 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 43 | import com.android.launcher3.IconCache.IconLoadRequest; |
Sunny Goyal | 2d7cca1 | 2017-01-03 16:52:43 -0800 | [diff] [blame] | 44 | import com.android.launcher3.IconCache.ItemInfoUpdateReceiver; |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 45 | import com.android.launcher3.Launcher.OnResumeCallback; |
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; |
Tony | 7308cde | 2017-06-27 22:38:33 -0700 | [diff] [blame] | 48 | import com.android.launcher3.folder.FolderIcon; |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 49 | import com.android.launcher3.graphics.DrawableFactory; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 50 | import com.android.launcher3.graphics.IconPalette; |
Sunny Goyal | 96ac68a | 2017-02-02 16:37:21 -0800 | [diff] [blame] | 51 | import com.android.launcher3.graphics.PreloadIconDrawable; |
Hyunyoung Song | 2bd3d7d | 2015-05-21 13:04:53 -0700 | [diff] [blame] | 52 | import com.android.launcher3.model.PackageItemInfo; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 53 | |
Sunny Goyal | c469aad | 2015-10-01 11:24:23 -0700 | [diff] [blame] | 54 | import java.text.NumberFormat; |
| 55 | |
Sunny Goyal | d230307 | 2018-08-14 15:21:45 -0700 | [diff] [blame] | 56 | import androidx.core.graphics.ColorUtils; |
| 57 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 58 | /** |
| 59 | * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan |
| 60 | * because we want to make the bubble taller than the text and TextView's clip is |
| 61 | * too aggressive. |
| 62 | */ |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 63 | public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, OnResumeCallback { |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 64 | |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 65 | private static final int DISPLAY_WORKSPACE = 0; |
| 66 | private static final int DISPLAY_ALL_APPS = 1; |
Sunny Goyal | baec6ff | 2016-09-14 11:26:21 -0700 | [diff] [blame] | 67 | private static final int DISPLAY_FOLDER = 2; |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 68 | |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 69 | private static final int[] STATE_PRESSED = new int[] {android.R.attr.state_pressed}; |
| 70 | |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 71 | |
| 72 | private static final Property<BubbleTextView, Float> BADGE_SCALE_PROPERTY |
| 73 | = new Property<BubbleTextView, Float>(Float.TYPE, "badgeScale") { |
| 74 | @Override |
| 75 | public Float get(BubbleTextView bubbleTextView) { |
| 76 | return bubbleTextView.mBadgeScale; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void set(BubbleTextView bubbleTextView, Float value) { |
| 81 | bubbleTextView.mBadgeScale = value; |
| 82 | bubbleTextView.invalidate(); |
| 83 | } |
| 84 | }; |
| 85 | |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 86 | public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY |
| 87 | = new Property<BubbleTextView, Float>(Float.class, "textAlpha") { |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 88 | @Override |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 89 | public Float get(BubbleTextView bubbleTextView) { |
| 90 | return bubbleTextView.mTextAlpha; |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 94 | public void set(BubbleTextView bubbleTextView, Float alpha) { |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 95 | bubbleTextView.setTextAlpha(alpha); |
| 96 | } |
| 97 | }; |
| 98 | |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 99 | private final BaseDraggingActivity mActivity; |
| 100 | private Drawable mIcon; |
| 101 | private final boolean mCenterVertically; |
| 102 | |
| 103 | private final CheckLongPressHelper mLongPressHelper; |
| 104 | private final StylusEventHelper mStylusEventHelper; |
| 105 | private final float mSlop; |
| 106 | |
| 107 | private final boolean mLayoutHorizontal; |
| 108 | private final int mIconSize; |
| 109 | |
| 110 | @ViewDebug.ExportedProperty(category = "launcher") |
| 111 | private boolean mIsIconVisible = true; |
| 112 | @ViewDebug.ExportedProperty(category = "launcher") |
| 113 | private int mTextColor; |
| 114 | @ViewDebug.ExportedProperty(category = "launcher") |
| 115 | private float mTextAlpha = 1; |
| 116 | |
Tony | 4043b05 | 2018-08-17 13:47:39 -0700 | [diff] [blame^] | 117 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 118 | private BadgeInfo mBadgeInfo; |
| 119 | private BadgeRenderer mBadgeRenderer; |
| 120 | private int mBadgeColor; |
Tony | 4043b05 | 2018-08-17 13:47:39 -0700 | [diff] [blame^] | 121 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 122 | private float mBadgeScale; |
Tony | 4043b05 | 2018-08-17 13:47:39 -0700 | [diff] [blame^] | 123 | private Animator mBadgeScaleAnim; |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 124 | private boolean mForceHideBadge; |
| 125 | private Point mTempSpaceForBadgeOffset = new Point(); |
| 126 | private Rect mTempIconBounds = new Rect(); |
| 127 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 128 | @ViewDebug.ExportedProperty(category = "launcher") |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 129 | private boolean mStayPressed; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 130 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 131 | private boolean mIgnorePressedStateChange; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 132 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 133 | private boolean mDisableRelayout = false; |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 134 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 135 | private IconLoadRequest mIconLoadRequest; |
| 136 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 137 | public BubbleTextView(Context context) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 138 | this(context, null, 0); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | public BubbleTextView(Context context, AttributeSet attrs) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 142 | this(context, attrs, 0); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 146 | super(context, attrs, defStyle); |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 147 | mActivity = BaseDraggingActivity.fromContext(context); |
| 148 | DeviceProfile grid = mActivity.getDeviceProfile(); |
Sunny Goyal | 9314b7c | 2017-06-23 10:36:27 -0700 | [diff] [blame] | 149 | mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 150 | |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 151 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 152 | R.styleable.BubbleTextView, defStyle, 0); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 153 | mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 154 | |
| 155 | int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE); |
| 156 | int defaultIconSize = grid.iconSizePx; |
| 157 | if (display == DISPLAY_WORKSPACE) { |
Winson Chung | 44d0aac | 2015-05-11 18:02:55 -0700 | [diff] [blame] | 158 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx); |
Jon Miranda | 0966072 | 2017-06-14 14:20:14 -0700 | [diff] [blame] | 159 | setCompoundDrawablePadding(grid.iconDrawablePaddingPx); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 160 | } else if (display == DISPLAY_ALL_APPS) { |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 161 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx); |
| 162 | setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 163 | defaultIconSize = grid.allAppsIconSizePx; |
Sunny Goyal | baec6ff | 2016-09-14 11:26:21 -0700 | [diff] [blame] | 164 | } else if (display == DISPLAY_FOLDER) { |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 165 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx); |
Sunny Goyal | baec6ff | 2016-09-14 11:26:21 -0700 | [diff] [blame] | 166 | setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx); |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 167 | defaultIconSize = grid.folderChildIconSizePx; |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 168 | } |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 169 | mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 170 | |
| 171 | mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride, |
| 172 | defaultIconSize); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 173 | a.recycle(); |
| 174 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 175 | mLongPressHelper = new CheckLongPressHelper(this); |
Mady Mellor | bb83520 | 2015-07-15 16:34:34 -0700 | [diff] [blame] | 176 | mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 177 | |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 178 | setEllipsize(TruncateAt.END); |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 179 | setAccessibilityDelegate(mActivity.getAccessibilityDelegate()); |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 180 | setTextAlpha(1f); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 183 | @Override |
| 184 | protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { |
| 185 | // Disable marques when not focused to that, so that updating text does not cause relayout. |
| 186 | setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END); |
| 187 | super.onFocusChanged(focused, direction, previouslyFocusedRect); |
| 188 | } |
| 189 | |
Jon Miranda | 5254944 | 2017-10-26 11:28:06 -0700 | [diff] [blame] | 190 | /** |
| 191 | * Resets the view so it can be recycled. |
| 192 | */ |
| 193 | public void reset() { |
| 194 | mBadgeInfo = null; |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 195 | mBadgeColor = Color.TRANSPARENT; |
Tony | 4043b05 | 2018-08-17 13:47:39 -0700 | [diff] [blame^] | 196 | cancelBadgeScaleAnim(); |
Jon Miranda | 48043ee | 2017-11-01 15:40:22 -0700 | [diff] [blame] | 197 | mBadgeScale = 0f; |
Jon Miranda | 5254944 | 2017-10-26 11:28:06 -0700 | [diff] [blame] | 198 | mForceHideBadge = false; |
| 199 | } |
| 200 | |
Tony | 4043b05 | 2018-08-17 13:47:39 -0700 | [diff] [blame^] | 201 | private void cancelBadgeScaleAnim() { |
| 202 | if (mBadgeScaleAnim != null) { |
| 203 | mBadgeScaleAnim.cancel(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | private void animateBadgeScale(float... badgeScales) { |
| 208 | cancelBadgeScaleAnim(); |
| 209 | mBadgeScaleAnim = ObjectAnimator.ofFloat(this, BADGE_SCALE_PROPERTY, badgeScales); |
| 210 | mBadgeScaleAnim.addListener(new AnimatorListenerAdapter() { |
| 211 | @Override |
| 212 | public void onAnimationEnd(Animator animation) { |
| 213 | mBadgeScaleAnim = null; |
| 214 | } |
| 215 | }); |
| 216 | mBadgeScaleAnim.start(); |
| 217 | } |
| 218 | |
Sunny Goyal | 1cd01b0 | 2016-11-09 10:43:58 -0800 | [diff] [blame] | 219 | public void applyFromShortcutInfo(ShortcutInfo info) { |
| 220 | applyFromShortcutInfo(info, false); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Sunny Goyal | 1cd01b0 | 2016-11-09 10:43:58 -0800 | [diff] [blame] | 223 | public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) { |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 224 | applyIconAndLabel(info); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 225 | setTag(info); |
Mario Bertschler | 8ff9e1d | 2017-08-08 16:26:18 -0700 | [diff] [blame] | 226 | if (promiseStateChanged || (info.hasPromiseIconUi())) { |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 227 | applyPromiseState(promiseStateChanged); |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 228 | } |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 229 | |
Tony Wickham | 1e61849 | 2017-02-02 12:57:18 -0800 | [diff] [blame] | 230 | applyBadgeState(info, false /* animate */); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 233 | public void applyFromApplicationInfo(AppInfo info) { |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 234 | applyIconAndLabel(info); |
Sunny Goyal | f420438 | 2016-07-13 10:46:07 -0700 | [diff] [blame] | 235 | |
Winson Chung | 888b3a1 | 2015-03-13 11:14:16 -0700 | [diff] [blame] | 236 | // We don't need to check the info since it's not a ShortcutInfo |
| 237 | super.setTag(info); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 238 | |
| 239 | // Verify high res immediately |
| 240 | verifyHighRes(); |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 241 | |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 242 | if (info instanceof PromiseAppInfo) { |
| 243 | PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info; |
| 244 | applyProgressLevel(promiseAppInfo.level); |
| 245 | } |
Tony Wickham | 1e61849 | 2017-02-02 12:57:18 -0800 | [diff] [blame] | 246 | applyBadgeState(info, false /* animate */); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 249 | public void applyFromPackageItemInfo(PackageItemInfo info) { |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 250 | applyIconAndLabel(info); |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 251 | // We don't need to check the info since it's not a ShortcutInfo |
| 252 | super.setTag(info); |
| 253 | |
| 254 | // Verify high res immediately |
| 255 | verifyHighRes(); |
| 256 | } |
| 257 | |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 258 | private void applyIconAndLabel(ItemInfoWithIcon info) { |
| 259 | FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(info); |
| 260 | mBadgeColor = IconPalette.getMutedColor(info.iconColor, 0.54f); |
| 261 | |
Sunny Goyal | f420438 | 2016-07-13 10:46:07 -0700 | [diff] [blame] | 262 | setIcon(iconDrawable); |
| 263 | setText(info.title); |
| 264 | if (info.contentDescription != null) { |
| 265 | setContentDescription(info.isDisabled() |
| 266 | ? getContext().getString(R.string.disabled_app_label, info.contentDescription) |
| 267 | : info.contentDescription); |
| 268 | } |
| 269 | } |
| 270 | |
Winson Chung | 7501adf | 2015-06-02 11:24:28 -0700 | [diff] [blame] | 271 | /** |
| 272 | * Overrides the default long press timeout. |
| 273 | */ |
| 274 | public void setLongPressTimeout(int longPressTimeout) { |
| 275 | mLongPressHelper.setLongPressTimeout(longPressTimeout); |
| 276 | } |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 277 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 278 | @Override |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 279 | public void setTag(Object tag) { |
| 280 | if (tag != null) { |
| 281 | LauncherModel.checkItemInfo((ItemInfo) tag); |
| 282 | } |
| 283 | super.setTag(tag); |
| 284 | } |
| 285 | |
| 286 | @Override |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 287 | public void refreshDrawableState() { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 288 | if (!mIgnorePressedStateChange) { |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 289 | super.refreshDrawableState(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 290 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 291 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 292 | |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 293 | @Override |
| 294 | protected int[] onCreateDrawableState(int extraSpace) { |
| 295 | final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 296 | if (mStayPressed) { |
| 297 | mergeDrawableStates(drawableState, STATE_PRESSED); |
| 298 | } |
| 299 | return drawableState; |
| 300 | } |
| 301 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 302 | /** Returns the icon for this view. */ |
| 303 | public Drawable getIcon() { |
| 304 | return mIcon; |
| 305 | } |
| 306 | |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 307 | @Override |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 308 | public boolean onTouchEvent(MotionEvent event) { |
| 309 | // Call the superclass onTouchEvent first, because sometimes it changes the state to |
| 310 | // isPressed() on an ACTION_UP |
| 311 | boolean result = super.onTouchEvent(event); |
| 312 | |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 313 | // 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] | 314 | if (mStylusEventHelper.onMotionEvent(event)) { |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 315 | mLongPressHelper.cancelLongPress(); |
| 316 | result = true; |
| 317 | } |
| 318 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 319 | switch (event.getAction()) { |
| 320 | case MotionEvent.ACTION_DOWN: |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 321 | // If we're in a stylus button press, don't check for long press. |
| 322 | if (!mStylusEventHelper.inStylusButtonPressed()) { |
| 323 | mLongPressHelper.postCheckForLongPress(); |
| 324 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 325 | break; |
| 326 | case MotionEvent.ACTION_CANCEL: |
| 327 | case MotionEvent.ACTION_UP: |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 328 | mLongPressHelper.cancelLongPress(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 329 | break; |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 330 | case MotionEvent.ACTION_MOVE: |
| 331 | if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) { |
| 332 | mLongPressHelper.cancelLongPress(); |
| 333 | } |
| 334 | break; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 335 | } |
| 336 | return result; |
| 337 | } |
| 338 | |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 339 | void setStayPressed(boolean stayPressed) { |
| 340 | mStayPressed = stayPressed; |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 341 | refreshDrawableState(); |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 344 | @Override |
| 345 | public void onLauncherResume() { |
| 346 | // Reset the pressed state of icon that was locked in the press state while activity |
| 347 | // was launching |
| 348 | setStayPressed(false); |
| 349 | } |
| 350 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 351 | void clearPressedBackground() { |
| 352 | setPressed(false); |
| 353 | setStayPressed(false); |
| 354 | } |
| 355 | |
| 356 | @Override |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 357 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 358 | // Unlike touch events, keypress event propagate pressed state change immediately, |
| 359 | // without waiting for onClickHandler to execute. Disable pressed state changes here |
| 360 | // to avoid flickering. |
| 361 | mIgnorePressedStateChange = true; |
| 362 | boolean result = super.onKeyUp(keyCode, event); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 363 | mIgnorePressedStateChange = false; |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 364 | refreshDrawableState(); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 365 | return result; |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 366 | } |
Patrick Dubroy | a017c03 | 2011-03-09 15:58:32 -0800 | [diff] [blame] | 367 | |
Sunny Goyal | 9314b7c | 2017-06-23 10:36:27 -0700 | [diff] [blame] | 368 | @SuppressWarnings("wrongcall") |
| 369 | protected void drawWithoutBadge(Canvas canvas) { |
| 370 | super.onDraw(canvas); |
| 371 | } |
| 372 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 373 | @Override |
Sunny Goyal | 9314b7c | 2017-06-23 10:36:27 -0700 | [diff] [blame] | 374 | public void onDraw(Canvas canvas) { |
| 375 | super.onDraw(canvas); |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 376 | drawBadgeIfNecessary(canvas); |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Draws the icon badge in the top right corner of the icon bounds. |
| 381 | * @param canvas The canvas to draw to. |
| 382 | */ |
Sunny Goyal | 9314b7c | 2017-06-23 10:36:27 -0700 | [diff] [blame] | 383 | protected void drawBadgeIfNecessary(Canvas canvas) { |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 384 | if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) { |
| 385 | getIconBounds(mTempIconBounds); |
| 386 | mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop()); |
| 387 | final int scrollX = getScrollX(); |
| 388 | final int scrollY = getScrollY(); |
| 389 | canvas.translate(scrollX, scrollY); |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 390 | mBadgeRenderer.draw(canvas, mBadgeColor, mTempIconBounds, mBadgeScale, |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 391 | mTempSpaceForBadgeOffset); |
| 392 | canvas.translate(-scrollX, -scrollY); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | public void forceHideBadge(boolean forceHideBadge) { |
| 397 | if (mForceHideBadge == forceHideBadge) { |
| 398 | return; |
| 399 | } |
| 400 | mForceHideBadge = forceHideBadge; |
| 401 | |
| 402 | if (forceHideBadge) { |
| 403 | invalidate(); |
| 404 | } else if (hasBadge()) { |
Tony | 4043b05 | 2018-08-17 13:47:39 -0700 | [diff] [blame^] | 405 | animateBadgeScale(0, 1); |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
| 409 | private boolean hasBadge() { |
Tony Wickham | 0530e8c | 2017-04-26 18:13:56 -0700 | [diff] [blame] | 410 | return mBadgeInfo != null; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | public void getIconBounds(Rect outBounds) { |
Tony | 05d98c2 | 2018-07-23 08:01:15 -0700 | [diff] [blame] | 414 | getIconBounds(this, outBounds, mIconSize); |
| 415 | } |
| 416 | |
| 417 | public static void getIconBounds(View iconView, Rect outBounds, int iconSize) { |
| 418 | int top = iconView.getPaddingTop(); |
| 419 | int left = (iconView.getWidth() - iconSize) / 2; |
| 420 | int right = left + iconSize; |
| 421 | int bottom = top + iconSize; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 422 | outBounds.set(left, top, right, bottom); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 423 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 424 | |
| 425 | @Override |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 426 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 427 | if (mCenterVertically) { |
| 428 | Paint.FontMetrics fm = getPaint().getFontMetrics(); |
| 429 | int cellHeightPx = mIconSize + getCompoundDrawablePadding() + |
| 430 | (int) Math.ceil(fm.bottom - fm.top); |
| 431 | int height = MeasureSpec.getSize(heightMeasureSpec); |
| 432 | setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(), |
| 433 | getPaddingBottom()); |
| 434 | } |
| 435 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 436 | } |
| 437 | |
| 438 | @Override |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 439 | public void setTextColor(int color) { |
| 440 | mTextColor = color; |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 441 | super.setTextColor(getModifiedColor()); |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 444 | @Override |
| 445 | public void setTextColor(ColorStateList colors) { |
| 446 | mTextColor = colors.getDefaultColor(); |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 447 | if (Float.compare(mTextAlpha, 1) == 0) { |
| 448 | super.setTextColor(colors); |
| 449 | } else { |
| 450 | super.setTextColor(getModifiedColor()); |
| 451 | } |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Tony | 7308cde | 2017-06-27 22:38:33 -0700 | [diff] [blame] | 454 | public boolean shouldTextBeVisible() { |
| 455 | // Text should be visible everywhere but the hotseat. |
| 456 | Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag(); |
| 457 | ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null; |
| 458 | return info == null || info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT; |
| 459 | } |
| 460 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 461 | public void setTextVisibility(boolean visible) { |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 462 | setTextAlpha(visible ? 1 : 0); |
| 463 | } |
| 464 | |
| 465 | private void setTextAlpha(float alpha) { |
| 466 | mTextAlpha = alpha; |
| 467 | super.setTextColor(getModifiedColor()); |
| 468 | } |
| 469 | |
| 470 | private int getModifiedColor() { |
| 471 | if (mTextAlpha == 0) { |
| 472 | // Special case to prevent text shadows in high contrast mode |
| 473 | return Color.TRANSPARENT; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 474 | } |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 475 | return ColorUtils.setAlphaComponent( |
| 476 | mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha)); |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Creates an animator to fade the text in or out. |
| 481 | * @param fadeIn Whether the text should fade in or fade out. |
| 482 | */ |
Sunny Goyal | 4e5a878 | 2017-06-23 09:34:06 -0700 | [diff] [blame] | 483 | public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) { |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 484 | float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0; |
| 485 | return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha); |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 488 | @Override |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 489 | public void cancelLongPress() { |
| 490 | super.cancelLongPress(); |
| 491 | |
| 492 | mLongPressHelper.cancelLongPress(); |
| 493 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 494 | |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 495 | public void applyPromiseState(boolean promiseStateChanged) { |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 496 | if (getTag() instanceof ShortcutInfo) { |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 497 | ShortcutInfo info = (ShortcutInfo) getTag(); |
Mario Bertschler | 8ff9e1d | 2017-08-08 16:26:18 -0700 | [diff] [blame] | 498 | final boolean isPromise = info.hasPromiseIconUi(); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 499 | final int progressLevel = isPromise ? |
| 500 | ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ? |
| 501 | info.getInstallProgress() : 0)) : 100; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 502 | |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 503 | PreloadIconDrawable preloadDrawable = applyProgressLevel(progressLevel); |
| 504 | if (preloadDrawable != null && promiseStateChanged) { |
| 505 | preloadDrawable.maybePerformFinishedAnimation(); |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | public PreloadIconDrawable applyProgressLevel(int progressLevel) { |
| 511 | if (getTag() instanceof ItemInfoWithIcon) { |
| 512 | ItemInfoWithIcon info = (ItemInfoWithIcon) getTag(); |
Mario Bertschler | 230612f | 2017-09-27 17:05:21 -0700 | [diff] [blame] | 513 | if (progressLevel >= 100) { |
| 514 | setContentDescription(info.contentDescription != null |
| 515 | ? info.contentDescription : ""); |
| 516 | } else if (progressLevel > 0) { |
| 517 | setContentDescription(getContext() |
| 518 | .getString(R.string.app_downloading_title, info.title, |
| 519 | NumberFormat.getPercentInstance().format(progressLevel * 0.01))); |
| 520 | } else { |
| 521 | setContentDescription(getContext() |
| 522 | .getString(R.string.app_waiting_download_title, info.title)); |
| 523 | } |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 524 | if (mIcon != null) { |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 525 | final PreloadIconDrawable preloadDrawable; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 526 | if (mIcon instanceof PreloadIconDrawable) { |
| 527 | preloadDrawable = (PreloadIconDrawable) mIcon; |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 528 | preloadDrawable.setLevel(progressLevel); |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 529 | } else { |
Sunny Goyal | 96ac68a | 2017-02-02 16:37:21 -0800 | [diff] [blame] | 530 | preloadDrawable = DrawableFactory.get(getContext()) |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 531 | .newPendingIcon(info, getContext()); |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 532 | preloadDrawable.setLevel(progressLevel); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 533 | setIcon(preloadDrawable); |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 534 | } |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 535 | return preloadDrawable; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 536 | } |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 537 | } |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 538 | return null; |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 539 | } |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 540 | |
Tony Wickham | 1e61849 | 2017-02-02 12:57:18 -0800 | [diff] [blame] | 541 | public void applyBadgeState(ItemInfo itemInfo, boolean animate) { |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 542 | if (mIcon instanceof FastBitmapDrawable) { |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 543 | boolean wasBadged = mBadgeInfo != null; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 544 | mBadgeInfo = mActivity.getBadgeInfoForItem(itemInfo); |
Tony Wickham | 2fe09f2 | 2017-04-25 12:46:04 -0700 | [diff] [blame] | 545 | boolean isBadged = mBadgeInfo != null; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 546 | float newBadgeScale = isBadged ? 1f : 0; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 547 | mBadgeRenderer = mActivity.getDeviceProfile().mBadgeRenderer; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 548 | if (wasBadged || isBadged) { |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 549 | // Animate when a badge is first added or when it is removed. |
| 550 | if (animate && (wasBadged ^ isBadged) && isShown()) { |
Tony | 4043b05 | 2018-08-17 13:47:39 -0700 | [diff] [blame^] | 551 | animateBadgeScale(newBadgeScale); |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 552 | } else { |
Tony | 4043b05 | 2018-08-17 13:47:39 -0700 | [diff] [blame^] | 553 | cancelBadgeScaleAnim(); |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 554 | mBadgeScale = newBadgeScale; |
| 555 | invalidate(); |
| 556 | } |
| 557 | } |
Tony Wickham | 305e920 | 2018-01-23 17:46:47 -0800 | [diff] [blame] | 558 | if (itemInfo.contentDescription != null) { |
| 559 | if (hasBadge()) { |
| 560 | int count = mBadgeInfo.getNotificationCount(); |
| 561 | setContentDescription(getContext().getResources().getQuantityString( |
| 562 | R.plurals.badged_app_label, count, itemInfo.contentDescription, count)); |
| 563 | } else { |
| 564 | setContentDescription(itemInfo.contentDescription); |
| 565 | } |
| 566 | } |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 570 | /** |
| 571 | * Sets the icon for this view based on the layout direction. |
| 572 | */ |
Tony Wickham | 377ed3f | 2016-07-20 15:21:04 -0700 | [diff] [blame] | 573 | private void setIcon(Drawable icon) { |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 574 | if (mIsIconVisible) { |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 575 | applyCompoundDrawables(icon); |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 576 | } |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 577 | mIcon = icon; |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | public void setIconVisible(boolean visible) { |
| 581 | mIsIconVisible = visible; |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 582 | Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT); |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 583 | applyCompoundDrawables(icon); |
Tony Wickham | 377ed3f | 2016-07-20 15:21:04 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | protected void applyCompoundDrawables(Drawable icon) { |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 587 | // If we had already set an icon before, disable relayout as the icon size is the |
| 588 | // same as before. |
| 589 | mDisableRelayout = mIcon != null; |
| 590 | |
| 591 | icon.setBounds(0, 0, mIconSize, mIconSize); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 592 | if (mLayoutHorizontal) { |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 593 | setCompoundDrawablesRelative(icon, null, null, null); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 594 | } else { |
Tony Wickham | 377ed3f | 2016-07-20 15:21:04 -0700 | [diff] [blame] | 595 | setCompoundDrawables(null, icon, null, null); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 596 | } |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 597 | mDisableRelayout = false; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 598 | } |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 599 | |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 600 | @Override |
| 601 | public void requestLayout() { |
| 602 | if (!mDisableRelayout) { |
| 603 | super.requestLayout(); |
| 604 | } |
| 605 | } |
| 606 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 607 | /** |
| 608 | * Applies the item info if it is same as what the view is pointing to currently. |
| 609 | */ |
Sunny Goyal | 2d7cca1 | 2017-01-03 16:52:43 -0800 | [diff] [blame] | 610 | @Override |
| 611 | public void reapplyItemInfo(ItemInfoWithIcon info) { |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 612 | if (getTag() == info) { |
| 613 | mIconLoadRequest = null; |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 614 | mDisableRelayout = true; |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 615 | |
Jon Miranda | 7f522a2 | 2017-07-28 11:56:47 -0700 | [diff] [blame] | 616 | // Optimization: Starting in N, pre-uploads the bitmap to RenderThread. |
| 617 | info.iconBitmap.prepareToDraw(); |
| 618 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 619 | if (info instanceof AppInfo) { |
| 620 | applyFromApplicationInfo((AppInfo) info); |
| 621 | } else if (info instanceof ShortcutInfo) { |
Sunny Goyal | 1cd01b0 | 2016-11-09 10:43:58 -0800 | [diff] [blame] | 622 | applyFromShortcutInfo((ShortcutInfo) info); |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 623 | mActivity.invalidateParent(info); |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 624 | } else if (info instanceof PackageItemInfo) { |
| 625 | applyFromPackageItemInfo((PackageItemInfo) info); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 626 | } |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 627 | |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 628 | mDisableRelayout = false; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * Verifies that the current icon is high-res otherwise posts a request to load the icon. |
| 634 | */ |
| 635 | public void verifyHighRes() { |
| 636 | if (mIconLoadRequest != null) { |
| 637 | mIconLoadRequest.cancel(); |
| 638 | mIconLoadRequest = null; |
| 639 | } |
Sunny Goyal | 2d7cca1 | 2017-01-03 16:52:43 -0800 | [diff] [blame] | 640 | if (getTag() instanceof ItemInfoWithIcon) { |
| 641 | ItemInfoWithIcon info = (ItemInfoWithIcon) getTag(); |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 642 | if (info.usingLowResIcon) { |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 643 | mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache() |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 644 | .updateIconInBackground(BubbleTextView.this, info); |
| 645 | } |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 646 | } |
| 647 | } |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 648 | |
Jon Miranda | 4717011 | 2017-03-03 14:57:14 -0800 | [diff] [blame] | 649 | public int getIconSize() { |
| 650 | return mIconSize; |
| 651 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 652 | } |