blob: dbb797dc55898e0ab396762b6d3bb92581ab2970 [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) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700131 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700132 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700133 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700134 }
Winson1f064272016-07-18 17:18:02 -0700135 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700136
137 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
138 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700139 a.recycle();
140
Sunny Goyal15872da2014-07-08 15:43:54 -0700141 if (mCustomShadowsEnabled) {
142 // Draw the background itself as the parent is drawn twice.
143 mBackground = getBackground();
144 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700145
146 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
147 float density = getResources().getDisplayMetrics().density;
148 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Sunny Goyal15872da2014-07-08 15:43:54 -0700149 } else {
150 mBackground = null;
151 }
Winson Chungb745afb2015-03-02 11:51:23 -0800152
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800153 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700154 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800155
Sunny Goyal10629b02016-09-01 12:50:11 -0700156 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700157 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 }
159
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700160 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
161 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700162 }
163
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700164 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700165 boolean promiseStateChanged) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700166 applyIconAndLabel(info.getIcon(iconCache), info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800167 setTag(info);
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) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700174 applyIconAndLabel(info.iconBitmap, info);
175
Winson Chung888b3a12015-03-13 11:14:16 -0700176 // We don't need to check the info since it's not a ShortcutInfo
177 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700178
179 // Verify high res immediately
180 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700181 }
182
Sunny Goyal0e08f162015-05-12 11:32:39 -0700183 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700184 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700185 // We don't need to check the info since it's not a ShortcutInfo
186 super.setTag(info);
187
188 // Verify high res immediately
189 verifyHighRes();
190 }
191
Sunny Goyalf4204382016-07-13 10:46:07 -0700192 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
193 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(icon);
Tony Wickham6b910a22016-11-08 10:40:34 -0800194 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700195 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;
Tony Wickham6b910a22016-11-08 10:40:34 -0800263 if (isPressed() || mStayPressed) {
Kenny Guy44cba692016-01-21 19:50:02 +0000264 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700265 } else {
266 d.animateState(FastBitmapDrawable.State.NORMAL);
267 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800269 }
270
271 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700272 public void setOnLongClickListener(OnLongClickListener l) {
273 super.setOnLongClickListener(l);
274 mOnLongClickListener = l;
275 }
276
277 public OnLongClickListener getOnLongClickListener() {
278 return mOnLongClickListener;
279 }
280
281 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800282 public boolean onTouchEvent(MotionEvent event) {
283 // Call the superclass onTouchEvent first, because sometimes it changes the state to
284 // isPressed() on an ACTION_UP
285 boolean result = super.onTouchEvent(event);
286
Mady Melloref044dd2015-06-02 15:35:07 -0700287 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700288 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700289 mLongPressHelper.cancelLongPress();
290 result = true;
291 }
292
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800293 switch (event.getAction()) {
294 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700295 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800296 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
297 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700298 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700299 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800300 }
Winson Chung88f33452012-02-23 15:23:44 -0800301
Mady Melloref044dd2015-06-02 15:35:07 -0700302 // If we're in a stylus button press, don't check for long press.
303 if (!mStylusEventHelper.inStylusButtonPressed()) {
304 mLongPressHelper.postCheckForLongPress();
305 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800306 break;
307 case MotionEvent.ACTION_CANCEL:
308 case MotionEvent.ACTION_UP:
309 // If we've touched down and up on an item, and it's still not "pressed", then
310 // destroy the pressed outline
311 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700312 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800313 }
Winson Chung88f33452012-02-23 15:23:44 -0800314
315 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800316 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400317 case MotionEvent.ACTION_MOVE:
318 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
319 mLongPressHelper.cancelLongPress();
320 }
321 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800322 }
323 return result;
324 }
325
Michael Jurkaddd62e92011-02-16 17:49:14 -0800326 void setStayPressed(boolean stayPressed) {
327 mStayPressed = stayPressed;
328 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700329 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700330 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700331 } else {
332 if (mPressedBackground == null) {
333 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
334 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800335 }
Sunny Goyal508da152014-08-14 10:53:27 -0700336
337 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700338 ViewParent parent = getParent();
339 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
340 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
341 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700342 }
343
344 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800345 }
346
Sunny Goyal508da152014-08-14 10:53:27 -0700347 void clearPressedBackground() {
348 setPressed(false);
349 setStayPressed(false);
350 }
351
352 @Override
353 public boolean onKeyDown(int keyCode, KeyEvent event) {
354 if (super.onKeyDown(keyCode, event)) {
355 // Pre-create shadow so show immediately on click.
356 if (mPressedBackground == null) {
357 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700358 }
Sunny Goyal508da152014-08-14 10:53:27 -0700359 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700360 }
Sunny Goyal508da152014-08-14 10:53:27 -0700361 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800362 }
363
Sunny Goyal508da152014-08-14 10:53:27 -0700364 @Override
365 public boolean onKeyUp(int keyCode, KeyEvent event) {
366 // Unlike touch events, keypress event propagate pressed state change immediately,
367 // without waiting for onClickHandler to execute. Disable pressed state changes here
368 // to avoid flickering.
369 mIgnorePressedStateChange = true;
370 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700371
Sunny Goyal508da152014-08-14 10:53:27 -0700372 mPressedBackground = null;
373 mIgnorePressedStateChange = false;
374 updateIconState();
375 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800376 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800377
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 @Override
379 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700380 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700381 super.draw(canvas);
382 return;
383 }
384
Michael Jurkabdb5c532011-02-01 15:05:06 -0800385 final Drawable background = mBackground;
386 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700387 final int scrollX = getScrollX();
388 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800389
Michael Jurkabdb5c532011-02-01 15:05:06 -0800390 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700391 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800392 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800394
395 if ((scrollX | scrollY) == 0) {
396 background.draw(canvas);
397 } else {
398 canvas.translate(scrollX, scrollY);
399 background.draw(canvas);
400 canvas.translate(-scrollX, -scrollY);
401 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800403
404 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800405 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800406 getPaint().clearShadowLayer();
407 super.draw(canvas);
408 return;
409 }
410
Michael Jurkabdb5c532011-02-01 15:05:06 -0800411 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700412 float density = getResources().getDisplayMetrics().density;
413 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800414 super.draw(canvas);
415 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700416 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
417 getScrollX() + getWidth(),
418 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700419 getPaint().setShadowLayer(
420 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800421 super.draw(canvas);
422 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400424
425 @Override
426 protected void onAttachedToWindow() {
427 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700428
Winson Chung656d11c2010-11-29 17:15:47 -0800429 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700430
Winson Chungb745afb2015-03-02 11:51:23 -0800431 if (mIcon instanceof PreloadIconDrawable) {
432 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700433 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400434 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400435 }
436
437 @Override
Winson1f064272016-07-18 17:18:02 -0700438 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
439 if (mCenterVertically) {
440 Paint.FontMetrics fm = getPaint().getFontMetrics();
441 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
442 (int) Math.ceil(fm.bottom - fm.top);
443 int height = MeasureSpec.getSize(heightMeasureSpec);
444 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
445 getPaddingBottom());
446 }
447 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
448 }
449
450 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400451 protected void onDetachedFromWindow() {
452 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800453 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400454 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700455
Adam Cohen477828c2013-09-20 12:05:49 -0700456 @Override
457 public void setTextColor(int color) {
458 mTextColor = color;
459 super.setTextColor(color);
460 }
461
Adam Cohen96bb7982014-07-07 11:58:56 -0700462 @Override
463 public void setTextColor(ColorStateList colors) {
464 mTextColor = colors.getDefaultColor();
465 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700466 }
467
Winson Chung5f8afe62013-08-12 16:19:28 -0700468 public void setTextVisibility(boolean visible) {
469 Resources res = getResources();
470 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700471 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700472 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700473 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700474 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700475 }
476
Winson Chungaffd7b42010-08-20 15:11:56 -0700477 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800478 public void cancelLongPress() {
479 super.cancelLongPress();
480
481 mLongPressHelper.cancelLongPress();
482 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500483
Sunny Goyal34942622014-08-29 17:20:55 -0700484 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700485 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400486 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700487 final boolean isPromise = info.isPromise();
488 final int progressLevel = isPromise ?
489 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
490 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700491
Sunny Goyalc469aad2015-10-01 11:24:23 -0700492 setContentDescription(progressLevel > 0 ?
493 getContext().getString(R.string.app_downloading_title, info.title,
494 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
495 getContext().getString(R.string.app_waiting_download_title, info.title));
496
Winson Chungb745afb2015-03-02 11:51:23 -0800497 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700498 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800499 if (mIcon instanceof PreloadIconDrawable) {
500 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700501 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800502 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700503 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700504 }
505
506 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700507 if (promiseStateChanged) {
508 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700509 }
Sunny Goyale755d462014-07-22 13:48:29 -0700510 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400511 }
512 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700513
514 private Theme getPreloaderTheme() {
515 Object tag = getTag();
516 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
517 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
518 : R.style.PreloadIcon;
519 Theme theme = sPreloaderThemes.get(style);
520 if (theme == null) {
521 theme = getResources().newTheme();
522 theme.applyStyle(style, true);
523 sPreloaderThemes.put(style, theme);
524 }
525 return theme;
526 }
Winson Chungb745afb2015-03-02 11:51:23 -0800527
528 /**
529 * Sets the icon for this view based on the layout direction.
530 */
Sunny Goyal70660032015-05-14 00:07:08 -0700531 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Tony Wickham377ed3f2016-07-20 15:21:04 -0700532 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800533 mIcon = icon;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700534 if (mIconSize != -1) {
535 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800536 }
Tony Wickham377ed3f2016-07-20 15:21:04 -0700537 applyCompoundDrawables(mIcon);
538 }
539
540 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800541 if (mLayoutHorizontal) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700542 if (Utilities.ATLEAST_JB_MR1) {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700543 setCompoundDrawablesRelative(icon, null, null, null);
Sunny Goyal70660032015-05-14 00:07:08 -0700544 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700545 setCompoundDrawables(icon, null, null, null);
Sunny Goyal70660032015-05-14 00:07:08 -0700546 }
Winson Chungb745afb2015-03-02 11:51:23 -0800547 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700548 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800549 }
Winson Chungb745afb2015-03-02 11:51:23 -0800550 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700551
Sunny Goyal69b75642015-05-15 17:00:24 -0700552 @Override
553 public void requestLayout() {
554 if (!mDisableRelayout) {
555 super.requestLayout();
556 }
557 }
558
Sunny Goyal34b65272015-03-11 16:56:52 -0700559 /**
560 * Applies the item info if it is same as what the view is pointing to currently.
561 */
562 public void reapplyItemInfo(final ItemInfo info) {
563 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700564 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
565 if (mIcon instanceof FastBitmapDrawable) {
566 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
567 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700568 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700569 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700570
Sunny Goyal34b65272015-03-11 16:56:52 -0700571 if (info instanceof AppInfo) {
572 applyFromApplicationInfo((AppInfo) info);
573 } else if (info instanceof ShortcutInfo) {
574 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700575 LauncherAppState.getInstance().getIconCache());
Sunny Goyal317698b2015-07-29 11:45:41 -0700576 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
577 View folderIcon =
578 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
579 if (folderIcon != null) {
580 folderIcon.invalidate();
581 }
582 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700583 } else if (info instanceof PackageItemInfo) {
584 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700585 }
Winsonc0880492015-08-21 11:16:27 -0700586
587 // If we are reapplying over an old icon, then we should update the new icon to the same
588 // state as the old icon
589 if (mIcon instanceof FastBitmapDrawable) {
590 ((FastBitmapDrawable) mIcon).setState(prevState);
591 }
592
Sunny Goyal69b75642015-05-15 17:00:24 -0700593 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700594 }
595 }
596
597 /**
598 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
599 */
600 public void verifyHighRes() {
601 if (mIconLoadRequest != null) {
602 mIconLoadRequest.cancel();
603 mIconLoadRequest = null;
604 }
605 if (getTag() instanceof AppInfo) {
606 AppInfo info = (AppInfo) getTag();
607 if (info.usingLowResIcon) {
608 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
609 .updateIconInBackground(BubbleTextView.this, info);
610 }
611 } else if (getTag() instanceof ShortcutInfo) {
612 ShortcutInfo info = (ShortcutInfo) getTag();
613 if (info.usingLowResIcon) {
614 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
615 .updateIconInBackground(BubbleTextView.this, info);
616 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700617 } else if (getTag() instanceof PackageItemInfo) {
618 PackageItemInfo info = (PackageItemInfo) getTag();
619 if (info.usingLowResIcon) {
620 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
621 .updateIconInBackground(BubbleTextView.this, info);
622 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700623 }
624 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700625
Winson Chungb1777442015-06-16 13:35:04 -0700626 @Override
Winsonc0880492015-08-21 11:16:27 -0700627 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
628 // We can only set the fast scroll focus state on a FastBitmapDrawable
629 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700630 return;
631 }
632
Winsonc0880492015-08-21 11:16:27 -0700633 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
634 if (animated) {
635 FastBitmapDrawable.State prevState = d.getCurrentState();
636 if (d.animateState(focusState)) {
637 // If the state was updated, then update the view accordingly
638 animate().scaleX(focusState.viewScale)
639 .scaleY(focusState.viewScale)
640 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
641 .setDuration(d.getDurationForStateChange(prevState, focusState))
642 .start();
643 }
644 } else {
645 if (d.setState(focusState)) {
646 // If the state was updated, then update the view accordingly
647 animate().cancel();
648 setScaleX(focusState.viewScale);
649 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700650 }
651 }
652 }
653
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700654 /**
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700655 * Returns true if the view can show custom shortcuts.
656 */
657 public boolean hasDeepShortcuts() {
658 return !mLauncher.getShortcutIdsForItem((ItemInfo) getTag()).isEmpty();
659 }
660
661 /**
Winsonc0880492015-08-21 11:16:27 -0700662 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
663 */
664 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
665 final FastBitmapDrawable.State toState) {
666 switch (toState) {
667 case NORMAL:
668 switch (fromState) {
669 case FAST_SCROLL_HIGHLIGHTED:
670 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
671 }
672 }
673 return 0;
674 }
675
676 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700677 * Interface to be implemented by the grand parent to allow click shadow effect.
678 */
Winsonc0880492015-08-21 11:16:27 -0700679 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700680 void setPressedIcon(BubbleTextView icon, Bitmap background);
681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682}