blob: bb4b2cec1d1367eab55313f03a46b658e65f1797 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal70660032015-05-14 00:07:08 -070019import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080020import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070021import android.content.res.ColorStateList;
Winson Chung656d11c2010-11-29 17:15:47 -080022import android.content.res.Resources;
Sunny Goyal95abbb32014-08-04 10:53:22 -070023import android.content.res.Resources.Theme;
Adam Cohen96bb7982014-07-07 11:58:56 -070024import android.content.res.TypedArray;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080025import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.graphics.Canvas;
Winson1f064272016-07-18 17:18:02 -070027import android.graphics.Paint;
Michael Jurka137142e2011-01-05 20:57:04 -080028import android.graphics.Region;
Winson5fbe0742015-09-30 15:33:00 -070029import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.drawable.Drawable;
Sunny Goyal70660032015-05-14 00:07:08 -070031import android.os.Build;
Winson Chung656d11c2010-11-29 17:15:47 -080032import android.util.AttributeSet;
Sunny Goyal95abbb32014-08-04 10:53:22 -070033import android.util.SparseArray;
Winson Chung5f8afe62013-08-12 16:19:28 -070034import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070035import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080036import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070037import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040038import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080039import android.view.ViewDebug;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070040import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080041import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070042
Sunny Goyal34b65272015-03-11 16:56:52 -070043import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal26119432016-02-18 22:09:23 +000044import com.android.launcher3.folder.FolderIcon;
Sunny Goyal10629b02016-09-01 12:50:11 -070045import com.android.launcher3.graphics.HolographicOutlineHelper;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070046import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070047
Sunny Goyalc469aad2015-10-01 11:24:23 -070048import java.text.NumberFormat;
49
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050/**
51 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
52 * because we want to make the bubble taller than the text and TextView's clip is
53 * too aggressive.
54 */
Winson Chungb1777442015-06-16 13:35:04 -070055public class BubbleTextView extends TextView
56 implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070057
Sameer Padala8fd74832014-09-08 16:00:29 -070058 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070059
Sunny Goyal8f8f3982016-07-25 17:08:38 -070060 // Dimensions in DP
61 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
62 private static final float KEY_SHADOW_RADIUS = 1f;
63 private static final float KEY_SHADOW_OFFSET = 0.5f;
64 private static final int AMBIENT_SHADOW_COLOR = 0x33000000;
65 private static final int KEY_SHADOW_COLOR = 0x66000000;
Winson Chung656d11c2010-11-29 17:15:47 -080066
Sunny Goyaldfaccf62015-05-11 16:30:44 -070067 private static final int DISPLAY_WORKSPACE = 0;
68 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070069 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070070
Sunny Goyal53d7ee42015-05-22 12:25:45 -070071 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080072 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070073 private final boolean mCenterVertically;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080074 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070075 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080076 private final CheckLongPressHelper mLongPressHelper;
77 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070078 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080079
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080080 private boolean mBackgroundSizeChanged;
81
Sunny Goyal508da152014-08-14 10:53:27 -070082 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080083
Jason Monk02dd7ae2014-04-15 15:23:31 -040084 private float mSlop;
85
Winson Chung93f98ea2015-03-10 16:28:47 -070086 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070087 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080088 private final boolean mLayoutHorizontal;
89 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080090 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080091 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070092
Sunny Goyal4ffec482016-02-09 11:28:52 -080093 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -080094 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -080095 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -070096 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -080097 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -070098 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050099
Sunny Goyal34b65272015-03-11 16:56:52 -0700100 private IconLoadRequest mIconLoadRequest;
101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700103 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 }
105
106 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700107 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 }
109
110 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
111 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700112 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700113 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700114
Adam Cohen96bb7982014-07-07 11:58:56 -0700115 TypedArray a = context.obtainStyledAttributes(attrs,
116 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700117 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800118 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700119 mDeferShadowGenerationOnTouch =
120 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700121
122 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
123 int defaultIconSize = grid.iconSizePx;
124 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700125 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700126 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700127 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
128 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700129 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700130 } else if (display == DISPLAY_FOLDER) {
Jon Mirandabf7d8122016-11-03 15:29:29 -0700131 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.folderChildTextSizePx);
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700132 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Jon Mirandabf7d8122016-11-03 15:29:29 -0700133 defaultIconSize = grid.folderChildIconSizePx;
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700134 }
Winson1f064272016-07-18 17:18:02 -0700135 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700136
137 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
138 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700139 a.recycle();
140
Sunny Goyal15872da2014-07-08 15:43:54 -0700141 if (mCustomShadowsEnabled) {
142 // Draw the background itself as the parent is drawn twice.
143 mBackground = getBackground();
144 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700145
146 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
147 float density = getResources().getDisplayMetrics().density;
148 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Sunny Goyal15872da2014-07-08 15:43:54 -0700149 } else {
150 mBackground = null;
151 }
Winson Chungb745afb2015-03-02 11:51:23 -0800152
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800153 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700154 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800155
Sunny Goyal10629b02016-09-01 12:50:11 -0700156 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700157 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 }
159
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700160 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
161 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700162 }
163
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700164 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700165 boolean promiseStateChanged) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700166 applyIconAndLabel(info.getIcon(iconCache), info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800167 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700168 if (promiseStateChanged || info.isPromise()) {
169 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500170 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800171 }
172
Sunny Goyal508da152014-08-14 10:53:27 -0700173 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700174 applyIconAndLabel(info.iconBitmap, info);
175
Winson Chung888b3a12015-03-13 11:14:16 -0700176 // We don't need to check the info since it's not a ShortcutInfo
177 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700178
179 // Verify high res immediately
180 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700181 }
182
Sunny Goyal0e08f162015-05-12 11:32:39 -0700183 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700184 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700185 // We don't need to check the info since it's not a ShortcutInfo
186 super.setTag(info);
187
188 // Verify high res immediately
189 verifyHighRes();
190 }
191
Sunny Goyalf4204382016-07-13 10:46:07 -0700192 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
193 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(icon);
194 if (info.isDisabled()) {
195 iconDrawable.setState(FastBitmapDrawable.State.DISABLED);
196 }
197 setIcon(iconDrawable);
198 setText(info.title);
199 if (info.contentDescription != null) {
200 setContentDescription(info.isDisabled()
201 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
202 : info.contentDescription);
203 }
204 }
205
Winson Chung7501adf2015-06-02 11:24:28 -0700206 /**
Winson5fbe0742015-09-30 15:33:00 -0700207 * Used for measurement only, sets some dummy values on this view.
208 */
209 public void applyDummyInfo() {
210 ColorDrawable d = new ColorDrawable();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700211 setIcon(mLauncher.resizeIconDrawable(d));
Winson5fbe0742015-09-30 15:33:00 -0700212 setText("");
213 }
214
215 /**
Winson Chung7501adf2015-06-02 11:24:28 -0700216 * Overrides the default long press timeout.
217 */
218 public void setLongPressTimeout(int longPressTimeout) {
219 mLongPressHelper.setLongPressTimeout(longPressTimeout);
220 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 @Override
223 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700224 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 mBackgroundSizeChanged = true;
226 }
227 return super.setFrame(left, top, right, bottom);
228 }
229
230 @Override
231 protected boolean verifyDrawable(Drawable who) {
232 return who == mBackground || super.verifyDrawable(who);
233 }
234
235 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700236 public void setTag(Object tag) {
237 if (tag != null) {
238 LauncherModel.checkItemInfo((ItemInfo) tag);
239 }
240 super.setTag(tag);
241 }
242
243 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700244 public void setPressed(boolean pressed) {
245 super.setPressed(pressed);
246
247 if (!mIgnorePressedStateChange) {
248 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800249 }
Sunny Goyal508da152014-08-14 10:53:27 -0700250 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800251
Winson Chungb745afb2015-03-02 11:51:23 -0800252 /** Returns the icon for this view. */
253 public Drawable getIcon() {
254 return mIcon;
255 }
256
257 /** Returns whether the layout is horizontal. */
258 public boolean isLayoutHorizontal() {
259 return mLayoutHorizontal;
260 }
261
Sunny Goyal508da152014-08-14 10:53:27 -0700262 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800263 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700264 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
Kenny Guy44cba692016-01-21 19:50:02 +0000265 if (getTag() instanceof ItemInfo
266 && ((ItemInfo) getTag()).isDisabled()) {
Sunny Goyalcd5b7f92015-10-20 15:17:00 -0700267 d.animateState(FastBitmapDrawable.State.DISABLED);
Kenny Guy44cba692016-01-21 19:50:02 +0000268 } else if (isPressed() || mStayPressed) {
269 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700270 } else {
271 d.animateState(FastBitmapDrawable.State.NORMAL);
272 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800274 }
275
276 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700277 public void setOnLongClickListener(OnLongClickListener l) {
278 super.setOnLongClickListener(l);
279 mOnLongClickListener = l;
280 }
281
282 public OnLongClickListener getOnLongClickListener() {
283 return mOnLongClickListener;
284 }
285
286 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800287 public boolean onTouchEvent(MotionEvent event) {
288 // Call the superclass onTouchEvent first, because sometimes it changes the state to
289 // isPressed() on an ACTION_UP
290 boolean result = super.onTouchEvent(event);
291
Mady Melloref044dd2015-06-02 15:35:07 -0700292 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700293 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700294 mLongPressHelper.cancelLongPress();
295 result = true;
296 }
297
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800298 switch (event.getAction()) {
299 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700300 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800301 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
302 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700303 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700304 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800305 }
Winson Chung88f33452012-02-23 15:23:44 -0800306
Mady Melloref044dd2015-06-02 15:35:07 -0700307 // If we're in a stylus button press, don't check for long press.
308 if (!mStylusEventHelper.inStylusButtonPressed()) {
309 mLongPressHelper.postCheckForLongPress();
310 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800311 break;
312 case MotionEvent.ACTION_CANCEL:
313 case MotionEvent.ACTION_UP:
314 // If we've touched down and up on an item, and it's still not "pressed", then
315 // destroy the pressed outline
316 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700317 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800318 }
Winson Chung88f33452012-02-23 15:23:44 -0800319
320 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800321 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400322 case MotionEvent.ACTION_MOVE:
323 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
324 mLongPressHelper.cancelLongPress();
325 }
326 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800327 }
328 return result;
329 }
330
Michael Jurkaddd62e92011-02-16 17:49:14 -0800331 void setStayPressed(boolean stayPressed) {
332 mStayPressed = stayPressed;
333 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700334 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700335 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700336 } else {
337 if (mPressedBackground == null) {
338 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
339 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800340 }
Sunny Goyal508da152014-08-14 10:53:27 -0700341
342 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700343 ViewParent parent = getParent();
344 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
345 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
346 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700347 }
348
349 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800350 }
351
Sunny Goyal508da152014-08-14 10:53:27 -0700352 void clearPressedBackground() {
353 setPressed(false);
354 setStayPressed(false);
355 }
356
357 @Override
358 public boolean onKeyDown(int keyCode, KeyEvent event) {
359 if (super.onKeyDown(keyCode, event)) {
360 // Pre-create shadow so show immediately on click.
361 if (mPressedBackground == null) {
362 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700363 }
Sunny Goyal508da152014-08-14 10:53:27 -0700364 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700365 }
Sunny Goyal508da152014-08-14 10:53:27 -0700366 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800367 }
368
Sunny Goyal508da152014-08-14 10:53:27 -0700369 @Override
370 public boolean onKeyUp(int keyCode, KeyEvent event) {
371 // Unlike touch events, keypress event propagate pressed state change immediately,
372 // without waiting for onClickHandler to execute. Disable pressed state changes here
373 // to avoid flickering.
374 mIgnorePressedStateChange = true;
375 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700376
Sunny Goyal508da152014-08-14 10:53:27 -0700377 mPressedBackground = null;
378 mIgnorePressedStateChange = false;
379 updateIconState();
380 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800381 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 @Override
384 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700385 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700386 super.draw(canvas);
387 return;
388 }
389
Michael Jurkabdb5c532011-02-01 15:05:06 -0800390 final Drawable background = mBackground;
391 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700392 final int scrollX = getScrollX();
393 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800394
Michael Jurkabdb5c532011-02-01 15:05:06 -0800395 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700396 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800397 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800399
400 if ((scrollX | scrollY) == 0) {
401 background.draw(canvas);
402 } else {
403 canvas.translate(scrollX, scrollY);
404 background.draw(canvas);
405 canvas.translate(-scrollX, -scrollY);
406 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800408
409 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800410 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800411 getPaint().clearShadowLayer();
412 super.draw(canvas);
413 return;
414 }
415
Michael Jurkabdb5c532011-02-01 15:05:06 -0800416 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700417 float density = getResources().getDisplayMetrics().density;
418 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800419 super.draw(canvas);
420 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700421 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
422 getScrollX() + getWidth(),
423 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700424 getPaint().setShadowLayer(
425 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800426 super.draw(canvas);
427 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400429
430 @Override
431 protected void onAttachedToWindow() {
432 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700433
Winson Chung656d11c2010-11-29 17:15:47 -0800434 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700435
Winson Chungb745afb2015-03-02 11:51:23 -0800436 if (mIcon instanceof PreloadIconDrawable) {
437 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700438 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400439 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400440 }
441
442 @Override
Winson1f064272016-07-18 17:18:02 -0700443 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
444 if (mCenterVertically) {
445 Paint.FontMetrics fm = getPaint().getFontMetrics();
446 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
447 (int) Math.ceil(fm.bottom - fm.top);
448 int height = MeasureSpec.getSize(heightMeasureSpec);
449 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
450 getPaddingBottom());
451 }
452 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
453 }
454
455 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400456 protected void onDetachedFromWindow() {
457 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800458 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400459 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700460
Adam Cohen477828c2013-09-20 12:05:49 -0700461 @Override
462 public void setTextColor(int color) {
463 mTextColor = color;
464 super.setTextColor(color);
465 }
466
Adam Cohen96bb7982014-07-07 11:58:56 -0700467 @Override
468 public void setTextColor(ColorStateList colors) {
469 mTextColor = colors.getDefaultColor();
470 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700471 }
472
Winson Chung5f8afe62013-08-12 16:19:28 -0700473 public void setTextVisibility(boolean visible) {
474 Resources res = getResources();
475 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700476 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700477 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700478 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700479 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700480 }
481
Winson Chungaffd7b42010-08-20 15:11:56 -0700482 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800483 public void cancelLongPress() {
484 super.cancelLongPress();
485
486 mLongPressHelper.cancelLongPress();
487 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500488
Sunny Goyal34942622014-08-29 17:20:55 -0700489 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700490 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400491 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700492 final boolean isPromise = info.isPromise();
493 final int progressLevel = isPromise ?
494 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
495 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700496
Sunny Goyalc469aad2015-10-01 11:24:23 -0700497 setContentDescription(progressLevel > 0 ?
498 getContext().getString(R.string.app_downloading_title, info.title,
499 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
500 getContext().getString(R.string.app_waiting_download_title, info.title));
501
Winson Chungb745afb2015-03-02 11:51:23 -0800502 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700503 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800504 if (mIcon instanceof PreloadIconDrawable) {
505 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700506 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800507 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700508 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700509 }
510
511 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700512 if (promiseStateChanged) {
513 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700514 }
Sunny Goyale755d462014-07-22 13:48:29 -0700515 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400516 }
517 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700518
519 private Theme getPreloaderTheme() {
520 Object tag = getTag();
521 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
522 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
523 : R.style.PreloadIcon;
524 Theme theme = sPreloaderThemes.get(style);
525 if (theme == null) {
526 theme = getResources().newTheme();
527 theme.applyStyle(style, true);
528 sPreloaderThemes.put(style, theme);
529 }
530 return theme;
531 }
Winson Chungb745afb2015-03-02 11:51:23 -0800532
533 /**
534 * Sets the icon for this view based on the layout direction.
535 */
Sunny Goyal70660032015-05-14 00:07:08 -0700536 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Tony Wickham377ed3f2016-07-20 15:21:04 -0700537 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800538 mIcon = icon;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700539 if (mIconSize != -1) {
540 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800541 }
Tony Wickham377ed3f2016-07-20 15:21:04 -0700542 applyCompoundDrawables(mIcon);
543 }
544
545 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800546 if (mLayoutHorizontal) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700547 if (Utilities.ATLEAST_JB_MR1) {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700548 setCompoundDrawablesRelative(icon, null, null, null);
Sunny Goyal70660032015-05-14 00:07:08 -0700549 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700550 setCompoundDrawables(icon, null, null, null);
Sunny Goyal70660032015-05-14 00:07:08 -0700551 }
Winson Chungb745afb2015-03-02 11:51:23 -0800552 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700553 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800554 }
Winson Chungb745afb2015-03-02 11:51:23 -0800555 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700556
Sunny Goyal69b75642015-05-15 17:00:24 -0700557 @Override
558 public void requestLayout() {
559 if (!mDisableRelayout) {
560 super.requestLayout();
561 }
562 }
563
Sunny Goyal34b65272015-03-11 16:56:52 -0700564 /**
565 * Applies the item info if it is same as what the view is pointing to currently.
566 */
567 public void reapplyItemInfo(final ItemInfo info) {
568 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700569 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
570 if (mIcon instanceof FastBitmapDrawable) {
571 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
572 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700573 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700574 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700575
Sunny Goyal34b65272015-03-11 16:56:52 -0700576 if (info instanceof AppInfo) {
577 applyFromApplicationInfo((AppInfo) info);
578 } else if (info instanceof ShortcutInfo) {
579 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700580 LauncherAppState.getInstance().getIconCache());
Sunny Goyal317698b2015-07-29 11:45:41 -0700581 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
582 View folderIcon =
583 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
584 if (folderIcon != null) {
585 folderIcon.invalidate();
586 }
587 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700588 } else if (info instanceof PackageItemInfo) {
589 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700590 }
Winsonc0880492015-08-21 11:16:27 -0700591
592 // If we are reapplying over an old icon, then we should update the new icon to the same
593 // state as the old icon
594 if (mIcon instanceof FastBitmapDrawable) {
595 ((FastBitmapDrawable) mIcon).setState(prevState);
596 }
597
Sunny Goyal69b75642015-05-15 17:00:24 -0700598 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700599 }
600 }
601
602 /**
603 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
604 */
605 public void verifyHighRes() {
606 if (mIconLoadRequest != null) {
607 mIconLoadRequest.cancel();
608 mIconLoadRequest = null;
609 }
610 if (getTag() instanceof AppInfo) {
611 AppInfo info = (AppInfo) getTag();
612 if (info.usingLowResIcon) {
613 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
614 .updateIconInBackground(BubbleTextView.this, info);
615 }
616 } else if (getTag() instanceof ShortcutInfo) {
617 ShortcutInfo info = (ShortcutInfo) getTag();
618 if (info.usingLowResIcon) {
619 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
620 .updateIconInBackground(BubbleTextView.this, info);
621 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700622 } else if (getTag() instanceof PackageItemInfo) {
623 PackageItemInfo info = (PackageItemInfo) getTag();
624 if (info.usingLowResIcon) {
625 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
626 .updateIconInBackground(BubbleTextView.this, info);
627 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700628 }
629 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700630
Winson Chungb1777442015-06-16 13:35:04 -0700631 @Override
Winsonc0880492015-08-21 11:16:27 -0700632 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
633 // We can only set the fast scroll focus state on a FastBitmapDrawable
634 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700635 return;
636 }
637
Winsonc0880492015-08-21 11:16:27 -0700638 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
639 if (animated) {
640 FastBitmapDrawable.State prevState = d.getCurrentState();
641 if (d.animateState(focusState)) {
642 // If the state was updated, then update the view accordingly
643 animate().scaleX(focusState.viewScale)
644 .scaleY(focusState.viewScale)
645 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
646 .setDuration(d.getDurationForStateChange(prevState, focusState))
647 .start();
648 }
649 } else {
650 if (d.setState(focusState)) {
651 // If the state was updated, then update the view accordingly
652 animate().cancel();
653 setScaleX(focusState.viewScale);
654 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700655 }
656 }
657 }
658
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700659 /**
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700660 * Returns true if the view can show custom shortcuts.
661 */
662 public boolean hasDeepShortcuts() {
663 return !mLauncher.getShortcutIdsForItem((ItemInfo) getTag()).isEmpty();
664 }
665
666 /**
Winsonc0880492015-08-21 11:16:27 -0700667 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
668 */
669 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
670 final FastBitmapDrawable.State toState) {
671 switch (toState) {
672 case NORMAL:
673 switch (fromState) {
674 case FAST_SCROLL_HIGHLIGHTED:
675 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
676 }
677 }
678 return 0;
679 }
680
681 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700682 * Interface to be implemented by the grand parent to allow click shadow effect.
683 */
Winsonc0880492015-08-21 11:16:27 -0700684 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700685 void setPressedIcon(BubbleTextView icon, Bitmap background);
686 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687}