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 | |
Federico Baron | 4eb502a | 2022-12-14 00:04:53 -0800 | [diff] [blame] | 19 | import static com.android.launcher3.config.FeatureFlags.ENABLE_DOWNLOAD_APP_UX_V2; |
Sunny Goyal | 0d69cb6 | 2021-10-04 15:31:38 -0700 | [diff] [blame] | 20 | import static com.android.launcher3.config.FeatureFlags.ENABLE_ICON_LABEL_AUTO_SCALING; |
Sunny Goyal | 1416843 | 2019-10-24 15:59:49 -0700 | [diff] [blame] | 21 | import static com.android.launcher3.graphics.PreloadIconDrawable.newPendingIcon; |
Sunny Goyal | d872a97 | 2021-11-24 18:07:04 -0800 | [diff] [blame] | 22 | import static com.android.launcher3.icons.BitmapInfo.FLAG_NO_BADGE; |
| 23 | import static com.android.launcher3.icons.BitmapInfo.FLAG_THEMED; |
Sunny Goyal | 066ace1 | 2018-11-05 11:08:31 -0800 | [diff] [blame] | 24 | import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound; |
Federico Baron | 4eb502a | 2022-12-14 00:04:53 -0800 | [diff] [blame] | 25 | import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_INCREMENTAL_DOWNLOAD_ACTIVE; |
| 26 | import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE; |
Sunny Goyal | 066ace1 | 2018-11-05 11:08:31 -0800 | [diff] [blame] | 27 | |
Tony | 4043b05 | 2018-08-17 13:47:39 -0700 | [diff] [blame] | 28 | import android.animation.Animator; |
| 29 | import android.animation.AnimatorListenerAdapter; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 30 | import android.animation.ObjectAnimator; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 31 | import android.content.Context; |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 32 | import android.content.res.ColorStateList; |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 33 | import android.content.res.TypedArray; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 34 | import android.graphics.Canvas; |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 35 | import android.graphics.Color; |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 36 | import android.graphics.Paint; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 37 | import android.graphics.Rect; |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 38 | import android.graphics.drawable.ColorDrawable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 39 | import android.graphics.drawable.Drawable; |
Steven Ng | 707b118 | 2021-09-13 11:54:58 +0100 | [diff] [blame] | 40 | import android.icu.text.MessageFormat; |
Sunny Goyal | 0d69cb6 | 2021-10-04 15:31:38 -0700 | [diff] [blame] | 41 | import android.text.TextPaint; |
Andy Wickham | 56be73b | 2022-01-18 09:13:43 -0800 | [diff] [blame] | 42 | import android.text.TextUtils; |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 43 | import android.text.TextUtils.TruncateAt; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 44 | import android.util.AttributeSet; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 45 | import android.util.Property; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 46 | import android.util.TypedValue; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 47 | import android.view.KeyEvent; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 48 | import android.view.MotionEvent; |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 49 | import android.view.View; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 50 | import android.view.ViewDebug; |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 51 | import android.widget.TextView; |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 52 | |
Schneider Victor-tulias | 6a5f431 | 2021-01-12 19:30:02 +0000 | [diff] [blame] | 53 | import androidx.annotation.Nullable; |
Hyunyoung Song | 5809a40 | 2021-02-02 01:19:18 -0800 | [diff] [blame] | 54 | import androidx.annotation.UiThread; |
Brandon Dayauon | cf88ea1 | 2023-01-06 12:38:55 -0800 | [diff] [blame] | 55 | import androidx.annotation.VisibleForTesting; |
Schneider Victor-tulias | 350b462 | 2020-09-18 11:16:30 -0700 | [diff] [blame] | 56 | |
Schneider Victor-tulias | bdb0dd8 | 2021-11-03 16:26:41 -0700 | [diff] [blame] | 57 | import com.android.launcher3.accessibility.BaseAccessibilityDelegate; |
Brandon Dayauon | cf88ea1 | 2023-01-06 12:38:55 -0800 | [diff] [blame] | 58 | import com.android.launcher3.config.FeatureFlags; |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 59 | import com.android.launcher3.dot.DotInfo; |
Sunny Goyal | f0ff70a | 2022-05-11 07:23:10 -0700 | [diff] [blame] | 60 | import com.android.launcher3.dragndrop.DragOptions.PreDragCondition; |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 61 | import com.android.launcher3.dragndrop.DraggableView; |
Tony | 7308cde | 2017-06-27 22:38:33 -0700 | [diff] [blame] | 62 | import com.android.launcher3.folder.FolderIcon; |
Tony Wickham | e1cb93f | 2019-05-03 11:27:32 -0700 | [diff] [blame] | 63 | import com.android.launcher3.graphics.IconShape; |
Sunny Goyal | 96ac68a | 2017-02-02 16:37:21 -0800 | [diff] [blame] | 64 | import com.android.launcher3.graphics.PreloadIconDrawable; |
Tony Wickham | 8912b04 | 2018-11-29 15:28:53 -0800 | [diff] [blame] | 65 | import com.android.launcher3.icons.DotRenderer; |
Sunny Goyal | 572aca4 | 2021-03-24 15:21:39 -0700 | [diff] [blame] | 66 | import com.android.launcher3.icons.FastBitmapDrawable; |
Sunny Goyal | fe8e4a9 | 2018-11-13 19:43:57 -0800 | [diff] [blame] | 67 | import com.android.launcher3.icons.IconCache.ItemInfoUpdateReceiver; |
Sunny Goyal | 572aca4 | 2021-03-24 15:21:39 -0700 | [diff] [blame] | 68 | import com.android.launcher3.icons.PlaceHolderIconDrawable; |
Sunny Goyal | 79e52fc | 2021-02-03 10:22:28 -0800 | [diff] [blame] | 69 | import com.android.launcher3.icons.cache.HandlerRunnable; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 70 | import com.android.launcher3.model.data.AppInfo; |
| 71 | import com.android.launcher3.model.data.ItemInfo; |
| 72 | import com.android.launcher3.model.data.ItemInfoWithIcon; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 73 | import com.android.launcher3.model.data.WorkspaceItemInfo; |
Sunny Goyal | f0ff70a | 2022-05-11 07:23:10 -0700 | [diff] [blame] | 74 | import com.android.launcher3.popup.PopupContainerWithArrow; |
Brandon Dayauon | cf88ea1 | 2023-01-06 12:38:55 -0800 | [diff] [blame] | 75 | import com.android.launcher3.search.StringMatcherUtility; |
| 76 | import com.android.launcher3.util.IntArray; |
Tony Wickham | 1e29b04 | 2023-03-16 19:58:30 +0000 | [diff] [blame] | 77 | import com.android.launcher3.util.MultiTranslateDelegate; |
Samuel Fufa | 167210a | 2020-05-12 18:40:11 -0700 | [diff] [blame] | 78 | import com.android.launcher3.util.SafeCloseable; |
Sunny Goyal | f0ff70a | 2022-05-11 07:23:10 -0700 | [diff] [blame] | 79 | import com.android.launcher3.util.ShortcutUtil; |
fbaron | 1406a0f | 2023-05-11 08:46:46 -0700 | [diff] [blame] | 80 | import com.android.launcher3.util.Themes; |
Sunny Goyal | fe8e4a9 | 2018-11-13 19:43:57 -0800 | [diff] [blame] | 81 | import com.android.launcher3.views.ActivityContext; |
Jon Miranda | 9b78e19 | 2019-08-30 18:42:01 -0700 | [diff] [blame] | 82 | import com.android.launcher3.views.IconLabelDotView; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 83 | |
Sunny Goyal | c469aad | 2015-10-01 11:24:23 -0700 | [diff] [blame] | 84 | import java.text.NumberFormat; |
Steven Ng | 707b118 | 2021-09-13 11:54:58 +0100 | [diff] [blame] | 85 | import java.util.HashMap; |
| 86 | import java.util.Locale; |
Sunny Goyal | c469aad | 2015-10-01 11:24:23 -0700 | [diff] [blame] | 87 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 88 | /** |
| 89 | * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan |
| 90 | * because we want to make the bubble taller than the text and TextView's clip is |
| 91 | * too aggressive. |
| 92 | */ |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 93 | public class BubbleTextView extends TextView implements ItemInfoUpdateReceiver, |
Hyunyoung Song | db9a53d | 2021-02-17 00:35:10 -0800 | [diff] [blame] | 94 | IconLabelDotView, DraggableView, Reorderable { |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 95 | |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 96 | private static final int DISPLAY_WORKSPACE = 0; |
| 97 | private static final int DISPLAY_ALL_APPS = 1; |
Sunny Goyal | baec6ff | 2016-09-14 11:26:21 -0700 | [diff] [blame] | 98 | private static final int DISPLAY_FOLDER = 2; |
Hyunyoung Song | e3d32b5 | 2021-06-03 21:20:18 -0700 | [diff] [blame] | 99 | protected static final int DISPLAY_TASKBAR = 5; |
| 100 | private static final int DISPLAY_SEARCH_RESULT = 6; |
| 101 | private static final int DISPLAY_SEARCH_RESULT_SMALL = 7; |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 102 | |
Sunny Goyal | 486adb0 | 2021-10-21 11:29:52 -0700 | [diff] [blame] | 103 | private static final float MIN_LETTER_SPACING = -0.05f; |
Sunny Goyal | 0d69cb6 | 2021-10-04 15:31:38 -0700 | [diff] [blame] | 104 | private static final int MAX_SEARCH_LOOP_COUNT = 20; |
Brandon Dayauon | cf88ea1 | 2023-01-06 12:38:55 -0800 | [diff] [blame] | 105 | private static final Character NEW_LINE = '\n'; |
| 106 | private static final String EMPTY = ""; |
| 107 | private static final StringMatcherUtility.StringMatcher MATCHER = |
| 108 | StringMatcherUtility.StringMatcher.getInstance(); |
Sunny Goyal | 0d69cb6 | 2021-10-04 15:31:38 -0700 | [diff] [blame] | 109 | |
Samuel Fufa | d6bacdc | 2020-08-23 21:56:07 -0700 | [diff] [blame] | 110 | private static final int[] STATE_PRESSED = new int[]{android.R.attr.state_pressed}; |
Samuel Fufa | 314761a | 2020-10-14 10:15:07 -0700 | [diff] [blame] | 111 | |
Adam Cohen | 1d13c0b | 2020-04-21 16:29:12 -0700 | [diff] [blame] | 112 | private float mScaleForReorderBounce = 1f; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 113 | |
Brandon Dayauon | cf88ea1 | 2023-01-06 12:38:55 -0800 | [diff] [blame] | 114 | private IntArray mBreakPointsIntArray; |
| 115 | private CharSequence mLastOriginalText; |
| 116 | private CharSequence mLastModifiedText; |
| 117 | |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 118 | private static final Property<BubbleTextView, Float> DOT_SCALE_PROPERTY |
| 119 | = new Property<BubbleTextView, Float>(Float.TYPE, "dotScale") { |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 120 | @Override |
| 121 | public Float get(BubbleTextView bubbleTextView) { |
Tony Wickham | 8912b04 | 2018-11-29 15:28:53 -0800 | [diff] [blame] | 122 | return bubbleTextView.mDotParams.scale; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void set(BubbleTextView bubbleTextView, Float value) { |
Tony Wickham | 8912b04 | 2018-11-29 15:28:53 -0800 | [diff] [blame] | 127 | bubbleTextView.mDotParams.scale = value; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 128 | bubbleTextView.invalidate(); |
| 129 | } |
| 130 | }; |
| 131 | |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 132 | public static final Property<BubbleTextView, Float> TEXT_ALPHA_PROPERTY |
| 133 | = new Property<BubbleTextView, Float>(Float.class, "textAlpha") { |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 134 | @Override |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 135 | public Float get(BubbleTextView bubbleTextView) { |
| 136 | return bubbleTextView.mTextAlpha; |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 140 | public void set(BubbleTextView bubbleTextView, Float alpha) { |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 141 | bubbleTextView.setTextAlpha(alpha); |
| 142 | } |
| 143 | }; |
| 144 | |
Sunny Goyal | 82dfc15 | 2023-02-24 16:50:09 -0800 | [diff] [blame] | 145 | private final MultiTranslateDelegate mTranslateDelegate = new MultiTranslateDelegate(this); |
Sunny Goyal | fe8e4a9 | 2018-11-13 19:43:57 -0800 | [diff] [blame] | 146 | private final ActivityContext mActivity; |
Sunny Goyal | c4c3269 | 2021-05-21 14:23:29 -0700 | [diff] [blame] | 147 | private FastBitmapDrawable mIcon; |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 148 | private boolean mCenterVertically; |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 149 | |
Brandon Dayauon | cf88ea1 | 2023-01-06 12:38:55 -0800 | [diff] [blame] | 150 | protected int mDisplay; |
Samuel Fufa | c96fa24 | 2019-09-26 23:06:32 -0700 | [diff] [blame] | 151 | |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 152 | private final CheckLongPressHelper mLongPressHelper; |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 153 | |
| 154 | private final boolean mLayoutHorizontal; |
Hyunyoung Song | c2f024b | 2021-11-03 23:16:53 -0700 | [diff] [blame] | 155 | private final boolean mIsRtl; |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 156 | private final int mIconSize; |
| 157 | |
| 158 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | d872a97 | 2021-11-24 18:07:04 -0800 | [diff] [blame] | 159 | private boolean mHideBadge = false; |
| 160 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 161 | private boolean mIsIconVisible = true; |
| 162 | @ViewDebug.ExportedProperty(category = "launcher") |
| 163 | private int mTextColor; |
| 164 | @ViewDebug.ExportedProperty(category = "launcher") |
Jon Spivack | 6886214 | 2021-10-28 18:37:19 -0700 | [diff] [blame] | 165 | private ColorStateList mTextColorStateList; |
| 166 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 167 | private float mTextAlpha = 1; |
| 168 | |
Tony | 4043b05 | 2018-08-17 13:47:39 -0700 | [diff] [blame] | 169 | @ViewDebug.ExportedProperty(category = "launcher") |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 170 | private DotInfo mDotInfo; |
Tony Wickham | 8912b04 | 2018-11-29 15:28:53 -0800 | [diff] [blame] | 171 | private DotRenderer mDotRenderer; |
| 172 | @ViewDebug.ExportedProperty(category = "launcher", deepExport = true) |
Sunny Goyal | 6755587 | 2021-05-21 12:56:55 -0700 | [diff] [blame] | 173 | protected DotRenderer.DrawParams mDotParams; |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 174 | private Animator mDotScaleAnim; |
| 175 | private boolean mForceHideDot; |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 176 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 177 | @ViewDebug.ExportedProperty(category = "launcher") |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 178 | private boolean mStayPressed; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 179 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 180 | private boolean mIgnorePressedStateChange; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 181 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 182 | private boolean mDisableRelayout = false; |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 183 | |
Sunny Goyal | 79e52fc | 2021-02-03 10:22:28 -0800 | [diff] [blame] | 184 | private HandlerRunnable mIconLoadRequest; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 185 | |
Schneider Victor-tulias | 7bbec50 | 2020-10-29 11:39:24 -0400 | [diff] [blame] | 186 | private boolean mEnableIconUpdateAnimation = false; |
| 187 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 188 | public BubbleTextView(Context context) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 189 | this(context, null, 0); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | public BubbleTextView(Context context, AttributeSet attrs) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 193 | this(context, attrs, 0); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 197 | super(context, attrs, defStyle); |
Sunny Goyal | fe8e4a9 | 2018-11-13 19:43:57 -0800 | [diff] [blame] | 198 | mActivity = ActivityContext.lookupContext(context); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 199 | |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 200 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 201 | R.styleable.BubbleTextView, defStyle, 0); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 202 | mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false); |
Hyunyoung Song | c2f024b | 2021-11-03 23:16:53 -0700 | [diff] [blame] | 203 | mIsRtl = (getResources().getConfiguration().getLayoutDirection() |
| 204 | == View.LAYOUT_DIRECTION_RTL); |
Sunny Goyal | c4d3201 | 2020-04-03 17:10:11 -0700 | [diff] [blame] | 205 | DeviceProfile grid = mActivity.getDeviceProfile(); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 206 | |
Samuel Fufa | c96fa24 | 2019-09-26 23:06:32 -0700 | [diff] [blame] | 207 | mDisplay = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE); |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 208 | final int defaultIconSize; |
Samuel Fufa | c96fa24 | 2019-09-26 23:06:32 -0700 | [diff] [blame] | 209 | if (mDisplay == DISPLAY_WORKSPACE) { |
Winson Chung | 44d0aac | 2015-05-11 18:02:55 -0700 | [diff] [blame] | 210 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx); |
Jon Miranda | 0966072 | 2017-06-14 14:20:14 -0700 | [diff] [blame] | 211 | setCompoundDrawablePadding(grid.iconDrawablePaddingPx); |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 212 | defaultIconSize = grid.iconSizePx; |
Thales Lima | 7945649 | 2023-05-23 11:32:22 +0100 | [diff] [blame^] | 213 | setCenterVertically(grid.iconCenterVertically); |
Samuel Fufa | c96fa24 | 2019-09-26 23:06:32 -0700 | [diff] [blame] | 214 | } else if (mDisplay == DISPLAY_ALL_APPS) { |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 215 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx); |
| 216 | setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 217 | defaultIconSize = grid.allAppsIconSizePx; |
Samuel Fufa | c96fa24 | 2019-09-26 23:06:32 -0700 | [diff] [blame] | 218 | } else if (mDisplay == DISPLAY_FOLDER) { |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 219 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx); |
Sunny Goyal | baec6ff | 2016-09-14 11:26:21 -0700 | [diff] [blame] | 220 | setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx); |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame] | 221 | defaultIconSize = grid.folderChildIconSizePx; |
Hyunyoung Song | e3d32b5 | 2021-06-03 21:20:18 -0700 | [diff] [blame] | 222 | } else if (mDisplay == DISPLAY_SEARCH_RESULT) { |
Brandon Dayauon | b1957cd | 2023-03-14 13:27:30 -0700 | [diff] [blame] | 223 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx); |
Hyunyoung Song | e3d32b5 | 2021-06-03 21:20:18 -0700 | [diff] [blame] | 224 | defaultIconSize = getResources().getDimensionPixelSize(R.dimen.search_row_icon_size); |
| 225 | } else if (mDisplay == DISPLAY_SEARCH_RESULT_SMALL) { |
| 226 | defaultIconSize = getResources().getDimensionPixelSize( |
| 227 | R.dimen.search_row_small_icon_size); |
Tony Wickham | 794fe4f | 2021-01-11 14:54:23 -0600 | [diff] [blame] | 228 | } else if (mDisplay == DISPLAY_TASKBAR) { |
| 229 | defaultIconSize = grid.iconSizePx; |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 230 | } else { |
Samuel Fufa | 4a1d9b1 | 2019-09-19 17:28:00 -0700 | [diff] [blame] | 231 | // widget_selection or shortcut_popup |
Sunny Goyal | c4d3201 | 2020-04-03 17:10:11 -0700 | [diff] [blame] | 232 | defaultIconSize = grid.iconSizePx; |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 233 | } |
Samuel Fufa | 4a1d9b1 | 2019-09-19 17:28:00 -0700 | [diff] [blame] | 234 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 235 | mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 236 | |
| 237 | mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride, |
| 238 | defaultIconSize); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 239 | a.recycle(); |
| 240 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 241 | mLongPressHelper = new CheckLongPressHelper(this); |
| 242 | |
Tony Wickham | 8912b04 | 2018-11-29 15:28:53 -0800 | [diff] [blame] | 243 | mDotParams = new DotRenderer.DrawParams(); |
| 244 | |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 245 | setEllipsize(TruncateAt.END); |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 246 | setAccessibilityDelegate(mActivity.getAccessibilityDelegate()); |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 247 | setTextAlpha(1f); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 250 | @Override |
| 251 | protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { |
| 252 | // Disable marques when not focused to that, so that updating text does not cause relayout. |
| 253 | setEllipsize(focused ? TruncateAt.MARQUEE : TruncateAt.END); |
| 254 | super.onFocusChanged(focused, direction, previouslyFocusedRect); |
| 255 | } |
| 256 | |
Sunny Goyal | d872a97 | 2021-11-24 18:07:04 -0800 | [diff] [blame] | 257 | public void setHideBadge(boolean hideBadge) { |
| 258 | mHideBadge = hideBadge; |
| 259 | } |
| 260 | |
Jon Miranda | 5254944 | 2017-10-26 11:28:06 -0700 | [diff] [blame] | 261 | /** |
| 262 | * Resets the view so it can be recycled. |
| 263 | */ |
| 264 | public void reset() { |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 265 | mDotInfo = null; |
James O'Leary | 1557ba3 | 2022-05-04 16:49:03 +0000 | [diff] [blame] | 266 | mDotParams.dotColor = Color.TRANSPARENT; |
| 267 | mDotParams.appColor = Color.TRANSPARENT; |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 268 | cancelDotScaleAnim(); |
Tony Wickham | 8912b04 | 2018-11-29 15:28:53 -0800 | [diff] [blame] | 269 | mDotParams.scale = 0f; |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 270 | mForceHideDot = false; |
Samuel Fufa | ba6b47f | 2019-11-07 10:27:41 -0800 | [diff] [blame] | 271 | setBackground(null); |
hyunyoungs | 825480f | 2023-03-24 10:11:15 -0700 | [diff] [blame] | 272 | if (FeatureFlags.ENABLE_TWOLINE_ALLAPPS.get() |
| 273 | || FeatureFlags.ENABLE_TWOLINE_DEVICESEARCH.get()) { |
| 274 | setMaxLines(1); |
| 275 | } |
Sunny Goyal | 52f4c16 | 2022-04-23 16:02:03 +0000 | [diff] [blame] | 276 | |
| 277 | setTag(null); |
| 278 | if (mIconLoadRequest != null) { |
| 279 | mIconLoadRequest.cancel(); |
| 280 | mIconLoadRequest = null; |
| 281 | } |
Jon Miranda | 5254944 | 2017-10-26 11:28:06 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 284 | private void cancelDotScaleAnim() { |
| 285 | if (mDotScaleAnim != null) { |
| 286 | mDotScaleAnim.cancel(); |
Tony | 4043b05 | 2018-08-17 13:47:39 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 290 | private void animateDotScale(float... dotScales) { |
| 291 | cancelDotScaleAnim(); |
| 292 | mDotScaleAnim = ObjectAnimator.ofFloat(this, DOT_SCALE_PROPERTY, dotScales); |
| 293 | mDotScaleAnim.addListener(new AnimatorListenerAdapter() { |
Tony | 4043b05 | 2018-08-17 13:47:39 -0700 | [diff] [blame] | 294 | @Override |
| 295 | public void onAnimationEnd(Animator animation) { |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 296 | mDotScaleAnim = null; |
Tony | 4043b05 | 2018-08-17 13:47:39 -0700 | [diff] [blame] | 297 | } |
| 298 | }); |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 299 | mDotScaleAnim.start(); |
Tony | 4043b05 | 2018-08-17 13:47:39 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Hyunyoung Song | 5809a40 | 2021-02-02 01:19:18 -0800 | [diff] [blame] | 302 | @UiThread |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 303 | public void applyFromWorkspaceItem(WorkspaceItemInfo info) { |
Tony Wickham | 175d152 | 2021-08-25 09:18:41 -0700 | [diff] [blame] | 304 | applyFromWorkspaceItem(info, /* animate = */ false, /* staggerIndex = */ 0); |
| 305 | } |
| 306 | |
| 307 | @UiThread |
| 308 | public void applyFromWorkspaceItem(WorkspaceItemInfo info, boolean animate, int staggerIndex) { |
Federico Baron | 4eb502a | 2022-12-14 00:04:53 -0800 | [diff] [blame] | 309 | applyFromWorkspaceItem(info, null); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Tony Wickham | 175d152 | 2021-08-25 09:18:41 -0700 | [diff] [blame] | 312 | /** |
| 313 | * Returns whether the newInfo differs from the current getTag(). |
| 314 | */ |
| 315 | public boolean shouldAnimateIconChange(WorkspaceItemInfo newInfo) { |
| 316 | WorkspaceItemInfo oldInfo = getTag() instanceof WorkspaceItemInfo |
| 317 | ? (WorkspaceItemInfo) getTag() |
| 318 | : null; |
| 319 | boolean changedIcons = oldInfo != null && oldInfo.getTargetComponent() != null |
| 320 | && newInfo.getTargetComponent() != null |
| 321 | && !oldInfo.getTargetComponent().equals(newInfo.getTargetComponent()); |
| 322 | return changedIcons && isShown(); |
| 323 | } |
| 324 | |
Adam Cohen | 29794c5 | 2019-06-21 12:50:30 -0700 | [diff] [blame] | 325 | @Override |
| 326 | public void setAccessibilityDelegate(AccessibilityDelegate delegate) { |
Schneider Victor-tulias | bdb0dd8 | 2021-11-03 16:26:41 -0700 | [diff] [blame] | 327 | if (delegate instanceof BaseAccessibilityDelegate) { |
Adam Cohen | 29794c5 | 2019-06-21 12:50:30 -0700 | [diff] [blame] | 328 | super.setAccessibilityDelegate(delegate); |
| 329 | } else { |
| 330 | // NO-OP |
| 331 | // Workaround for b/129745295 where RecyclerView is setting our Accessibility |
| 332 | // delegate incorrectly. There are no cases when we shouldn't be using the |
| 333 | // LauncherAccessibilityDelegate for BubbleTextView. |
| 334 | } |
| 335 | } |
| 336 | |
Hyunyoung Song | 5809a40 | 2021-02-02 01:19:18 -0800 | [diff] [blame] | 337 | @UiThread |
Federico Baron | 4eb502a | 2022-12-14 00:04:53 -0800 | [diff] [blame] | 338 | public void applyFromWorkspaceItem(WorkspaceItemInfo info, PreloadIconDrawable icon) { |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 339 | applyIconAndLabel(info); |
sfufa@google.com | ca76de0 | 2021-09-24 08:55:48 -0700 | [diff] [blame] | 340 | setItemInfo(info); |
Federico Baron | 4eb502a | 2022-12-14 00:04:53 -0800 | [diff] [blame] | 341 | applyLoadingState(icon); |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 342 | applyDotState(info, false /* animate */); |
Schneider Victor-tulias | 5f7f4cb | 2021-01-28 13:51:23 -0800 | [diff] [blame] | 343 | setDownloadStateContentDescription(info, info.getProgressLevel()); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Hyunyoung Song | 5809a40 | 2021-02-02 01:19:18 -0800 | [diff] [blame] | 346 | @UiThread |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 347 | public void applyFromApplicationInfo(AppInfo info) { |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 348 | applyIconAndLabel(info); |
Sunny Goyal | f420438 | 2016-07-13 10:46:07 -0700 | [diff] [blame] | 349 | |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 350 | // We don't need to check the info since it's not a WorkspaceItemInfo |
sfufa@google.com | ca76de0 | 2021-09-24 08:55:48 -0700 | [diff] [blame] | 351 | setItemInfo(info); |
| 352 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 353 | |
| 354 | // Verify high res immediately |
| 355 | verifyHighRes(); |
Tony Wickham | 010d255 | 2017-01-20 08:15:28 -0800 | [diff] [blame] | 356 | |
Schneider Victor-tulias | 6a5f431 | 2021-01-12 19:30:02 +0000 | [diff] [blame] | 357 | if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) != 0) { |
Schneider Victor-tulias | b40b98d | 2021-01-21 14:41:07 -0800 | [diff] [blame] | 358 | applyProgressLevel(); |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 359 | } |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 360 | applyDotState(info, false /* animate */); |
Schneider Victor-tulias | 5f7f4cb | 2021-01-28 13:51:23 -0800 | [diff] [blame] | 361 | setDownloadStateContentDescription(info, info.getProgressLevel()); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Samuel Fufa | d6bacdc | 2020-08-23 21:56:07 -0700 | [diff] [blame] | 364 | /** |
| 365 | * Apply label and tag using a generic {@link ItemInfoWithIcon} |
| 366 | */ |
Hyunyoung Song | 5809a40 | 2021-02-02 01:19:18 -0800 | [diff] [blame] | 367 | @UiThread |
Samuel Fufa | d6bacdc | 2020-08-23 21:56:07 -0700 | [diff] [blame] | 368 | public void applyFromItemInfoWithIcon(ItemInfoWithIcon info) { |
Sunny Goyal | 179249d | 2017-12-19 16:49:24 -0800 | [diff] [blame] | 369 | applyIconAndLabel(info); |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 370 | // We don't need to check the info since it's not a WorkspaceItemInfo |
sfufa@google.com | ca76de0 | 2021-09-24 08:55:48 -0700 | [diff] [blame] | 371 | setItemInfo(info); |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 372 | |
| 373 | // Verify high res immediately |
| 374 | verifyHighRes(); |
Schneider Victor-tulias | 5f7f4cb | 2021-01-28 13:51:23 -0800 | [diff] [blame] | 375 | |
| 376 | setDownloadStateContentDescription(info, info.getProgressLevel()); |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Sunny Goyal | f0ff70a | 2022-05-11 07:23:10 -0700 | [diff] [blame] | 379 | protected void setItemInfo(ItemInfoWithIcon itemInfo) { |
sfufa@google.com | ca76de0 | 2021-09-24 08:55:48 -0700 | [diff] [blame] | 380 | setTag(itemInfo); |
sfufa@google.com | 0fe464d | 2021-10-14 14:34:50 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Hyunyoung Song | 5809a40 | 2021-02-02 01:19:18 -0800 | [diff] [blame] | 383 | @UiThread |
Samuel Fufa | c282018 | 2021-01-22 11:47:25 -0600 | [diff] [blame] | 384 | protected void applyIconAndLabel(ItemInfoWithIcon info) { |
Brandon Dayauon | e640a9e | 2023-02-03 14:12:25 -0800 | [diff] [blame] | 385 | int flags = shouldUseTheme() ? FLAG_THEMED : 0; |
Sunny Goyal | d872a97 | 2021-11-24 18:07:04 -0800 | [diff] [blame] | 386 | if (mHideBadge) { |
| 387 | flags |= FLAG_NO_BADGE; |
| 388 | } |
| 389 | FastBitmapDrawable iconDrawable = info.newIcon(getContext(), flags); |
James O'Leary | 1557ba3 | 2022-05-04 16:49:03 +0000 | [diff] [blame] | 390 | mDotParams.appColor = iconDrawable.getIconColor(); |
fbaron | 1406a0f | 2023-05-11 08:46:46 -0700 | [diff] [blame] | 391 | mDotParams.dotColor = Themes.getAttrColor(getContext(), R.attr.notificationDotColor); |
Sunny Goyal | f420438 | 2016-07-13 10:46:07 -0700 | [diff] [blame] | 392 | setIcon(iconDrawable); |
Schneider Victor-tulias | 6a5f431 | 2021-01-12 19:30:02 +0000 | [diff] [blame] | 393 | applyLabel(info); |
| 394 | } |
| 395 | |
Brandon Dayauon | e640a9e | 2023-02-03 14:12:25 -0800 | [diff] [blame] | 396 | protected boolean shouldUseTheme() { |
| 397 | return mDisplay == DISPLAY_WORKSPACE || mDisplay == DISPLAY_FOLDER |
| 398 | || mDisplay == DISPLAY_TASKBAR; |
| 399 | } |
| 400 | |
Brandon Dayauon | 7d67331 | 2023-04-03 12:32:24 -0700 | [diff] [blame] | 401 | /** |
| 402 | * Only if actual text can be displayed in two line, the {@code true} value will be effective. |
| 403 | */ |
| 404 | protected boolean shouldUseTwoLine() { |
| 405 | return (FeatureFlags.ENABLE_TWOLINE_ALLAPPS.get() && mDisplay == DISPLAY_ALL_APPS) |
| 406 | || (FeatureFlags.ENABLE_TWOLINE_DEVICESEARCH.get() |
| 407 | && mDisplay == DISPLAY_SEARCH_RESULT); |
| 408 | } |
| 409 | |
Hyunyoung Song | 5809a40 | 2021-02-02 01:19:18 -0800 | [diff] [blame] | 410 | @UiThread |
Brandon Dayauon | cf88ea1 | 2023-01-06 12:38:55 -0800 | [diff] [blame] | 411 | @VisibleForTesting |
| 412 | public void applyLabel(ItemInfoWithIcon info) { |
| 413 | CharSequence label = info.title; |
| 414 | if (label != null) { |
| 415 | mLastOriginalText = label; |
| 416 | mLastModifiedText = mLastOriginalText; |
| 417 | mBreakPointsIntArray = StringMatcherUtility.getListOfBreakpoints(label, MATCHER); |
| 418 | setText(label); |
| 419 | } |
Sunny Goyal | f420438 | 2016-07-13 10:46:07 -0700 | [diff] [blame] | 420 | if (info.contentDescription != null) { |
| 421 | setContentDescription(info.isDisabled() |
| 422 | ? getContext().getString(R.string.disabled_app_label, info.contentDescription) |
| 423 | : info.contentDescription); |
| 424 | } |
| 425 | } |
| 426 | |
Brandon Dayauon | cf88ea1 | 2023-01-06 12:38:55 -0800 | [diff] [blame] | 427 | /** This is used for testing to forcefully set the display to ALL_APPS */ |
| 428 | @VisibleForTesting |
| 429 | public void setDisplayAllApps() { |
| 430 | mDisplay = DISPLAY_ALL_APPS; |
| 431 | } |
| 432 | |
Winson Chung | 7501adf | 2015-06-02 11:24:28 -0700 | [diff] [blame] | 433 | /** |
| 434 | * Overrides the default long press timeout. |
| 435 | */ |
Tony | 2ca999c | 2018-09-24 17:24:51 -0400 | [diff] [blame] | 436 | public void setLongPressTimeoutFactor(float longPressTimeoutFactor) { |
| 437 | mLongPressHelper.setLongPressTimeoutFactor(longPressTimeoutFactor); |
Winson Chung | 7501adf | 2015-06-02 11:24:28 -0700 | [diff] [blame] | 438 | } |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 439 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 440 | @Override |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 441 | public void refreshDrawableState() { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 442 | if (!mIgnorePressedStateChange) { |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 443 | super.refreshDrawableState(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 444 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 445 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 446 | |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 447 | @Override |
| 448 | protected int[] onCreateDrawableState(int extraSpace) { |
| 449 | final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 450 | if (mStayPressed) { |
| 451 | mergeDrawableStates(drawableState, STATE_PRESSED); |
| 452 | } |
| 453 | return drawableState; |
| 454 | } |
| 455 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 456 | /** Returns the icon for this view. */ |
Sunny Goyal | c4c3269 | 2021-05-21 14:23:29 -0700 | [diff] [blame] | 457 | public FastBitmapDrawable getIcon() { |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 458 | return mIcon; |
| 459 | } |
| 460 | |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 461 | @Override |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 462 | public boolean onTouchEvent(MotionEvent event) { |
Samuel Fufa | 4a1d9b1 | 2019-09-19 17:28:00 -0700 | [diff] [blame] | 463 | // ignore events if they happen in padding area |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 464 | if (event.getAction() == MotionEvent.ACTION_DOWN |
Sunny Goyal | 44a3b20 | 2020-07-09 08:27:06 -0700 | [diff] [blame] | 465 | && shouldIgnoreTouchDown(event.getX(), event.getY())) { |
Samuel Fufa | 4a1d9b1 | 2019-09-19 17:28:00 -0700 | [diff] [blame] | 466 | return false; |
| 467 | } |
Sunny Goyal | 17feee8 | 2020-03-24 13:55:15 -0700 | [diff] [blame] | 468 | if (isLongClickable()) { |
| 469 | super.onTouchEvent(event); |
| 470 | mLongPressHelper.onTouchEvent(event); |
| 471 | // Keep receiving the rest of the events |
| 472 | return true; |
| 473 | } else { |
| 474 | return super.onTouchEvent(event); |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 475 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Sunny Goyal | 44a3b20 | 2020-07-09 08:27:06 -0700 | [diff] [blame] | 478 | /** |
| 479 | * Returns true if the touch down at the provided position be ignored |
| 480 | */ |
| 481 | protected boolean shouldIgnoreTouchDown(float x, float y) { |
Tony Wickham | 42b0395 | 2021-08-31 10:41:36 -0700 | [diff] [blame] | 482 | if (mDisplay == DISPLAY_TASKBAR) { |
| 483 | // Allow touching within padding on taskbar, given icon sizes are smaller. |
| 484 | return false; |
| 485 | } |
Sunny Goyal | 44a3b20 | 2020-07-09 08:27:06 -0700 | [diff] [blame] | 486 | return y < getPaddingTop() |
| 487 | || x < getPaddingLeft() |
| 488 | || y > getHeight() - getPaddingBottom() |
| 489 | || x > getWidth() - getPaddingRight(); |
| 490 | } |
| 491 | |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 492 | void setStayPressed(boolean stayPressed) { |
| 493 | mStayPressed = stayPressed; |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 494 | refreshDrawableState(); |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 495 | } |
| 496 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 497 | @Override |
Hyunyoung Song | ef468d8 | 2019-01-03 01:02:43 -0800 | [diff] [blame] | 498 | public void onVisibilityAggregated(boolean isVisible) { |
| 499 | super.onVisibilityAggregated(isVisible); |
| 500 | if (mIcon != null) { |
Hyunyoung Song | a237385 | 2019-01-04 12:01:32 -0800 | [diff] [blame] | 501 | mIcon.setVisible(isVisible, false); |
Hyunyoung Song | ef468d8 | 2019-01-03 01:02:43 -0800 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
Tony Wickham | 8ac277e | 2021-05-24 15:47:38 -0700 | [diff] [blame] | 505 | public void clearPressedBackground() { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 506 | setPressed(false); |
| 507 | setStayPressed(false); |
| 508 | } |
| 509 | |
| 510 | @Override |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 511 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 512 | // Unlike touch events, keypress event propagate pressed state change immediately, |
| 513 | // without waiting for onClickHandler to execute. Disable pressed state changes here |
| 514 | // to avoid flickering. |
| 515 | mIgnorePressedStateChange = true; |
| 516 | boolean result = super.onKeyUp(keyCode, event); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 517 | mIgnorePressedStateChange = false; |
Sunny Goyal | 2a76e3f | 2017-02-16 13:33:15 -0800 | [diff] [blame] | 518 | refreshDrawableState(); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 519 | return result; |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 520 | } |
Patrick Dubroy | a017c03 | 2011-03-09 15:58:32 -0800 | [diff] [blame] | 521 | |
Sunny Goyal | 0d69cb6 | 2021-10-04 15:31:38 -0700 | [diff] [blame] | 522 | @Override |
| 523 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 524 | super.onSizeChanged(w, h, oldw, oldh); |
| 525 | checkForEllipsis(); |
| 526 | } |
| 527 | |
| 528 | @Override |
| 529 | protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { |
| 530 | super.onTextChanged(text, start, lengthBefore, lengthAfter); |
| 531 | checkForEllipsis(); |
| 532 | } |
| 533 | |
| 534 | private void checkForEllipsis() { |
| 535 | if (!ENABLE_ICON_LABEL_AUTO_SCALING.get()) { |
| 536 | return; |
| 537 | } |
| 538 | float width = getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight(); |
| 539 | if (width <= 0) { |
| 540 | return; |
| 541 | } |
| 542 | setLetterSpacing(0); |
| 543 | |
| 544 | String text = getText().toString(); |
| 545 | TextPaint paint = getPaint(); |
| 546 | if (paint.measureText(text) < width) { |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | float spacing = findBestSpacingValue(paint, text, width, MIN_LETTER_SPACING); |
| 551 | // Reset the paint value so that the call to TextView does appropriate diff. |
| 552 | paint.setLetterSpacing(0); |
| 553 | setLetterSpacing(spacing); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Find the appropriate text spacing to display the provided text |
| 558 | * @param paint the paint used by the text view |
| 559 | * @param text the text to display |
| 560 | * @param allowedWidthPx available space to render the text |
| 561 | * @param minSpacingEm minimum spacing allowed between characters |
| 562 | * @return the final textSpacing value |
| 563 | * |
| 564 | * @see #setLetterSpacing(float) |
| 565 | */ |
| 566 | private float findBestSpacingValue(TextPaint paint, String text, float allowedWidthPx, |
| 567 | float minSpacingEm) { |
| 568 | paint.setLetterSpacing(minSpacingEm); |
| 569 | if (paint.measureText(text) > allowedWidthPx) { |
| 570 | // If there is no result at high limit, we can do anything more |
| 571 | return minSpacingEm; |
| 572 | } |
| 573 | |
| 574 | float lowLimit = 0; |
| 575 | float highLimit = minSpacingEm; |
| 576 | |
| 577 | for (int i = 0; i < MAX_SEARCH_LOOP_COUNT; i++) { |
| 578 | float value = (lowLimit + highLimit) / 2; |
| 579 | paint.setLetterSpacing(value); |
| 580 | if (paint.measureText(text) < allowedWidthPx) { |
| 581 | highLimit = value; |
| 582 | } else { |
| 583 | lowLimit = value; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | // At the end error on the higher side |
| 588 | return highLimit; |
| 589 | } |
| 590 | |
Sunny Goyal | 9314b7c | 2017-06-23 10:36:27 -0700 | [diff] [blame] | 591 | @SuppressWarnings("wrongcall") |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 592 | protected void drawWithoutDot(Canvas canvas) { |
Sunny Goyal | 9314b7c | 2017-06-23 10:36:27 -0700 | [diff] [blame] | 593 | super.onDraw(canvas); |
| 594 | } |
| 595 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 596 | @Override |
Sunny Goyal | 9314b7c | 2017-06-23 10:36:27 -0700 | [diff] [blame] | 597 | public void onDraw(Canvas canvas) { |
| 598 | super.onDraw(canvas); |
Tony Wickham | 22a5a68 | 2020-11-02 17:04:58 -0800 | [diff] [blame] | 599 | drawDotIfNecessary(canvas); |
Samuel Fufa | 314761a | 2020-10-14 10:15:07 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 602 | /** |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 603 | * Draws the notification dot in the top right corner of the icon bounds. |
Samuel Fufa | d6bacdc | 2020-08-23 21:56:07 -0700 | [diff] [blame] | 604 | * |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 605 | * @param canvas The canvas to draw to. |
| 606 | */ |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 607 | protected void drawDotIfNecessary(Canvas canvas) { |
| 608 | if (!mForceHideDot && (hasDot() || mDotParams.scale > 0)) { |
Tony Wickham | 8912b04 | 2018-11-29 15:28:53 -0800 | [diff] [blame] | 609 | getIconBounds(mDotParams.iconBounds); |
Samuel Fufa | d6bacdc | 2020-08-23 21:56:07 -0700 | [diff] [blame] | 610 | Utilities.scaleRectAboutCenter(mDotParams.iconBounds, |
| 611 | IconShape.getNormalizationScale()); |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 612 | final int scrollX = getScrollX(); |
| 613 | final int scrollY = getScrollY(); |
| 614 | canvas.translate(scrollX, scrollY); |
Tony Wickham | 8912b04 | 2018-11-29 15:28:53 -0800 | [diff] [blame] | 615 | mDotRenderer.draw(canvas, mDotParams); |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 616 | canvas.translate(-scrollX, -scrollY); |
| 617 | } |
| 618 | } |
| 619 | |
Jon Miranda | 9b78e19 | 2019-08-30 18:42:01 -0700 | [diff] [blame] | 620 | @Override |
| 621 | public void setForceHideDot(boolean forceHideDot) { |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 622 | if (mForceHideDot == forceHideDot) { |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 623 | return; |
| 624 | } |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 625 | mForceHideDot = forceHideDot; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 626 | |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 627 | if (forceHideDot) { |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 628 | invalidate(); |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 629 | } else if (hasDot()) { |
| 630 | animateDotScale(0, 1); |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 634 | private boolean hasDot() { |
| 635 | return mDotInfo != null; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Hyunyoung Song | c2f024b | 2021-11-03 23:16:53 -0700 | [diff] [blame] | 638 | /** |
| 639 | * Get the icon bounds on the view depending on the layout type. |
| 640 | */ |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 641 | public void getIconBounds(Rect outBounds) { |
Hyunyoung Song | c2f024b | 2021-11-03 23:16:53 -0700 | [diff] [blame] | 642 | getIconBounds(mIconSize, outBounds); |
Tony | 05d98c2 | 2018-07-23 08:01:15 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Hyunyoung Song | c2f024b | 2021-11-03 23:16:53 -0700 | [diff] [blame] | 645 | /** |
| 646 | * Get the icon bounds on the view depending on the layout type. |
| 647 | */ |
| 648 | public void getIconBounds(int iconSize, Rect outBounds) { |
Sunny Goyal | 47d4d81 | 2022-11-04 16:43:42 -0700 | [diff] [blame] | 649 | outBounds.set(0, 0, iconSize, iconSize); |
Hyunyoung Song | c2f024b | 2021-11-03 23:16:53 -0700 | [diff] [blame] | 650 | if (mLayoutHorizontal) { |
Sunny Goyal | 47d4d81 | 2022-11-04 16:43:42 -0700 | [diff] [blame] | 651 | int top = (getHeight() - iconSize) / 2; |
Hyunyoung Song | c2f024b | 2021-11-03 23:16:53 -0700 | [diff] [blame] | 652 | if (mIsRtl) { |
Sunny Goyal | 47d4d81 | 2022-11-04 16:43:42 -0700 | [diff] [blame] | 653 | outBounds.offsetTo(getWidth() - iconSize - getPaddingRight(), top); |
Hyunyoung Song | c2f024b | 2021-11-03 23:16:53 -0700 | [diff] [blame] | 654 | } else { |
Sunny Goyal | 47d4d81 | 2022-11-04 16:43:42 -0700 | [diff] [blame] | 655 | outBounds.offsetTo(getPaddingLeft(), top); |
Hyunyoung Song | c2f024b | 2021-11-03 23:16:53 -0700 | [diff] [blame] | 656 | } |
| 657 | } else { |
Sunny Goyal | 47d4d81 | 2022-11-04 16:43:42 -0700 | [diff] [blame] | 658 | outBounds.offset((getWidth() - iconSize) / 2, getPaddingTop()); |
Hyunyoung Song | c2f024b | 2021-11-03 23:16:53 -0700 | [diff] [blame] | 659 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 660 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 661 | |
Jon Miranda | 228877d | 2021-02-09 11:05:00 -0500 | [diff] [blame] | 662 | /** |
| 663 | * Sets whether to vertically center the content. |
| 664 | */ |
| 665 | public void setCenterVertically(boolean centerVertically) { |
| 666 | mCenterVertically = centerVertically; |
| 667 | } |
| 668 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 669 | @Override |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 670 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 671 | if (mCenterVertically) { |
| 672 | Paint.FontMetrics fm = getPaint().getFontMetrics(); |
| 673 | int cellHeightPx = mIconSize + getCompoundDrawablePadding() + |
| 674 | (int) Math.ceil(fm.bottom - fm.top); |
| 675 | int height = MeasureSpec.getSize(heightMeasureSpec); |
| 676 | setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(), |
| 677 | getPaddingBottom()); |
| 678 | } |
Brandon Dayauon | 7d67331 | 2023-04-03 12:32:24 -0700 | [diff] [blame] | 679 | // Only apply two line for all_apps and device search only if necessary. |
| 680 | if (shouldUseTwoLine() && (mLastOriginalText != null)) { |
Brandon Dayauon | cf88ea1 | 2023-01-06 12:38:55 -0800 | [diff] [blame] | 681 | CharSequence modifiedString = modifyTitleToSupportMultiLine( |
| 682 | MeasureSpec.getSize(widthMeasureSpec) - getCompoundPaddingLeft() |
| 683 | - getCompoundPaddingRight(), |
| 684 | mLastOriginalText, |
| 685 | getPaint(), mBreakPointsIntArray); |
| 686 | if (!TextUtils.equals(modifiedString, mLastModifiedText)) { |
| 687 | mLastModifiedText = modifiedString; |
| 688 | setText(modifiedString); |
| 689 | // if text contains NEW_LINE, set max lines to 2 |
| 690 | if (TextUtils.indexOf(modifiedString, NEW_LINE) != -1) { |
| 691 | setSingleLine(false); |
| 692 | setMaxLines(2); |
| 693 | } else { |
| 694 | setSingleLine(true); |
| 695 | setMaxLines(1); |
| 696 | } |
| 697 | } |
| 698 | } |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 699 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 700 | } |
| 701 | |
| 702 | @Override |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 703 | public void setTextColor(int color) { |
| 704 | mTextColor = color; |
Jon Spivack | 6886214 | 2021-10-28 18:37:19 -0700 | [diff] [blame] | 705 | mTextColorStateList = null; |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 706 | super.setTextColor(getModifiedColor()); |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 709 | @Override |
| 710 | public void setTextColor(ColorStateList colors) { |
| 711 | mTextColor = colors.getDefaultColor(); |
Jon Spivack | 6886214 | 2021-10-28 18:37:19 -0700 | [diff] [blame] | 712 | mTextColorStateList = colors; |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 713 | if (Float.compare(mTextAlpha, 1) == 0) { |
| 714 | super.setTextColor(colors); |
| 715 | } else { |
| 716 | super.setTextColor(getModifiedColor()); |
| 717 | } |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Tony | 7308cde | 2017-06-27 22:38:33 -0700 | [diff] [blame] | 720 | public boolean shouldTextBeVisible() { |
| 721 | // Text should be visible everywhere but the hotseat. |
| 722 | Object tag = getParent() instanceof FolderIcon ? ((View) getParent()).getTag() : getTag(); |
| 723 | ItemInfo info = tag instanceof ItemInfo ? (ItemInfo) tag : null; |
Samuel Fufa | efb665d | 2019-10-30 13:24:14 -0700 | [diff] [blame] | 724 | return info == null || (info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT |
| 725 | && info.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION); |
Tony | 7308cde | 2017-06-27 22:38:33 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 728 | public void setTextVisibility(boolean visible) { |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 729 | setTextAlpha(visible ? 1 : 0); |
| 730 | } |
| 731 | |
| 732 | private void setTextAlpha(float alpha) { |
| 733 | mTextAlpha = alpha; |
Jon Spivack | 6886214 | 2021-10-28 18:37:19 -0700 | [diff] [blame] | 734 | if (mTextColorStateList != null) { |
| 735 | setTextColor(mTextColorStateList); |
| 736 | } else { |
| 737 | super.setTextColor(getModifiedColor()); |
| 738 | } |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | private int getModifiedColor() { |
| 742 | if (mTextAlpha == 0) { |
| 743 | // Special case to prevent text shadows in high contrast mode |
| 744 | return Color.TRANSPARENT; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 745 | } |
Sunny Goyal | 066ace1 | 2018-11-05 11:08:31 -0800 | [diff] [blame] | 746 | return setColorAlphaBound(mTextColor, Math.round(Color.alpha(mTextColor) * mTextAlpha)); |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /** |
| 750 | * Creates an animator to fade the text in or out. |
Samuel Fufa | d6bacdc | 2020-08-23 21:56:07 -0700 | [diff] [blame] | 751 | * |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 752 | * @param fadeIn Whether the text should fade in or fade out. |
| 753 | */ |
Sunny Goyal | 4e5a878 | 2017-06-23 09:34:06 -0700 | [diff] [blame] | 754 | public ObjectAnimator createTextAlphaAnimator(boolean fadeIn) { |
Sunny Goyal | ed7a693 | 2018-04-13 11:41:50 -0700 | [diff] [blame] | 755 | float toAlpha = shouldTextBeVisible() && fadeIn ? 1 : 0; |
Jon Miranda | 83cdca6 | 2019-04-15 13:01:05 -0700 | [diff] [blame] | 756 | return ObjectAnimator.ofFloat(this, TEXT_ALPHA_PROPERTY, toAlpha); |
Tony | 8f40280 | 2017-06-16 17:24:54 -0700 | [diff] [blame] | 757 | } |
| 758 | |
Brandon Dayauon | cf88ea1 | 2023-01-06 12:38:55 -0800 | [diff] [blame] | 759 | /** |
| 760 | * Generate a new string that will support two line text depending on the current string. |
| 761 | * This method calculates the limited width of a text view and creates a string to fit as |
| 762 | * many words as it can until the limit is reached. Once the limit is reached, we decide to |
| 763 | * either return the original title or continue on a new line. How to get the new string is by |
| 764 | * iterating through the list of break points and determining if the strings between the break |
| 765 | * points can fit within the line it is in. |
| 766 | * Example assuming each character takes up one spot: |
| 767 | * title = "Battery Stats", breakpoint = [6], stringPtr = 0, limitedWidth = 7 |
| 768 | * We get the current word -> from sublist(0, breakpoint[i]+1) so sublist (0,7) -> Battery, |
| 769 | * now stringPtr = 7 then from sublist(7) the current string is " Stats" and the runningWidth |
| 770 | * at this point exceeds limitedWidth and so we put " Stats" onto the next line (after checking |
| 771 | * if the first char is a SPACE, we trim to append "Stats". So resulting string would be |
| 772 | * "Battery\nStats" |
| 773 | */ |
| 774 | public static CharSequence modifyTitleToSupportMultiLine(int limitedWidth, CharSequence title, |
| 775 | TextPaint paint, IntArray breakPoints) { |
| 776 | // current title is less than the width allowed so we can just skip |
| 777 | if (title == null || paint.measureText(title, 0, title.length()) <= limitedWidth) { |
| 778 | return title; |
| 779 | } |
| 780 | float currentWordWidth, runningWidth = 0; |
| 781 | CharSequence currentWord; |
| 782 | StringBuilder newString = new StringBuilder(); |
| 783 | int stringPtr = 0; |
| 784 | for (int i = 0; i < breakPoints.size()+1; i++) { |
| 785 | if (i < breakPoints.size()) { |
| 786 | currentWord = title.subSequence(stringPtr, breakPoints.get(i)+1); |
| 787 | } else { |
| 788 | // last word from recent breakpoint until the end of the string |
| 789 | currentWord = title.subSequence(stringPtr, title.length()); |
| 790 | } |
| 791 | currentWordWidth = paint.measureText(currentWord,0, currentWord.length()); |
| 792 | runningWidth += currentWordWidth; |
| 793 | if (runningWidth <= limitedWidth) { |
| 794 | newString.append(currentWord); |
| 795 | } else { |
| 796 | // there is no more space |
| 797 | if (i == 0) { |
| 798 | // if the first words exceeds width, just return as the first line will ellipse |
| 799 | return title; |
| 800 | } else { |
| 801 | // If putting word onto a new line, make sure there is no space or new line |
| 802 | // character in the beginning of the current word and just put in the rest of |
| 803 | // the characters. |
| 804 | CharSequence lastCharacters = title.subSequence(stringPtr, title.length()); |
| 805 | int beginningLetterType = |
| 806 | Character.getType(Character.codePointAt(lastCharacters,0)); |
| 807 | if (beginningLetterType == Character.SPACE_SEPARATOR |
| 808 | || beginningLetterType == Character.LINE_SEPARATOR) { |
| 809 | lastCharacters = lastCharacters.length() > 1 |
| 810 | ? lastCharacters.subSequence(1, lastCharacters.length()) |
| 811 | : EMPTY; |
| 812 | } |
| 813 | newString.append(NEW_LINE).append(lastCharacters); |
| 814 | return newString.toString(); |
| 815 | } |
| 816 | } |
| 817 | if (i >= breakPoints.size()) { |
| 818 | // no need to look forward into the string if we've already finished processing |
| 819 | break; |
| 820 | } |
| 821 | stringPtr = breakPoints.get(i)+1; |
| 822 | } |
| 823 | return newString.toString(); |
| 824 | } |
| 825 | |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 826 | @Override |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 827 | public void cancelLongPress() { |
| 828 | super.cancelLongPress(); |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 829 | mLongPressHelper.cancelLongPress(); |
| 830 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 831 | |
Samuel Fufa | c282018 | 2021-01-22 11:47:25 -0600 | [diff] [blame] | 832 | /** |
| 833 | * Applies the loading progress value to the progress bar. |
Schneider Victor-tulias | 6a5f431 | 2021-01-12 19:30:02 +0000 | [diff] [blame] | 834 | * |
| 835 | * If this app is installing, the progress bar will be updated with the installation progress. |
| 836 | * If this app is installed and downloading incrementally, the progress bar will be updated |
| 837 | * with the total download progress. |
| 838 | */ |
Federico Baron | 4eb502a | 2022-12-14 00:04:53 -0800 | [diff] [blame] | 839 | public void applyLoadingState(PreloadIconDrawable icon) { |
Schneider Victor-tulias | b40b98d | 2021-01-21 14:41:07 -0800 | [diff] [blame] | 840 | if (getTag() instanceof ItemInfoWithIcon) { |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 841 | WorkspaceItemInfo info = (WorkspaceItemInfo) getTag(); |
Federico Baron | 4eb502a | 2022-12-14 00:04:53 -0800 | [diff] [blame] | 842 | if ((info.runtimeStatusFlags & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0 |
| 843 | || info.hasPromiseIconUi() |
| 844 | || (info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0 |
| 845 | || (ENABLE_DOWNLOAD_APP_UX_V2.get() && icon != null)) { |
Federico Baron | db599b2 | 2023-03-09 14:44:41 -0800 | [diff] [blame] | 846 | updateProgressBarUi(info.getProgressLevel() == 100 ? icon : null); |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
Federico Baron | 4eb502a | 2022-12-14 00:04:53 -0800 | [diff] [blame] | 851 | private void updateProgressBarUi(PreloadIconDrawable oldIcon) { |
| 852 | FastBitmapDrawable originalIcon = mIcon; |
Schneider Victor-tulias | b40b98d | 2021-01-21 14:41:07 -0800 | [diff] [blame] | 853 | PreloadIconDrawable preloadDrawable = applyProgressLevel(); |
Federico Baron | 4eb502a | 2022-12-14 00:04:53 -0800 | [diff] [blame] | 854 | if (preloadDrawable != null && oldIcon != null) { |
| 855 | preloadDrawable.maybePerformFinishedAnimation(oldIcon, () -> setIcon(originalIcon)); |
Schneider Victor-tulias | 6a5f431 | 2021-01-12 19:30:02 +0000 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
| 859 | /** Applies the given progress level to the this icon's progress bar. */ |
| 860 | @Nullable |
Schneider Victor-tulias | b40b98d | 2021-01-21 14:41:07 -0800 | [diff] [blame] | 861 | public PreloadIconDrawable applyProgressLevel() { |
| 862 | if (!(getTag() instanceof ItemInfoWithIcon)) { |
| 863 | return null; |
| 864 | } |
| 865 | |
| 866 | ItemInfoWithIcon info = (ItemInfoWithIcon) getTag(); |
| 867 | int progressLevel = info.getProgressLevel(); |
| 868 | if (progressLevel >= 100) { |
| 869 | setContentDescription(info.contentDescription != null |
| 870 | ? info.contentDescription : ""); |
| 871 | } else if (progressLevel > 0) { |
Schneider Victor-tulias | 5f7f4cb | 2021-01-28 13:51:23 -0800 | [diff] [blame] | 872 | setDownloadStateContentDescription(info, progressLevel); |
Schneider Victor-tulias | b40b98d | 2021-01-21 14:41:07 -0800 | [diff] [blame] | 873 | } else { |
| 874 | setContentDescription(getContext() |
| 875 | .getString(R.string.app_waiting_download_title, info.title)); |
| 876 | } |
| 877 | if (mIcon != null) { |
| 878 | PreloadIconDrawable preloadIconDrawable; |
| 879 | if (mIcon instanceof PreloadIconDrawable) { |
| 880 | preloadIconDrawable = (PreloadIconDrawable) mIcon; |
| 881 | preloadIconDrawable.setLevel(progressLevel); |
fbaron | 6664d8c | 2023-04-04 13:02:59 -0700 | [diff] [blame] | 882 | preloadIconDrawable.setIsDisabled(ENABLE_DOWNLOAD_APP_UX_V2.get() |
| 883 | ? info.getProgressLevel() == 0 |
| 884 | : !info.isAppStartable()); |
Mario Bertschler | 230612f | 2017-09-27 17:05:21 -0700 | [diff] [blame] | 885 | } else { |
Schneider Victor-tulias | b40b98d | 2021-01-21 14:41:07 -0800 | [diff] [blame] | 886 | preloadIconDrawable = makePreloadIcon(); |
| 887 | setIcon(preloadIconDrawable); |
Mario Bertschler | 230612f | 2017-09-27 17:05:21 -0700 | [diff] [blame] | 888 | } |
Schneider Victor-tulias | b40b98d | 2021-01-21 14:41:07 -0800 | [diff] [blame] | 889 | return preloadIconDrawable; |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 890 | } |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 891 | return null; |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 892 | } |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 893 | |
Schneider Victor-tulias | b40b98d | 2021-01-21 14:41:07 -0800 | [diff] [blame] | 894 | /** |
| 895 | * Creates a PreloadIconDrawable with the appropriate progress level without mutating this |
| 896 | * object. |
| 897 | */ |
| 898 | @Nullable |
| 899 | public PreloadIconDrawable makePreloadIcon() { |
| 900 | if (!(getTag() instanceof ItemInfoWithIcon)) { |
| 901 | return null; |
| 902 | } |
| 903 | |
| 904 | ItemInfoWithIcon info = (ItemInfoWithIcon) getTag(); |
| 905 | int progressLevel = info.getProgressLevel(); |
| 906 | final PreloadIconDrawable preloadDrawable = newPendingIcon(getContext(), info); |
| 907 | |
| 908 | preloadDrawable.setLevel(progressLevel); |
fbaron | 6664d8c | 2023-04-04 13:02:59 -0700 | [diff] [blame] | 909 | preloadDrawable.setIsDisabled(ENABLE_DOWNLOAD_APP_UX_V2.get() |
| 910 | ? info.getProgressLevel() == 0 |
| 911 | : !info.isAppStartable()); |
Schneider Victor-tulias | b40b98d | 2021-01-21 14:41:07 -0800 | [diff] [blame] | 912 | return preloadDrawable; |
| 913 | } |
| 914 | |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 915 | public void applyDotState(ItemInfo itemInfo, boolean animate) { |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 916 | if (mIcon instanceof FastBitmapDrawable) { |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 917 | boolean wasDotted = mDotInfo != null; |
| 918 | mDotInfo = mActivity.getDotInfoForItem(itemInfo); |
| 919 | boolean isDotted = mDotInfo != null; |
| 920 | float newDotScale = isDotted ? 1f : 0; |
Samuel Fufa | c96fa24 | 2019-09-26 23:06:32 -0700 | [diff] [blame] | 921 | if (mDisplay == DISPLAY_ALL_APPS) { |
| 922 | mDotRenderer = mActivity.getDeviceProfile().mDotRendererAllApps; |
| 923 | } else { |
| 924 | mDotRenderer = mActivity.getDeviceProfile().mDotRendererWorkSpace; |
| 925 | } |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 926 | if (wasDotted || isDotted) { |
| 927 | // Animate when a dot is first added or when it is removed. |
| 928 | if (animate && (wasDotted ^ isDotted) && isShown()) { |
| 929 | animateDotScale(newDotScale); |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 930 | } else { |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 931 | cancelDotScaleAnim(); |
| 932 | mDotParams.scale = newDotScale; |
Tony Wickham | 1237df0 | 2017-02-24 08:59:36 -0800 | [diff] [blame] | 933 | invalidate(); |
| 934 | } |
| 935 | } |
Andy Wickham | 56be73b | 2022-01-18 09:13:43 -0800 | [diff] [blame] | 936 | if (!TextUtils.isEmpty(itemInfo.contentDescription)) { |
vadimt | da6dad5 | 2019-05-21 13:24:23 -0700 | [diff] [blame] | 937 | if (itemInfo.isDisabled()) { |
| 938 | setContentDescription(getContext().getString(R.string.disabled_app_label, |
| 939 | itemInfo.contentDescription)); |
| 940 | } else if (hasDot()) { |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 941 | int count = mDotInfo.getNotificationCount(); |
Steven Ng | 707b118 | 2021-09-13 11:54:58 +0100 | [diff] [blame] | 942 | setContentDescription( |
| 943 | getAppLabelPluralString(itemInfo.contentDescription.toString(), count)); |
Tony Wickham | 305e920 | 2018-01-23 17:46:47 -0800 | [diff] [blame] | 944 | } else { |
| 945 | setContentDescription(itemInfo.contentDescription); |
| 946 | } |
| 947 | } |
Tony Wickham | 9a8d11f | 2017-01-11 09:53:12 -0800 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | |
Schneider Victor-tulias | 5f7f4cb | 2021-01-28 13:51:23 -0800 | [diff] [blame] | 951 | private void setDownloadStateContentDescription(ItemInfoWithIcon info, int progressLevel) { |
| 952 | if ((info.runtimeStatusFlags & ItemInfoWithIcon.FLAG_SHOW_DOWNLOAD_PROGRESS_MASK) |
| 953 | != 0) { |
| 954 | String percentageString = NumberFormat.getPercentInstance() |
| 955 | .format(progressLevel * 0.01); |
Federico Baron | 4eb502a | 2022-12-14 00:04:53 -0800 | [diff] [blame] | 956 | if ((info.runtimeStatusFlags & FLAG_INSTALL_SESSION_ACTIVE) != 0) { |
Schneider Victor-tulias | 5f7f4cb | 2021-01-28 13:51:23 -0800 | [diff] [blame] | 957 | setContentDescription(getContext() |
| 958 | .getString( |
| 959 | R.string.app_installing_title, info.title, percentageString)); |
| 960 | } else if ((info.runtimeStatusFlags |
Federico Baron | 4eb502a | 2022-12-14 00:04:53 -0800 | [diff] [blame] | 961 | & FLAG_INCREMENTAL_DOWNLOAD_ACTIVE) != 0) { |
Schneider Victor-tulias | 5f7f4cb | 2021-01-28 13:51:23 -0800 | [diff] [blame] | 962 | setContentDescription(getContext() |
| 963 | .getString( |
| 964 | R.string.app_downloading_title, info.title, percentageString)); |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 969 | /** |
| 970 | * Sets the icon for this view based on the layout direction. |
| 971 | */ |
Sunny Goyal | c4c3269 | 2021-05-21 14:23:29 -0700 | [diff] [blame] | 972 | protected void setIcon(FastBitmapDrawable icon) { |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 973 | if (mIsIconVisible) { |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 974 | applyCompoundDrawables(icon); |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 975 | } |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 976 | mIcon = icon; |
Hyunyoung Song | a237385 | 2019-01-04 12:01:32 -0800 | [diff] [blame] | 977 | if (mIcon != null) { |
| 978 | mIcon.setVisible(getWindowVisibility() == VISIBLE && isShown(), false); |
| 979 | } |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Jon Miranda | 9b78e19 | 2019-08-30 18:42:01 -0700 | [diff] [blame] | 982 | @Override |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 983 | public void setIconVisible(boolean visible) { |
| 984 | mIsIconVisible = visible; |
Sunny Goyal | 30ac97d | 2020-06-24 23:47:46 -0700 | [diff] [blame] | 985 | if (!mIsIconVisible) { |
| 986 | resetIconScale(); |
| 987 | } |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 988 | Drawable icon = visible ? mIcon : new ColorDrawable(Color.TRANSPARENT); |
Tony | 17b7f9b | 2017-05-23 12:19:09 -0700 | [diff] [blame] | 989 | applyCompoundDrawables(icon); |
Tony Wickham | 377ed3f | 2016-07-20 15:21:04 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Tony Wickham | 1e29b04 | 2023-03-16 19:58:30 +0000 | [diff] [blame] | 992 | /** Sets the icon visual state to disabled or not. */ |
| 993 | public void setIconDisabled(boolean isDisabled) { |
| 994 | if (mIcon != null) { |
| 995 | mIcon.setIsDisabled(isDisabled); |
| 996 | } |
| 997 | } |
| 998 | |
Schneider Victor-tulias | 7bbec50 | 2020-10-29 11:39:24 -0400 | [diff] [blame] | 999 | protected boolean iconUpdateAnimationEnabled() { |
| 1000 | return mEnableIconUpdateAnimation; |
| 1001 | } |
| 1002 | |
Tony Wickham | 377ed3f | 2016-07-20 15:21:04 -0700 | [diff] [blame] | 1003 | protected void applyCompoundDrawables(Drawable icon) { |
Jon Miranda | fe74997 | 2022-03-15 10:02:39 -0700 | [diff] [blame] | 1004 | if (icon == null) { |
| 1005 | // Icon can be null when we use the BubbleTextView for text only. |
| 1006 | return; |
| 1007 | } |
| 1008 | |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 1009 | // If we had already set an icon before, disable relayout as the icon size is the |
| 1010 | // same as before. |
| 1011 | mDisableRelayout = mIcon != null; |
| 1012 | |
| 1013 | icon.setBounds(0, 0, mIconSize, mIconSize); |
Schneider Victor-tulias | 350b462 | 2020-09-18 11:16:30 -0700 | [diff] [blame] | 1014 | |
| 1015 | updateIcon(icon); |
| 1016 | |
| 1017 | // If the current icon is a placeholder color, animate its update. |
Schneider Victor-tulias | 3ee7018 | 2020-10-21 14:01:53 -0400 | [diff] [blame] | 1018 | if (mIcon != null |
| 1019 | && mIcon instanceof PlaceHolderIconDrawable |
Schneider Victor-tulias | 7bbec50 | 2020-10-29 11:39:24 -0400 | [diff] [blame] | 1020 | && iconUpdateAnimationEnabled()) { |
Steven Ng | e92bc55 | 2021-02-10 17:10:15 +0000 | [diff] [blame] | 1021 | ((PlaceHolderIconDrawable) mIcon).animateIconUpdate(icon); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 1022 | } |
Schneider Victor-tulias | 350b462 | 2020-09-18 11:16:30 -0700 | [diff] [blame] | 1023 | |
Sunny Goyal | 66dccad | 2018-03-19 12:00:51 -0700 | [diff] [blame] | 1024 | mDisableRelayout = false; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 1025 | } |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 1026 | |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 1027 | @Override |
| 1028 | public void requestLayout() { |
| 1029 | if (!mDisableRelayout) { |
| 1030 | super.requestLayout(); |
| 1031 | } |
| 1032 | } |
| 1033 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 1034 | /** |
| 1035 | * Applies the item info if it is same as what the view is pointing to currently. |
| 1036 | */ |
Sunny Goyal | 2d7cca1 | 2017-01-03 16:52:43 -0800 | [diff] [blame] | 1037 | @Override |
| 1038 | public void reapplyItemInfo(ItemInfoWithIcon info) { |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 1039 | if (getTag() == info) { |
| 1040 | mIconLoadRequest = null; |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 1041 | mDisableRelayout = true; |
Schneider Victor-tulias | 7bbec50 | 2020-10-29 11:39:24 -0400 | [diff] [blame] | 1042 | mEnableIconUpdateAnimation = true; |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 1043 | |
Jon Miranda | 7f522a2 | 2017-07-28 11:56:47 -0700 | [diff] [blame] | 1044 | // Optimization: Starting in N, pre-uploads the bitmap to RenderThread. |
Sunny Goyal | 3808a69 | 2019-10-25 13:41:28 -0700 | [diff] [blame] | 1045 | info.bitmap.icon.prepareToDraw(); |
Jon Miranda | 7f522a2 | 2017-07-28 11:56:47 -0700 | [diff] [blame] | 1046 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 1047 | if (info instanceof AppInfo) { |
| 1048 | applyFromApplicationInfo((AppInfo) info); |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 1049 | } else if (info instanceof WorkspaceItemInfo) { |
| 1050 | applyFromWorkspaceItem((WorkspaceItemInfo) info); |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 1051 | mActivity.invalidateParent(info); |
Sunny Goyal | f0ff70a | 2022-05-11 07:23:10 -0700 | [diff] [blame] | 1052 | } else if (info != null) { |
| 1053 | applyFromItemInfoWithIcon(info); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 1054 | } |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 1055 | |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 1056 | mDisableRelayout = false; |
Schneider Victor-tulias | 7bbec50 | 2020-10-29 11:39:24 -0400 | [diff] [blame] | 1057 | mEnableIconUpdateAnimation = false; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | /** |
| 1062 | * Verifies that the current icon is high-res otherwise posts a request to load the icon. |
| 1063 | */ |
| 1064 | public void verifyHighRes() { |
| 1065 | if (mIconLoadRequest != null) { |
| 1066 | mIconLoadRequest.cancel(); |
| 1067 | mIconLoadRequest = null; |
| 1068 | } |
Sunny Goyal | 2d7cca1 | 2017-01-03 16:52:43 -0800 | [diff] [blame] | 1069 | if (getTag() instanceof ItemInfoWithIcon) { |
| 1070 | ItemInfoWithIcon info = (ItemInfoWithIcon) getTag(); |
Sunny Goyal | 2b787e5 | 2018-08-20 15:01:03 -0700 | [diff] [blame] | 1071 | if (info.usingLowResIcon()) { |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 1072 | mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache() |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 1073 | .updateIconInBackground(BubbleTextView.this, info); |
| 1074 | } |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 1075 | } |
| 1076 | } |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 1077 | |
Jon Miranda | 4717011 | 2017-03-03 14:57:14 -0800 | [diff] [blame] | 1078 | public int getIconSize() { |
| 1079 | return mIconSize; |
| 1080 | } |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 1081 | |
Brandon Dayauon | d3755f4 | 2022-08-29 11:48:27 -0700 | [diff] [blame] | 1082 | public boolean isDisplaySearchResult() { |
| 1083 | return mDisplay == DISPLAY_SEARCH_RESULT || |
| 1084 | mDisplay == DISPLAY_SEARCH_RESULT_SMALL; |
| 1085 | } |
| 1086 | |
Sunny Goyal | 82dfc15 | 2023-02-24 16:50:09 -0800 | [diff] [blame] | 1087 | @Override |
| 1088 | public MultiTranslateDelegate getTranslateDelegate() { |
| 1089 | return mTranslateDelegate; |
Adam Cohen | 1d13c0b | 2020-04-21 16:29:12 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | @Override |
Adam Cohen | 1d13c0b | 2020-04-21 16:29:12 -0700 | [diff] [blame] | 1093 | public void setReorderBounceScale(float scale) { |
| 1094 | mScaleForReorderBounce = scale; |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1095 | super.setScaleX(scale); |
| 1096 | super.setScaleY(scale); |
| 1097 | } |
| 1098 | |
Sunny Goyal | 82dfc15 | 2023-02-24 16:50:09 -0800 | [diff] [blame] | 1099 | @Override |
Adam Cohen | 1d13c0b | 2020-04-21 16:29:12 -0700 | [diff] [blame] | 1100 | public float getReorderBounceScale() { |
| 1101 | return mScaleForReorderBounce; |
Adam Cohen | d916206 | 2020-03-24 16:35:35 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 1104 | @Override |
| 1105 | public int getViewType() { |
| 1106 | return DRAGGABLE_ICON; |
| 1107 | } |
| 1108 | |
| 1109 | @Override |
Adam Cohen | c77bc45 | 2020-05-07 11:55:19 -0700 | [diff] [blame] | 1110 | public void getWorkspaceVisualDragBounds(Rect bounds) { |
Hyunyoung Song | c2f024b | 2021-11-03 23:16:53 -0700 | [diff] [blame] | 1111 | getIconBounds(mIconSize, bounds); |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 1112 | } |
| 1113 | |
Adam Cohen | c77bc45 | 2020-05-07 11:55:19 -0700 | [diff] [blame] | 1114 | public void getSourceVisualDragBounds(Rect bounds) { |
Hyunyoung Song | c2f024b | 2021-11-03 23:16:53 -0700 | [diff] [blame] | 1115 | getIconBounds(mIconSize, bounds); |
Adam Cohen | c77bc45 | 2020-05-07 11:55:19 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 1118 | @Override |
Samuel Fufa | 167210a | 2020-05-12 18:40:11 -0700 | [diff] [blame] | 1119 | public SafeCloseable prepareDrawDragView() { |
Sunny Goyal | 30ac97d | 2020-06-24 23:47:46 -0700 | [diff] [blame] | 1120 | resetIconScale(); |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 1121 | setForceHideDot(true); |
Sunny Goyal | 8b9919d | 2021-04-07 14:45:17 -0700 | [diff] [blame] | 1122 | return () -> { }; |
Adam Cohen | 6508699 | 2020-02-19 08:40:49 -0800 | [diff] [blame] | 1123 | } |
Sunny Goyal | 30ac97d | 2020-06-24 23:47:46 -0700 | [diff] [blame] | 1124 | |
| 1125 | private void resetIconScale() { |
Sunny Goyal | 52f4c16 | 2022-04-23 16:02:03 +0000 | [diff] [blame] | 1126 | if (mIcon != null) { |
| 1127 | mIcon.resetScale(); |
Sunny Goyal | 30ac97d | 2020-06-24 23:47:46 -0700 | [diff] [blame] | 1128 | } |
| 1129 | } |
Schneider Victor-tulias | 350b462 | 2020-09-18 11:16:30 -0700 | [diff] [blame] | 1130 | |
| 1131 | private void updateIcon(Drawable newIcon) { |
| 1132 | if (mLayoutHorizontal) { |
| 1133 | setCompoundDrawablesRelative(newIcon, null, null, null); |
| 1134 | } else { |
| 1135 | setCompoundDrawables(null, newIcon, null, null); |
| 1136 | } |
| 1137 | } |
Steven Ng | 707b118 | 2021-09-13 11:54:58 +0100 | [diff] [blame] | 1138 | |
| 1139 | private String getAppLabelPluralString(String appName, int notificationCount) { |
| 1140 | MessageFormat icuCountFormat = new MessageFormat( |
| 1141 | getResources().getString(R.string.dotted_app_label), |
| 1142 | Locale.getDefault()); |
| 1143 | HashMap<String, Object> args = new HashMap(); |
| 1144 | args.put("app_name", appName); |
| 1145 | args.put("count", notificationCount); |
| 1146 | return icuCountFormat.format(args); |
| 1147 | } |
Sunny Goyal | f0ff70a | 2022-05-11 07:23:10 -0700 | [diff] [blame] | 1148 | |
| 1149 | /** |
| 1150 | * Starts a long press action and returns the corresponding pre-drag condition |
| 1151 | */ |
| 1152 | public PreDragCondition startLongPressAction() { |
| 1153 | PopupContainerWithArrow popup = PopupContainerWithArrow.showForIcon(this); |
| 1154 | return popup != null ? popup.createPreDragCondition(true) : null; |
| 1155 | } |
| 1156 | |
| 1157 | /** |
| 1158 | * Returns true if the view can show long-press popup |
| 1159 | */ |
| 1160 | public boolean canShowLongPressPopup() { |
| 1161 | return getTag() instanceof ItemInfo && ShortcutUtil.supportsShortcuts((ItemInfo) getTag()); |
| 1162 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1163 | } |