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