blob: b8b43c9c305d3bb21b60535aefc741ea89098b2e [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
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070020import android.content.res.ColorStateList;
Winson Chung656d11c2010-11-29 17:15:47 -080021import android.content.res.Resources;
Sunny Goyal95abbb32014-08-04 10:53:22 -070022import android.content.res.Resources.Theme;
Adam Cohen96bb7982014-07-07 11:58:56 -070023import android.content.res.TypedArray;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080024import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.graphics.Canvas;
Winson1f064272016-07-18 17:18:02 -070026import android.graphics.Paint;
Michael Jurka137142e2011-01-05 20:57:04 -080027import android.graphics.Region;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.graphics.drawable.Drawable;
Winson Chung656d11c2010-11-29 17:15:47 -080029import android.util.AttributeSet;
Sunny Goyal95abbb32014-08-04 10:53:22 -070030import android.util.SparseArray;
Winson Chung5f8afe62013-08-12 16:19:28 -070031import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070032import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080033import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070034import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040035import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080036import android.view.ViewDebug;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070037import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080038import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070039
Sunny Goyal34b65272015-03-11 16:56:52 -070040import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal2d7cca12017-01-03 16:52:43 -080041import com.android.launcher3.IconCache.ItemInfoUpdateReceiver;
Sunny Goyal26119432016-02-18 22:09:23 +000042import com.android.launcher3.folder.FolderIcon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080043import com.android.launcher3.graphics.DrawableFactory;
Sunny Goyal10629b02016-09-01 12:50:11 -070044import com.android.launcher3.graphics.HolographicOutlineHelper;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070045import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070046
Sunny Goyalc469aad2015-10-01 11:24:23 -070047import java.text.NumberFormat;
48
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049/**
50 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
51 * because we want to make the bubble taller than the text and TextView's clip is
52 * too aggressive.
53 */
Winson Chungb1777442015-06-16 13:35:04 -070054public class BubbleTextView extends TextView
Sunny Goyal2d7cca12017-01-03 16:52:43 -080055 implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView, ItemInfoUpdateReceiver {
Sunny Goyal95abbb32014-08-04 10:53:22 -070056
Sameer Padala8fd74832014-09-08 16:00:29 -070057 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070058
Sunny Goyal8f8f3982016-07-25 17:08:38 -070059 // Dimensions in DP
60 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
61 private static final float KEY_SHADOW_RADIUS = 1f;
62 private static final float KEY_SHADOW_OFFSET = 0.5f;
63 private static final int AMBIENT_SHADOW_COLOR = 0x33000000;
64 private static final int KEY_SHADOW_COLOR = 0x66000000;
Winson Chung656d11c2010-11-29 17:15:47 -080065
Sunny Goyaldfaccf62015-05-11 16:30:44 -070066 private static final int DISPLAY_WORKSPACE = 0;
67 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070068 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070069
Sunny Goyal53d7ee42015-05-22 12:25:45 -070070 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080071 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070072 private final boolean mCenterVertically;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080073 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070074 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080075 private final CheckLongPressHelper mLongPressHelper;
76 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070077 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080078
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080079 private boolean mBackgroundSizeChanged;
80
Sunny Goyal508da152014-08-14 10:53:27 -070081 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080082
Jason Monk02dd7ae2014-04-15 15:23:31 -040083 private float mSlop;
84
Winson Chung93f98ea2015-03-10 16:28:47 -070085 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070086 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080087 private final boolean mLayoutHorizontal;
88 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080089 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080090 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070091
Sunny Goyal4ffec482016-02-09 11:28:52 -080092 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -080093 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -080094 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -070095 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -070097 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050098
Sunny Goyal34b65272015-03-11 16:56:52 -070099 private IconLoadRequest mIconLoadRequest;
100
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700102 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 }
104
105 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700106 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 }
108
109 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
110 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700111 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700112 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700113
Adam Cohen96bb7982014-07-07 11:58:56 -0700114 TypedArray a = context.obtainStyledAttributes(attrs,
115 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700116 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800117 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700118 mDeferShadowGenerationOnTouch =
119 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700120
121 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
122 int defaultIconSize = grid.iconSizePx;
123 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700124 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700125 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700126 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
127 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700128 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700129 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700130 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700131 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700132 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700133 }
Winson1f064272016-07-18 17:18:02 -0700134 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700135
136 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
137 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700138 a.recycle();
139
Sunny Goyal15872da2014-07-08 15:43:54 -0700140 if (mCustomShadowsEnabled) {
141 // Draw the background itself as the parent is drawn twice.
142 mBackground = getBackground();
143 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700144
145 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
146 float density = getResources().getDisplayMetrics().density;
147 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Sunny Goyal15872da2014-07-08 15:43:54 -0700148 } else {
149 mBackground = null;
150 }
Winson Chungb745afb2015-03-02 11:51:23 -0800151
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800152 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700153 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800154
Sunny Goyal10629b02016-09-01 12:50:11 -0700155 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700156 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 }
158
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800159 public void applyFromShortcutInfo(ShortcutInfo info) {
160 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700161 }
162
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800163 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
164 applyIconAndLabel(info.iconBitmap, 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) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800191 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800192 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700193 setIcon(iconDrawable);
194 setText(info.title);
195 if (info.contentDescription != null) {
196 setContentDescription(info.isDisabled()
197 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
198 : info.contentDescription);
199 }
200 }
201
Winson Chung7501adf2015-06-02 11:24:28 -0700202 /**
203 * Overrides the default long press timeout.
204 */
205 public void setLongPressTimeout(int longPressTimeout) {
206 mLongPressHelper.setLongPressTimeout(longPressTimeout);
207 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 @Override
210 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700211 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 mBackgroundSizeChanged = true;
213 }
214 return super.setFrame(left, top, right, bottom);
215 }
216
217 @Override
218 protected boolean verifyDrawable(Drawable who) {
219 return who == mBackground || super.verifyDrawable(who);
220 }
221
222 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700223 public void setTag(Object tag) {
224 if (tag != null) {
225 LauncherModel.checkItemInfo((ItemInfo) tag);
226 }
227 super.setTag(tag);
228 }
229
230 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700231 public void setPressed(boolean pressed) {
232 super.setPressed(pressed);
233
234 if (!mIgnorePressedStateChange) {
235 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800236 }
Sunny Goyal508da152014-08-14 10:53:27 -0700237 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800238
Winson Chungb745afb2015-03-02 11:51:23 -0800239 /** Returns the icon for this view. */
240 public Drawable getIcon() {
241 return mIcon;
242 }
243
244 /** Returns whether the layout is horizontal. */
245 public boolean isLayoutHorizontal() {
246 return mLayoutHorizontal;
247 }
248
Sunny Goyal508da152014-08-14 10:53:27 -0700249 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800250 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700251 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
Tony Wickham6b910a22016-11-08 10:40:34 -0800252 if (isPressed() || mStayPressed) {
Kenny Guy44cba692016-01-21 19:50:02 +0000253 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700254 } else {
255 d.animateState(FastBitmapDrawable.State.NORMAL);
256 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800258 }
259
260 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700261 public void setOnLongClickListener(OnLongClickListener l) {
262 super.setOnLongClickListener(l);
263 mOnLongClickListener = l;
264 }
265
266 public OnLongClickListener getOnLongClickListener() {
267 return mOnLongClickListener;
268 }
269
270 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800271 public boolean onTouchEvent(MotionEvent event) {
272 // Call the superclass onTouchEvent first, because sometimes it changes the state to
273 // isPressed() on an ACTION_UP
274 boolean result = super.onTouchEvent(event);
275
Mady Melloref044dd2015-06-02 15:35:07 -0700276 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700277 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700278 mLongPressHelper.cancelLongPress();
279 result = true;
280 }
281
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800282 switch (event.getAction()) {
283 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700284 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800285 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
286 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700287 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700288 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800289 }
Winson Chung88f33452012-02-23 15:23:44 -0800290
Mady Melloref044dd2015-06-02 15:35:07 -0700291 // If we're in a stylus button press, don't check for long press.
292 if (!mStylusEventHelper.inStylusButtonPressed()) {
293 mLongPressHelper.postCheckForLongPress();
294 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800295 break;
296 case MotionEvent.ACTION_CANCEL:
297 case MotionEvent.ACTION_UP:
298 // If we've touched down and up on an item, and it's still not "pressed", then
299 // destroy the pressed outline
300 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700301 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800302 }
Winson Chung88f33452012-02-23 15:23:44 -0800303
304 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800305 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400306 case MotionEvent.ACTION_MOVE:
307 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
308 mLongPressHelper.cancelLongPress();
309 }
310 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800311 }
312 return result;
313 }
314
Michael Jurkaddd62e92011-02-16 17:49:14 -0800315 void setStayPressed(boolean stayPressed) {
316 mStayPressed = stayPressed;
317 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700318 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700319 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700320 } else {
321 if (mPressedBackground == null) {
322 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
323 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800324 }
Sunny Goyal508da152014-08-14 10:53:27 -0700325
326 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700327 ViewParent parent = getParent();
328 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
329 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
330 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700331 }
332
333 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800334 }
335
Sunny Goyal508da152014-08-14 10:53:27 -0700336 void clearPressedBackground() {
337 setPressed(false);
338 setStayPressed(false);
339 }
340
341 @Override
342 public boolean onKeyDown(int keyCode, KeyEvent event) {
343 if (super.onKeyDown(keyCode, event)) {
344 // Pre-create shadow so show immediately on click.
345 if (mPressedBackground == null) {
346 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700347 }
Sunny Goyal508da152014-08-14 10:53:27 -0700348 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700349 }
Sunny Goyal508da152014-08-14 10:53:27 -0700350 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800351 }
352
Sunny Goyal508da152014-08-14 10:53:27 -0700353 @Override
354 public boolean onKeyUp(int keyCode, KeyEvent event) {
355 // Unlike touch events, keypress event propagate pressed state change immediately,
356 // without waiting for onClickHandler to execute. Disable pressed state changes here
357 // to avoid flickering.
358 mIgnorePressedStateChange = true;
359 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700360
Sunny Goyal508da152014-08-14 10:53:27 -0700361 mPressedBackground = null;
362 mIgnorePressedStateChange = false;
363 updateIconState();
364 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800365 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800366
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 @Override
368 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700369 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700370 super.draw(canvas);
371 return;
372 }
373
Michael Jurkabdb5c532011-02-01 15:05:06 -0800374 final Drawable background = mBackground;
375 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700376 final int scrollX = getScrollX();
377 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800378
Michael Jurkabdb5c532011-02-01 15:05:06 -0800379 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700380 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800381 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800383
384 if ((scrollX | scrollY) == 0) {
385 background.draw(canvas);
386 } else {
387 canvas.translate(scrollX, scrollY);
388 background.draw(canvas);
389 canvas.translate(-scrollX, -scrollY);
390 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800392
393 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800394 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800395 getPaint().clearShadowLayer();
396 super.draw(canvas);
397 return;
398 }
399
Michael Jurkabdb5c532011-02-01 15:05:06 -0800400 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700401 float density = getResources().getDisplayMetrics().density;
402 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800403 super.draw(canvas);
404 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700405 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
406 getScrollX() + getWidth(),
407 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700408 getPaint().setShadowLayer(
409 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800410 super.draw(canvas);
411 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400413
414 @Override
415 protected void onAttachedToWindow() {
416 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700417
Winson Chung656d11c2010-11-29 17:15:47 -0800418 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700419
Winson Chungb745afb2015-03-02 11:51:23 -0800420 if (mIcon instanceof PreloadIconDrawable) {
421 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700422 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400423 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400424 }
425
426 @Override
Winson1f064272016-07-18 17:18:02 -0700427 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
428 if (mCenterVertically) {
429 Paint.FontMetrics fm = getPaint().getFontMetrics();
430 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
431 (int) Math.ceil(fm.bottom - fm.top);
432 int height = MeasureSpec.getSize(heightMeasureSpec);
433 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
434 getPaddingBottom());
435 }
436 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
437 }
438
439 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400440 protected void onDetachedFromWindow() {
441 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800442 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400443 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700444
Adam Cohen477828c2013-09-20 12:05:49 -0700445 @Override
446 public void setTextColor(int color) {
447 mTextColor = color;
448 super.setTextColor(color);
449 }
450
Adam Cohen96bb7982014-07-07 11:58:56 -0700451 @Override
452 public void setTextColor(ColorStateList colors) {
453 mTextColor = colors.getDefaultColor();
454 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700455 }
456
Winson Chung5f8afe62013-08-12 16:19:28 -0700457 public void setTextVisibility(boolean visible) {
458 Resources res = getResources();
459 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700460 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700461 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700462 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700463 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700464 }
465
Winson Chungaffd7b42010-08-20 15:11:56 -0700466 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800467 public void cancelLongPress() {
468 super.cancelLongPress();
469
470 mLongPressHelper.cancelLongPress();
471 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500472
Sunny Goyal34942622014-08-29 17:20:55 -0700473 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700474 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400475 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700476 final boolean isPromise = info.isPromise();
477 final int progressLevel = isPromise ?
478 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
479 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700480
Sunny Goyalc469aad2015-10-01 11:24:23 -0700481 setContentDescription(progressLevel > 0 ?
482 getContext().getString(R.string.app_downloading_title, info.title,
483 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
484 getContext().getString(R.string.app_waiting_download_title, info.title));
485
Winson Chungb745afb2015-03-02 11:51:23 -0800486 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700487 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800488 if (mIcon instanceof PreloadIconDrawable) {
489 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700490 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800491 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700492 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700493 }
494
495 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700496 if (promiseStateChanged) {
497 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700498 }
Sunny Goyale755d462014-07-22 13:48:29 -0700499 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400500 }
501 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700502
503 private Theme getPreloaderTheme() {
504 Object tag = getTag();
505 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
506 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
507 : R.style.PreloadIcon;
508 Theme theme = sPreloaderThemes.get(style);
509 if (theme == null) {
510 theme = getResources().newTheme();
511 theme.applyStyle(style, true);
512 sPreloaderThemes.put(style, theme);
513 }
514 return theme;
515 }
Winson Chungb745afb2015-03-02 11:51:23 -0800516
517 /**
518 * Sets the icon for this view based on the layout direction.
519 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700520 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800521 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800522 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700523 applyCompoundDrawables(mIcon);
524 }
525
526 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800527 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800528 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800529 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700530 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800531 }
Winson Chungb745afb2015-03-02 11:51:23 -0800532 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700533
Sunny Goyal69b75642015-05-15 17:00:24 -0700534 @Override
535 public void requestLayout() {
536 if (!mDisableRelayout) {
537 super.requestLayout();
538 }
539 }
540
Sunny Goyal34b65272015-03-11 16:56:52 -0700541 /**
542 * Applies the item info if it is same as what the view is pointing to currently.
543 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800544 @Override
545 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700546 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700547 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
548 if (mIcon instanceof FastBitmapDrawable) {
549 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
550 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700551 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700552 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700553
Sunny Goyal34b65272015-03-11 16:56:52 -0700554 if (info instanceof AppInfo) {
555 applyFromApplicationInfo((AppInfo) info);
556 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800557 applyFromShortcutInfo((ShortcutInfo) info);
Sunny Goyal317698b2015-07-29 11:45:41 -0700558 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
559 View folderIcon =
560 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
561 if (folderIcon != null) {
562 folderIcon.invalidate();
563 }
564 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700565 } else if (info instanceof PackageItemInfo) {
566 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700567 }
Winsonc0880492015-08-21 11:16:27 -0700568
569 // If we are reapplying over an old icon, then we should update the new icon to the same
570 // state as the old icon
571 if (mIcon instanceof FastBitmapDrawable) {
572 ((FastBitmapDrawable) mIcon).setState(prevState);
573 }
574
Sunny Goyal69b75642015-05-15 17:00:24 -0700575 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700576 }
577 }
578
579 /**
580 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
581 */
582 public void verifyHighRes() {
583 if (mIconLoadRequest != null) {
584 mIconLoadRequest.cancel();
585 mIconLoadRequest = null;
586 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800587 if (getTag() instanceof ItemInfoWithIcon) {
588 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700589 if (info.usingLowResIcon) {
590 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
591 .updateIconInBackground(BubbleTextView.this, info);
592 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700593 }
594 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700595
Winson Chungb1777442015-06-16 13:35:04 -0700596 @Override
Winsonc0880492015-08-21 11:16:27 -0700597 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
598 // We can only set the fast scroll focus state on a FastBitmapDrawable
599 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700600 return;
601 }
602
Winsonc0880492015-08-21 11:16:27 -0700603 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
604 if (animated) {
605 FastBitmapDrawable.State prevState = d.getCurrentState();
606 if (d.animateState(focusState)) {
607 // If the state was updated, then update the view accordingly
608 animate().scaleX(focusState.viewScale)
609 .scaleY(focusState.viewScale)
610 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
611 .setDuration(d.getDurationForStateChange(prevState, focusState))
612 .start();
613 }
614 } else {
615 if (d.setState(focusState)) {
616 // If the state was updated, then update the view accordingly
617 animate().cancel();
618 setScaleX(focusState.viewScale);
619 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700620 }
621 }
622 }
623
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700624 /**
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700625 * Returns true if the view can show custom shortcuts.
626 */
627 public boolean hasDeepShortcuts() {
628 return !mLauncher.getShortcutIdsForItem((ItemInfo) getTag()).isEmpty();
629 }
630
631 /**
Winsonc0880492015-08-21 11:16:27 -0700632 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
633 */
634 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
635 final FastBitmapDrawable.State toState) {
636 switch (toState) {
637 case NORMAL:
638 switch (fromState) {
639 case FAST_SCROLL_HIGHLIGHTED:
640 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
641 }
642 }
643 return 0;
644 }
645
646 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700647 * Interface to be implemented by the grand parent to allow click shadow effect.
648 */
Winsonc0880492015-08-21 11:16:27 -0700649 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700650 void setPressedIcon(BubbleTextView icon, Bitmap background);
651 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652}