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