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