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