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