blob: 5e9e7e2074cc84f0089939cb2c9e1a8250bf91f6 [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 Goyal26119432016-02-18 22:09:23 +000041import com.android.launcher3.folder.FolderIcon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080042import com.android.launcher3.graphics.DrawableFactory;
Sunny Goyal10629b02016-09-01 12:50:11 -070043import com.android.launcher3.graphics.HolographicOutlineHelper;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070044import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070045
Sunny Goyalc469aad2015-10-01 11:24:23 -070046import java.text.NumberFormat;
47
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048/**
49 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
50 * because we want to make the bubble taller than the text and TextView's clip is
51 * too aggressive.
52 */
Winson Chungb1777442015-06-16 13:35:04 -070053public class BubbleTextView extends TextView
54 implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070055
Sameer Padala8fd74832014-09-08 16:00:29 -070056 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070057
Sunny Goyal8f8f3982016-07-25 17:08:38 -070058 // Dimensions in DP
59 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
60 private static final float KEY_SHADOW_RADIUS = 1f;
61 private static final float KEY_SHADOW_OFFSET = 0.5f;
62 private static final int AMBIENT_SHADOW_COLOR = 0x33000000;
63 private static final int KEY_SHADOW_COLOR = 0x66000000;
Winson Chung656d11c2010-11-29 17:15:47 -080064
Sunny Goyaldfaccf62015-05-11 16:30:44 -070065 private static final int DISPLAY_WORKSPACE = 0;
66 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070067 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070068
Sunny Goyal53d7ee42015-05-22 12:25:45 -070069 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080070 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070071 private final boolean mCenterVertically;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080072 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070073 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080074 private final CheckLongPressHelper mLongPressHelper;
75 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070076 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080077
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080078 private boolean mBackgroundSizeChanged;
79
Sunny Goyal508da152014-08-14 10:53:27 -070080 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080081
Jason Monk02dd7ae2014-04-15 15:23:31 -040082 private float mSlop;
83
Winson Chung93f98ea2015-03-10 16:28:47 -070084 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070085 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080086 private final boolean mLayoutHorizontal;
87 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080088 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080089 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070090
Sunny Goyal4ffec482016-02-09 11:28:52 -080091 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -080092 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -080093 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -070094 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -080095 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -070096 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050097
Sunny Goyal34b65272015-03-11 16:56:52 -070098 private IconLoadRequest mIconLoadRequest;
99
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700101 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 }
103
104 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700105 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 }
107
108 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
109 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700110 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700111 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700112
Adam Cohen96bb7982014-07-07 11:58:56 -0700113 TypedArray a = context.obtainStyledAttributes(attrs,
114 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700115 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800116 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700117 mDeferShadowGenerationOnTouch =
118 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700119
120 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
121 int defaultIconSize = grid.iconSizePx;
122 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700123 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700124 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700125 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
126 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700127 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700128 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700129 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700130 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700131 defaultIconSize = grid.folderChildIconSizePx;
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 Goyal1cd01b02016-11-09 10:43:58 -0800158 public void applyFromShortcutInfo(ShortcutInfo info) {
159 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700160 }
161
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800162 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
163 applyIconAndLabel(info.iconBitmap, info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800164 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700165 if (promiseStateChanged || info.isPromise()) {
166 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500167 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800168 }
169
Sunny Goyal508da152014-08-14 10:53:27 -0700170 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700171 applyIconAndLabel(info.iconBitmap, info);
172
Winson Chung888b3a12015-03-13 11:14:16 -0700173 // We don't need to check the info since it's not a ShortcutInfo
174 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700175
176 // Verify high res immediately
177 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700178 }
179
Sunny Goyal0e08f162015-05-12 11:32:39 -0700180 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700181 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700182 // We don't need to check the info since it's not a ShortcutInfo
183 super.setTag(info);
184
185 // Verify high res immediately
186 verifyHighRes();
187 }
188
Sunny Goyalf4204382016-07-13 10:46:07 -0700189 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800190 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800191 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700192 setIcon(iconDrawable);
193 setText(info.title);
194 if (info.contentDescription != null) {
195 setContentDescription(info.isDisabled()
196 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
197 : info.contentDescription);
198 }
199 }
200
Winson Chung7501adf2015-06-02 11:24:28 -0700201 /**
202 * Overrides the default long press timeout.
203 */
204 public void setLongPressTimeout(int longPressTimeout) {
205 mLongPressHelper.setLongPressTimeout(longPressTimeout);
206 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 @Override
209 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700210 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 mBackgroundSizeChanged = true;
212 }
213 return super.setFrame(left, top, right, bottom);
214 }
215
216 @Override
217 protected boolean verifyDrawable(Drawable who) {
218 return who == mBackground || super.verifyDrawable(who);
219 }
220
221 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700222 public void setTag(Object tag) {
223 if (tag != null) {
224 LauncherModel.checkItemInfo((ItemInfo) tag);
225 }
226 super.setTag(tag);
227 }
228
229 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700230 public void setPressed(boolean pressed) {
231 super.setPressed(pressed);
232
233 if (!mIgnorePressedStateChange) {
234 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800235 }
Sunny Goyal508da152014-08-14 10:53:27 -0700236 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800237
Winson Chungb745afb2015-03-02 11:51:23 -0800238 /** Returns the icon for this view. */
239 public Drawable getIcon() {
240 return mIcon;
241 }
242
243 /** Returns whether the layout is horizontal. */
244 public boolean isLayoutHorizontal() {
245 return mLayoutHorizontal;
246 }
247
Sunny Goyal508da152014-08-14 10:53:27 -0700248 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800249 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700250 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
Tony Wickham6b910a22016-11-08 10:40:34 -0800251 if (isPressed() || mStayPressed) {
Kenny Guy44cba692016-01-21 19:50:02 +0000252 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700253 } else {
254 d.animateState(FastBitmapDrawable.State.NORMAL);
255 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800257 }
258
259 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700260 public void setOnLongClickListener(OnLongClickListener l) {
261 super.setOnLongClickListener(l);
262 mOnLongClickListener = l;
263 }
264
265 public OnLongClickListener getOnLongClickListener() {
266 return mOnLongClickListener;
267 }
268
269 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800270 public boolean onTouchEvent(MotionEvent event) {
271 // Call the superclass onTouchEvent first, because sometimes it changes the state to
272 // isPressed() on an ACTION_UP
273 boolean result = super.onTouchEvent(event);
274
Mady Melloref044dd2015-06-02 15:35:07 -0700275 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700276 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700277 mLongPressHelper.cancelLongPress();
278 result = true;
279 }
280
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800281 switch (event.getAction()) {
282 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700283 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800284 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
285 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700286 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700287 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800288 }
Winson Chung88f33452012-02-23 15:23:44 -0800289
Mady Melloref044dd2015-06-02 15:35:07 -0700290 // If we're in a stylus button press, don't check for long press.
291 if (!mStylusEventHelper.inStylusButtonPressed()) {
292 mLongPressHelper.postCheckForLongPress();
293 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800294 break;
295 case MotionEvent.ACTION_CANCEL:
296 case MotionEvent.ACTION_UP:
297 // If we've touched down and up on an item, and it's still not "pressed", then
298 // destroy the pressed outline
299 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700300 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800301 }
Winson Chung88f33452012-02-23 15:23:44 -0800302
303 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800304 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400305 case MotionEvent.ACTION_MOVE:
306 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
307 mLongPressHelper.cancelLongPress();
308 }
309 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800310 }
311 return result;
312 }
313
Michael Jurkaddd62e92011-02-16 17:49:14 -0800314 void setStayPressed(boolean stayPressed) {
315 mStayPressed = stayPressed;
316 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700317 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700318 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700319 } else {
320 if (mPressedBackground == null) {
321 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
322 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800323 }
Sunny Goyal508da152014-08-14 10:53:27 -0700324
325 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700326 ViewParent parent = getParent();
327 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
328 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
329 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700330 }
331
332 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800333 }
334
Sunny Goyal508da152014-08-14 10:53:27 -0700335 void clearPressedBackground() {
336 setPressed(false);
337 setStayPressed(false);
338 }
339
340 @Override
341 public boolean onKeyDown(int keyCode, KeyEvent event) {
342 if (super.onKeyDown(keyCode, event)) {
343 // Pre-create shadow so show immediately on click.
344 if (mPressedBackground == null) {
345 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700346 }
Sunny Goyal508da152014-08-14 10:53:27 -0700347 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700348 }
Sunny Goyal508da152014-08-14 10:53:27 -0700349 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800350 }
351
Sunny Goyal508da152014-08-14 10:53:27 -0700352 @Override
353 public boolean onKeyUp(int keyCode, KeyEvent event) {
354 // Unlike touch events, keypress event propagate pressed state change immediately,
355 // without waiting for onClickHandler to execute. Disable pressed state changes here
356 // to avoid flickering.
357 mIgnorePressedStateChange = true;
358 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700359
Sunny Goyal508da152014-08-14 10:53:27 -0700360 mPressedBackground = null;
361 mIgnorePressedStateChange = false;
362 updateIconState();
363 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800364 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800365
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 @Override
367 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700368 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700369 super.draw(canvas);
370 return;
371 }
372
Michael Jurkabdb5c532011-02-01 15:05:06 -0800373 final Drawable background = mBackground;
374 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700375 final int scrollX = getScrollX();
376 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800377
Michael Jurkabdb5c532011-02-01 15:05:06 -0800378 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700379 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800380 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800382
383 if ((scrollX | scrollY) == 0) {
384 background.draw(canvas);
385 } else {
386 canvas.translate(scrollX, scrollY);
387 background.draw(canvas);
388 canvas.translate(-scrollX, -scrollY);
389 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800391
392 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800393 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800394 getPaint().clearShadowLayer();
395 super.draw(canvas);
396 return;
397 }
398
Michael Jurkabdb5c532011-02-01 15:05:06 -0800399 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700400 float density = getResources().getDisplayMetrics().density;
401 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800402 super.draw(canvas);
403 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700404 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
405 getScrollX() + getWidth(),
406 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700407 getPaint().setShadowLayer(
408 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800409 super.draw(canvas);
410 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400412
413 @Override
414 protected void onAttachedToWindow() {
415 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700416
Winson Chung656d11c2010-11-29 17:15:47 -0800417 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700418
Winson Chungb745afb2015-03-02 11:51:23 -0800419 if (mIcon instanceof PreloadIconDrawable) {
420 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700421 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400422 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400423 }
424
425 @Override
Winson1f064272016-07-18 17:18:02 -0700426 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
427 if (mCenterVertically) {
428 Paint.FontMetrics fm = getPaint().getFontMetrics();
429 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
430 (int) Math.ceil(fm.bottom - fm.top);
431 int height = MeasureSpec.getSize(heightMeasureSpec);
432 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
433 getPaddingBottom());
434 }
435 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
436 }
437
438 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400439 protected void onDetachedFromWindow() {
440 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800441 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400442 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700443
Adam Cohen477828c2013-09-20 12:05:49 -0700444 @Override
445 public void setTextColor(int color) {
446 mTextColor = color;
447 super.setTextColor(color);
448 }
449
Adam Cohen96bb7982014-07-07 11:58:56 -0700450 @Override
451 public void setTextColor(ColorStateList colors) {
452 mTextColor = colors.getDefaultColor();
453 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700454 }
455
Winson Chung5f8afe62013-08-12 16:19:28 -0700456 public void setTextVisibility(boolean visible) {
457 Resources res = getResources();
458 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700459 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700460 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700461 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700462 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700463 }
464
Winson Chungaffd7b42010-08-20 15:11:56 -0700465 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800466 public void cancelLongPress() {
467 super.cancelLongPress();
468
469 mLongPressHelper.cancelLongPress();
470 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500471
Sunny Goyal34942622014-08-29 17:20:55 -0700472 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700473 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400474 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700475 final boolean isPromise = info.isPromise();
476 final int progressLevel = isPromise ?
477 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
478 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700479
Sunny Goyalc469aad2015-10-01 11:24:23 -0700480 setContentDescription(progressLevel > 0 ?
481 getContext().getString(R.string.app_downloading_title, info.title,
482 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
483 getContext().getString(R.string.app_waiting_download_title, info.title));
484
Winson Chungb745afb2015-03-02 11:51:23 -0800485 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700486 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800487 if (mIcon instanceof PreloadIconDrawable) {
488 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700489 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800490 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700491 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700492 }
493
494 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700495 if (promiseStateChanged) {
496 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700497 }
Sunny Goyale755d462014-07-22 13:48:29 -0700498 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400499 }
500 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700501
502 private Theme getPreloaderTheme() {
503 Object tag = getTag();
504 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
505 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
506 : R.style.PreloadIcon;
507 Theme theme = sPreloaderThemes.get(style);
508 if (theme == null) {
509 theme = getResources().newTheme();
510 theme.applyStyle(style, true);
511 sPreloaderThemes.put(style, theme);
512 }
513 return theme;
514 }
Winson Chungb745afb2015-03-02 11:51:23 -0800515
516 /**
517 * Sets the icon for this view based on the layout direction.
518 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700519 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800520 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800521 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700522 applyCompoundDrawables(mIcon);
523 }
524
525 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800526 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800527 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800528 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700529 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800530 }
Winson Chungb745afb2015-03-02 11:51:23 -0800531 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700532
Sunny Goyal69b75642015-05-15 17:00:24 -0700533 @Override
534 public void requestLayout() {
535 if (!mDisableRelayout) {
536 super.requestLayout();
537 }
538 }
539
Sunny Goyal34b65272015-03-11 16:56:52 -0700540 /**
541 * Applies the item info if it is same as what the view is pointing to currently.
542 */
543 public void reapplyItemInfo(final ItemInfo info) {
544 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700545 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
546 if (mIcon instanceof FastBitmapDrawable) {
547 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
548 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700549 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700550 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700551
Sunny Goyal34b65272015-03-11 16:56:52 -0700552 if (info instanceof AppInfo) {
553 applyFromApplicationInfo((AppInfo) info);
554 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800555 applyFromShortcutInfo((ShortcutInfo) info);
Sunny Goyal317698b2015-07-29 11:45:41 -0700556 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
557 View folderIcon =
558 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
559 if (folderIcon != null) {
560 folderIcon.invalidate();
561 }
562 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700563 } else if (info instanceof PackageItemInfo) {
564 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700565 }
Winsonc0880492015-08-21 11:16:27 -0700566
567 // If we are reapplying over an old icon, then we should update the new icon to the same
568 // state as the old icon
569 if (mIcon instanceof FastBitmapDrawable) {
570 ((FastBitmapDrawable) mIcon).setState(prevState);
571 }
572
Sunny Goyal69b75642015-05-15 17:00:24 -0700573 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700574 }
575 }
576
577 /**
578 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
579 */
580 public void verifyHighRes() {
581 if (mIconLoadRequest != null) {
582 mIconLoadRequest.cancel();
583 mIconLoadRequest = null;
584 }
585 if (getTag() instanceof AppInfo) {
586 AppInfo info = (AppInfo) getTag();
587 if (info.usingLowResIcon) {
588 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
589 .updateIconInBackground(BubbleTextView.this, info);
590 }
591 } else if (getTag() instanceof ShortcutInfo) {
592 ShortcutInfo info = (ShortcutInfo) getTag();
593 if (info.usingLowResIcon) {
594 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
595 .updateIconInBackground(BubbleTextView.this, info);
596 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700597 } else if (getTag() instanceof PackageItemInfo) {
598 PackageItemInfo info = (PackageItemInfo) getTag();
599 if (info.usingLowResIcon) {
600 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
601 .updateIconInBackground(BubbleTextView.this, info);
602 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700603 }
604 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700605
Winson Chungb1777442015-06-16 13:35:04 -0700606 @Override
Winsonc0880492015-08-21 11:16:27 -0700607 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
608 // We can only set the fast scroll focus state on a FastBitmapDrawable
609 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700610 return;
611 }
612
Winsonc0880492015-08-21 11:16:27 -0700613 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
614 if (animated) {
615 FastBitmapDrawable.State prevState = d.getCurrentState();
616 if (d.animateState(focusState)) {
617 // If the state was updated, then update the view accordingly
618 animate().scaleX(focusState.viewScale)
619 .scaleY(focusState.viewScale)
620 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
621 .setDuration(d.getDurationForStateChange(prevState, focusState))
622 .start();
623 }
624 } else {
625 if (d.setState(focusState)) {
626 // If the state was updated, then update the view accordingly
627 animate().cancel();
628 setScaleX(focusState.viewScale);
629 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700630 }
631 }
632 }
633
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700634 /**
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700635 * Returns true if the view can show custom shortcuts.
636 */
637 public boolean hasDeepShortcuts() {
638 return !mLauncher.getShortcutIdsForItem((ItemInfo) getTag()).isEmpty();
639 }
640
641 /**
Winsonc0880492015-08-21 11:16:27 -0700642 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
643 */
644 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
645 final FastBitmapDrawable.State toState) {
646 switch (toState) {
647 case NORMAL:
648 switch (fromState) {
649 case FAST_SCROLL_HIGHLIGHTED:
650 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
651 }
652 }
653 return 0;
654 }
655
656 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700657 * Interface to be implemented by the grand parent to allow click shadow effect.
658 */
Winsonc0880492015-08-21 11:16:27 -0700659 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700660 void setPressedIcon(BubbleTextView icon, Bitmap background);
661 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662}