blob: a294fa538106b391b0c0e7b622781203efb0bb5f [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;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070045import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070046
Sunny Goyalc469aad2015-10-01 11:24:23 -070047import java.text.NumberFormat;
48
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049/**
50 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
51 * because we want to make the bubble taller than the text and TextView's clip is
52 * too aggressive.
53 */
Winson Chungb1777442015-06-16 13:35:04 -070054public class BubbleTextView extends TextView
55 implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070056
Sameer Padala8fd74832014-09-08 16:00:29 -070057 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070058
Sunny Goyal8f8f3982016-07-25 17:08:38 -070059 // Dimensions in DP
60 private static final float AMBIENT_SHADOW_RADIUS = 2.5f;
61 private static final float KEY_SHADOW_RADIUS = 1f;
62 private static final float KEY_SHADOW_OFFSET = 0.5f;
63 private static final int AMBIENT_SHADOW_COLOR = 0x33000000;
64 private static final int KEY_SHADOW_COLOR = 0x66000000;
Winson Chung656d11c2010-11-29 17:15:47 -080065
Sunny Goyaldfaccf62015-05-11 16:30:44 -070066 private static final int DISPLAY_WORKSPACE = 0;
67 private static final int DISPLAY_ALL_APPS = 1;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -070068 private static final int DISPLAY_FOLDER = 2;
Sunny Goyaldfaccf62015-05-11 16:30:44 -070069
Sunny Goyal53d7ee42015-05-22 12:25:45 -070070 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080071 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070072 private final boolean mCenterVertically;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080073 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070074 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080075 private final CheckLongPressHelper mLongPressHelper;
76 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070077 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080078
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080079 private boolean mBackgroundSizeChanged;
80
Sunny Goyal508da152014-08-14 10:53:27 -070081 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080082
Jason Monk02dd7ae2014-04-15 15:23:31 -040083 private float mSlop;
84
Winson Chung93f98ea2015-03-10 16:28:47 -070085 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070086 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080087 private final boolean mLayoutHorizontal;
88 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080089 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080090 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070091
Sunny Goyal4ffec482016-02-09 11:28:52 -080092 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -080093 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -080094 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -070095 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -070097 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050098
Sunny Goyal34b65272015-03-11 16:56:52 -070099 private IconLoadRequest mIconLoadRequest;
100
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700102 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 }
104
105 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700106 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 }
108
109 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
110 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700111 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700112 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700113
Adam Cohen96bb7982014-07-07 11:58:56 -0700114 TypedArray a = context.obtainStyledAttributes(attrs,
115 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700116 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800117 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700118 mDeferShadowGenerationOnTouch =
119 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700120
121 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
122 int defaultIconSize = grid.iconSizePx;
123 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700124 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700125 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700126 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
127 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700128 defaultIconSize = grid.allAppsIconSizePx;
Sunny Goyalbaec6ff2016-09-14 11:26:21 -0700129 } else if (display == DISPLAY_FOLDER) {
130 setCompoundDrawablePadding(grid.folderChildDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700131 }
Winson1f064272016-07-18 17:18:02 -0700132 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700133
134 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
135 defaultIconSize);
Adam Cohen96bb7982014-07-07 11:58:56 -0700136 a.recycle();
137
Sunny Goyal15872da2014-07-08 15:43:54 -0700138 if (mCustomShadowsEnabled) {
139 // Draw the background itself as the parent is drawn twice.
140 mBackground = getBackground();
141 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700142
143 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
144 float density = getResources().getDisplayMetrics().density;
145 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Sunny Goyal15872da2014-07-08 15:43:54 -0700146 } else {
147 mBackground = null;
148 }
Winson Chungb745afb2015-03-02 11:51:23 -0800149
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800150 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700151 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800152
153 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700154 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 }
156
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700157 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
158 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700159 }
160
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700161 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700162 boolean promiseStateChanged) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700163 applyIconAndLabel(info.getIcon(iconCache), 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) {
190 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(icon);
191 if (info.isDisabled()) {
192 iconDrawable.setState(FastBitmapDrawable.State.DISABLED);
193 }
194 setIcon(iconDrawable);
195 setText(info.title);
196 if (info.contentDescription != null) {
197 setContentDescription(info.isDisabled()
198 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
199 : info.contentDescription);
200 }
201 }
202
Winson Chung7501adf2015-06-02 11:24:28 -0700203 /**
Winson5fbe0742015-09-30 15:33:00 -0700204 * Used for measurement only, sets some dummy values on this view.
205 */
206 public void applyDummyInfo() {
207 ColorDrawable d = new ColorDrawable();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700208 setIcon(mLauncher.resizeIconDrawable(d));
Winson5fbe0742015-09-30 15:33:00 -0700209 setText("");
210 }
211
212 /**
Winson Chung7501adf2015-06-02 11:24:28 -0700213 * Overrides the default long press timeout.
214 */
215 public void setLongPressTimeout(int longPressTimeout) {
216 mLongPressHelper.setLongPressTimeout(longPressTimeout);
217 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 @Override
220 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700221 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 mBackgroundSizeChanged = true;
223 }
224 return super.setFrame(left, top, right, bottom);
225 }
226
227 @Override
228 protected boolean verifyDrawable(Drawable who) {
229 return who == mBackground || super.verifyDrawable(who);
230 }
231
232 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700233 public void setTag(Object tag) {
234 if (tag != null) {
235 LauncherModel.checkItemInfo((ItemInfo) tag);
236 }
237 super.setTag(tag);
238 }
239
240 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700241 public void setPressed(boolean pressed) {
242 super.setPressed(pressed);
243
244 if (!mIgnorePressedStateChange) {
245 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800246 }
Sunny Goyal508da152014-08-14 10:53:27 -0700247 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800248
Winson Chungb745afb2015-03-02 11:51:23 -0800249 /** Returns the icon for this view. */
250 public Drawable getIcon() {
251 return mIcon;
252 }
253
254 /** Returns whether the layout is horizontal. */
255 public boolean isLayoutHorizontal() {
256 return mLayoutHorizontal;
257 }
258
Sunny Goyal508da152014-08-14 10:53:27 -0700259 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800260 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700261 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
Kenny Guy44cba692016-01-21 19:50:02 +0000262 if (getTag() instanceof ItemInfo
263 && ((ItemInfo) getTag()).isDisabled()) {
Sunny Goyalcd5b7f92015-10-20 15:17:00 -0700264 d.animateState(FastBitmapDrawable.State.DISABLED);
Kenny Guy44cba692016-01-21 19:50:02 +0000265 } else if (isPressed() || mStayPressed) {
266 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700267 } else {
268 d.animateState(FastBitmapDrawable.State.NORMAL);
269 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800271 }
272
273 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700274 public void setOnLongClickListener(OnLongClickListener l) {
275 super.setOnLongClickListener(l);
276 mOnLongClickListener = l;
277 }
278
279 public OnLongClickListener getOnLongClickListener() {
280 return mOnLongClickListener;
281 }
282
283 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800284 public boolean onTouchEvent(MotionEvent event) {
285 // Call the superclass onTouchEvent first, because sometimes it changes the state to
286 // isPressed() on an ACTION_UP
287 boolean result = super.onTouchEvent(event);
288
Mady Melloref044dd2015-06-02 15:35:07 -0700289 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700290 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700291 mLongPressHelper.cancelLongPress();
292 result = true;
293 }
294
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800295 switch (event.getAction()) {
296 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700297 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800298 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
299 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700300 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700301 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800302 }
Winson Chung88f33452012-02-23 15:23:44 -0800303
Mady Melloref044dd2015-06-02 15:35:07 -0700304 // If we're in a stylus button press, don't check for long press.
305 if (!mStylusEventHelper.inStylusButtonPressed()) {
306 mLongPressHelper.postCheckForLongPress();
307 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800308 break;
309 case MotionEvent.ACTION_CANCEL:
310 case MotionEvent.ACTION_UP:
311 // If we've touched down and up on an item, and it's still not "pressed", then
312 // destroy the pressed outline
313 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700314 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800315 }
Winson Chung88f33452012-02-23 15:23:44 -0800316
317 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800318 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400319 case MotionEvent.ACTION_MOVE:
320 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
321 mLongPressHelper.cancelLongPress();
322 }
323 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800324 }
325 return result;
326 }
327
Michael Jurkaddd62e92011-02-16 17:49:14 -0800328 void setStayPressed(boolean stayPressed) {
329 mStayPressed = stayPressed;
330 if (!stayPressed) {
Sunny Goyal6c651132016-02-22 16:10:44 -0800331 HolographicOutlineHelper.obtain(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700332 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700333 } else {
334 if (mPressedBackground == null) {
335 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
336 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800337 }
Sunny Goyal508da152014-08-14 10:53:27 -0700338
339 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700340 ViewParent parent = getParent();
341 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
342 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
343 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700344 }
345
346 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800347 }
348
Sunny Goyal508da152014-08-14 10:53:27 -0700349 void clearPressedBackground() {
350 setPressed(false);
351 setStayPressed(false);
352 }
353
354 @Override
355 public boolean onKeyDown(int keyCode, KeyEvent event) {
356 if (super.onKeyDown(keyCode, event)) {
357 // Pre-create shadow so show immediately on click.
358 if (mPressedBackground == null) {
359 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700360 }
Sunny Goyal508da152014-08-14 10:53:27 -0700361 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700362 }
Sunny Goyal508da152014-08-14 10:53:27 -0700363 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800364 }
365
Sunny Goyal508da152014-08-14 10:53:27 -0700366 @Override
367 public boolean onKeyUp(int keyCode, KeyEvent event) {
368 // Unlike touch events, keypress event propagate pressed state change immediately,
369 // without waiting for onClickHandler to execute. Disable pressed state changes here
370 // to avoid flickering.
371 mIgnorePressedStateChange = true;
372 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700373
Sunny Goyal508da152014-08-14 10:53:27 -0700374 mPressedBackground = null;
375 mIgnorePressedStateChange = false;
376 updateIconState();
377 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800378 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800379
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 @Override
381 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700382 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700383 super.draw(canvas);
384 return;
385 }
386
Michael Jurkabdb5c532011-02-01 15:05:06 -0800387 final Drawable background = mBackground;
388 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700389 final int scrollX = getScrollX();
390 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800391
Michael Jurkabdb5c532011-02-01 15:05:06 -0800392 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700393 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800394 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800396
397 if ((scrollX | scrollY) == 0) {
398 background.draw(canvas);
399 } else {
400 canvas.translate(scrollX, scrollY);
401 background.draw(canvas);
402 canvas.translate(-scrollX, -scrollY);
403 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800405
406 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800407 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800408 getPaint().clearShadowLayer();
409 super.draw(canvas);
410 return;
411 }
412
Michael Jurkabdb5c532011-02-01 15:05:06 -0800413 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700414 float density = getResources().getDisplayMetrics().density;
415 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800416 super.draw(canvas);
417 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700418 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
419 getScrollX() + getWidth(),
420 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700421 getPaint().setShadowLayer(
422 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800423 super.draw(canvas);
424 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400426
427 @Override
428 protected void onAttachedToWindow() {
429 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700430
Winson Chung656d11c2010-11-29 17:15:47 -0800431 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700432
Winson Chungb745afb2015-03-02 11:51:23 -0800433 if (mIcon instanceof PreloadIconDrawable) {
434 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700435 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400436 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400437 }
438
439 @Override
Winson1f064272016-07-18 17:18:02 -0700440 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
441 if (mCenterVertically) {
442 Paint.FontMetrics fm = getPaint().getFontMetrics();
443 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
444 (int) Math.ceil(fm.bottom - fm.top);
445 int height = MeasureSpec.getSize(heightMeasureSpec);
446 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
447 getPaddingBottom());
448 }
449 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
450 }
451
452 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400453 protected void onDetachedFromWindow() {
454 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800455 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400456 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700457
Adam Cohen477828c2013-09-20 12:05:49 -0700458 @Override
459 public void setTextColor(int color) {
460 mTextColor = color;
461 super.setTextColor(color);
462 }
463
Adam Cohen96bb7982014-07-07 11:58:56 -0700464 @Override
465 public void setTextColor(ColorStateList colors) {
466 mTextColor = colors.getDefaultColor();
467 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700468 }
469
Winson Chung5f8afe62013-08-12 16:19:28 -0700470 public void setTextVisibility(boolean visible) {
471 Resources res = getResources();
472 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700473 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700474 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700475 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700476 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700477 }
478
Winson Chungaffd7b42010-08-20 15:11:56 -0700479 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800480 public void cancelLongPress() {
481 super.cancelLongPress();
482
483 mLongPressHelper.cancelLongPress();
484 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500485
Sunny Goyal34942622014-08-29 17:20:55 -0700486 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700487 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400488 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700489 final boolean isPromise = info.isPromise();
490 final int progressLevel = isPromise ?
491 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
492 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700493
Sunny Goyalc469aad2015-10-01 11:24:23 -0700494 setContentDescription(progressLevel > 0 ?
495 getContext().getString(R.string.app_downloading_title, info.title,
496 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
497 getContext().getString(R.string.app_waiting_download_title, info.title));
498
Winson Chungb745afb2015-03-02 11:51:23 -0800499 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700500 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800501 if (mIcon instanceof PreloadIconDrawable) {
502 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700503 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800504 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700505 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700506 }
507
508 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700509 if (promiseStateChanged) {
510 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700511 }
Sunny Goyale755d462014-07-22 13:48:29 -0700512 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400513 }
514 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700515
516 private Theme getPreloaderTheme() {
517 Object tag = getTag();
518 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
519 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
520 : R.style.PreloadIcon;
521 Theme theme = sPreloaderThemes.get(style);
522 if (theme == null) {
523 theme = getResources().newTheme();
524 theme.applyStyle(style, true);
525 sPreloaderThemes.put(style, theme);
526 }
527 return theme;
528 }
Winson Chungb745afb2015-03-02 11:51:23 -0800529
530 /**
531 * Sets the icon for this view based on the layout direction.
532 */
Sunny Goyal70660032015-05-14 00:07:08 -0700533 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Tony Wickham377ed3f2016-07-20 15:21:04 -0700534 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800535 mIcon = icon;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700536 if (mIconSize != -1) {
537 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800538 }
Tony Wickham377ed3f2016-07-20 15:21:04 -0700539 applyCompoundDrawables(mIcon);
540 }
541
542 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800543 if (mLayoutHorizontal) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700544 if (Utilities.ATLEAST_JB_MR1) {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700545 setCompoundDrawablesRelative(icon, null, null, null);
Sunny Goyal70660032015-05-14 00:07:08 -0700546 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700547 setCompoundDrawables(icon, null, null, null);
Sunny Goyal70660032015-05-14 00:07:08 -0700548 }
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 */
564 public void reapplyItemInfo(final ItemInfo info) {
565 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700566 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
567 if (mIcon instanceof FastBitmapDrawable) {
568 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
569 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700570 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700571 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700572
Sunny Goyal34b65272015-03-11 16:56:52 -0700573 if (info instanceof AppInfo) {
574 applyFromApplicationInfo((AppInfo) info);
575 } else if (info instanceof ShortcutInfo) {
576 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700577 LauncherAppState.getInstance().getIconCache());
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 }
607 if (getTag() instanceof AppInfo) {
608 AppInfo info = (AppInfo) getTag();
609 if (info.usingLowResIcon) {
610 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
611 .updateIconInBackground(BubbleTextView.this, info);
612 }
613 } else if (getTag() instanceof ShortcutInfo) {
614 ShortcutInfo info = (ShortcutInfo) getTag();
615 if (info.usingLowResIcon) {
616 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
617 .updateIconInBackground(BubbleTextView.this, info);
618 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700619 } else if (getTag() instanceof PackageItemInfo) {
620 PackageItemInfo info = (PackageItemInfo) getTag();
621 if (info.usingLowResIcon) {
622 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
623 .updateIconInBackground(BubbleTextView.this, info);
624 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700625 }
626 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700627
Winson Chungb1777442015-06-16 13:35:04 -0700628 @Override
Winsonc0880492015-08-21 11:16:27 -0700629 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
630 // We can only set the fast scroll focus state on a FastBitmapDrawable
631 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700632 return;
633 }
634
Winsonc0880492015-08-21 11:16:27 -0700635 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
636 if (animated) {
637 FastBitmapDrawable.State prevState = d.getCurrentState();
638 if (d.animateState(focusState)) {
639 // If the state was updated, then update the view accordingly
640 animate().scaleX(focusState.viewScale)
641 .scaleY(focusState.viewScale)
642 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
643 .setDuration(d.getDurationForStateChange(prevState, focusState))
644 .start();
645 }
646 } else {
647 if (d.setState(focusState)) {
648 // If the state was updated, then update the view accordingly
649 animate().cancel();
650 setScaleX(focusState.viewScale);
651 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700652 }
653 }
654 }
655
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700656 /**
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700657 * Returns true if the view can show custom shortcuts.
658 */
659 public boolean hasDeepShortcuts() {
660 return !mLauncher.getShortcutIdsForItem((ItemInfo) getTag()).isEmpty();
661 }
662
663 /**
Winsonc0880492015-08-21 11:16:27 -0700664 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
665 */
666 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
667 final FastBitmapDrawable.State toState) {
668 switch (toState) {
669 case NORMAL:
670 switch (fromState) {
671 case FAST_SCROLL_HIGHLIGHTED:
672 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
673 }
674 }
675 return 0;
676 }
677
678 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700679 * Interface to be implemented by the grand parent to allow click shadow effect.
680 */
Winsonc0880492015-08-21 11:16:27 -0700681 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700682 void setPressedIcon(BubbleTextView icon, Bitmap background);
683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684}