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; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 20 | import android.content.res.Resources; |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 22 | import android.graphics.Canvas; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 23 | import android.graphics.Rect; |
Michael Jurka | 137142e | 2011-01-05 20:57:04 -0800 | [diff] [blame] | 24 | import android.graphics.Region; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 25 | import android.graphics.Region.Op; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | import android.graphics.drawable.Drawable; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 27 | import android.util.AttributeSet; |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 28 | import android.util.Log; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 29 | import android.util.TypedValue; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 30 | import android.view.MotionEvent; |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 31 | import android.view.ViewConfiguration; |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 32 | import android.widget.TextView; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 33 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 34 | /** |
| 35 | * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan |
| 36 | * because we want to make the bubble taller than the text and TextView's clip is |
| 37 | * too aggressive. |
| 38 | */ |
Michael Jurka | 08ee770 | 2011-08-11 16:53:35 -0700 | [diff] [blame] | 39 | public class BubbleTextView extends TextView { |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 40 | static final float SHADOW_LARGE_RADIUS = 4.0f; |
| 41 | static final float SHADOW_SMALL_RADIUS = 1.75f; |
| 42 | static final float SHADOW_Y_OFFSET = 2.0f; |
Winson Chung | c7aef8c | 2011-09-15 18:53:04 -0700 | [diff] [blame] | 43 | static final int SHADOW_LARGE_COLOUR = 0xDD000000; |
| 44 | static final int SHADOW_SMALL_COLOUR = 0xCC000000; |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 45 | static final float PADDING_H = 8.0f; |
| 46 | static final float PADDING_V = 3.0f; |
| 47 | |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 48 | private static final String TAG = "BubbleTextView"; |
| 49 | |
| 50 | private static final boolean DEBUG = false; |
| 51 | |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 52 | private int mPrevAlpha = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 53 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 54 | private HolographicOutlineHelper mOutlineHelper; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 55 | private final Canvas mTempCanvas = new Canvas(); |
| 56 | private final Rect mTempRect = new Rect(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 57 | private boolean mDidInvalidateForPressedState; |
| 58 | private Bitmap mPressedOrFocusedBackground; |
| 59 | private int mFocusedOutlineColor; |
| 60 | private int mFocusedGlowColor; |
| 61 | private int mPressedOutlineColor; |
| 62 | private int mPressedGlowColor; |
| 63 | |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 64 | private float mSlop; |
| 65 | |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 66 | private int mTextColor; |
| 67 | private boolean mShadowsEnabled = true; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 68 | private boolean mIsTextVisible; |
| 69 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 70 | private boolean mBackgroundSizeChanged; |
| 71 | private Drawable mBackground; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 72 | |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 73 | private boolean mStayPressed; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 74 | private CheckLongPressHelper mLongPressHelper; |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 75 | private int mInstallState; |
| 76 | |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 77 | private CharSequence mDefaultText = ""; |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 78 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 79 | public BubbleTextView(Context context) { |
| 80 | super(context); |
| 81 | init(); |
| 82 | } |
| 83 | |
| 84 | public BubbleTextView(Context context, AttributeSet attrs) { |
| 85 | super(context, attrs); |
| 86 | init(); |
| 87 | } |
| 88 | |
| 89 | public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { |
| 90 | super(context, attrs, defStyle); |
| 91 | init(); |
| 92 | } |
| 93 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 94 | public void onFinishInflate() { |
| 95 | super.onFinishInflate(); |
| 96 | |
| 97 | // Ensure we are using the right text size |
| 98 | LauncherAppState app = LauncherAppState.getInstance(); |
| 99 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 100 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx); |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 101 | setTextColor(getResources().getColor(R.color.workspace_icon_text_color)); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 102 | } |
| 103 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 104 | private void init() { |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 105 | mLongPressHelper = new CheckLongPressHelper(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 106 | mBackground = getBackground(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 107 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 108 | mOutlineHelper = HolographicOutlineHelper.obtain(getContext()); |
| 109 | |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 110 | final Resources res = getContext().getResources(); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 111 | mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor = |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 112 | res.getColor(R.color.outline_color); |
Michael Jurka | e7e3f6c | 2011-02-01 21:08:29 -0800 | [diff] [blame] | 113 | |
| 114 | setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 117 | public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) { |
| 118 | Bitmap b = info.getIcon(iconCache); |
Winson Chung | cdef044 | 2013-09-19 17:32:58 -0700 | [diff] [blame] | 119 | LauncherAppState app = LauncherAppState.getInstance(); |
| 120 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 121 | |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 122 | Drawable iconDrawable = Utilities.createIconDrawable(b); |
| 123 | setCompoundDrawables(null, iconDrawable, null, null); |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 124 | setCompoundDrawablePadding(grid.iconDrawablePaddingPx); |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 125 | setTag(info); |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 126 | if (info.isPromise()) { |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame^] | 127 | applyState(); |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 128 | } |
Michael Jurka | 67b2f6c | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 129 | } |
| 130 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 131 | @Override |
| 132 | protected boolean setFrame(int left, int top, int right, int bottom) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 133 | if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 134 | mBackgroundSizeChanged = true; |
| 135 | } |
| 136 | return super.setFrame(left, top, right, bottom); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | protected boolean verifyDrawable(Drawable who) { |
| 141 | return who == mBackground || super.verifyDrawable(who); |
| 142 | } |
| 143 | |
| 144 | @Override |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 145 | public void setTag(Object tag) { |
| 146 | if (tag != null) { |
| 147 | LauncherModel.checkItemInfo((ItemInfo) tag); |
| 148 | } |
| 149 | super.setTag(tag); |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame^] | 150 | if (tag instanceof ShortcutInfo) { |
| 151 | final ShortcutInfo info = (ShortcutInfo) tag; |
| 152 | mDefaultText = info.title; |
| 153 | setText(mDefaultText); |
| 154 | } |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 158 | protected void drawableStateChanged() { |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 159 | if (isPressed()) { |
| 160 | // In this case, we have already created the pressed outline on ACTION_DOWN, |
| 161 | // so we just need to do an invalidate to trigger draw |
| 162 | if (!mDidInvalidateForPressedState) { |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 163 | setCellLayoutPressedOrFocusedIcon(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 164 | } |
| 165 | } else { |
| 166 | // Otherwise, either clear the pressed/focused background, or create a background |
| 167 | // for the focused state |
| 168 | final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null; |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 169 | if (!mStayPressed) { |
| 170 | mPressedOrFocusedBackground = null; |
| 171 | } |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 172 | if (isFocused()) { |
Gilles Debunne | c786952 | 2011-11-28 18:03:56 -0800 | [diff] [blame] | 173 | if (getLayout() == null) { |
Patrick Dubroy | a017c03 | 2011-03-09 15:58:32 -0800 | [diff] [blame] | 174 | // In some cases, we get focus before we have been layed out. Set the |
| 175 | // background to null so that it will get created when the view is drawn. |
| 176 | mPressedOrFocusedBackground = null; |
| 177 | } else { |
| 178 | mPressedOrFocusedBackground = createGlowingOutline( |
| 179 | mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor); |
| 180 | } |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 181 | mStayPressed = false; |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 182 | setCellLayoutPressedOrFocusedIcon(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 183 | } |
| 184 | final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null; |
| 185 | if (!backgroundEmptyBefore && backgroundEmptyNow) { |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 186 | setCellLayoutPressedOrFocusedIcon(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 190 | Drawable d = mBackground; |
| 191 | if (d != null && d.isStateful()) { |
| 192 | d.setState(getDrawableState()); |
| 193 | } |
| 194 | super.drawableStateChanged(); |
| 195 | } |
| 196 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 197 | /** |
Patrick Dubroy | cd95371 | 2011-02-28 15:16:42 -0800 | [diff] [blame] | 198 | * Draw this BubbleTextView into the given Canvas. |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 199 | * |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 200 | * @param destCanvas the canvas to draw on |
| 201 | * @param padding the horizontal and vertical padding to use when drawing |
| 202 | */ |
| 203 | private void drawWithPadding(Canvas destCanvas, int padding) { |
| 204 | final Rect clipRect = mTempRect; |
| 205 | getDrawingRect(clipRect); |
| 206 | |
| 207 | // adjust the clip rect so that we don't include the text label |
| 208 | clipRect.bottom = |
| 209 | getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0); |
| 210 | |
| 211 | // Draw the View into the bitmap. |
| 212 | // The translate of scrollX and scrollY is necessary when drawing TextViews, because |
| 213 | // they set scrollX and scrollY to large values to achieve centered text |
| 214 | destCanvas.save(); |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 215 | destCanvas.scale(getScaleX(), getScaleY(), |
| 216 | (getWidth() + padding) / 2, (getHeight() + padding) / 2); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 217 | destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2); |
| 218 | destCanvas.clipRect(clipRect, Op.REPLACE); |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 219 | draw(destCanvas); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 220 | destCanvas.restore(); |
| 221 | } |
| 222 | |
Adam Cohen | 7397e62 | 2013-10-24 12:11:34 -0700 | [diff] [blame] | 223 | public void setGlowColor(int color) { |
| 224 | mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor = color; |
| 225 | } |
| 226 | |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 227 | /** |
| 228 | * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. |
| 229 | * Responsibility for the bitmap is transferred to the caller. |
| 230 | */ |
| 231 | private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 232 | final int padding = mOutlineHelper.mMaxOuterBlurRadius; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 233 | final Bitmap b = Bitmap.createBitmap( |
| 234 | getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888); |
| 235 | |
| 236 | canvas.setBitmap(b); |
| 237 | drawWithPadding(canvas, padding); |
| 238 | mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 239 | canvas.setBitmap(null); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 240 | |
| 241 | return b; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public boolean onTouchEvent(MotionEvent event) { |
| 246 | // Call the superclass onTouchEvent first, because sometimes it changes the state to |
| 247 | // isPressed() on an ACTION_UP |
| 248 | boolean result = super.onTouchEvent(event); |
| 249 | |
| 250 | switch (event.getAction()) { |
| 251 | case MotionEvent.ACTION_DOWN: |
| 252 | // So that the pressed outline is visible immediately when isPressed() is true, |
| 253 | // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time |
| 254 | // to create it) |
| 255 | if (mPressedOrFocusedBackground == null) { |
| 256 | mPressedOrFocusedBackground = createGlowingOutline( |
| 257 | mTempCanvas, mPressedGlowColor, mPressedOutlineColor); |
| 258 | } |
| 259 | // Invalidate so the pressed state is visible, or set a flag so we know that we |
| 260 | // have to call invalidate as soon as the state is "pressed" |
| 261 | if (isPressed()) { |
| 262 | mDidInvalidateForPressedState = true; |
Michael Jurka | e6235dd | 2011-10-04 15:02:05 -0700 | [diff] [blame] | 263 | setCellLayoutPressedOrFocusedIcon(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 264 | } else { |
| 265 | mDidInvalidateForPressedState = false; |
| 266 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 267 | |
| 268 | mLongPressHelper.postCheckForLongPress(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 269 | break; |
| 270 | case MotionEvent.ACTION_CANCEL: |
| 271 | case MotionEvent.ACTION_UP: |
| 272 | // If we've touched down and up on an item, and it's still not "pressed", then |
| 273 | // destroy the pressed outline |
| 274 | if (!isPressed()) { |
| 275 | mPressedOrFocusedBackground = null; |
| 276 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 277 | |
| 278 | mLongPressHelper.cancelLongPress(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 279 | break; |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 280 | case MotionEvent.ACTION_MOVE: |
| 281 | if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) { |
| 282 | mLongPressHelper.cancelLongPress(); |
| 283 | } |
| 284 | break; |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 285 | } |
| 286 | return result; |
| 287 | } |
| 288 | |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 289 | void setStayPressed(boolean stayPressed) { |
| 290 | mStayPressed = stayPressed; |
| 291 | if (!stayPressed) { |
| 292 | mPressedOrFocusedBackground = null; |
| 293 | } |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 294 | setCellLayoutPressedOrFocusedIcon(); |
| 295 | } |
| 296 | |
| 297 | void setCellLayoutPressedOrFocusedIcon() { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 298 | if (getParent() instanceof ShortcutAndWidgetContainer) { |
| 299 | ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent(); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 300 | if (parent != null) { |
| 301 | CellLayout layout = (CellLayout) parent.getParent(); |
| 302 | layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null); |
| 303 | } |
Patrick Dubroy | d69e113 | 2011-03-15 10:29:01 -0700 | [diff] [blame] | 304 | } |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Winson Chung | 1e9cbfe | 2011-09-30 16:52:26 -0700 | [diff] [blame] | 307 | void clearPressedOrFocusedBackground() { |
| 308 | mPressedOrFocusedBackground = null; |
| 309 | setCellLayoutPressedOrFocusedIcon(); |
| 310 | } |
| 311 | |
Patrick Dubroy | 3499d8c | 2011-03-10 17:17:23 -0800 | [diff] [blame] | 312 | Bitmap getPressedOrFocusedBackground() { |
| 313 | return mPressedOrFocusedBackground; |
| 314 | } |
| 315 | |
| 316 | int getPressedOrFocusedBackgroundPadding() { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 317 | return mOutlineHelper.mMaxOuterBlurRadius / 2; |
Michael Jurka | ddd62e9 | 2011-02-16 17:49:14 -0800 | [diff] [blame] | 318 | } |
Patrick Dubroy | a017c03 | 2011-03-09 15:58:32 -0800 | [diff] [blame] | 319 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 320 | @Override |
| 321 | public void draw(Canvas canvas) { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 322 | if (!mShadowsEnabled) { |
| 323 | super.draw(canvas); |
| 324 | return; |
| 325 | } |
| 326 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 327 | final Drawable background = mBackground; |
| 328 | if (background != null) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 329 | final int scrollX = getScrollX(); |
| 330 | final int scrollY = getScrollY(); |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 331 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 332 | if (mBackgroundSizeChanged) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 333 | background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop()); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 334 | mBackgroundSizeChanged = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 335 | } |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 336 | |
| 337 | if ((scrollX | scrollY) == 0) { |
| 338 | background.draw(canvas); |
| 339 | } else { |
| 340 | canvas.translate(scrollX, scrollY); |
| 341 | background.draw(canvas); |
| 342 | canvas.translate(-scrollX, -scrollY); |
| 343 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 344 | } |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 345 | |
| 346 | // If text is transparent, don't draw any shadow |
Andrew Flynn | bc239a1 | 2012-03-06 11:39:49 -0800 | [diff] [blame] | 347 | if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) { |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 348 | getPaint().clearShadowLayer(); |
| 349 | super.draw(canvas); |
| 350 | return; |
| 351 | } |
| 352 | |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 353 | // We enhance the shadow by drawing the shadow twice |
Michael Jurka | e7e3f6c | 2011-02-01 21:08:29 -0800 | [diff] [blame] | 354 | 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] | 355 | super.draw(canvas); |
| 356 | canvas.save(Canvas.CLIP_SAVE_FLAG); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 357 | canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(), |
| 358 | getScrollX() + getWidth(), |
| 359 | getScrollY() + getHeight(), Region.Op.INTERSECT); |
Michael Jurka | e7e3f6c | 2011-02-01 21:08:29 -0800 | [diff] [blame] | 360 | getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 361 | super.draw(canvas); |
| 362 | canvas.restore(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 363 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 364 | |
| 365 | @Override |
| 366 | protected void onAttachedToWindow() { |
| 367 | super.onAttachedToWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 368 | if (mBackground != null) mBackground.setCallback(this); |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 369 | mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | @Override |
| 373 | protected void onDetachedFromWindow() { |
| 374 | super.onDetachedFromWindow(); |
Winson Chung | 656d11c | 2010-11-29 17:15:47 -0800 | [diff] [blame] | 375 | if (mBackground != null) mBackground.setCallback(null); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 376 | } |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 377 | |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 378 | @Override |
| 379 | public void setTextColor(int color) { |
| 380 | mTextColor = color; |
| 381 | super.setTextColor(color); |
| 382 | } |
| 383 | |
| 384 | public void setShadowsEnabled(boolean enabled) { |
| 385 | mShadowsEnabled = enabled; |
| 386 | getPaint().clearShadowLayer(); |
| 387 | invalidate(); |
| 388 | } |
| 389 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 390 | public void setTextVisibility(boolean visible) { |
| 391 | Resources res = getResources(); |
| 392 | if (visible) { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 393 | super.setTextColor(mTextColor); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 394 | } else { |
Adam Cohen | 477828c | 2013-09-20 12:05:49 -0700 | [diff] [blame] | 395 | super.setTextColor(res.getColor(android.R.color.transparent)); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 396 | } |
| 397 | mIsTextVisible = visible; |
| 398 | } |
| 399 | |
| 400 | public boolean isTextVisible() { |
| 401 | return mIsTextVisible; |
| 402 | } |
| 403 | |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 404 | @Override |
| 405 | protected boolean onSetAlpha(int alpha) { |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 406 | if (mPrevAlpha != alpha) { |
| 407 | mPrevAlpha = alpha; |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 408 | super.onSetAlpha(alpha); |
| 409 | } |
| 410 | return true; |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 411 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 412 | |
| 413 | @Override |
| 414 | public void cancelLongPress() { |
| 415 | super.cancelLongPress(); |
| 416 | |
| 417 | mLongPressHelper.cancelLongPress(); |
| 418 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 419 | |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame^] | 420 | public void applyState() { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 421 | int alpha = getResources().getInteger(R.integer.promise_icon_alpha); |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame^] | 422 | final int state = getState(); |
| 423 | if (DEBUG) Log.d(TAG, "applying icon state: " + state); |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 424 | |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame^] | 425 | switch(state) { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 426 | case ShortcutInfo.PACKAGE_STATE_DEFAULT: |
| 427 | super.setText(mDefaultText); |
| 428 | alpha = 255; |
| 429 | break; |
| 430 | |
| 431 | case ShortcutInfo.PACKAGE_STATE_ENQUEUED: |
| 432 | setText(R.string.package_state_enqueued); |
| 433 | break; |
| 434 | |
| 435 | case ShortcutInfo.PACKAGE_STATE_DOWNLOADING: |
| 436 | setText(R.string.package_state_downloading); |
| 437 | break; |
| 438 | |
| 439 | case ShortcutInfo.PACKAGE_STATE_INSTALLING: |
| 440 | setText(R.string.package_state_installing); |
| 441 | break; |
| 442 | |
| 443 | case ShortcutInfo.PACKAGE_STATE_ERROR: |
| 444 | setText(R.string.package_state_error); |
| 445 | break; |
| 446 | |
| 447 | case ShortcutInfo.PACKAGE_STATE_UNKNOWN: |
| 448 | default: |
| 449 | setText(R.string.package_state_unknown); |
| 450 | break; |
| 451 | } |
| 452 | if (DEBUG) Log.d(TAG, "setting icon alpha to: " + alpha); |
| 453 | Drawable[] drawables = getCompoundDrawables(); |
| 454 | for (int i = 0; i < drawables.length; i++) { |
| 455 | if (drawables[i] != null) { |
| 456 | drawables[i].setAlpha(alpha); |
| 457 | } |
| 458 | } |
| 459 | } |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame^] | 460 | |
| 461 | private int getState() { |
| 462 | if (! (getTag() instanceof ShortcutInfo)) { |
| 463 | return ShortcutInfo.PACKAGE_STATE_DEFAULT; |
| 464 | } else { |
| 465 | ShortcutInfo info = (ShortcutInfo) getTag(); |
| 466 | return info.getState(); |
| 467 | } |
| 468 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 469 | } |