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 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 19 | import android.annotation.TargetApi; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 21 | import android.content.res.ColorStateList; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 22 | import android.content.res.Resources; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 23 | import android.content.res.Resources.Theme; |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 24 | import android.content.res.TypedArray; |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 25 | import android.graphics.Bitmap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | import android.graphics.Canvas; |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 27 | import android.graphics.Paint; |
Michael Jurka | 137142e | 2011-01-05 20:57:04 -0800 | [diff] [blame] | 28 | import android.graphics.Region; |
Winson | 5fbe074 | 2015-09-30 15:33:00 -0700 | [diff] [blame] | 29 | import android.graphics.drawable.ColorDrawable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | import android.graphics.drawable.Drawable; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 31 | import android.os.Build; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 32 | import android.util.AttributeSet; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 33 | import android.util.SparseArray; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 34 | import android.util.TypedValue; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 35 | import android.view.KeyEvent; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 36 | import android.view.MotionEvent; |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 37 | import android.view.View; |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 38 | import android.view.ViewConfiguration; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 39 | import android.view.ViewDebug; |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 40 | import android.view.ViewParent; |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 41 | import android.widget.TextView; |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 42 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 43 | import com.android.launcher3.IconCache.IconLoadRequest; |
Sunny Goyal | 2611943 | 2016-02-18 22:09:23 +0000 | [diff] [blame] | 44 | import com.android.launcher3.folder.FolderIcon; |
Sunny Goyal | 10629b0 | 2016-09-01 12:50:11 -0700 | [diff] [blame] | 45 | import com.android.launcher3.graphics.HolographicOutlineHelper; |
Hyunyoung Song | 2bd3d7d | 2015-05-21 13:04:53 -0700 | [diff] [blame] | 46 | import com.android.launcher3.model.PackageItemInfo; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 47 | |
Sunny Goyal | c469aad | 2015-10-01 11:24:23 -0700 | [diff] [blame] | 48 | import java.text.NumberFormat; |
| 49 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 50 | /** |
| 51 | * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan |
| 52 | * because we want to make the bubble taller than the text and TextView's clip is |
| 53 | * too aggressive. |
| 54 | */ |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 55 | public class BubbleTextView extends TextView |
| 56 | implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView { |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 57 | |
Sameer Padala | 8fd7483 | 2014-09-08 16:00:29 -0700 | [diff] [blame] | 58 | private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 59 | |
Sunny Goyal | 8f8f398 | 2016-07-25 17:08:38 -0700 | [diff] [blame] | 60 | // Dimensions in DP |
| 61 | private static final float AMBIENT_SHADOW_RADIUS = 2.5f; |
| 62 | private static final float KEY_SHADOW_RADIUS = 1f; |
| 63 | private static final float KEY_SHADOW_OFFSET = 0.5f; |
| 64 | private static final int AMBIENT_SHADOW_COLOR = 0x33000000; |
| 65 | private static final int KEY_SHADOW_COLOR = 0x66000000; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 66 | |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 67 | private static final int DISPLAY_WORKSPACE = 0; |
| 68 | private static final int DISPLAY_ALL_APPS = 1; |
Sunny Goyal | baec6ff | 2016-09-14 11:26:21 -0700 | [diff] [blame] | 69 | private static final int DISPLAY_FOLDER = 2; |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 70 | |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 71 | private final Launcher mLauncher; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 72 | private Drawable mIcon; |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 73 | private final boolean mCenterVertically; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 74 | private final Drawable mBackground; |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 75 | private OnLongClickListener mOnLongClickListener; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 76 | private final CheckLongPressHelper mLongPressHelper; |
| 77 | private final HolographicOutlineHelper mOutlineHelper; |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 78 | private final StylusEventHelper mStylusEventHelper; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 79 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 80 | private boolean mBackgroundSizeChanged; |
| 81 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 82 | private Bitmap mPressedBackground; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 83 | |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 84 | private float mSlop; |
| 85 | |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 86 | private final boolean mDeferShadowGenerationOnTouch; |
Sunny Goyal | 15872da | 2014-07-08 15:43:54 -0700 | [diff] [blame] | 87 | private final boolean mCustomShadowsEnabled; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 88 | private final boolean mLayoutHorizontal; |
| 89 | private final int mIconSize; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 90 | @ViewDebug.ExportedProperty(category = "launcher") |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 91 | private int mTextColor; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 92 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 93 | @ViewDebug.ExportedProperty(category = "launcher") |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 94 | private boolean mStayPressed; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 95 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 96 | private boolean mIgnorePressedStateChange; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 97 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 98 | private boolean mDisableRelayout = false; |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 99 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 100 | private IconLoadRequest mIconLoadRequest; |
| 101 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 102 | public BubbleTextView(Context context) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 103 | this(context, null, 0); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | public BubbleTextView(Context context, AttributeSet attrs) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 107 | this(context, attrs, 0); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 111 | super(context, attrs, defStyle); |
Andrew Sapperstein | abef55a | 2016-06-19 12:49:00 -0700 | [diff] [blame] | 112 | mLauncher = Launcher.getLauncher(context); |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 113 | DeviceProfile grid = mLauncher.getDeviceProfile(); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 114 | |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 115 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 116 | R.styleable.BubbleTextView, defStyle, 0); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 117 | mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 118 | mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false); |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 119 | mDeferShadowGenerationOnTouch = |
| 120 | a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 121 | |
| 122 | int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE); |
| 123 | int defaultIconSize = grid.iconSizePx; |
| 124 | if (display == DISPLAY_WORKSPACE) { |
Winson Chung | 44d0aac | 2015-05-11 18:02:55 -0700 | [diff] [blame] | 125 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 126 | } else if (display == DISPLAY_ALL_APPS) { |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 127 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx); |
| 128 | setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 129 | defaultIconSize = grid.allAppsIconSizePx; |
Sunny Goyal | baec6ff | 2016-09-14 11:26:21 -0700 | [diff] [blame] | 130 | } else if (display == DISPLAY_FOLDER) { |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame^] | 131 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx); |
Sunny Goyal | baec6ff | 2016-09-14 11:26:21 -0700 | [diff] [blame] | 132 | setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx); |
Jon Miranda | bf7d812 | 2016-11-03 15:29:29 -0700 | [diff] [blame^] | 133 | defaultIconSize = grid.folderChildIconSizePx; |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 134 | } |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 135 | mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 136 | |
| 137 | mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride, |
| 138 | defaultIconSize); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 139 | a.recycle(); |
| 140 | |
Sunny Goyal | 15872da | 2014-07-08 15:43:54 -0700 | [diff] [blame] | 141 | if (mCustomShadowsEnabled) { |
| 142 | // Draw the background itself as the parent is drawn twice. |
| 143 | mBackground = getBackground(); |
| 144 | setBackground(null); |
Sunny Goyal | 8f8f398 | 2016-07-25 17:08:38 -0700 | [diff] [blame] | 145 | |
| 146 | // Set shadow layer as the larger shadow to that the textView does not clip the shadow. |
| 147 | float density = getResources().getDisplayMetrics().density; |
| 148 | setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR); |
Sunny Goyal | 15872da | 2014-07-08 15:43:54 -0700 | [diff] [blame] | 149 | } else { |
| 150 | mBackground = null; |
| 151 | } |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 152 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 153 | mLongPressHelper = new CheckLongPressHelper(this); |
Mady Mellor | bb83520 | 2015-07-15 16:34:34 -0700 | [diff] [blame] | 154 | mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 155 | |
Sunny Goyal | 10629b0 | 2016-09-01 12:50:11 -0700 | [diff] [blame] | 156 | mOutlineHelper = HolographicOutlineHelper.getInstance(getContext()); |
Sunny Goyal | ae50284 | 2016-06-17 08:43:56 -0700 | [diff] [blame] | 157 | setAccessibilityDelegate(mLauncher.getAccessibilityDelegate()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 160 | public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) { |
| 161 | applyFromShortcutInfo(info, iconCache, false); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 164 | public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache, |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 165 | boolean promiseStateChanged) { |
Sunny Goyal | f420438 | 2016-07-13 10:46:07 -0700 | [diff] [blame] | 166 | applyIconAndLabel(info.getIcon(iconCache), info); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 167 | setTag(info); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 168 | if (promiseStateChanged || info.isPromise()) { |
| 169 | applyState(promiseStateChanged); |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 170 | } |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 173 | public void applyFromApplicationInfo(AppInfo info) { |
Sunny Goyal | f420438 | 2016-07-13 10:46:07 -0700 | [diff] [blame] | 174 | applyIconAndLabel(info.iconBitmap, info); |
| 175 | |
Winson Chung | 888b3a1 | 2015-03-13 11:14:16 -0700 | [diff] [blame] | 176 | // We don't need to check the info since it's not a ShortcutInfo |
| 177 | super.setTag(info); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 178 | |
| 179 | // Verify high res immediately |
| 180 | verifyHighRes(); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 183 | public void applyFromPackageItemInfo(PackageItemInfo info) { |
Sunny Goyal | f420438 | 2016-07-13 10:46:07 -0700 | [diff] [blame] | 184 | applyIconAndLabel(info.iconBitmap, info); |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 185 | // We don't need to check the info since it's not a ShortcutInfo |
| 186 | super.setTag(info); |
| 187 | |
| 188 | // Verify high res immediately |
| 189 | verifyHighRes(); |
| 190 | } |
| 191 | |
Sunny Goyal | f420438 | 2016-07-13 10:46:07 -0700 | [diff] [blame] | 192 | private void applyIconAndLabel(Bitmap icon, ItemInfo info) { |
| 193 | FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(icon); |
| 194 | if (info.isDisabled()) { |
| 195 | iconDrawable.setState(FastBitmapDrawable.State.DISABLED); |
| 196 | } |
| 197 | setIcon(iconDrawable); |
| 198 | setText(info.title); |
| 199 | if (info.contentDescription != null) { |
| 200 | setContentDescription(info.isDisabled() |
| 201 | ? getContext().getString(R.string.disabled_app_label, info.contentDescription) |
| 202 | : info.contentDescription); |
| 203 | } |
| 204 | } |
| 205 | |
Winson Chung | 7501adf | 2015-06-02 11:24:28 -0700 | [diff] [blame] | 206 | /** |
Winson | 5fbe074 | 2015-09-30 15:33:00 -0700 | [diff] [blame] | 207 | * Used for measurement only, sets some dummy values on this view. |
| 208 | */ |
| 209 | public void applyDummyInfo() { |
| 210 | ColorDrawable d = new ColorDrawable(); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 211 | setIcon(mLauncher.resizeIconDrawable(d)); |
Winson | 5fbe074 | 2015-09-30 15:33:00 -0700 | [diff] [blame] | 212 | setText(""); |
| 213 | } |
| 214 | |
| 215 | /** |
Winson Chung | 7501adf | 2015-06-02 11:24:28 -0700 | [diff] [blame] | 216 | * Overrides the default long press timeout. |
| 217 | */ |
| 218 | public void setLongPressTimeout(int longPressTimeout) { |
| 219 | mLongPressHelper.setLongPressTimeout(longPressTimeout); |
| 220 | } |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 221 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 222 | @Override |
| 223 | protected boolean setFrame(int left, int top, int right, int bottom) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 224 | if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 225 | mBackgroundSizeChanged = true; |
| 226 | } |
| 227 | return super.setFrame(left, top, right, bottom); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | protected boolean verifyDrawable(Drawable who) { |
| 232 | return who == mBackground || super.verifyDrawable(who); |
| 233 | } |
| 234 | |
| 235 | @Override |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 236 | public void setTag(Object tag) { |
| 237 | if (tag != null) { |
| 238 | LauncherModel.checkItemInfo((ItemInfo) tag); |
| 239 | } |
| 240 | super.setTag(tag); |
| 241 | } |
| 242 | |
| 243 | @Override |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 244 | public void setPressed(boolean pressed) { |
| 245 | super.setPressed(pressed); |
| 246 | |
| 247 | if (!mIgnorePressedStateChange) { |
| 248 | updateIconState(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 249 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 250 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 251 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 252 | /** Returns the icon for this view. */ |
| 253 | public Drawable getIcon() { |
| 254 | return mIcon; |
| 255 | } |
| 256 | |
| 257 | /** Returns whether the layout is horizontal. */ |
| 258 | public boolean isLayoutHorizontal() { |
| 259 | return mLayoutHorizontal; |
| 260 | } |
| 261 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 262 | private void updateIconState() { |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 263 | if (mIcon instanceof FastBitmapDrawable) { |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 264 | FastBitmapDrawable d = (FastBitmapDrawable) mIcon; |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 265 | if (getTag() instanceof ItemInfo |
| 266 | && ((ItemInfo) getTag()).isDisabled()) { |
Sunny Goyal | cd5b7f9 | 2015-10-20 15:17:00 -0700 | [diff] [blame] | 267 | d.animateState(FastBitmapDrawable.State.DISABLED); |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 268 | } else if (isPressed() || mStayPressed) { |
| 269 | d.animateState(FastBitmapDrawable.State.PRESSED); |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 270 | } else { |
| 271 | d.animateState(FastBitmapDrawable.State.NORMAL); |
| 272 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 273 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | @Override |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 277 | public void setOnLongClickListener(OnLongClickListener l) { |
| 278 | super.setOnLongClickListener(l); |
| 279 | mOnLongClickListener = l; |
| 280 | } |
| 281 | |
| 282 | public OnLongClickListener getOnLongClickListener() { |
| 283 | return mOnLongClickListener; |
| 284 | } |
| 285 | |
| 286 | @Override |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 287 | public boolean onTouchEvent(MotionEvent event) { |
| 288 | // Call the superclass onTouchEvent first, because sometimes it changes the state to |
| 289 | // isPressed() on an ACTION_UP |
| 290 | boolean result = super.onTouchEvent(event); |
| 291 | |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 292 | // 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] | 293 | if (mStylusEventHelper.onMotionEvent(event)) { |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 294 | mLongPressHelper.cancelLongPress(); |
| 295 | result = true; |
| 296 | } |
| 297 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 298 | switch (event.getAction()) { |
| 299 | case MotionEvent.ACTION_DOWN: |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 300 | // So that the pressed outline is visible immediately on setStayPressed(), |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 301 | // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time |
| 302 | // to create it) |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 303 | if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 304 | mPressedBackground = mOutlineHelper.createMediumDropShadow(this); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 305 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 306 | |
Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 307 | // If we're in a stylus button press, don't check for long press. |
| 308 | if (!mStylusEventHelper.inStylusButtonPressed()) { |
| 309 | mLongPressHelper.postCheckForLongPress(); |
| 310 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 311 | break; |
| 312 | case MotionEvent.ACTION_CANCEL: |
| 313 | case MotionEvent.ACTION_UP: |
| 314 | // If we've touched down and up on an item, and it's still not "pressed", then |
| 315 | // destroy the pressed outline |
| 316 | if (!isPressed()) { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 317 | mPressedBackground = null; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 318 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 319 | |
| 320 | mLongPressHelper.cancelLongPress(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 321 | break; |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 322 | case MotionEvent.ACTION_MOVE: |
| 323 | if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) { |
| 324 | mLongPressHelper.cancelLongPress(); |
| 325 | } |
| 326 | break; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 327 | } |
| 328 | return result; |
| 329 | } |
| 330 | |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 331 | void setStayPressed(boolean stayPressed) { |
| 332 | mStayPressed = stayPressed; |
| 333 | if (!stayPressed) { |
Sunny Goyal | 10629b0 | 2016-09-01 12:50:11 -0700 | [diff] [blame] | 334 | HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 335 | mPressedBackground = null; |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 336 | } else { |
| 337 | if (mPressedBackground == null) { |
| 338 | mPressedBackground = mOutlineHelper.createMediumDropShadow(this); |
| 339 | } |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 340 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 341 | |
| 342 | // Only show the shadow effect when persistent pressed state is set. |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 343 | ViewParent parent = getParent(); |
| 344 | if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) { |
| 345 | ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon( |
| 346 | this, mPressedBackground); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | updateIconState(); |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 352 | void clearPressedBackground() { |
| 353 | setPressed(false); |
| 354 | setStayPressed(false); |
| 355 | } |
| 356 | |
| 357 | @Override |
| 358 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 359 | if (super.onKeyDown(keyCode, event)) { |
| 360 | // Pre-create shadow so show immediately on click. |
| 361 | if (mPressedBackground == null) { |
| 362 | mPressedBackground = mOutlineHelper.createMediumDropShadow(this); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 363 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 364 | return true; |
Patrick Dubroy | d69e113 | 2011-03-15 10:29:01 -0700 | [diff] [blame] | 365 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 366 | return false; |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 369 | @Override |
| 370 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 371 | // Unlike touch events, keypress event propagate pressed state change immediately, |
| 372 | // without waiting for onClickHandler to execute. Disable pressed state changes here |
| 373 | // to avoid flickering. |
| 374 | mIgnorePressedStateChange = true; |
| 375 | boolean result = super.onKeyUp(keyCode, event); |
Winson Chung | 1e9cbfe | 2011-09-30 16:52:26 -0700 | [diff] [blame] | 376 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 377 | mPressedBackground = null; |
| 378 | mIgnorePressedStateChange = false; |
| 379 | updateIconState(); |
| 380 | return result; |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 381 | } |
Patrick Dubroy | a017c03 | 2011-03-09 15:58:32 -0800 | [diff] [blame] | 382 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 383 | @Override |
| 384 | public void draw(Canvas canvas) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 385 | if (!mCustomShadowsEnabled) { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 386 | super.draw(canvas); |
| 387 | return; |
| 388 | } |
| 389 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 390 | final Drawable background = mBackground; |
| 391 | if (background != null) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 392 | final int scrollX = getScrollX(); |
| 393 | final int scrollY = getScrollY(); |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 394 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 395 | if (mBackgroundSizeChanged) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 396 | background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop()); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 397 | mBackgroundSizeChanged = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 398 | } |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 399 | |
| 400 | if ((scrollX | scrollY) == 0) { |
| 401 | background.draw(canvas); |
| 402 | } else { |
| 403 | canvas.translate(scrollX, scrollY); |
| 404 | background.draw(canvas); |
| 405 | canvas.translate(-scrollX, -scrollY); |
| 406 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 407 | } |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 408 | |
| 409 | // If text is transparent, don't draw any shadow |
Andrew Flynn | bc239a1 | 2012-03-06 11:39:49 -0800 | [diff] [blame] | 410 | if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) { |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 411 | getPaint().clearShadowLayer(); |
| 412 | super.draw(canvas); |
| 413 | return; |
| 414 | } |
| 415 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 416 | // We enhance the shadow by drawing the shadow twice |
Sunny Goyal | 8f8f398 | 2016-07-25 17:08:38 -0700 | [diff] [blame] | 417 | float density = getResources().getDisplayMetrics().density; |
| 418 | getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 419 | super.draw(canvas); |
| 420 | canvas.save(Canvas.CLIP_SAVE_FLAG); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 421 | canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(), |
| 422 | getScrollX() + getWidth(), |
| 423 | getScrollY() + getHeight(), Region.Op.INTERSECT); |
Sunny Goyal | 8f8f398 | 2016-07-25 17:08:38 -0700 | [diff] [blame] | 424 | getPaint().setShadowLayer( |
| 425 | density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 426 | super.draw(canvas); |
| 427 | canvas.restore(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 428 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 429 | |
| 430 | @Override |
| 431 | protected void onAttachedToWindow() { |
| 432 | super.onAttachedToWindow(); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 433 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 434 | if (mBackground != null) mBackground.setCallback(this); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 435 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 436 | if (mIcon instanceof PreloadIconDrawable) { |
| 437 | ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme()); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 438 | } |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 439 | mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | @Override |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 443 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 444 | if (mCenterVertically) { |
| 445 | Paint.FontMetrics fm = getPaint().getFontMetrics(); |
| 446 | int cellHeightPx = mIconSize + getCompoundDrawablePadding() + |
| 447 | (int) Math.ceil(fm.bottom - fm.top); |
| 448 | int height = MeasureSpec.getSize(heightMeasureSpec); |
| 449 | setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(), |
| 450 | getPaddingBottom()); |
| 451 | } |
| 452 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 453 | } |
| 454 | |
| 455 | @Override |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 456 | protected void onDetachedFromWindow() { |
| 457 | super.onDetachedFromWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 458 | if (mBackground != null) mBackground.setCallback(null); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 459 | } |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 460 | |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 461 | @Override |
| 462 | public void setTextColor(int color) { |
| 463 | mTextColor = color; |
| 464 | super.setTextColor(color); |
| 465 | } |
| 466 | |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 467 | @Override |
| 468 | public void setTextColor(ColorStateList colors) { |
| 469 | mTextColor = colors.getDefaultColor(); |
| 470 | super.setTextColor(colors); |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 473 | public void setTextVisibility(boolean visible) { |
| 474 | Resources res = getResources(); |
| 475 | if (visible) { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 476 | super.setTextColor(mTextColor); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 477 | } else { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 478 | super.setTextColor(res.getColor(android.R.color.transparent)); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 479 | } |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 482 | @Override |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 483 | public void cancelLongPress() { |
| 484 | super.cancelLongPress(); |
| 485 | |
| 486 | mLongPressHelper.cancelLongPress(); |
| 487 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 488 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 489 | public void applyState(boolean promiseStateChanged) { |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 490 | if (getTag() instanceof ShortcutInfo) { |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 491 | ShortcutInfo info = (ShortcutInfo) getTag(); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 492 | final boolean isPromise = info.isPromise(); |
| 493 | final int progressLevel = isPromise ? |
| 494 | ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ? |
| 495 | info.getInstallProgress() : 0)) : 100; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 496 | |
Sunny Goyal | c469aad | 2015-10-01 11:24:23 -0700 | [diff] [blame] | 497 | setContentDescription(progressLevel > 0 ? |
| 498 | getContext().getString(R.string.app_downloading_title, info.title, |
| 499 | NumberFormat.getPercentInstance().format(progressLevel * 0.01)) : |
| 500 | getContext().getString(R.string.app_waiting_download_title, info.title)); |
| 501 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 502 | if (mIcon != null) { |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 503 | final PreloadIconDrawable preloadDrawable; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 504 | if (mIcon instanceof PreloadIconDrawable) { |
| 505 | preloadDrawable = (PreloadIconDrawable) mIcon; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 506 | } else { |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 507 | preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme()); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 508 | setIcon(preloadDrawable); |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | preloadDrawable.setLevel(progressLevel); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 512 | if (promiseStateChanged) { |
| 513 | preloadDrawable.maybePerformFinishedAnimation(); |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 514 | } |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 515 | } |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 516 | } |
| 517 | } |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 518 | |
| 519 | private Theme getPreloaderTheme() { |
| 520 | Object tag = getTag(); |
| 521 | int style = ((tag != null) && (tag instanceof ShortcutInfo) && |
| 522 | (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder |
| 523 | : R.style.PreloadIcon; |
| 524 | Theme theme = sPreloaderThemes.get(style); |
| 525 | if (theme == null) { |
| 526 | theme = getResources().newTheme(); |
| 527 | theme.applyStyle(style, true); |
| 528 | sPreloaderThemes.put(style, theme); |
| 529 | } |
| 530 | return theme; |
| 531 | } |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 532 | |
| 533 | /** |
| 534 | * Sets the icon for this view based on the layout direction. |
| 535 | */ |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 536 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) |
Tony Wickham | 377ed3f | 2016-07-20 15:21:04 -0700 | [diff] [blame] | 537 | private void setIcon(Drawable icon) { |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 538 | mIcon = icon; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 539 | if (mIconSize != -1) { |
| 540 | mIcon.setBounds(0, 0, mIconSize, mIconSize); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 541 | } |
Tony Wickham | 377ed3f | 2016-07-20 15:21:04 -0700 | [diff] [blame] | 542 | applyCompoundDrawables(mIcon); |
| 543 | } |
| 544 | |
| 545 | protected void applyCompoundDrawables(Drawable icon) { |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 546 | if (mLayoutHorizontal) { |
Sunny Goyal | 9fc953b | 2015-08-17 12:24:25 -0700 | [diff] [blame] | 547 | if (Utilities.ATLEAST_JB_MR1) { |
Tony Wickham | 377ed3f | 2016-07-20 15:21:04 -0700 | [diff] [blame] | 548 | setCompoundDrawablesRelative(icon, null, null, null); |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 549 | } else { |
Tony Wickham | 377ed3f | 2016-07-20 15:21:04 -0700 | [diff] [blame] | 550 | setCompoundDrawables(icon, null, null, null); |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 551 | } |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 552 | } else { |
Tony Wickham | 377ed3f | 2016-07-20 15:21:04 -0700 | [diff] [blame] | 553 | setCompoundDrawables(null, icon, null, null); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 554 | } |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 555 | } |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 556 | |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 557 | @Override |
| 558 | public void requestLayout() { |
| 559 | if (!mDisableRelayout) { |
| 560 | super.requestLayout(); |
| 561 | } |
| 562 | } |
| 563 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 564 | /** |
| 565 | * Applies the item info if it is same as what the view is pointing to currently. |
| 566 | */ |
| 567 | public void reapplyItemInfo(final ItemInfo info) { |
| 568 | if (getTag() == info) { |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 569 | FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL; |
| 570 | if (mIcon instanceof FastBitmapDrawable) { |
| 571 | prevState = ((FastBitmapDrawable) mIcon).getCurrentState(); |
| 572 | } |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 573 | mIconLoadRequest = null; |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 574 | mDisableRelayout = true; |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 575 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 576 | if (info instanceof AppInfo) { |
| 577 | applyFromApplicationInfo((AppInfo) info); |
| 578 | } else if (info instanceof ShortcutInfo) { |
| 579 | applyFromShortcutInfo((ShortcutInfo) info, |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 580 | LauncherAppState.getInstance().getIconCache()); |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 581 | if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) { |
| 582 | View folderIcon = |
| 583 | mLauncher.getWorkspace().getHomescreenIconByItemId(info.container); |
| 584 | if (folderIcon != null) { |
| 585 | folderIcon.invalidate(); |
| 586 | } |
| 587 | } |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 588 | } else if (info instanceof PackageItemInfo) { |
| 589 | applyFromPackageItemInfo((PackageItemInfo) info); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 590 | } |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 591 | |
| 592 | // If we are reapplying over an old icon, then we should update the new icon to the same |
| 593 | // state as the old icon |
| 594 | if (mIcon instanceof FastBitmapDrawable) { |
| 595 | ((FastBitmapDrawable) mIcon).setState(prevState); |
| 596 | } |
| 597 | |
Sunny Goyal | 69b7564 | 2015-05-15 17:00:24 -0700 | [diff] [blame] | 598 | mDisableRelayout = false; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Verifies that the current icon is high-res otherwise posts a request to load the icon. |
| 604 | */ |
| 605 | public void verifyHighRes() { |
| 606 | if (mIconLoadRequest != null) { |
| 607 | mIconLoadRequest.cancel(); |
| 608 | mIconLoadRequest = null; |
| 609 | } |
| 610 | if (getTag() instanceof AppInfo) { |
| 611 | AppInfo info = (AppInfo) getTag(); |
| 612 | if (info.usingLowResIcon) { |
| 613 | mIconLoadRequest = LauncherAppState.getInstance().getIconCache() |
| 614 | .updateIconInBackground(BubbleTextView.this, info); |
| 615 | } |
| 616 | } else if (getTag() instanceof ShortcutInfo) { |
| 617 | ShortcutInfo info = (ShortcutInfo) getTag(); |
| 618 | if (info.usingLowResIcon) { |
| 619 | mIconLoadRequest = LauncherAppState.getInstance().getIconCache() |
| 620 | .updateIconInBackground(BubbleTextView.this, info); |
| 621 | } |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 622 | } else if (getTag() instanceof PackageItemInfo) { |
| 623 | PackageItemInfo info = (PackageItemInfo) getTag(); |
| 624 | if (info.usingLowResIcon) { |
| 625 | mIconLoadRequest = LauncherAppState.getInstance().getIconCache() |
| 626 | .updateIconInBackground(BubbleTextView.this, info); |
| 627 | } |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 628 | } |
| 629 | } |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 630 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 631 | @Override |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 632 | public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) { |
| 633 | // We can only set the fast scroll focus state on a FastBitmapDrawable |
| 634 | if (!(mIcon instanceof FastBitmapDrawable)) { |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 635 | return; |
| 636 | } |
| 637 | |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 638 | FastBitmapDrawable d = (FastBitmapDrawable) mIcon; |
| 639 | if (animated) { |
| 640 | FastBitmapDrawable.State prevState = d.getCurrentState(); |
| 641 | if (d.animateState(focusState)) { |
| 642 | // If the state was updated, then update the view accordingly |
| 643 | animate().scaleX(focusState.viewScale) |
| 644 | .scaleY(focusState.viewScale) |
| 645 | .setStartDelay(getStartDelayForStateChange(prevState, focusState)) |
| 646 | .setDuration(d.getDurationForStateChange(prevState, focusState)) |
| 647 | .start(); |
| 648 | } |
| 649 | } else { |
| 650 | if (d.setState(focusState)) { |
| 651 | // If the state was updated, then update the view accordingly |
| 652 | animate().cancel(); |
| 653 | setScaleX(focusState.viewScale); |
| 654 | setScaleY(focusState.viewScale); |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 659 | /** |
Sunny Goyal | 3ffa64d | 2016-07-25 13:54:32 -0700 | [diff] [blame] | 660 | * Returns true if the view can show custom shortcuts. |
| 661 | */ |
| 662 | public boolean hasDeepShortcuts() { |
| 663 | return !mLauncher.getShortcutIdsForItem((ItemInfo) getTag()).isEmpty(); |
| 664 | } |
| 665 | |
| 666 | /** |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 667 | * Returns the start delay when animating between certain {@link FastBitmapDrawable} states. |
| 668 | */ |
| 669 | private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState, |
| 670 | final FastBitmapDrawable.State toState) { |
| 671 | switch (toState) { |
| 672 | case NORMAL: |
| 673 | switch (fromState) { |
| 674 | case FAST_SCROLL_HIGHLIGHTED: |
| 675 | return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4; |
| 676 | } |
| 677 | } |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | /** |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 682 | * Interface to be implemented by the grand parent to allow click shadow effect. |
| 683 | */ |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 684 | public interface BubbleTextShadowHandler { |
Sunny Goyal | 4b6eb26 | 2015-05-14 19:24:40 -0700 | [diff] [blame] | 685 | void setPressedIcon(BubbleTextView icon, Bitmap background); |
| 686 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 687 | } |