blob: a0be8ea2b299e303b338da6e0d566149b91469c1 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Winson Chungb1777442015-06-16 13:35:04 -070019import android.animation.ObjectAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070020import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070022import android.content.res.ColorStateList;
Winson Chung656d11c2010-11-29 17:15:47 -080023import android.content.res.Resources;
Sunny Goyal95abbb32014-08-04 10:53:22 -070024import android.content.res.Resources.Theme;
Adam Cohen96bb7982014-07-07 11:58:56 -070025import android.content.res.TypedArray;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080026import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.graphics.Canvas;
Winson Chungb1777442015-06-16 13:35:04 -070028import android.graphics.Paint;
Michael Jurka137142e2011-01-05 20:57:04 -080029import android.graphics.Region;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.drawable.Drawable;
Sunny Goyal70660032015-05-14 00:07:08 -070031import android.os.Build;
Winson Chung656d11c2010-11-29 17:15:47 -080032import android.util.AttributeSet;
Sunny Goyal95abbb32014-08-04 10:53:22 -070033import android.util.SparseArray;
Winson Chung5f8afe62013-08-12 16:19:28 -070034import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070035import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080036import android.view.MotionEvent;
Jason Monk02dd7ae2014-04-15 15:23:31 -040037import android.view.ViewConfiguration;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070038import android.view.ViewParent;
Winson Chungb1777442015-06-16 13:35:04 -070039import android.view.animation.AccelerateInterpolator;
40import android.view.animation.DecelerateInterpolator;
Michael Jurkabdb5c532011-02-01 15:05:06 -080041import android.widget.TextView;
Sunny Goyal34b65272015-03-11 16:56:52 -070042import com.android.launcher3.IconCache.IconLoadRequest;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070043import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070044
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045/**
46 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
47 * because we want to make the bubble taller than the text and TextView's clip is
48 * too aggressive.
49 */
Winson Chungb1777442015-06-16 13:35:04 -070050public class BubbleTextView extends TextView
51 implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070052
Sameer Padala8fd74832014-09-08 16:00:29 -070053 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070054
Sunny Goyal508da152014-08-14 10:53:27 -070055 private static final float SHADOW_LARGE_RADIUS = 4.0f;
56 private static final float SHADOW_SMALL_RADIUS = 1.75f;
57 private static final float SHADOW_Y_OFFSET = 2.0f;
58 private static final int SHADOW_LARGE_COLOUR = 0xDD000000;
59 private static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080060
Sunny Goyaldfaccf62015-05-11 16:30:44 -070061 private static final int DISPLAY_WORKSPACE = 0;
62 private static final int DISPLAY_ALL_APPS = 1;
63
Winson Chungb1777442015-06-16 13:35:04 -070064 private static final float FAST_SCROLL_FOCUS_MAX_SCALE = 1.15f;
65 private static final int FAST_SCROLL_FOCUS_MODE_NONE = 0;
66 private static final int FAST_SCROLL_FOCUS_MODE_SCALE_ICON = 1;
67 private static final int FAST_SCROLL_FOCUS_MODE_DRAW_CIRCLE_BG = 2;
68 private static final int FAST_SCROLL_FOCUS_FADE_IN_DURATION = 175;
69 private static final int FAST_SCROLL_FOCUS_FADE_OUT_DURATION = 125;
70
Sunny Goyal53d7ee42015-05-22 12:25:45 -070071 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080072 private Drawable mIcon;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080073 private final Drawable mBackground;
74 private final CheckLongPressHelper mLongPressHelper;
75 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070076 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080077
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080078 private boolean mBackgroundSizeChanged;
79
Sunny Goyal508da152014-08-14 10:53:27 -070080 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080081
Jason Monk02dd7ae2014-04-15 15:23:31 -040082 private float mSlop;
83
Winson Chung93f98ea2015-03-10 16:28:47 -070084 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070085 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080086 private final boolean mLayoutHorizontal;
87 private final int mIconSize;
Winson Chungb745afb2015-03-02 11:51:23 -080088 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070089
Michael Jurkaddd62e92011-02-16 17:49:14 -080090 private boolean mStayPressed;
Sunny Goyal508da152014-08-14 10:53:27 -070091 private boolean mIgnorePressedStateChange;
Sunny Goyal69b75642015-05-15 17:00:24 -070092 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050093
Winson Chungb1777442015-06-16 13:35:04 -070094 private ObjectAnimator mFastScrollFocusAnimator;
95 private Paint mFastScrollFocusBgPaint;
96 private float mFastScrollFocusFraction;
97 private boolean mFastScrollFocused;
98 private final int mFastScrollMode = FAST_SCROLL_FOCUS_MODE_SCALE_ICON;
99
Sunny Goyal34b65272015-03-11 16:56:52 -0700100 private IconLoadRequest mIconLoadRequest;
101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700103 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 }
105
106 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700107 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 }
109
110 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
111 super(context, attrs, defStyle);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700112 mLauncher = (Launcher) context;
113 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700114
Adam Cohen96bb7982014-07-07 11:58:56 -0700115 TypedArray a = context.obtainStyledAttributes(attrs,
116 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700117 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800118 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700119 mDeferShadowGenerationOnTouch =
120 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700121
122 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
123 int defaultIconSize = grid.iconSizePx;
124 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700125 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700126 } else if (display == DISPLAY_ALL_APPS) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700127 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700128 defaultIconSize = grid.allAppsIconSizePx;
129 }
130
131 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
132 defaultIconSize);
133
Adam Cohen96bb7982014-07-07 11:58:56 -0700134 a.recycle();
135
Sunny Goyal15872da2014-07-08 15:43:54 -0700136 if (mCustomShadowsEnabled) {
137 // Draw the background itself as the parent is drawn twice.
138 mBackground = getBackground();
139 setBackground(null);
140 } else {
141 mBackground = null;
142 }
Winson Chungb745afb2015-03-02 11:51:23 -0800143
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800144 mLongPressHelper = new CheckLongPressHelper(this);
Mady Melloref044dd2015-06-02 15:35:07 -0700145 mStylusEventHelper = new StylusEventHelper(this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800146
147 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
148 if (mCustomShadowsEnabled) {
149 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
150 }
151
Winson Chungb1777442015-06-16 13:35:04 -0700152 if (mFastScrollMode == FAST_SCROLL_FOCUS_MODE_DRAW_CIRCLE_BG) {
153 mFastScrollFocusBgPaint = new Paint();
154 mFastScrollFocusBgPaint.setAntiAlias(true);
155 mFastScrollFocusBgPaint.setColor(
156 getResources().getColor(R.color.container_fastscroll_thumb_active_color));
157 }
158
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800159 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 }
161
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700162 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
163 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700164 }
165
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700166 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700167 boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800168 Bitmap b = info.getIcon(iconCache);
169
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700170 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(b);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700171 iconDrawable.setGhostModeEnabled(info.isDisabled != 0);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700172
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700173 setIcon(iconDrawable, mIconSize);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100174 if (info.contentDescription != null) {
175 setContentDescription(info.contentDescription);
176 }
Sunny Goyal508da152014-08-14 10:53:27 -0700177 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800178 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700179
Sunny Goyal34942622014-08-29 17:20:55 -0700180 if (promiseStateChanged || info.isPromise()) {
181 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500182 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800183 }
184
Sunny Goyal508da152014-08-14 10:53:27 -0700185 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700186 setIcon(mLauncher.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700187 setText(info.title);
188 if (info.contentDescription != null) {
189 setContentDescription(info.contentDescription);
190 }
Winson Chung888b3a12015-03-13 11:14:16 -0700191 // We don't need to check the info since it's not a ShortcutInfo
192 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700193
194 // Verify high res immediately
195 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700196 }
197
Sunny Goyal0e08f162015-05-12 11:32:39 -0700198 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700199 setIcon(mLauncher.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700200 setText(info.title);
201 if (info.contentDescription != null) {
202 setContentDescription(info.contentDescription);
203 }
204 // We don't need to check the info since it's not a ShortcutInfo
205 super.setTag(info);
206
207 // Verify high res immediately
208 verifyHighRes();
209 }
210
Winson Chung7501adf2015-06-02 11:24:28 -0700211 /**
212 * Overrides the default long press timeout.
213 */
214 public void setLongPressTimeout(int longPressTimeout) {
215 mLongPressHelper.setLongPressTimeout(longPressTimeout);
216 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 @Override
219 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700220 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 mBackgroundSizeChanged = true;
222 }
223 return super.setFrame(left, top, right, bottom);
224 }
225
226 @Override
227 protected boolean verifyDrawable(Drawable who) {
228 return who == mBackground || super.verifyDrawable(who);
229 }
230
231 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700232 public void setTag(Object tag) {
233 if (tag != null) {
234 LauncherModel.checkItemInfo((ItemInfo) tag);
235 }
236 super.setTag(tag);
237 }
238
239 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700240 public void setPressed(boolean pressed) {
241 super.setPressed(pressed);
242
243 if (!mIgnorePressedStateChange) {
244 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800245 }
Sunny Goyal508da152014-08-14 10:53:27 -0700246 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800247
Winson Chungb745afb2015-03-02 11:51:23 -0800248 /** Returns the icon for this view. */
249 public Drawable getIcon() {
250 return mIcon;
251 }
252
253 /** Returns whether the layout is horizontal. */
254 public boolean isLayoutHorizontal() {
255 return mLayoutHorizontal;
256 }
257
Sunny Goyal508da152014-08-14 10:53:27 -0700258 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800259 if (mIcon instanceof FastBitmapDrawable) {
260 ((FastBitmapDrawable) mIcon).setPressed(isPressed() || mStayPressed);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800262 }
263
264 @Override
265 public boolean onTouchEvent(MotionEvent event) {
266 // Call the superclass onTouchEvent first, because sometimes it changes the state to
267 // isPressed() on an ACTION_UP
268 boolean result = super.onTouchEvent(event);
269
Mady Melloref044dd2015-06-02 15:35:07 -0700270 // Check for a stylus button press, if it occurs cancel any long press checks.
271 if (mStylusEventHelper.checkAndPerformStylusEvent(event)) {
272 mLongPressHelper.cancelLongPress();
273 result = true;
274 }
275
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800276 switch (event.getAction()) {
277 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700278 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800279 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
280 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700281 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700282 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800283 }
Winson Chung88f33452012-02-23 15:23:44 -0800284
Mady Melloref044dd2015-06-02 15:35:07 -0700285 // If we're in a stylus button press, don't check for long press.
286 if (!mStylusEventHelper.inStylusButtonPressed()) {
287 mLongPressHelper.postCheckForLongPress();
288 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800289 break;
290 case MotionEvent.ACTION_CANCEL:
291 case MotionEvent.ACTION_UP:
292 // If we've touched down and up on an item, and it's still not "pressed", then
293 // destroy the pressed outline
294 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700295 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800296 }
Winson Chung88f33452012-02-23 15:23:44 -0800297
298 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800299 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400300 case MotionEvent.ACTION_MOVE:
301 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
302 mLongPressHelper.cancelLongPress();
303 }
304 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800305 }
306 return result;
307 }
308
Michael Jurkaddd62e92011-02-16 17:49:14 -0800309 void setStayPressed(boolean stayPressed) {
310 mStayPressed = stayPressed;
311 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700312 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700313 } else {
314 if (mPressedBackground == null) {
315 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
316 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800317 }
Sunny Goyal508da152014-08-14 10:53:27 -0700318
319 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700320 ViewParent parent = getParent();
321 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
322 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
323 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700324 }
325
326 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800327 }
328
Sunny Goyal508da152014-08-14 10:53:27 -0700329 void clearPressedBackground() {
330 setPressed(false);
331 setStayPressed(false);
332 }
333
334 @Override
335 public boolean onKeyDown(int keyCode, KeyEvent event) {
336 if (super.onKeyDown(keyCode, event)) {
337 // Pre-create shadow so show immediately on click.
338 if (mPressedBackground == null) {
339 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700340 }
Sunny Goyal508da152014-08-14 10:53:27 -0700341 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700342 }
Sunny Goyal508da152014-08-14 10:53:27 -0700343 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800344 }
345
Sunny Goyal508da152014-08-14 10:53:27 -0700346 @Override
347 public boolean onKeyUp(int keyCode, KeyEvent event) {
348 // Unlike touch events, keypress event propagate pressed state change immediately,
349 // without waiting for onClickHandler to execute. Disable pressed state changes here
350 // to avoid flickering.
351 mIgnorePressedStateChange = true;
352 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700353
Sunny Goyal508da152014-08-14 10:53:27 -0700354 mPressedBackground = null;
355 mIgnorePressedStateChange = false;
356 updateIconState();
357 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800358 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 @Override
361 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700362 if (!mCustomShadowsEnabled) {
Winson Chungb1777442015-06-16 13:35:04 -0700363 // Draw the fast scroll focus bg if we have one
364 if (mFastScrollMode == FAST_SCROLL_FOCUS_MODE_DRAW_CIRCLE_BG &&
365 mFastScrollFocusFraction > 0f) {
366 DeviceProfile grid = mLauncher.getDeviceProfile();
367 int iconCenterX = getScrollX() + (getWidth() / 2);
368 int iconCenterY = getScrollY() + getPaddingTop() + (grid.iconSizePx / 2);
369 canvas.drawCircle(iconCenterX, iconCenterY,
370 mFastScrollFocusFraction * (getWidth() / 2), mFastScrollFocusBgPaint);
371 }
372
Adam Cohen477828c2013-09-20 12:05:49 -0700373 super.draw(canvas);
Winson Chungb1777442015-06-16 13:35:04 -0700374
Adam Cohen477828c2013-09-20 12:05:49 -0700375 return;
376 }
377
Michael Jurkabdb5c532011-02-01 15:05:06 -0800378 final Drawable background = mBackground;
379 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700380 final int scrollX = getScrollX();
381 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800382
Michael Jurkabdb5c532011-02-01 15:05:06 -0800383 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700384 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800385 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800387
388 if ((scrollX | scrollY) == 0) {
389 background.draw(canvas);
390 } else {
391 canvas.translate(scrollX, scrollY);
392 background.draw(canvas);
393 canvas.translate(-scrollX, -scrollY);
394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800396
397 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800398 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800399 getPaint().clearShadowLayer();
400 super.draw(canvas);
401 return;
402 }
403
Michael Jurkabdb5c532011-02-01 15:05:06 -0800404 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800405 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800406 super.draw(canvas);
407 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700408 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
409 getScrollX() + getWidth(),
410 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800411 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800412 super.draw(canvas);
413 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400415
416 @Override
417 protected void onAttachedToWindow() {
418 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700419
Winson Chung656d11c2010-11-29 17:15:47 -0800420 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700421
Winson Chungb745afb2015-03-02 11:51:23 -0800422 if (mIcon instanceof PreloadIconDrawable) {
423 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700424 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400425 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400426 }
427
428 @Override
429 protected void onDetachedFromWindow() {
430 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800431 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400432 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700433
Adam Cohen477828c2013-09-20 12:05:49 -0700434 @Override
435 public void setTextColor(int color) {
436 mTextColor = color;
437 super.setTextColor(color);
438 }
439
Adam Cohen96bb7982014-07-07 11:58:56 -0700440 @Override
441 public void setTextColor(ColorStateList colors) {
442 mTextColor = colors.getDefaultColor();
443 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700444 }
445
Winson Chung5f8afe62013-08-12 16:19:28 -0700446 public void setTextVisibility(boolean visible) {
447 Resources res = getResources();
448 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700449 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700450 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700451 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700452 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700453 }
454
Winson Chungaffd7b42010-08-20 15:11:56 -0700455 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800456 public void cancelLongPress() {
457 super.cancelLongPress();
458
459 mLongPressHelper.cancelLongPress();
460 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500461
Sunny Goyal34942622014-08-29 17:20:55 -0700462 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700463 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400464 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700465 final boolean isPromise = info.isPromise();
466 final int progressLevel = isPromise ?
467 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
468 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700469
Winson Chungb745afb2015-03-02 11:51:23 -0800470 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700471 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800472 if (mIcon instanceof PreloadIconDrawable) {
473 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700474 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800475 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700476 setIcon(preloadDrawable, mIconSize);
Sunny Goyale755d462014-07-22 13:48:29 -0700477 }
478
479 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700480 if (promiseStateChanged) {
481 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700482 }
Sunny Goyale755d462014-07-22 13:48:29 -0700483 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400484 }
485 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700486
487 private Theme getPreloaderTheme() {
488 Object tag = getTag();
489 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
490 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
491 : R.style.PreloadIcon;
492 Theme theme = sPreloaderThemes.get(style);
493 if (theme == null) {
494 theme = getResources().newTheme();
495 theme.applyStyle(style, true);
496 sPreloaderThemes.put(style, theme);
497 }
498 return theme;
499 }
Winson Chungb745afb2015-03-02 11:51:23 -0800500
501 /**
502 * Sets the icon for this view based on the layout direction.
503 */
Sunny Goyal70660032015-05-14 00:07:08 -0700504 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700505 private Drawable setIcon(Drawable icon, int iconSize) {
Winson Chungb745afb2015-03-02 11:51:23 -0800506 mIcon = icon;
507 if (iconSize != -1) {
508 mIcon.setBounds(0, 0, iconSize, iconSize);
509 }
510 if (mLayoutHorizontal) {
Sunny Goyal70660032015-05-14 00:07:08 -0700511 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
512 setCompoundDrawablesRelative(mIcon, null, null, null);
513 } else {
514 setCompoundDrawables(mIcon, null, null, null);
515 }
Winson Chungb745afb2015-03-02 11:51:23 -0800516 } else {
Sunny Goyal70660032015-05-14 00:07:08 -0700517 setCompoundDrawables(null, mIcon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800518 }
Winson Chungb745afb2015-03-02 11:51:23 -0800519 return icon;
520 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700521
Sunny Goyal69b75642015-05-15 17:00:24 -0700522 @Override
523 public void requestLayout() {
524 if (!mDisableRelayout) {
525 super.requestLayout();
526 }
527 }
528
Sunny Goyal34b65272015-03-11 16:56:52 -0700529 /**
530 * Applies the item info if it is same as what the view is pointing to currently.
531 */
532 public void reapplyItemInfo(final ItemInfo info) {
533 if (getTag() == info) {
534 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700535 mDisableRelayout = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700536 if (info instanceof AppInfo) {
537 applyFromApplicationInfo((AppInfo) info);
538 } else if (info instanceof ShortcutInfo) {
539 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700540 LauncherAppState.getInstance().getIconCache());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700541 } else if (info instanceof PackageItemInfo) {
542 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700543 }
Sunny Goyal69b75642015-05-15 17:00:24 -0700544 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700545 }
546 }
547
548 /**
549 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
550 */
551 public void verifyHighRes() {
552 if (mIconLoadRequest != null) {
553 mIconLoadRequest.cancel();
554 mIconLoadRequest = null;
555 }
556 if (getTag() instanceof AppInfo) {
557 AppInfo info = (AppInfo) getTag();
558 if (info.usingLowResIcon) {
559 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
560 .updateIconInBackground(BubbleTextView.this, info);
561 }
562 } else if (getTag() instanceof ShortcutInfo) {
563 ShortcutInfo info = (ShortcutInfo) getTag();
564 if (info.usingLowResIcon) {
565 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
566 .updateIconInBackground(BubbleTextView.this, info);
567 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700568 } else if (getTag() instanceof PackageItemInfo) {
569 PackageItemInfo info = (PackageItemInfo) getTag();
570 if (info.usingLowResIcon) {
571 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
572 .updateIconInBackground(BubbleTextView.this, info);
573 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700574 }
575 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700576
Winson Chungb1777442015-06-16 13:35:04 -0700577 // Setters & getters for the animation
578 public void setFastScrollFocus(float fraction) {
579 mFastScrollFocusFraction = fraction;
580 if (mFastScrollMode == FAST_SCROLL_FOCUS_MODE_SCALE_ICON) {
581 setScaleX(1f + fraction * (FAST_SCROLL_FOCUS_MAX_SCALE - 1f));
582 setScaleY(1f + fraction * (FAST_SCROLL_FOCUS_MAX_SCALE - 1f));
583 } else {
584 invalidate();
585 }
586 }
587
588 public float getFastScrollFocus() {
589 return mFastScrollFocusFraction;
590 }
591
592 @Override
593 public void setFastScrollFocused(final boolean focused, boolean animated) {
594 if (mFastScrollMode == FAST_SCROLL_FOCUS_MODE_NONE) {
595 return;
596 }
597
598 if (mFastScrollFocused != focused) {
599 mFastScrollFocused = focused;
600
601 if (animated) {
602 // Clean up the previous focus animator
603 if (mFastScrollFocusAnimator != null) {
604 mFastScrollFocusAnimator.cancel();
605 }
606 mFastScrollFocusAnimator = ObjectAnimator.ofFloat(this, "fastScrollFocus",
607 focused ? 1f : 0f);
608 if (focused) {
609 mFastScrollFocusAnimator.setInterpolator(new DecelerateInterpolator());
610 } else {
611 mFastScrollFocusAnimator.setInterpolator(new AccelerateInterpolator());
612 }
613 mFastScrollFocusAnimator.setDuration(focused ?
614 FAST_SCROLL_FOCUS_FADE_IN_DURATION : FAST_SCROLL_FOCUS_FADE_OUT_DURATION);
615 mFastScrollFocusAnimator.start();
616 } else {
617 mFastScrollFocusFraction = focused ? 1f : 0f;
618 }
619 }
620 }
621
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700622 /**
623 * Interface to be implemented by the grand parent to allow click shadow effect.
624 */
625 public static interface BubbleTextShadowHandler {
626 void setPressedIcon(BubbleTextView icon, Bitmap background);
627 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628}