blob: 7d693ec2bdb52048b18e4b547c426b8344ad2f3b [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;
Winson1f064272016-07-18 17:18:02 -070027import android.graphics.Paint;
Michael Jurka137142e2011-01-05 20:57:04 -080028import android.graphics.Region;
Winson5fbe0742015-09-30 15:33:00 -070029import android.graphics.drawable.ColorDrawable;
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;
Sunny Goyal317698b2015-07-29 11:45:41 -070037import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040038import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080039import android.view.ViewDebug;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070040import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080041import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070042
Sunny Goyal34b65272015-03-11 16:56:52 -070043import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal26119432016-02-18 22:09:23 +000044import com.android.launcher3.folder.FolderIcon;
Sunny Goyal10629b02016-09-01 12:50:11 -070045import com.android.launcher3.graphics.HolographicOutlineHelper;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070046import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070047
Sunny Goyalc469aad2015-10-01 11:24:23 -070048import java.text.NumberFormat;
49
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050/**
51 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
52 * because we want to make the bubble taller than the text and TextView's clip is
53 * too aggressive.
54 */
Winson Chungb1777442015-06-16 13:35:04 -070055public class BubbleTextView extends TextView
56 implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070057
Sameer Padala8fd74832014-09-08 16:00:29 -070058 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070059
Sunny Goyal8f8f3982016-07-25 17:08:38 -070060 // Dimensions in DP
61 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
62 private static final float KEY_SHADOW_RADIUS = 1f;
63 private static final float KEY_SHADOW_OFFSET = 0.5f;
64 private static final int AMBIENT_SHADOW_COLOR = 0x33000000;
65 private static final int KEY_SHADOW_COLOR = 0x66000000;
Winson Chung656d11c2010-11-29 17:15:47 -080066
Sunny Goyaldfaccf62015-05-11 16:30:44 -070067 private static final int DISPLAY_WORKSPACE = 0;
68 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070069 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070070
Sunny Goyal53d7ee42015-05-22 12:25:45 -070071 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080072 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070073 private final boolean mCenterVertically;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080074 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070075 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080076 private final CheckLongPressHelper mLongPressHelper;
77 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070078 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080079
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080080 private boolean mBackgroundSizeChanged;
81
Sunny Goyal508da152014-08-14 10:53:27 -070082 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080083
Jason Monk02dd7ae2014-04-15 15:23:31 -040084 private float mSlop;
85
Winson Chung93f98ea2015-03-10 16:28:47 -070086 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070087 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080088 private final boolean mLayoutHorizontal;
89 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080090 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080091 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070092
Sunny Goyal4ffec482016-02-09 11:28:52 -080093 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -080094 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -080095 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -070096 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -080097 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -070098 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050099
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);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700112 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700113 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) {
Winson1f064272016-07-18 17:18:02 -0700127 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
128 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700129 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700130 } else if (display == DISPLAY_FOLDER) {
131 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700132 }
Winson1f064272016-07-18 17:18:02 -0700133 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700134
135 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
136 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700137 a.recycle();
138
Sunny Goyal15872da2014-07-08 15:43:54 -0700139 if (mCustomShadowsEnabled) {
140 // Draw the background itself as the parent is drawn twice.
141 mBackground = getBackground();
142 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700143
144 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
145 float density = getResources().getDisplayMetrics().density;
146 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Sunny Goyal15872da2014-07-08 15:43:54 -0700147 } else {
148 mBackground = null;
149 }
Winson Chungb745afb2015-03-02 11:51:23 -0800150
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800151 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700152 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800153
Sunny Goyal10629b02016-09-01 12:50:11 -0700154 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700155 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 }
157
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700158 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
159 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700160 }
161
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700162 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700163 boolean promiseStateChanged) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700164 applyIconAndLabel(info.getIcon(iconCache), info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800165 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700166 if (promiseStateChanged || info.isPromise()) {
167 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500168 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800169 }
170
Sunny Goyal508da152014-08-14 10:53:27 -0700171 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700172 applyIconAndLabel(info.iconBitmap, info);
173
Winson Chung888b3a12015-03-13 11:14:16 -0700174 // We don't need to check the info since it's not a ShortcutInfo
175 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700176
177 // Verify high res immediately
178 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700179 }
180
Sunny Goyal0e08f162015-05-12 11:32:39 -0700181 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700182 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700183 // We don't need to check the info since it's not a ShortcutInfo
184 super.setTag(info);
185
186 // Verify high res immediately
187 verifyHighRes();
188 }
189
Sunny Goyalf4204382016-07-13 10:46:07 -0700190 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
191 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(icon);
192 if (info.isDisabled()) {
193 iconDrawable.setState(FastBitmapDrawable.State.DISABLED);
194 }
195 setIcon(iconDrawable);
196 setText(info.title);
197 if (info.contentDescription != null) {
198 setContentDescription(info.isDisabled()
199 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
200 : info.contentDescription);
201 }
202 }
203
Winson Chung7501adf2015-06-02 11:24:28 -0700204 /**
Winson5fbe0742015-09-30 15:33:00 -0700205 * Used for measurement only, sets some dummy values on this view.
206 */
207 public void applyDummyInfo() {
208 ColorDrawable d = new ColorDrawable();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700209 setIcon(mLauncher.resizeIconDrawable(d));
Winson5fbe0742015-09-30 15:33:00 -0700210 setText("");
211 }
212
213 /**
Winson Chung7501adf2015-06-02 11:24:28 -0700214 * Overrides the default long press timeout.
215 */
216 public void setLongPressTimeout(int longPressTimeout) {
217 mLongPressHelper.setLongPressTimeout(longPressTimeout);
218 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700219
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 @Override
221 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700222 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 mBackgroundSizeChanged = true;
224 }
225 return super.setFrame(left, top, right, bottom);
226 }
227
228 @Override
229 protected boolean verifyDrawable(Drawable who) {
230 return who == mBackground || super.verifyDrawable(who);
231 }
232
233 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700234 public void setTag(Object tag) {
235 if (tag != null) {
236 LauncherModel.checkItemInfo((ItemInfo) tag);
237 }
238 super.setTag(tag);
239 }
240
241 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700242 public void setPressed(boolean pressed) {
243 super.setPressed(pressed);
244
245 if (!mIgnorePressedStateChange) {
246 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800247 }
Sunny Goyal508da152014-08-14 10:53:27 -0700248 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800249
Winson Chungb745afb2015-03-02 11:51:23 -0800250 /** Returns the icon for this view. */
251 public Drawable getIcon() {
252 return mIcon;
253 }
254
255 /** Returns whether the layout is horizontal. */
256 public boolean isLayoutHorizontal() {
257 return mLayoutHorizontal;
258 }
259
Sunny Goyal508da152014-08-14 10:53:27 -0700260 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800261 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700262 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
Kenny Guy44cba692016-01-21 19:50:02 +0000263 if (getTag() instanceof ItemInfo
264 && ((ItemInfo) getTag()).isDisabled()) {
Sunny Goyalcd5b7f92015-10-20 15:17:00 -0700265 d.animateState(FastBitmapDrawable.State.DISABLED);
Kenny Guy44cba692016-01-21 19:50:02 +0000266 } else if (isPressed() || mStayPressed) {
267 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700268 } else {
269 d.animateState(FastBitmapDrawable.State.NORMAL);
270 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800272 }
273
274 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700275 public void setOnLongClickListener(OnLongClickListener l) {
276 super.setOnLongClickListener(l);
277 mOnLongClickListener = l;
278 }
279
280 public OnLongClickListener getOnLongClickListener() {
281 return mOnLongClickListener;
282 }
283
284 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800285 public boolean onTouchEvent(MotionEvent event) {
286 // Call the superclass onTouchEvent first, because sometimes it changes the state to
287 // isPressed() on an ACTION_UP
288 boolean result = super.onTouchEvent(event);
289
Mady Melloref044dd2015-06-02 15:35:07 -0700290 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700291 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700292 mLongPressHelper.cancelLongPress();
293 result = true;
294 }
295
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800296 switch (event.getAction()) {
297 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700298 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800299 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
300 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700301 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700302 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800303 }
Winson Chung88f33452012-02-23 15:23:44 -0800304
Mady Melloref044dd2015-06-02 15:35:07 -0700305 // If we're in a stylus button press, don't check for long press.
306 if (!mStylusEventHelper.inStylusButtonPressed()) {
307 mLongPressHelper.postCheckForLongPress();
308 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800309 break;
310 case MotionEvent.ACTION_CANCEL:
311 case MotionEvent.ACTION_UP:
312 // If we've touched down and up on an item, and it's still not "pressed", then
313 // destroy the pressed outline
314 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700315 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800316 }
Winson Chung88f33452012-02-23 15:23:44 -0800317
318 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800319 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400320 case MotionEvent.ACTION_MOVE:
321 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
322 mLongPressHelper.cancelLongPress();
323 }
324 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800325 }
326 return result;
327 }
328
Michael Jurkaddd62e92011-02-16 17:49:14 -0800329 void setStayPressed(boolean stayPressed) {
330 mStayPressed = stayPressed;
331 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700332 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700333 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700334 } else {
335 if (mPressedBackground == null) {
336 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
337 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800338 }
Sunny Goyal508da152014-08-14 10:53:27 -0700339
340 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700341 ViewParent parent = getParent();
342 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
343 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
344 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700345 }
346
347 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800348 }
349
Sunny Goyal508da152014-08-14 10:53:27 -0700350 void clearPressedBackground() {
351 setPressed(false);
352 setStayPressed(false);
353 }
354
355 @Override
356 public boolean onKeyDown(int keyCode, KeyEvent event) {
357 if (super.onKeyDown(keyCode, event)) {
358 // Pre-create shadow so show immediately on click.
359 if (mPressedBackground == null) {
360 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700361 }
Sunny Goyal508da152014-08-14 10:53:27 -0700362 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700363 }
Sunny Goyal508da152014-08-14 10:53:27 -0700364 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800365 }
366
Sunny Goyal508da152014-08-14 10:53:27 -0700367 @Override
368 public boolean onKeyUp(int keyCode, KeyEvent event) {
369 // Unlike touch events, keypress event propagate pressed state change immediately,
370 // without waiting for onClickHandler to execute. Disable pressed state changes here
371 // to avoid flickering.
372 mIgnorePressedStateChange = true;
373 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700374
Sunny Goyal508da152014-08-14 10:53:27 -0700375 mPressedBackground = null;
376 mIgnorePressedStateChange = false;
377 updateIconState();
378 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800379 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800380
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 @Override
382 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700383 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700384 super.draw(canvas);
385 return;
386 }
387
Michael Jurkabdb5c532011-02-01 15:05:06 -0800388 final Drawable background = mBackground;
389 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700390 final int scrollX = getScrollX();
391 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800392
Michael Jurkabdb5c532011-02-01 15:05:06 -0800393 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700394 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800395 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800397
398 if ((scrollX | scrollY) == 0) {
399 background.draw(canvas);
400 } else {
401 canvas.translate(scrollX, scrollY);
402 background.draw(canvas);
403 canvas.translate(-scrollX, -scrollY);
404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800406
407 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800408 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800409 getPaint().clearShadowLayer();
410 super.draw(canvas);
411 return;
412 }
413
Michael Jurkabdb5c532011-02-01 15:05:06 -0800414 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700415 float density = getResources().getDisplayMetrics().density;
416 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800417 super.draw(canvas);
418 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700419 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
420 getScrollX() + getWidth(),
421 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700422 getPaint().setShadowLayer(
423 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800424 super.draw(canvas);
425 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400427
428 @Override
429 protected void onAttachedToWindow() {
430 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700431
Winson Chung656d11c2010-11-29 17:15:47 -0800432 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700433
Winson Chungb745afb2015-03-02 11:51:23 -0800434 if (mIcon instanceof PreloadIconDrawable) {
435 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700436 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400437 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400438 }
439
440 @Override
Winson1f064272016-07-18 17:18:02 -0700441 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
442 if (mCenterVertically) {
443 Paint.FontMetrics fm = getPaint().getFontMetrics();
444 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
445 (int) Math.ceil(fm.bottom - fm.top);
446 int height = MeasureSpec.getSize(heightMeasureSpec);
447 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
448 getPaddingBottom());
449 }
450 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
451 }
452
453 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400454 protected void onDetachedFromWindow() {
455 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800456 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400457 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700458
Adam Cohen477828c2013-09-20 12:05:49 -0700459 @Override
460 public void setTextColor(int color) {
461 mTextColor = color;
462 super.setTextColor(color);
463 }
464
Adam Cohen96bb7982014-07-07 11:58:56 -0700465 @Override
466 public void setTextColor(ColorStateList colors) {
467 mTextColor = colors.getDefaultColor();
468 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700469 }
470
Winson Chung5f8afe62013-08-12 16:19:28 -0700471 public void setTextVisibility(boolean visible) {
472 Resources res = getResources();
473 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700474 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700475 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700476 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700477 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700478 }
479
Winson Chungaffd7b42010-08-20 15:11:56 -0700480 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800481 public void cancelLongPress() {
482 super.cancelLongPress();
483
484 mLongPressHelper.cancelLongPress();
485 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500486
Sunny Goyal34942622014-08-29 17:20:55 -0700487 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700488 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400489 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700490 final boolean isPromise = info.isPromise();
491 final int progressLevel = isPromise ?
492 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
493 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700494
Sunny Goyalc469aad2015-10-01 11:24:23 -0700495 setContentDescription(progressLevel > 0 ?
496 getContext().getString(R.string.app_downloading_title, info.title,
497 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
498 getContext().getString(R.string.app_waiting_download_title, info.title));
499
Winson Chungb745afb2015-03-02 11:51:23 -0800500 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700501 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800502 if (mIcon instanceof PreloadIconDrawable) {
503 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700504 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800505 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700506 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700507 }
508
509 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700510 if (promiseStateChanged) {
511 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700512 }
Sunny Goyale755d462014-07-22 13:48:29 -0700513 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400514 }
515 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700516
517 private Theme getPreloaderTheme() {
518 Object tag = getTag();
519 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
520 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
521 : R.style.PreloadIcon;
522 Theme theme = sPreloaderThemes.get(style);
523 if (theme == null) {
524 theme = getResources().newTheme();
525 theme.applyStyle(style, true);
526 sPreloaderThemes.put(style, theme);
527 }
528 return theme;
529 }
Winson Chungb745afb2015-03-02 11:51:23 -0800530
531 /**
532 * Sets the icon for this view based on the layout direction.
533 */
Sunny Goyal70660032015-05-14 00:07:08 -0700534 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Tony Wickham377ed3f2016-07-20 15:21:04 -0700535 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800536 mIcon = icon;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700537 if (mIconSize != -1) {
538 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800539 }
Tony Wickham377ed3f2016-07-20 15:21:04 -0700540 applyCompoundDrawables(mIcon);
541 }
542
543 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800544 if (mLayoutHorizontal) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700545 if (Utilities.ATLEAST_JB_MR1) {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700546 setCompoundDrawablesRelative(icon, null, null, null);
Sunny Goyal70660032015-05-14 00:07:08 -0700547 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700548 setCompoundDrawables(icon, null, null, null);
Sunny Goyal70660032015-05-14 00:07:08 -0700549 }
Winson Chungb745afb2015-03-02 11:51:23 -0800550 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700551 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800552 }
Winson Chungb745afb2015-03-02 11:51:23 -0800553 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700554
Sunny Goyal69b75642015-05-15 17:00:24 -0700555 @Override
556 public void requestLayout() {
557 if (!mDisableRelayout) {
558 super.requestLayout();
559 }
560 }
561
Sunny Goyal34b65272015-03-11 16:56:52 -0700562 /**
563 * Applies the item info if it is same as what the view is pointing to currently.
564 */
565 public void reapplyItemInfo(final ItemInfo info) {
566 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700567 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
568 if (mIcon instanceof FastBitmapDrawable) {
569 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
570 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700571 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700572 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700573
Sunny Goyal34b65272015-03-11 16:56:52 -0700574 if (info instanceof AppInfo) {
575 applyFromApplicationInfo((AppInfo) info);
576 } else if (info instanceof ShortcutInfo) {
577 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700578 LauncherAppState.getInstance().getIconCache());
Sunny Goyal317698b2015-07-29 11:45:41 -0700579 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
580 View folderIcon =
581 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
582 if (folderIcon != null) {
583 folderIcon.invalidate();
584 }
585 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700586 } else if (info instanceof PackageItemInfo) {
587 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700588 }
Winsonc0880492015-08-21 11:16:27 -0700589
590 // If we are reapplying over an old icon, then we should update the new icon to the same
591 // state as the old icon
592 if (mIcon instanceof FastBitmapDrawable) {
593 ((FastBitmapDrawable) mIcon).setState(prevState);
594 }
595
Sunny Goyal69b75642015-05-15 17:00:24 -0700596 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700597 }
598 }
599
600 /**
601 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
602 */
603 public void verifyHighRes() {
604 if (mIconLoadRequest != null) {
605 mIconLoadRequest.cancel();
606 mIconLoadRequest = null;
607 }
608 if (getTag() instanceof AppInfo) {
609 AppInfo info = (AppInfo) getTag();
610 if (info.usingLowResIcon) {
611 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
612 .updateIconInBackground(BubbleTextView.this, info);
613 }
614 } else if (getTag() instanceof ShortcutInfo) {
615 ShortcutInfo info = (ShortcutInfo) getTag();
616 if (info.usingLowResIcon) {
617 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
618 .updateIconInBackground(BubbleTextView.this, info);
619 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700620 } else if (getTag() instanceof PackageItemInfo) {
621 PackageItemInfo info = (PackageItemInfo) getTag();
622 if (info.usingLowResIcon) {
623 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
624 .updateIconInBackground(BubbleTextView.this, info);
625 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700626 }
627 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700628
Winson Chungb1777442015-06-16 13:35:04 -0700629 @Override
Winsonc0880492015-08-21 11:16:27 -0700630 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
631 // We can only set the fast scroll focus state on a FastBitmapDrawable
632 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700633 return;
634 }
635
Winsonc0880492015-08-21 11:16:27 -0700636 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
637 if (animated) {
638 FastBitmapDrawable.State prevState = d.getCurrentState();
639 if (d.animateState(focusState)) {
640 // If the state was updated, then update the view accordingly
641 animate().scaleX(focusState.viewScale)
642 .scaleY(focusState.viewScale)
643 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
644 .setDuration(d.getDurationForStateChange(prevState, focusState))
645 .start();
646 }
647 } else {
648 if (d.setState(focusState)) {
649 // If the state was updated, then update the view accordingly
650 animate().cancel();
651 setScaleX(focusState.viewScale);
652 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700653 }
654 }
655 }
656
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700657 /**
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700658 * Returns true if the view can show custom shortcuts.
659 */
660 public boolean hasDeepShortcuts() {
661 return !mLauncher.getShortcutIdsForItem((ItemInfo) getTag()).isEmpty();
662 }
663
664 /**
Winsonc0880492015-08-21 11:16:27 -0700665 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
666 */
667 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
668 final FastBitmapDrawable.State toState) {
669 switch (toState) {
670 case NORMAL:
671 switch (fromState) {
672 case FAST_SCROLL_HIGHLIGHTED:
673 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
674 }
675 }
676 return 0;
677 }
678
679 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700680 * Interface to be implemented by the grand parent to allow click shadow effect.
681 */
Winsonc0880492015-08-21 11:16:27 -0700682 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700683 void setPressedIcon(BubbleTextView icon, Bitmap background);
684 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685}