blob: 2efe31fa02747bdd4179de94c273156fbaa5bbeb [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;
Tony Wickham9a8d11f2017-01-11 09:53:12 -080042import com.android.launcher3.badge.BadgeInfo;
Tony Wickham010d2552017-01-20 08:15:28 -080043import com.android.launcher3.badge.BadgeRenderer;
Sunny Goyal26119432016-02-18 22:09:23 +000044import com.android.launcher3.folder.FolderIcon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -080045import com.android.launcher3.graphics.DrawableFactory;
Sunny Goyal10629b02016-09-01 12:50:11 -070046import com.android.launcher3.graphics.HolographicOutlineHelper;
Tony Wickham9438ed42017-01-20 09:38:25 -080047import com.android.launcher3.graphics.IconPalette;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070048import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070049
Sunny Goyalc469aad2015-10-01 11:24:23 -070050import java.text.NumberFormat;
51
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052/**
53 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
54 * because we want to make the bubble taller than the text and TextView's clip is
55 * too aggressive.
56 */
Winson Chungb1777442015-06-16 13:35:04 -070057public class BubbleTextView extends TextView
Sunny Goyal2d7cca12017-01-03 16:52:43 -080058 implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView, ItemInfoUpdateReceiver {
Sunny Goyal95abbb32014-08-04 10:53:22 -070059
Sameer Padala8fd74832014-09-08 16:00:29 -070060 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070061
Sunny Goyal8f8f3982016-07-25 17:08:38 -070062 // Dimensions in DP
63 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
64 private static final float KEY_SHADOW_RADIUS = 1f;
65 private static final float KEY_SHADOW_OFFSET = 0.5f;
66 private static final int AMBIENT_SHADOW_COLOR = 0x33000000;
67 private static final int KEY_SHADOW_COLOR = 0x66000000;
Winson Chung656d11c2010-11-29 17:15:47 -080068
Sunny Goyaldfaccf62015-05-11 16:30:44 -070069 private static final int DISPLAY_WORKSPACE = 0;
70 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070071 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070072
Sunny Goyal53d7ee42015-05-22 12:25:45 -070073 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080074 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070075 private final boolean mCenterVertically;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080076 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070077 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080078 private final CheckLongPressHelper mLongPressHelper;
79 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070080 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080081
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080082 private boolean mBackgroundSizeChanged;
83
Sunny Goyal508da152014-08-14 10:53:27 -070084 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080085
Jason Monk02dd7ae2014-04-15 15:23:31 -040086 private float mSlop;
87
Winson Chung93f98ea2015-03-10 16:28:47 -070088 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070089 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080090 private final boolean mLayoutHorizontal;
91 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080092 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080093 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070094
Sunny Goyal4ffec482016-02-09 11:28:52 -080095 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -080096 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -080097 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -070098 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -080099 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -0700100 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500101
Sunny Goyal34b65272015-03-11 16:56:52 -0700102 private IconLoadRequest mIconLoadRequest;
103
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700105 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 }
107
108 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700109 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 }
111
112 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
113 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700114 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700115 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700116
Adam Cohen96bb7982014-07-07 11:58:56 -0700117 TypedArray a = context.obtainStyledAttributes(attrs,
118 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700119 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800120 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700121 mDeferShadowGenerationOnTouch =
122 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700123
124 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
125 int defaultIconSize = grid.iconSizePx;
126 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700127 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700128 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700129 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
130 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700131 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700132 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700133 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700134 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700135 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700136 }
Winson1f064272016-07-18 17:18:02 -0700137 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700138
139 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
140 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700141 a.recycle();
142
Sunny Goyal15872da2014-07-08 15:43:54 -0700143 if (mCustomShadowsEnabled) {
144 // Draw the background itself as the parent is drawn twice.
145 mBackground = getBackground();
146 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700147
148 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
149 float density = getResources().getDisplayMetrics().density;
150 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Sunny Goyal15872da2014-07-08 15:43:54 -0700151 } else {
152 mBackground = null;
153 }
Winson Chungb745afb2015-03-02 11:51:23 -0800154
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800155 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700156 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800157
Sunny Goyal10629b02016-09-01 12:50:11 -0700158 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700159 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 }
161
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800162 public void applyFromShortcutInfo(ShortcutInfo info) {
163 applyFromShortcutInfo(info, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700164 }
165
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800166 public void applyFromShortcutInfo(ShortcutInfo info, boolean promiseStateChanged) {
167 applyIconAndLabel(info.iconBitmap, info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800168 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700169 if (promiseStateChanged || info.isPromise()) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800170 applyPromiseState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500171 }
Tony Wickham010d2552017-01-20 08:15:28 -0800172
173 applyBadgeState(info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800174 }
175
Sunny Goyal508da152014-08-14 10:53:27 -0700176 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700177 applyIconAndLabel(info.iconBitmap, info);
178
Winson Chung888b3a12015-03-13 11:14:16 -0700179 // We don't need to check the info since it's not a ShortcutInfo
180 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700181
182 // Verify high res immediately
183 verifyHighRes();
Tony Wickham010d2552017-01-20 08:15:28 -0800184
185 applyBadgeState(info);
Sunny Goyal508da152014-08-14 10:53:27 -0700186 }
187
Sunny Goyal0e08f162015-05-12 11:32:39 -0700188 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700189 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700190 // We don't need to check the info since it's not a ShortcutInfo
191 super.setTag(info);
192
193 // Verify high res immediately
194 verifyHighRes();
195 }
196
Sunny Goyalf4204382016-07-13 10:46:07 -0700197 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800198 FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info);
Tony Wickham6b910a22016-11-08 10:40:34 -0800199 iconDrawable.setIsDisabled(info.isDisabled());
Sunny Goyalf4204382016-07-13 10:46:07 -0700200 setIcon(iconDrawable);
201 setText(info.title);
202 if (info.contentDescription != null) {
203 setContentDescription(info.isDisabled()
204 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
205 : info.contentDescription);
206 }
207 }
208
Winson Chung7501adf2015-06-02 11:24:28 -0700209 /**
210 * Overrides the default long press timeout.
211 */
212 public void setLongPressTimeout(int longPressTimeout) {
213 mLongPressHelper.setLongPressTimeout(longPressTimeout);
214 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 @Override
217 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700218 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 mBackgroundSizeChanged = true;
220 }
221 return super.setFrame(left, top, right, bottom);
222 }
223
224 @Override
225 protected boolean verifyDrawable(Drawable who) {
226 return who == mBackground || super.verifyDrawable(who);
227 }
228
229 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700230 public void setTag(Object tag) {
231 if (tag != null) {
232 LauncherModel.checkItemInfo((ItemInfo) tag);
233 }
234 super.setTag(tag);
235 }
236
237 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700238 public void setPressed(boolean pressed) {
239 super.setPressed(pressed);
240
241 if (!mIgnorePressedStateChange) {
242 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800243 }
Sunny Goyal508da152014-08-14 10:53:27 -0700244 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800245
Winson Chungb745afb2015-03-02 11:51:23 -0800246 /** Returns the icon for this view. */
247 public Drawable getIcon() {
248 return mIcon;
249 }
250
251 /** Returns whether the layout is horizontal. */
252 public boolean isLayoutHorizontal() {
253 return mLayoutHorizontal;
254 }
255
Sunny Goyal508da152014-08-14 10:53:27 -0700256 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800257 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700258 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
Tony Wickham6b910a22016-11-08 10:40:34 -0800259 if (isPressed() || mStayPressed) {
Kenny Guy44cba692016-01-21 19:50:02 +0000260 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700261 } else {
262 d.animateState(FastBitmapDrawable.State.NORMAL);
263 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800265 }
266
267 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700268 public void setOnLongClickListener(OnLongClickListener l) {
269 super.setOnLongClickListener(l);
270 mOnLongClickListener = l;
271 }
272
273 public OnLongClickListener getOnLongClickListener() {
274 return mOnLongClickListener;
275 }
276
277 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800278 public boolean onTouchEvent(MotionEvent event) {
279 // Call the superclass onTouchEvent first, because sometimes it changes the state to
280 // isPressed() on an ACTION_UP
281 boolean result = super.onTouchEvent(event);
282
Mady Melloref044dd2015-06-02 15:35:07 -0700283 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700284 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700285 mLongPressHelper.cancelLongPress();
286 result = true;
287 }
288
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800289 switch (event.getAction()) {
290 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700291 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800292 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
293 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700294 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700295 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800296 }
Winson Chung88f33452012-02-23 15:23:44 -0800297
Mady Melloref044dd2015-06-02 15:35:07 -0700298 // If we're in a stylus button press, don't check for long press.
299 if (!mStylusEventHelper.inStylusButtonPressed()) {
300 mLongPressHelper.postCheckForLongPress();
301 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800302 break;
303 case MotionEvent.ACTION_CANCEL:
304 case MotionEvent.ACTION_UP:
305 // If we've touched down and up on an item, and it's still not "pressed", then
306 // destroy the pressed outline
307 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700308 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800309 }
Winson Chung88f33452012-02-23 15:23:44 -0800310
311 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800312 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400313 case MotionEvent.ACTION_MOVE:
314 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
315 mLongPressHelper.cancelLongPress();
316 }
317 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800318 }
319 return result;
320 }
321
Michael Jurkaddd62e92011-02-16 17:49:14 -0800322 void setStayPressed(boolean stayPressed) {
323 mStayPressed = stayPressed;
324 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700325 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700326 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700327 } else {
328 if (mPressedBackground == null) {
329 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
330 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800331 }
Sunny Goyal508da152014-08-14 10:53:27 -0700332
333 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700334 ViewParent parent = getParent();
335 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
336 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
337 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700338 }
339
340 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800341 }
342
Sunny Goyal508da152014-08-14 10:53:27 -0700343 void clearPressedBackground() {
344 setPressed(false);
345 setStayPressed(false);
346 }
347
348 @Override
349 public boolean onKeyDown(int keyCode, KeyEvent event) {
350 if (super.onKeyDown(keyCode, event)) {
351 // Pre-create shadow so show immediately on click.
352 if (mPressedBackground == null) {
353 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700354 }
Sunny Goyal508da152014-08-14 10:53:27 -0700355 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700356 }
Sunny Goyal508da152014-08-14 10:53:27 -0700357 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800358 }
359
Sunny Goyal508da152014-08-14 10:53:27 -0700360 @Override
361 public boolean onKeyUp(int keyCode, KeyEvent event) {
362 // Unlike touch events, keypress event propagate pressed state change immediately,
363 // without waiting for onClickHandler to execute. Disable pressed state changes here
364 // to avoid flickering.
365 mIgnorePressedStateChange = true;
366 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700367
Sunny Goyal508da152014-08-14 10:53:27 -0700368 mPressedBackground = null;
369 mIgnorePressedStateChange = false;
370 updateIconState();
371 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800372 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 @Override
375 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700376 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700377 super.draw(canvas);
378 return;
379 }
380
Michael Jurkabdb5c532011-02-01 15:05:06 -0800381 final Drawable background = mBackground;
382 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700383 final int scrollX = getScrollX();
384 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800385
Michael Jurkabdb5c532011-02-01 15:05:06 -0800386 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700387 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800388 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800390
391 if ((scrollX | scrollY) == 0) {
392 background.draw(canvas);
393 } else {
394 canvas.translate(scrollX, scrollY);
395 background.draw(canvas);
396 canvas.translate(-scrollX, -scrollY);
397 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800399
400 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800401 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800402 getPaint().clearShadowLayer();
403 super.draw(canvas);
404 return;
405 }
406
Michael Jurkabdb5c532011-02-01 15:05:06 -0800407 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700408 float density = getResources().getDisplayMetrics().density;
409 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800410 super.draw(canvas);
411 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700412 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
413 getScrollX() + getWidth(),
414 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700415 getPaint().setShadowLayer(
416 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800417 super.draw(canvas);
418 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400420
421 @Override
422 protected void onAttachedToWindow() {
423 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700424
Winson Chung656d11c2010-11-29 17:15:47 -0800425 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700426
Winson Chungb745afb2015-03-02 11:51:23 -0800427 if (mIcon instanceof PreloadIconDrawable) {
428 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700429 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400430 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400431 }
432
433 @Override
Winson1f064272016-07-18 17:18:02 -0700434 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
435 if (mCenterVertically) {
436 Paint.FontMetrics fm = getPaint().getFontMetrics();
437 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
438 (int) Math.ceil(fm.bottom - fm.top);
439 int height = MeasureSpec.getSize(heightMeasureSpec);
440 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
441 getPaddingBottom());
442 }
443 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
444 }
445
446 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400447 protected void onDetachedFromWindow() {
448 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800449 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400450 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700451
Adam Cohen477828c2013-09-20 12:05:49 -0700452 @Override
453 public void setTextColor(int color) {
454 mTextColor = color;
455 super.setTextColor(color);
456 }
457
Adam Cohen96bb7982014-07-07 11:58:56 -0700458 @Override
459 public void setTextColor(ColorStateList colors) {
460 mTextColor = colors.getDefaultColor();
461 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700462 }
463
Winson Chung5f8afe62013-08-12 16:19:28 -0700464 public void setTextVisibility(boolean visible) {
465 Resources res = getResources();
466 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700467 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700468 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700469 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700470 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700471 }
472
Winson Chungaffd7b42010-08-20 15:11:56 -0700473 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800474 public void cancelLongPress() {
475 super.cancelLongPress();
476
477 mLongPressHelper.cancelLongPress();
478 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500479
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800480 public void applyPromiseState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700481 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400482 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700483 final boolean isPromise = info.isPromise();
484 final int progressLevel = isPromise ?
485 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
486 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700487
Sunny Goyalc469aad2015-10-01 11:24:23 -0700488 setContentDescription(progressLevel > 0 ?
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800489 getContext().getString(R.string.app_downloading_title, info.title,
490 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
Sunny Goyalc469aad2015-10-01 11:24:23 -0700491 getContext().getString(R.string.app_waiting_download_title, info.title));
492
Winson Chungb745afb2015-03-02 11:51:23 -0800493 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700494 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800495 if (mIcon instanceof PreloadIconDrawable) {
496 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700497 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800498 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700499 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700500 }
501
502 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700503 if (promiseStateChanged) {
504 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700505 }
Sunny Goyale755d462014-07-22 13:48:29 -0700506 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400507 }
508 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700509
Tony Wickham010d2552017-01-20 08:15:28 -0800510 public void applyBadgeState(ItemInfo itemInfo) {
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800511 if (mIcon instanceof FastBitmapDrawable) {
Tony Wickham010d2552017-01-20 08:15:28 -0800512 BadgeInfo badgeInfo = mLauncher.getPopupDataProvider().getBadgeInfoForItem(itemInfo);
Tony Wickham9a8d11f2017-01-11 09:53:12 -0800513 BadgeRenderer badgeRenderer = mLauncher.getDeviceProfile().mBadgeRenderer;
514 ((FastBitmapDrawable) mIcon).applyIconBadge(badgeInfo, badgeRenderer);
515 }
516 }
517
Tony Wickham9438ed42017-01-20 09:38:25 -0800518 public IconPalette getIconPalette() {
519 return mIcon instanceof FastBitmapDrawable ? ((FastBitmapDrawable) mIcon).getIconPalette()
520 : null;
521 }
522
Sunny Goyal95abbb32014-08-04 10:53:22 -0700523 private Theme getPreloaderTheme() {
524 Object tag = getTag();
525 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
526 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
527 : R.style.PreloadIcon;
528 Theme theme = sPreloaderThemes.get(style);
529 if (theme == null) {
530 theme = getResources().newTheme();
531 theme.applyStyle(style, true);
532 sPreloaderThemes.put(style, theme);
533 }
534 return theme;
535 }
Winson Chungb745afb2015-03-02 11:51:23 -0800536
537 /**
538 * Sets the icon for this view based on the layout direction.
539 */
Tony Wickham377ed3f2016-07-20 15:21:04 -0700540 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800541 mIcon = icon;
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800542 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Tony Wickham377ed3f2016-07-20 15:21:04 -0700543 applyCompoundDrawables(mIcon);
544 }
545
546 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800547 if (mLayoutHorizontal) {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800548 setCompoundDrawablesRelative(icon, null, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800549 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700550 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800551 }
Winson Chungb745afb2015-03-02 11:51:23 -0800552 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700553
Sunny Goyal69b75642015-05-15 17:00:24 -0700554 @Override
555 public void requestLayout() {
556 if (!mDisableRelayout) {
557 super.requestLayout();
558 }
559 }
560
Sunny Goyal34b65272015-03-11 16:56:52 -0700561 /**
562 * Applies the item info if it is same as what the view is pointing to currently.
563 */
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800564 @Override
565 public void reapplyItemInfo(ItemInfoWithIcon info) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700566 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700567 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
568 if (mIcon instanceof FastBitmapDrawable) {
569 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
570 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700571 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700572 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700573
Sunny Goyal34b65272015-03-11 16:56:52 -0700574 if (info instanceof AppInfo) {
575 applyFromApplicationInfo((AppInfo) info);
576 } else if (info instanceof ShortcutInfo) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -0800577 applyFromShortcutInfo((ShortcutInfo) info);
Sunny Goyal317698b2015-07-29 11:45:41 -0700578 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
579 View folderIcon =
580 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
581 if (folderIcon != null) {
582 folderIcon.invalidate();
583 }
584 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700585 } else if (info instanceof PackageItemInfo) {
586 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700587 }
Winsonc0880492015-08-21 11:16:27 -0700588
589 // If we are reapplying over an old icon, then we should update the new icon to the same
590 // state as the old icon
591 if (mIcon instanceof FastBitmapDrawable) {
592 ((FastBitmapDrawable) mIcon).setState(prevState);
593 }
594
Sunny Goyal69b75642015-05-15 17:00:24 -0700595 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700596 }
597 }
598
599 /**
600 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
601 */
602 public void verifyHighRes() {
603 if (mIconLoadRequest != null) {
604 mIconLoadRequest.cancel();
605 mIconLoadRequest = null;
606 }
Sunny Goyal2d7cca12017-01-03 16:52:43 -0800607 if (getTag() instanceof ItemInfoWithIcon) {
608 ItemInfoWithIcon info = (ItemInfoWithIcon) getTag();
Sunny Goyal0e08f162015-05-12 11:32:39 -0700609 if (info.usingLowResIcon) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800610 mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
Sunny Goyal0e08f162015-05-12 11:32:39 -0700611 .updateIconInBackground(BubbleTextView.this, info);
612 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700613 }
614 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700615
Winson Chungb1777442015-06-16 13:35:04 -0700616 @Override
Winsonc0880492015-08-21 11:16:27 -0700617 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
618 // We can only set the fast scroll focus state on a FastBitmapDrawable
619 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700620 return;
621 }
622
Winsonc0880492015-08-21 11:16:27 -0700623 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
624 if (animated) {
625 FastBitmapDrawable.State prevState = d.getCurrentState();
626 if (d.animateState(focusState)) {
627 // If the state was updated, then update the view accordingly
628 animate().scaleX(focusState.viewScale)
629 .scaleY(focusState.viewScale)
630 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
631 .setDuration(d.getDurationForStateChange(prevState, focusState))
632 .start();
633 }
634 } else {
635 if (d.setState(focusState)) {
636 // If the state was updated, then update the view accordingly
637 animate().cancel();
638 setScaleX(focusState.viewScale);
639 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700640 }
641 }
642 }
643
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700644 /**
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700645 * Returns true if the view can show custom shortcuts.
646 */
647 public boolean hasDeepShortcuts() {
Tony Wickham010d2552017-01-20 08:15:28 -0800648 return !mLauncher.getPopupDataProvider().getShortcutIdsForItem((ItemInfo) getTag())
649 .isEmpty();
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700650 }
651
652 /**
Winsonc0880492015-08-21 11:16:27 -0700653 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
654 */
655 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
656 final FastBitmapDrawable.State toState) {
657 switch (toState) {
658 case NORMAL:
659 switch (fromState) {
660 case FAST_SCROLL_HIGHLIGHTED:
661 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
662 }
663 }
664 return 0;
665 }
666
667 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700668 * Interface to be implemented by the grand parent to allow click shadow effect.
669 */
Winsonc0880492015-08-21 11:16:27 -0700670 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700671 void setPressedIcon(BubbleTextView icon, Bitmap background);
672 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673}