blob: be00aec34ea8d547c41316643b9d7f3dd45fb3f4 [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
Sunny Goyal70660032015-05-14 00:07:08 -070019import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080020import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070021import android.content.res.ColorStateList;
Winson Chung656d11c2010-11-29 17:15:47 -080022import android.content.res.Resources;
Sunny Goyal95abbb32014-08-04 10:53:22 -070023import android.content.res.Resources.Theme;
Adam Cohen96bb7982014-07-07 11:58:56 -070024import android.content.res.TypedArray;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080025import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.graphics.Canvas;
Michael Jurka137142e2011-01-05 20:57:04 -080027import android.graphics.Region;
Winson5fbe0742015-09-30 15:33:00 -070028import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.graphics.drawable.Drawable;
Sunny Goyal70660032015-05-14 00:07:08 -070030import android.os.Build;
Winson Chung656d11c2010-11-29 17:15:47 -080031import android.util.AttributeSet;
Sunny Goyal95abbb32014-08-04 10:53:22 -070032import android.util.SparseArray;
Winson Chung5f8afe62013-08-12 16:19:28 -070033import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070034import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080035import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070036import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040037import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080038import android.view.ViewDebug;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070039import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080040import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070041
Sunny Goyal34b65272015-03-11 16:56:52 -070042import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal26119432016-02-18 22:09:23 +000043import com.android.launcher3.folder.FolderIcon;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070044import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070045
Sunny Goyalc469aad2015-10-01 11:24:23 -070046import java.text.NumberFormat;
47
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048/**
49 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
50 * because we want to make the bubble taller than the text and TextView's clip is
51 * too aggressive.
52 */
Winson Chungb1777442015-06-16 13:35:04 -070053public class BubbleTextView extends TextView
54 implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070055
Sameer Padala8fd74832014-09-08 16:00:29 -070056 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070057
Sunny Goyal508da152014-08-14 10:53:27 -070058 private static final float SHADOW_LARGE_RADIUS = 4.0f;
59 private static final float SHADOW_SMALL_RADIUS = 1.75f;
60 private static final float SHADOW_Y_OFFSET = 2.0f;
61 private static final int SHADOW_LARGE_COLOUR = 0xDD000000;
62 private static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080063
Sunny Goyaldfaccf62015-05-11 16:30:44 -070064 private static final int DISPLAY_WORKSPACE = 0;
65 private static final int DISPLAY_ALL_APPS = 1;
66
Sunny Goyal53d7ee42015-05-22 12:25:45 -070067 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080068 private Drawable mIcon;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080069 private final Drawable mBackground;
70 private final CheckLongPressHelper mLongPressHelper;
71 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070072 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080073
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080074 private boolean mBackgroundSizeChanged;
75
Sunny Goyal508da152014-08-14 10:53:27 -070076 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080077
Jason Monk02dd7ae2014-04-15 15:23:31 -040078 private float mSlop;
79
Winson Chung93f98ea2015-03-10 16:28:47 -070080 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070081 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080082 private final boolean mLayoutHorizontal;
83 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080084 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080085 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070086
Sunny Goyal4ffec482016-02-09 11:28:52 -080087 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -080088 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -080089 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -070090 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -080091 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -070092 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050093
Sunny Goyal34b65272015-03-11 16:56:52 -070094 private IconLoadRequest mIconLoadRequest;
95
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070097 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098 }
99
100 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700101 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 }
103
104 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
105 super(context, attrs, defStyle);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700106 mLauncher = (Launcher) context;
107 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700108
Adam Cohen96bb7982014-07-07 11:58:56 -0700109 TypedArray a = context.obtainStyledAttributes(attrs,
110 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700111 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800112 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700113 mDeferShadowGenerationOnTouch =
114 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700115
116 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
117 int defaultIconSize = grid.iconSizePx;
118 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700119 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700120 } else if (display == DISPLAY_ALL_APPS) {
Sunny Goyal383e0a22015-12-10 12:28:21 -0800121 setTextSize(TypedValue.COMPLEX_UNIT_SP, grid.allAppsIconTextSizeSp);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700122 defaultIconSize = grid.allAppsIconSizePx;
123 }
124
125 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
126 defaultIconSize);
127
Adam Cohen96bb7982014-07-07 11:58:56 -0700128 a.recycle();
129
Sunny Goyal15872da2014-07-08 15:43:54 -0700130 if (mCustomShadowsEnabled) {
131 // Draw the background itself as the parent is drawn twice.
132 mBackground = getBackground();
133 setBackground(null);
134 } else {
135 mBackground = null;
136 }
Winson Chungb745afb2015-03-02 11:51:23 -0800137
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800138 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700139 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800140
141 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
142 if (mCustomShadowsEnabled) {
143 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
144 }
145
Sunny Goyalae502842016-06-17 08:43:56 -0700146 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 }
148
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700149 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
150 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700151 }
152
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700153 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700154 boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800155 Bitmap b = info.getIcon(iconCache);
156
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700157 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(b);
Kenny Guy44cba692016-01-21 19:50:02 +0000158 if (info.isDisabled()) {
Winsonc0880492015-08-21 11:16:27 -0700159 iconDrawable.setState(FastBitmapDrawable.State.DISABLED);
160 }
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700161 setIcon(iconDrawable, mIconSize);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100162 if (info.contentDescription != null) {
163 setContentDescription(info.contentDescription);
164 }
Sunny Goyal508da152014-08-14 10:53:27 -0700165 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800166 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700167
Sunny Goyal34942622014-08-29 17:20:55 -0700168 if (promiseStateChanged || info.isPromise()) {
169 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500170 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800171 }
172
Sunny Goyal508da152014-08-14 10:53:27 -0700173 public void applyFromApplicationInfo(AppInfo info) {
Kenny Guy44cba692016-01-21 19:50:02 +0000174 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(info.iconBitmap);
175 if (info.isDisabled()) {
176 iconDrawable.setState(FastBitmapDrawable.State.DISABLED);
177 }
178 setIcon(iconDrawable, mIconSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700179 setText(info.title);
180 if (info.contentDescription != null) {
181 setContentDescription(info.contentDescription);
182 }
Winson Chung888b3a12015-03-13 11:14:16 -0700183 // We don't need to check the info since it's not a ShortcutInfo
184 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700185
186 // Verify high res immediately
187 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700188 }
189
Sunny Goyal0e08f162015-05-12 11:32:39 -0700190 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700191 setIcon(mLauncher.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700192 setText(info.title);
193 if (info.contentDescription != null) {
194 setContentDescription(info.contentDescription);
195 }
196 // We don't need to check the info since it's not a ShortcutInfo
197 super.setTag(info);
198
199 // Verify high res immediately
200 verifyHighRes();
201 }
202
Winson Chung7501adf2015-06-02 11:24:28 -0700203 /**
Winson5fbe0742015-09-30 15:33:00 -0700204 * Used for measurement only, sets some dummy values on this view.
205 */
206 public void applyDummyInfo() {
207 ColorDrawable d = new ColorDrawable();
208 setIcon(mLauncher.resizeIconDrawable(d), mIconSize);
209 setText("");
210 }
211
212 /**
Winson Chung7501adf2015-06-02 11:24:28 -0700213 * Overrides the default long press timeout.
214 */
215 public void setLongPressTimeout(int longPressTimeout) {
216 mLongPressHelper.setLongPressTimeout(longPressTimeout);
217 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 @Override
220 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700221 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 mBackgroundSizeChanged = true;
223 }
224 return super.setFrame(left, top, right, bottom);
225 }
226
227 @Override
228 protected boolean verifyDrawable(Drawable who) {
229 return who == mBackground || super.verifyDrawable(who);
230 }
231
232 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700233 public void setTag(Object tag) {
234 if (tag != null) {
235 LauncherModel.checkItemInfo((ItemInfo) tag);
236 }
237 super.setTag(tag);
238 }
239
240 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700241 public void setPressed(boolean pressed) {
242 super.setPressed(pressed);
243
244 if (!mIgnorePressedStateChange) {
245 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800246 }
Sunny Goyal508da152014-08-14 10:53:27 -0700247 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800248
Winson Chungb745afb2015-03-02 11:51:23 -0800249 /** Returns the icon for this view. */
250 public Drawable getIcon() {
251 return mIcon;
252 }
253
254 /** Returns whether the layout is horizontal. */
255 public boolean isLayoutHorizontal() {
256 return mLayoutHorizontal;
257 }
258
Sunny Goyal508da152014-08-14 10:53:27 -0700259 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800260 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700261 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
Kenny Guy44cba692016-01-21 19:50:02 +0000262 if (getTag() instanceof ItemInfo
263 && ((ItemInfo) getTag()).isDisabled()) {
Sunny Goyalcd5b7f92015-10-20 15:17:00 -0700264 d.animateState(FastBitmapDrawable.State.DISABLED);
Kenny Guy44cba692016-01-21 19:50:02 +0000265 } else if (isPressed() || mStayPressed) {
266 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700267 } else {
268 d.animateState(FastBitmapDrawable.State.NORMAL);
269 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800271 }
272
273 @Override
274 public boolean onTouchEvent(MotionEvent event) {
275 // Call the superclass onTouchEvent first, because sometimes it changes the state to
276 // isPressed() on an ACTION_UP
277 boolean result = super.onTouchEvent(event);
278
Mady Melloref044dd2015-06-02 15:35:07 -0700279 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700280 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700281 mLongPressHelper.cancelLongPress();
282 result = true;
283 }
284
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800285 switch (event.getAction()) {
286 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700287 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800288 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
289 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700290 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700291 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800292 }
Winson Chung88f33452012-02-23 15:23:44 -0800293
Mady Melloref044dd2015-06-02 15:35:07 -0700294 // If we're in a stylus button press, don't check for long press.
295 if (!mStylusEventHelper.inStylusButtonPressed()) {
296 mLongPressHelper.postCheckForLongPress();
297 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800298 break;
299 case MotionEvent.ACTION_CANCEL:
300 case MotionEvent.ACTION_UP:
301 // If we've touched down and up on an item, and it's still not "pressed", then
302 // destroy the pressed outline
303 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700304 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800305 }
Winson Chung88f33452012-02-23 15:23:44 -0800306
307 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800308 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400309 case MotionEvent.ACTION_MOVE:
310 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
311 mLongPressHelper.cancelLongPress();
312 }
313 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800314 }
315 return result;
316 }
317
Michael Jurkaddd62e92011-02-16 17:49:14 -0800318 void setStayPressed(boolean stayPressed) {
319 mStayPressed = stayPressed;
320 if (!stayPressed) {
Sunny Goyal6c651132016-02-22 16:10:44 -0800321 HolographicOutlineHelper.obtain(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700322 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700323 } else {
324 if (mPressedBackground == null) {
325 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
326 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800327 }
Sunny Goyal508da152014-08-14 10:53:27 -0700328
329 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700330 ViewParent parent = getParent();
331 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
332 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
333 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700334 }
335
336 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800337 }
338
Sunny Goyal508da152014-08-14 10:53:27 -0700339 void clearPressedBackground() {
340 setPressed(false);
341 setStayPressed(false);
342 }
343
344 @Override
345 public boolean onKeyDown(int keyCode, KeyEvent event) {
346 if (super.onKeyDown(keyCode, event)) {
347 // Pre-create shadow so show immediately on click.
348 if (mPressedBackground == null) {
349 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700350 }
Sunny Goyal508da152014-08-14 10:53:27 -0700351 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700352 }
Sunny Goyal508da152014-08-14 10:53:27 -0700353 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800354 }
355
Sunny Goyal508da152014-08-14 10:53:27 -0700356 @Override
357 public boolean onKeyUp(int keyCode, KeyEvent event) {
358 // Unlike touch events, keypress event propagate pressed state change immediately,
359 // without waiting for onClickHandler to execute. Disable pressed state changes here
360 // to avoid flickering.
361 mIgnorePressedStateChange = true;
362 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700363
Sunny Goyal508da152014-08-14 10:53:27 -0700364 mPressedBackground = null;
365 mIgnorePressedStateChange = false;
366 updateIconState();
367 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800368 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800369
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 @Override
371 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700372 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700373 super.draw(canvas);
374 return;
375 }
376
Michael Jurkabdb5c532011-02-01 15:05:06 -0800377 final Drawable background = mBackground;
378 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700379 final int scrollX = getScrollX();
380 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800381
Michael Jurkabdb5c532011-02-01 15:05:06 -0800382 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700383 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800384 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800386
387 if ((scrollX | scrollY) == 0) {
388 background.draw(canvas);
389 } else {
390 canvas.translate(scrollX, scrollY);
391 background.draw(canvas);
392 canvas.translate(-scrollX, -scrollY);
393 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800395
396 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800397 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800398 getPaint().clearShadowLayer();
399 super.draw(canvas);
400 return;
401 }
402
Michael Jurkabdb5c532011-02-01 15:05:06 -0800403 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800404 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800405 super.draw(canvas);
406 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700407 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
408 getScrollX() + getWidth(),
409 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800410 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800411 super.draw(canvas);
412 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400414
415 @Override
416 protected void onAttachedToWindow() {
417 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700418
Winson Chung656d11c2010-11-29 17:15:47 -0800419 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700420
Winson Chungb745afb2015-03-02 11:51:23 -0800421 if (mIcon instanceof PreloadIconDrawable) {
422 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700423 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400424 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400425 }
426
427 @Override
428 protected void onDetachedFromWindow() {
429 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800430 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400431 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700432
Adam Cohen477828c2013-09-20 12:05:49 -0700433 @Override
434 public void setTextColor(int color) {
435 mTextColor = color;
436 super.setTextColor(color);
437 }
438
Adam Cohen96bb7982014-07-07 11:58:56 -0700439 @Override
440 public void setTextColor(ColorStateList colors) {
441 mTextColor = colors.getDefaultColor();
442 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700443 }
444
Winson Chung5f8afe62013-08-12 16:19:28 -0700445 public void setTextVisibility(boolean visible) {
446 Resources res = getResources();
447 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700448 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700449 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700450 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700451 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700452 }
453
Winson Chungaffd7b42010-08-20 15:11:56 -0700454 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800455 public void cancelLongPress() {
456 super.cancelLongPress();
457
458 mLongPressHelper.cancelLongPress();
459 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500460
Sunny Goyal34942622014-08-29 17:20:55 -0700461 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700462 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400463 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700464 final boolean isPromise = info.isPromise();
465 final int progressLevel = isPromise ?
466 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
467 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700468
Sunny Goyalc469aad2015-10-01 11:24:23 -0700469 setContentDescription(progressLevel > 0 ?
470 getContext().getString(R.string.app_downloading_title, info.title,
471 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
472 getContext().getString(R.string.app_waiting_download_title, info.title));
473
Winson Chungb745afb2015-03-02 11:51:23 -0800474 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700475 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800476 if (mIcon instanceof PreloadIconDrawable) {
477 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700478 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800479 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700480 setIcon(preloadDrawable, mIconSize);
Sunny Goyale755d462014-07-22 13:48:29 -0700481 }
482
483 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700484 if (promiseStateChanged) {
485 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700486 }
Sunny Goyale755d462014-07-22 13:48:29 -0700487 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400488 }
489 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700490
491 private Theme getPreloaderTheme() {
492 Object tag = getTag();
493 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
494 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
495 : R.style.PreloadIcon;
496 Theme theme = sPreloaderThemes.get(style);
497 if (theme == null) {
498 theme = getResources().newTheme();
499 theme.applyStyle(style, true);
500 sPreloaderThemes.put(style, theme);
501 }
502 return theme;
503 }
Winson Chungb745afb2015-03-02 11:51:23 -0800504
505 /**
506 * Sets the icon for this view based on the layout direction.
507 */
Sunny Goyal70660032015-05-14 00:07:08 -0700508 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700509 private Drawable setIcon(Drawable icon, int iconSize) {
Winson Chungb745afb2015-03-02 11:51:23 -0800510 mIcon = icon;
511 if (iconSize != -1) {
512 mIcon.setBounds(0, 0, iconSize, iconSize);
513 }
514 if (mLayoutHorizontal) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700515 if (Utilities.ATLEAST_JB_MR1) {
Sunny Goyal70660032015-05-14 00:07:08 -0700516 setCompoundDrawablesRelative(mIcon, null, null, null);
517 } else {
518 setCompoundDrawables(mIcon, null, null, null);
519 }
Winson Chungb745afb2015-03-02 11:51:23 -0800520 } else {
Sunny Goyal70660032015-05-14 00:07:08 -0700521 setCompoundDrawables(null, mIcon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800522 }
Winson Chungb745afb2015-03-02 11:51:23 -0800523 return icon;
524 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700525
Sunny Goyal69b75642015-05-15 17:00:24 -0700526 @Override
527 public void requestLayout() {
528 if (!mDisableRelayout) {
529 super.requestLayout();
530 }
531 }
532
Sunny Goyal34b65272015-03-11 16:56:52 -0700533 /**
534 * Applies the item info if it is same as what the view is pointing to currently.
535 */
536 public void reapplyItemInfo(final ItemInfo info) {
537 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700538 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
539 if (mIcon instanceof FastBitmapDrawable) {
540 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
541 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700542 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700543 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700544
Sunny Goyal34b65272015-03-11 16:56:52 -0700545 if (info instanceof AppInfo) {
546 applyFromApplicationInfo((AppInfo) info);
547 } else if (info instanceof ShortcutInfo) {
548 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700549 LauncherAppState.getInstance().getIconCache());
Sunny Goyal317698b2015-07-29 11:45:41 -0700550 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
551 View folderIcon =
552 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
553 if (folderIcon != null) {
554 folderIcon.invalidate();
555 }
556 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700557 } else if (info instanceof PackageItemInfo) {
558 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700559 }
Winsonc0880492015-08-21 11:16:27 -0700560
561 // If we are reapplying over an old icon, then we should update the new icon to the same
562 // state as the old icon
563 if (mIcon instanceof FastBitmapDrawable) {
564 ((FastBitmapDrawable) mIcon).setState(prevState);
565 }
566
Sunny Goyal69b75642015-05-15 17:00:24 -0700567 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700568 }
569 }
570
571 /**
572 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
573 */
574 public void verifyHighRes() {
575 if (mIconLoadRequest != null) {
576 mIconLoadRequest.cancel();
577 mIconLoadRequest = null;
578 }
579 if (getTag() instanceof AppInfo) {
580 AppInfo info = (AppInfo) getTag();
581 if (info.usingLowResIcon) {
582 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
583 .updateIconInBackground(BubbleTextView.this, info);
584 }
585 } else if (getTag() instanceof ShortcutInfo) {
586 ShortcutInfo info = (ShortcutInfo) getTag();
587 if (info.usingLowResIcon) {
588 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
589 .updateIconInBackground(BubbleTextView.this, info);
590 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700591 } else if (getTag() instanceof PackageItemInfo) {
592 PackageItemInfo info = (PackageItemInfo) getTag();
593 if (info.usingLowResIcon) {
594 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
595 .updateIconInBackground(BubbleTextView.this, info);
596 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700597 }
598 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700599
Winson Chungb1777442015-06-16 13:35:04 -0700600 @Override
Winsonc0880492015-08-21 11:16:27 -0700601 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
602 // We can only set the fast scroll focus state on a FastBitmapDrawable
603 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700604 return;
605 }
606
Winsonc0880492015-08-21 11:16:27 -0700607 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
608 if (animated) {
609 FastBitmapDrawable.State prevState = d.getCurrentState();
610 if (d.animateState(focusState)) {
611 // If the state was updated, then update the view accordingly
612 animate().scaleX(focusState.viewScale)
613 .scaleY(focusState.viewScale)
614 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
615 .setDuration(d.getDurationForStateChange(prevState, focusState))
616 .start();
617 }
618 } else {
619 if (d.setState(focusState)) {
620 // If the state was updated, then update the view accordingly
621 animate().cancel();
622 setScaleX(focusState.viewScale);
623 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700624 }
625 }
626 }
627
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700628 /**
Winsonc0880492015-08-21 11:16:27 -0700629 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
630 */
631 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
632 final FastBitmapDrawable.State toState) {
633 switch (toState) {
634 case NORMAL:
635 switch (fromState) {
636 case FAST_SCROLL_HIGHLIGHTED:
637 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
638 }
639 }
640 return 0;
641 }
642
643 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700644 * Interface to be implemented by the grand parent to allow click shadow effect.
645 */
Winsonc0880492015-08-21 11:16:27 -0700646 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700647 void setPressedIcon(BubbleTextView icon, Bitmap background);
648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649}