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