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