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; |
Michael Jurka | 137142e | 2011-01-05 20:57:04 -0800 | [diff] [blame] | 27 | import android.graphics.Region; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 29 | import android.os.Build; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 30 | import android.util.AttributeSet; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 31 | import android.util.SparseArray; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 32 | import android.util.TypedValue; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 33 | import android.view.KeyEvent; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 34 | import android.view.MotionEvent; |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 35 | import android.view.ViewConfiguration; |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 36 | import android.widget.TextView; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 37 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 38 | import com.android.launcher3.IconCache.IconLoadRequest; |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 39 | import com.android.launcher3.widget.PackageItemInfo; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 40 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | /** |
| 42 | * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan |
| 43 | * because we want to make the bubble taller than the text and TextView's clip is |
| 44 | * too aggressive. |
| 45 | */ |
Michael Jurka | 08ee770 | 2011-08-11 16:53:35 -0700 | [diff] [blame] | 46 | public class BubbleTextView extends TextView { |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 47 | |
Sameer Padala | 8fd7483 | 2014-09-08 16:00:29 -0700 | [diff] [blame] | 48 | private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 49 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 50 | private static final float SHADOW_LARGE_RADIUS = 4.0f; |
| 51 | private static final float SHADOW_SMALL_RADIUS = 1.75f; |
| 52 | private static final float SHADOW_Y_OFFSET = 2.0f; |
| 53 | private static final int SHADOW_LARGE_COLOUR = 0xDD000000; |
| 54 | private static final int SHADOW_SMALL_COLOUR = 0xCC000000; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 55 | static final float PADDING_V = 3.0f; |
| 56 | |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 57 | private static final int DISPLAY_WORKSPACE = 0; |
| 58 | private static final int DISPLAY_ALL_APPS = 1; |
| 59 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 60 | private Drawable mIcon; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 61 | private final Drawable mBackground; |
| 62 | private final CheckLongPressHelper mLongPressHelper; |
| 63 | private final HolographicOutlineHelper mOutlineHelper; |
| 64 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 65 | private boolean mBackgroundSizeChanged; |
| 66 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 67 | private Bitmap mPressedBackground; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 68 | |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 69 | private float mSlop; |
| 70 | |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 71 | private final boolean mDeferShadowGenerationOnTouch; |
Sunny Goyal | 15872da | 2014-07-08 15:43:54 -0700 | [diff] [blame] | 72 | private final boolean mCustomShadowsEnabled; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 73 | private final boolean mLayoutHorizontal; |
| 74 | private final int mIconSize; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 75 | private int mTextColor; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 76 | |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 77 | private boolean mStayPressed; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 78 | private boolean mIgnorePressedStateChange; |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 79 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 80 | private IconLoadRequest mIconLoadRequest; |
| 81 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 82 | public BubbleTextView(Context context) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 83 | this(context, null, 0); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public BubbleTextView(Context context, AttributeSet attrs) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 87 | this(context, attrs, 0); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 91 | super(context, attrs, defStyle); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 92 | LauncherAppState app = LauncherAppState.getInstance(); |
| 93 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 94 | |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 95 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 96 | R.styleable.BubbleTextView, defStyle, 0); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 97 | mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 98 | mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false); |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 99 | mDeferShadowGenerationOnTouch = |
| 100 | a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 101 | |
| 102 | int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE); |
| 103 | int defaultIconSize = grid.iconSizePx; |
| 104 | if (display == DISPLAY_WORKSPACE) { |
Winson Chung | 44d0aac | 2015-05-11 18:02:55 -0700 | [diff] [blame] | 105 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 106 | } else if (display == DISPLAY_ALL_APPS) { |
Winson Chung | 44d0aac | 2015-05-11 18:02:55 -0700 | [diff] [blame] | 107 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 108 | defaultIconSize = grid.allAppsIconSizePx; |
| 109 | } |
| 110 | |
| 111 | mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride, |
| 112 | defaultIconSize); |
| 113 | |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 114 | a.recycle(); |
| 115 | |
Sunny Goyal | 15872da | 2014-07-08 15:43:54 -0700 | [diff] [blame] | 116 | if (mCustomShadowsEnabled) { |
| 117 | // Draw the background itself as the parent is drawn twice. |
| 118 | mBackground = getBackground(); |
| 119 | setBackground(null); |
| 120 | } else { |
| 121 | mBackground = null; |
| 122 | } |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 123 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 124 | mLongPressHelper = new CheckLongPressHelper(this); |
| 125 | |
| 126 | mOutlineHelper = HolographicOutlineHelper.obtain(getContext()); |
| 127 | if (mCustomShadowsEnabled) { |
| 128 | setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR); |
| 129 | } |
| 130 | |
| 131 | setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 134 | public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) { |
| 135 | applyFromShortcutInfo(info, iconCache, false); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 138 | public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache, |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 139 | boolean promiseStateChanged) { |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 140 | Bitmap b = info.getIcon(iconCache); |
| 141 | |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 142 | FastBitmapDrawable iconDrawable = Utilities.createIconDrawable(b); |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 143 | iconDrawable.setGhostModeEnabled(info.isDisabled != 0); |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 144 | |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 145 | setIcon(iconDrawable, mIconSize); |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 146 | if (info.contentDescription != null) { |
| 147 | setContentDescription(info.contentDescription); |
| 148 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 149 | setText(info.title); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 150 | setTag(info); |
Sunny Goyal | 3484638 | 2014-07-09 00:09:28 -0700 | [diff] [blame] | 151 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 152 | if (promiseStateChanged || info.isPromise()) { |
| 153 | applyState(promiseStateChanged); |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 154 | } |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 157 | public void applyFromApplicationInfo(AppInfo info) { |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 158 | setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 159 | setText(info.title); |
| 160 | if (info.contentDescription != null) { |
| 161 | setContentDescription(info.contentDescription); |
| 162 | } |
Winson Chung | 888b3a1 | 2015-03-13 11:14:16 -0700 | [diff] [blame] | 163 | // We don't need to check the info since it's not a ShortcutInfo |
| 164 | super.setTag(info); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 165 | |
| 166 | // Verify high res immediately |
| 167 | verifyHighRes(); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 170 | public void applyFromPackageItemInfo(PackageItemInfo info) { |
| 171 | setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize); |
| 172 | setText(info.title); |
| 173 | if (info.contentDescription != null) { |
| 174 | setContentDescription(info.contentDescription); |
| 175 | } |
| 176 | // We don't need to check the info since it's not a ShortcutInfo |
| 177 | super.setTag(info); |
| 178 | |
| 179 | // Verify high res immediately |
| 180 | verifyHighRes(); |
| 181 | } |
| 182 | |
| 183 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 184 | @Override |
| 185 | protected boolean setFrame(int left, int top, int right, int bottom) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 186 | if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 187 | mBackgroundSizeChanged = true; |
| 188 | } |
| 189 | return super.setFrame(left, top, right, bottom); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | protected boolean verifyDrawable(Drawable who) { |
| 194 | return who == mBackground || super.verifyDrawable(who); |
| 195 | } |
| 196 | |
| 197 | @Override |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 198 | public void setTag(Object tag) { |
| 199 | if (tag != null) { |
| 200 | LauncherModel.checkItemInfo((ItemInfo) tag); |
| 201 | } |
| 202 | super.setTag(tag); |
| 203 | } |
| 204 | |
| 205 | @Override |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 206 | public void setPressed(boolean pressed) { |
| 207 | super.setPressed(pressed); |
| 208 | |
| 209 | if (!mIgnorePressedStateChange) { |
| 210 | updateIconState(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 211 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 212 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 213 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 214 | /** Returns the icon for this view. */ |
| 215 | public Drawable getIcon() { |
| 216 | return mIcon; |
| 217 | } |
| 218 | |
| 219 | /** Returns whether the layout is horizontal. */ |
| 220 | public boolean isLayoutHorizontal() { |
| 221 | return mLayoutHorizontal; |
| 222 | } |
| 223 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 224 | private void updateIconState() { |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 225 | if (mIcon instanceof FastBitmapDrawable) { |
| 226 | ((FastBitmapDrawable) mIcon).setPressed(isPressed() || mStayPressed); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 227 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public boolean onTouchEvent(MotionEvent event) { |
| 232 | // Call the superclass onTouchEvent first, because sometimes it changes the state to |
| 233 | // isPressed() on an ACTION_UP |
| 234 | boolean result = super.onTouchEvent(event); |
| 235 | |
| 236 | switch (event.getAction()) { |
| 237 | case MotionEvent.ACTION_DOWN: |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 238 | // So that the pressed outline is visible immediately on setStayPressed(), |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 239 | // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time |
| 240 | // to create it) |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 241 | if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 242 | mPressedBackground = mOutlineHelper.createMediumDropShadow(this); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 243 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 244 | |
| 245 | mLongPressHelper.postCheckForLongPress(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 246 | break; |
| 247 | case MotionEvent.ACTION_CANCEL: |
| 248 | case MotionEvent.ACTION_UP: |
| 249 | // If we've touched down and up on an item, and it's still not "pressed", then |
| 250 | // destroy the pressed outline |
| 251 | if (!isPressed()) { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 252 | mPressedBackground = null; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 253 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 254 | |
| 255 | mLongPressHelper.cancelLongPress(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 256 | break; |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 257 | case MotionEvent.ACTION_MOVE: |
| 258 | if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) { |
| 259 | mLongPressHelper.cancelLongPress(); |
| 260 | } |
| 261 | break; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 262 | } |
| 263 | return result; |
| 264 | } |
| 265 | |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 266 | void setStayPressed(boolean stayPressed) { |
| 267 | mStayPressed = stayPressed; |
| 268 | if (!stayPressed) { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 269 | mPressedBackground = null; |
Winson Chung | 93f98ea | 2015-03-10 16:28:47 -0700 | [diff] [blame] | 270 | } else { |
| 271 | if (mPressedBackground == null) { |
| 272 | mPressedBackground = mOutlineHelper.createMediumDropShadow(this); |
| 273 | } |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 274 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 275 | |
| 276 | // Only show the shadow effect when persistent pressed state is set. |
| 277 | if (getParent() instanceof ShortcutAndWidgetContainer) { |
| 278 | CellLayout layout = (CellLayout) getParent().getParent(); |
Sunny Goyal | 4fe5a37 | 2015-05-14 19:55:10 -0700 | [diff] [blame^] | 279 | layout.setPressedIcon(this, mPressedBackground); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | updateIconState(); |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 285 | void clearPressedBackground() { |
| 286 | setPressed(false); |
| 287 | setStayPressed(false); |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 292 | if (super.onKeyDown(keyCode, event)) { |
| 293 | // Pre-create shadow so show immediately on click. |
| 294 | if (mPressedBackground == null) { |
| 295 | mPressedBackground = mOutlineHelper.createMediumDropShadow(this); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 296 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 297 | return true; |
Patrick Dubroy | d69e113 | 2011-03-15 10:29:01 -0700 | [diff] [blame] | 298 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 299 | return false; |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 302 | @Override |
| 303 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 304 | // Unlike touch events, keypress event propagate pressed state change immediately, |
| 305 | // without waiting for onClickHandler to execute. Disable pressed state changes here |
| 306 | // to avoid flickering. |
| 307 | mIgnorePressedStateChange = true; |
| 308 | boolean result = super.onKeyUp(keyCode, event); |
Winson Chung | 1e9cbfe | 2011-09-30 16:52:26 -0700 | [diff] [blame] | 309 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 310 | mPressedBackground = null; |
| 311 | mIgnorePressedStateChange = false; |
| 312 | updateIconState(); |
| 313 | return result; |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 314 | } |
Patrick Dubroy | a017c03 | 2011-03-09 15:58:32 -0800 | [diff] [blame] | 315 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 316 | @Override |
| 317 | public void draw(Canvas canvas) { |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 318 | if (!mCustomShadowsEnabled) { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 319 | super.draw(canvas); |
| 320 | return; |
| 321 | } |
| 322 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 323 | final Drawable background = mBackground; |
| 324 | if (background != null) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 325 | final int scrollX = getScrollX(); |
| 326 | final int scrollY = getScrollY(); |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 327 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 328 | if (mBackgroundSizeChanged) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 329 | background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop()); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 330 | mBackgroundSizeChanged = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 331 | } |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 332 | |
| 333 | if ((scrollX | scrollY) == 0) { |
| 334 | background.draw(canvas); |
| 335 | } else { |
| 336 | canvas.translate(scrollX, scrollY); |
| 337 | background.draw(canvas); |
| 338 | canvas.translate(-scrollX, -scrollY); |
| 339 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 340 | } |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 341 | |
| 342 | // If text is transparent, don't draw any shadow |
Andrew Flynn | bc239a1 | 2012-03-06 11:39:49 -0800 | [diff] [blame] | 343 | if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) { |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 344 | getPaint().clearShadowLayer(); |
| 345 | super.draw(canvas); |
| 346 | return; |
| 347 | } |
| 348 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 349 | // We enhance the shadow by drawing the shadow twice |
Michael Jurka | e7e3f6c | 2011-02-01 21:08:29 -0800 | [diff] [blame] | 350 | 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] | 351 | super.draw(canvas); |
| 352 | canvas.save(Canvas.CLIP_SAVE_FLAG); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 353 | canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(), |
| 354 | getScrollX() + getWidth(), |
| 355 | getScrollY() + getHeight(), Region.Op.INTERSECT); |
Michael Jurka | e7e3f6c | 2011-02-01 21:08:29 -0800 | [diff] [blame] | 356 | getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 357 | super.draw(canvas); |
| 358 | canvas.restore(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 359 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 360 | |
| 361 | @Override |
| 362 | protected void onAttachedToWindow() { |
| 363 | super.onAttachedToWindow(); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 364 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 365 | if (mBackground != null) mBackground.setCallback(this); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 366 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 367 | if (mIcon instanceof PreloadIconDrawable) { |
| 368 | ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme()); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 369 | } |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 370 | mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | @Override |
| 374 | protected void onDetachedFromWindow() { |
| 375 | super.onDetachedFromWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 376 | if (mBackground != null) mBackground.setCallback(null); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 377 | } |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 378 | |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 379 | @Override |
| 380 | public void setTextColor(int color) { |
| 381 | mTextColor = color; |
| 382 | super.setTextColor(color); |
| 383 | } |
| 384 | |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 385 | @Override |
| 386 | public void setTextColor(ColorStateList colors) { |
| 387 | mTextColor = colors.getDefaultColor(); |
| 388 | super.setTextColor(colors); |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 391 | public void setTextVisibility(boolean visible) { |
| 392 | Resources res = getResources(); |
| 393 | if (visible) { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 394 | super.setTextColor(mTextColor); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 395 | } else { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 396 | super.setTextColor(res.getColor(android.R.color.transparent)); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 397 | } |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 400 | @Override |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 401 | public void cancelLongPress() { |
| 402 | super.cancelLongPress(); |
| 403 | |
| 404 | mLongPressHelper.cancelLongPress(); |
| 405 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 406 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 407 | public void applyState(boolean promiseStateChanged) { |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 408 | if (getTag() instanceof ShortcutInfo) { |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 409 | ShortcutInfo info = (ShortcutInfo) getTag(); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 410 | final boolean isPromise = info.isPromise(); |
| 411 | final int progressLevel = isPromise ? |
| 412 | ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ? |
| 413 | info.getInstallProgress() : 0)) : 100; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 414 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 415 | if (mIcon != null) { |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 416 | final PreloadIconDrawable preloadDrawable; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 417 | if (mIcon instanceof PreloadIconDrawable) { |
| 418 | preloadDrawable = (PreloadIconDrawable) mIcon; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 419 | } else { |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 420 | preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme()); |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 421 | setIcon(preloadDrawable, mIconSize); |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | preloadDrawable.setLevel(progressLevel); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 425 | if (promiseStateChanged) { |
| 426 | preloadDrawable.maybePerformFinishedAnimation(); |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 427 | } |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 428 | } |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 429 | } |
| 430 | } |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 431 | |
| 432 | private Theme getPreloaderTheme() { |
| 433 | Object tag = getTag(); |
| 434 | int style = ((tag != null) && (tag instanceof ShortcutInfo) && |
| 435 | (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder |
| 436 | : R.style.PreloadIcon; |
| 437 | Theme theme = sPreloaderThemes.get(style); |
| 438 | if (theme == null) { |
| 439 | theme = getResources().newTheme(); |
| 440 | theme.applyStyle(style, true); |
| 441 | sPreloaderThemes.put(style, theme); |
| 442 | } |
| 443 | return theme; |
| 444 | } |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 445 | |
| 446 | /** |
| 447 | * Sets the icon for this view based on the layout direction. |
| 448 | */ |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 449 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 450 | private Drawable setIcon(Drawable icon, int iconSize) { |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 451 | mIcon = icon; |
| 452 | if (iconSize != -1) { |
| 453 | mIcon.setBounds(0, 0, iconSize, iconSize); |
| 454 | } |
| 455 | if (mLayoutHorizontal) { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 456 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { |
| 457 | setCompoundDrawablesRelative(mIcon, null, null, null); |
| 458 | } else { |
| 459 | setCompoundDrawables(mIcon, null, null, null); |
| 460 | } |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 461 | } else { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 462 | setCompoundDrawables(null, mIcon, null, null); |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 463 | } |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 464 | return icon; |
| 465 | } |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 466 | |
| 467 | /** |
| 468 | * Applies the item info if it is same as what the view is pointing to currently. |
| 469 | */ |
| 470 | public void reapplyItemInfo(final ItemInfo info) { |
| 471 | if (getTag() == info) { |
| 472 | mIconLoadRequest = null; |
| 473 | if (info instanceof AppInfo) { |
| 474 | applyFromApplicationInfo((AppInfo) info); |
| 475 | } else if (info instanceof ShortcutInfo) { |
| 476 | applyFromShortcutInfo((ShortcutInfo) info, |
Sunny Goyal | dfaccf6 | 2015-05-11 16:30:44 -0700 | [diff] [blame] | 477 | LauncherAppState.getInstance().getIconCache()); |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 478 | } else if (info instanceof PackageItemInfo) { |
| 479 | applyFromPackageItemInfo((PackageItemInfo) info); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Verifies that the current icon is high-res otherwise posts a request to load the icon. |
| 486 | */ |
| 487 | public void verifyHighRes() { |
| 488 | if (mIconLoadRequest != null) { |
| 489 | mIconLoadRequest.cancel(); |
| 490 | mIconLoadRequest = null; |
| 491 | } |
| 492 | if (getTag() instanceof AppInfo) { |
| 493 | AppInfo info = (AppInfo) getTag(); |
| 494 | if (info.usingLowResIcon) { |
| 495 | mIconLoadRequest = LauncherAppState.getInstance().getIconCache() |
| 496 | .updateIconInBackground(BubbleTextView.this, info); |
| 497 | } |
| 498 | } else if (getTag() instanceof ShortcutInfo) { |
| 499 | ShortcutInfo info = (ShortcutInfo) getTag(); |
| 500 | if (info.usingLowResIcon) { |
| 501 | mIconLoadRequest = LauncherAppState.getInstance().getIconCache() |
| 502 | .updateIconInBackground(BubbleTextView.this, info); |
| 503 | } |
Sunny Goyal | 0e08f16 | 2015-05-12 11:32:39 -0700 | [diff] [blame] | 504 | } else if (getTag() instanceof PackageItemInfo) { |
| 505 | PackageItemInfo info = (PackageItemInfo) getTag(); |
| 506 | if (info.usingLowResIcon) { |
| 507 | mIconLoadRequest = LauncherAppState.getInstance().getIconCache() |
| 508 | .updateIconInBackground(BubbleTextView.this, info); |
| 509 | } |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 512 | } |