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