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