blob: 5cb2d4f3175f2aed318341a5c1ae0bcd68cfb6ef [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;
69
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;
129 }
Winson1f064272016-07-18 17:18:02 -0700130 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700131
132 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
133 defaultIconSize);
134
Adam Cohen96bb7982014-07-07 11:58:56 -0700135 a.recycle();
136
Sunny Goyal15872da2014-07-08 15:43:54 -0700137 if (mCustomShadowsEnabled) {
138 // Draw the background itself as the parent is drawn twice.
139 mBackground = getBackground();
140 setBackground(null);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700141
142 // Set shadow layer as the larger shadow to that the textView does not clip the shadow.
143 float density = getResources().getDisplayMetrics().density;
144 setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Sunny Goyal15872da2014-07-08 15:43:54 -0700145 } else {
146 mBackground = null;
147 }
Winson Chungb745afb2015-03-02 11:51:23 -0800148
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800149 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700150 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800151
Sunny Goyal10629b02016-09-01 12:50:11 -0700152 mOutlineHelper = HolographicOutlineHelper.getInstance(getContext());
Sunny Goyalae502842016-06-17 08:43:56 -0700153 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 }
155
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700156 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
157 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700158 }
159
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700160 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700161 boolean promiseStateChanged) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700162 applyIconAndLabel(info.getIcon(iconCache), info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800163 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700164 if (promiseStateChanged || info.isPromise()) {
165 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500166 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800167 }
168
Sunny Goyal508da152014-08-14 10:53:27 -0700169 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700170 applyIconAndLabel(info.iconBitmap, info);
171
Winson Chung888b3a12015-03-13 11:14:16 -0700172 // We don't need to check the info since it's not a ShortcutInfo
173 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700174
175 // Verify high res immediately
176 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700177 }
178
Sunny Goyal0e08f162015-05-12 11:32:39 -0700179 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700180 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700181 // We don't need to check the info since it's not a ShortcutInfo
182 super.setTag(info);
183
184 // Verify high res immediately
185 verifyHighRes();
186 }
187
Sunny Goyalf4204382016-07-13 10:46:07 -0700188 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
189 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(icon);
190 if (info.isDisabled()) {
191 iconDrawable.setState(FastBitmapDrawable.State.DISABLED);
192 }
193 setIcon(iconDrawable);
194 setText(info.title);
195 if (info.contentDescription != null) {
196 setContentDescription(info.isDisabled()
197 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
198 : info.contentDescription);
199 }
200 }
201
Winson Chung7501adf2015-06-02 11:24:28 -0700202 /**
Winson5fbe0742015-09-30 15:33:00 -0700203 * Used for measurement only, sets some dummy values on this view.
204 */
205 public void applyDummyInfo() {
206 ColorDrawable d = new ColorDrawable();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700207 setIcon(mLauncher.resizeIconDrawable(d));
Winson5fbe0742015-09-30 15:33:00 -0700208 setText("");
209 }
210
211 /**
Winson Chung7501adf2015-06-02 11:24:28 -0700212 * Overrides the default long press timeout.
213 */
214 public void setLongPressTimeout(int longPressTimeout) {
215 mLongPressHelper.setLongPressTimeout(longPressTimeout);
216 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 @Override
219 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700220 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 mBackgroundSizeChanged = true;
222 }
223 return super.setFrame(left, top, right, bottom);
224 }
225
226 @Override
227 protected boolean verifyDrawable(Drawable who) {
228 return who == mBackground || super.verifyDrawable(who);
229 }
230
231 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700232 public void setTag(Object tag) {
233 if (tag != null) {
234 LauncherModel.checkItemInfo((ItemInfo) tag);
235 }
236 super.setTag(tag);
237 }
238
239 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700240 public void setPressed(boolean pressed) {
241 super.setPressed(pressed);
242
243 if (!mIgnorePressedStateChange) {
244 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800245 }
Sunny Goyal508da152014-08-14 10:53:27 -0700246 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800247
Winson Chungb745afb2015-03-02 11:51:23 -0800248 /** Returns the icon for this view. */
249 public Drawable getIcon() {
250 return mIcon;
251 }
252
253 /** Returns whether the layout is horizontal. */
254 public boolean isLayoutHorizontal() {
255 return mLayoutHorizontal;
256 }
257
Sunny Goyal508da152014-08-14 10:53:27 -0700258 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800259 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700260 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
Kenny Guy44cba692016-01-21 19:50:02 +0000261 if (getTag() instanceof ItemInfo
262 && ((ItemInfo) getTag()).isDisabled()) {
Sunny Goyalcd5b7f92015-10-20 15:17:00 -0700263 d.animateState(FastBitmapDrawable.State.DISABLED);
Kenny Guy44cba692016-01-21 19:50:02 +0000264 } else if (isPressed() || mStayPressed) {
265 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700266 } else {
267 d.animateState(FastBitmapDrawable.State.NORMAL);
268 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800270 }
271
272 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700273 public void setOnLongClickListener(OnLongClickListener l) {
274 super.setOnLongClickListener(l);
275 mOnLongClickListener = l;
276 }
277
278 public OnLongClickListener getOnLongClickListener() {
279 return mOnLongClickListener;
280 }
281
282 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800283 public boolean onTouchEvent(MotionEvent event) {
284 // Call the superclass onTouchEvent first, because sometimes it changes the state to
285 // isPressed() on an ACTION_UP
286 boolean result = super.onTouchEvent(event);
287
Mady Melloref044dd2015-06-02 15:35:07 -0700288 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700289 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700290 mLongPressHelper.cancelLongPress();
291 result = true;
292 }
293
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800294 switch (event.getAction()) {
295 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700296 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800297 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
298 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700299 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700300 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800301 }
Winson Chung88f33452012-02-23 15:23:44 -0800302
Mady Melloref044dd2015-06-02 15:35:07 -0700303 // If we're in a stylus button press, don't check for long press.
304 if (!mStylusEventHelper.inStylusButtonPressed()) {
305 mLongPressHelper.postCheckForLongPress();
306 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800307 break;
308 case MotionEvent.ACTION_CANCEL:
309 case MotionEvent.ACTION_UP:
310 // If we've touched down and up on an item, and it's still not "pressed", then
311 // destroy the pressed outline
312 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700313 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800314 }
Winson Chung88f33452012-02-23 15:23:44 -0800315
316 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800317 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400318 case MotionEvent.ACTION_MOVE:
319 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
320 mLongPressHelper.cancelLongPress();
321 }
322 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800323 }
324 return result;
325 }
326
Michael Jurkaddd62e92011-02-16 17:49:14 -0800327 void setStayPressed(boolean stayPressed) {
328 mStayPressed = stayPressed;
329 if (!stayPressed) {
Sunny Goyal10629b02016-09-01 12:50:11 -0700330 HolographicOutlineHelper.getInstance(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700331 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700332 } else {
333 if (mPressedBackground == null) {
334 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
335 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800336 }
Sunny Goyal508da152014-08-14 10:53:27 -0700337
338 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700339 ViewParent parent = getParent();
340 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
341 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
342 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700343 }
344
345 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800346 }
347
Sunny Goyal508da152014-08-14 10:53:27 -0700348 void clearPressedBackground() {
349 setPressed(false);
350 setStayPressed(false);
351 }
352
353 @Override
354 public boolean onKeyDown(int keyCode, KeyEvent event) {
355 if (super.onKeyDown(keyCode, event)) {
356 // Pre-create shadow so show immediately on click.
357 if (mPressedBackground == null) {
358 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700359 }
Sunny Goyal508da152014-08-14 10:53:27 -0700360 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700361 }
Sunny Goyal508da152014-08-14 10:53:27 -0700362 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800363 }
364
Sunny Goyal508da152014-08-14 10:53:27 -0700365 @Override
366 public boolean onKeyUp(int keyCode, KeyEvent event) {
367 // Unlike touch events, keypress event propagate pressed state change immediately,
368 // without waiting for onClickHandler to execute. Disable pressed state changes here
369 // to avoid flickering.
370 mIgnorePressedStateChange = true;
371 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700372
Sunny Goyal508da152014-08-14 10:53:27 -0700373 mPressedBackground = null;
374 mIgnorePressedStateChange = false;
375 updateIconState();
376 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800377 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 @Override
380 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700381 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700382 super.draw(canvas);
383 return;
384 }
385
Michael Jurkabdb5c532011-02-01 15:05:06 -0800386 final Drawable background = mBackground;
387 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700388 final int scrollX = getScrollX();
389 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800390
Michael Jurkabdb5c532011-02-01 15:05:06 -0800391 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700392 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800393 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800395
396 if ((scrollX | scrollY) == 0) {
397 background.draw(canvas);
398 } else {
399 canvas.translate(scrollX, scrollY);
400 background.draw(canvas);
401 canvas.translate(-scrollX, -scrollY);
402 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800404
405 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800406 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800407 getPaint().clearShadowLayer();
408 super.draw(canvas);
409 return;
410 }
411
Michael Jurkabdb5c532011-02-01 15:05:06 -0800412 // We enhance the shadow by drawing the shadow twice
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700413 float density = getResources().getDisplayMetrics().density;
414 getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800415 super.draw(canvas);
416 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700417 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
418 getScrollX() + getWidth(),
419 getScrollY() + getHeight(), Region.Op.INTERSECT);
Sunny Goyal8f8f3982016-07-25 17:08:38 -0700420 getPaint().setShadowLayer(
421 density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800422 super.draw(canvas);
423 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400425
426 @Override
427 protected void onAttachedToWindow() {
428 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700429
Winson Chung656d11c2010-11-29 17:15:47 -0800430 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700431
Winson Chungb745afb2015-03-02 11:51:23 -0800432 if (mIcon instanceof PreloadIconDrawable) {
433 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700434 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400435 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400436 }
437
438 @Override
Winson1f064272016-07-18 17:18:02 -0700439 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
440 if (mCenterVertically) {
441 Paint.FontMetrics fm = getPaint().getFontMetrics();
442 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
443 (int) Math.ceil(fm.bottom - fm.top);
444 int height = MeasureSpec.getSize(heightMeasureSpec);
445 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
446 getPaddingBottom());
447 }
448 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
449 }
450
451 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400452 protected void onDetachedFromWindow() {
453 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800454 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400455 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700456
Adam Cohen477828c2013-09-20 12:05:49 -0700457 @Override
458 public void setTextColor(int color) {
459 mTextColor = color;
460 super.setTextColor(color);
461 }
462
Adam Cohen96bb7982014-07-07 11:58:56 -0700463 @Override
464 public void setTextColor(ColorStateList colors) {
465 mTextColor = colors.getDefaultColor();
466 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700467 }
468
Winson Chung5f8afe62013-08-12 16:19:28 -0700469 public void setTextVisibility(boolean visible) {
470 Resources res = getResources();
471 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700472 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700473 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700474 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700475 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700476 }
477
Winson Chungaffd7b42010-08-20 15:11:56 -0700478 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800479 public void cancelLongPress() {
480 super.cancelLongPress();
481
482 mLongPressHelper.cancelLongPress();
483 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500484
Sunny Goyal34942622014-08-29 17:20:55 -0700485 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700486 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400487 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700488 final boolean isPromise = info.isPromise();
489 final int progressLevel = isPromise ?
490 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
491 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700492
Sunny Goyalc469aad2015-10-01 11:24:23 -0700493 setContentDescription(progressLevel > 0 ?
494 getContext().getString(R.string.app_downloading_title, info.title,
495 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
496 getContext().getString(R.string.app_waiting_download_title, info.title));
497
Winson Chungb745afb2015-03-02 11:51:23 -0800498 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700499 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800500 if (mIcon instanceof PreloadIconDrawable) {
501 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700502 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800503 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700504 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700505 }
506
507 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700508 if (promiseStateChanged) {
509 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700510 }
Sunny Goyale755d462014-07-22 13:48:29 -0700511 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400512 }
513 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700514
515 private Theme getPreloaderTheme() {
516 Object tag = getTag();
517 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
518 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
519 : R.style.PreloadIcon;
520 Theme theme = sPreloaderThemes.get(style);
521 if (theme == null) {
522 theme = getResources().newTheme();
523 theme.applyStyle(style, true);
524 sPreloaderThemes.put(style, theme);
525 }
526 return theme;
527 }
Winson Chungb745afb2015-03-02 11:51:23 -0800528
529 /**
530 * Sets the icon for this view based on the layout direction.
531 */
Sunny Goyal70660032015-05-14 00:07:08 -0700532 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Tony Wickham377ed3f2016-07-20 15:21:04 -0700533 private void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800534 mIcon = icon;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700535 if (mIconSize != -1) {
536 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800537 }
Tony Wickham377ed3f2016-07-20 15:21:04 -0700538 applyCompoundDrawables(mIcon);
539 }
540
541 protected void applyCompoundDrawables(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800542 if (mLayoutHorizontal) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700543 if (Utilities.ATLEAST_JB_MR1) {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700544 setCompoundDrawablesRelative(icon, null, null, null);
Sunny Goyal70660032015-05-14 00:07:08 -0700545 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700546 setCompoundDrawables(icon, null, null, null);
Sunny Goyal70660032015-05-14 00:07:08 -0700547 }
Winson Chungb745afb2015-03-02 11:51:23 -0800548 } else {
Tony Wickham377ed3f2016-07-20 15:21:04 -0700549 setCompoundDrawables(null, icon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800550 }
Winson Chungb745afb2015-03-02 11:51:23 -0800551 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700552
Sunny Goyal69b75642015-05-15 17:00:24 -0700553 @Override
554 public void requestLayout() {
555 if (!mDisableRelayout) {
556 super.requestLayout();
557 }
558 }
559
Sunny Goyal34b65272015-03-11 16:56:52 -0700560 /**
561 * Applies the item info if it is same as what the view is pointing to currently.
562 */
563 public void reapplyItemInfo(final ItemInfo info) {
564 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700565 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
566 if (mIcon instanceof FastBitmapDrawable) {
567 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
568 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700569 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700570 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700571
Sunny Goyal34b65272015-03-11 16:56:52 -0700572 if (info instanceof AppInfo) {
573 applyFromApplicationInfo((AppInfo) info);
574 } else if (info instanceof ShortcutInfo) {
575 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700576 LauncherAppState.getInstance().getIconCache());
Sunny Goyal317698b2015-07-29 11:45:41 -0700577 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
578 View folderIcon =
579 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
580 if (folderIcon != null) {
581 folderIcon.invalidate();
582 }
583 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700584 } else if (info instanceof PackageItemInfo) {
585 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700586 }
Winsonc0880492015-08-21 11:16:27 -0700587
588 // If we are reapplying over an old icon, then we should update the new icon to the same
589 // state as the old icon
590 if (mIcon instanceof FastBitmapDrawable) {
591 ((FastBitmapDrawable) mIcon).setState(prevState);
592 }
593
Sunny Goyal69b75642015-05-15 17:00:24 -0700594 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700595 }
596 }
597
598 /**
599 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
600 */
601 public void verifyHighRes() {
602 if (mIconLoadRequest != null) {
603 mIconLoadRequest.cancel();
604 mIconLoadRequest = null;
605 }
606 if (getTag() instanceof AppInfo) {
607 AppInfo info = (AppInfo) getTag();
608 if (info.usingLowResIcon) {
609 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
610 .updateIconInBackground(BubbleTextView.this, info);
611 }
612 } else if (getTag() instanceof ShortcutInfo) {
613 ShortcutInfo info = (ShortcutInfo) getTag();
614 if (info.usingLowResIcon) {
615 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
616 .updateIconInBackground(BubbleTextView.this, info);
617 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700618 } else if (getTag() instanceof PackageItemInfo) {
619 PackageItemInfo info = (PackageItemInfo) getTag();
620 if (info.usingLowResIcon) {
621 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
622 .updateIconInBackground(BubbleTextView.this, info);
623 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700624 }
625 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700626
Winson Chungb1777442015-06-16 13:35:04 -0700627 @Override
Winsonc0880492015-08-21 11:16:27 -0700628 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
629 // We can only set the fast scroll focus state on a FastBitmapDrawable
630 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700631 return;
632 }
633
Winsonc0880492015-08-21 11:16:27 -0700634 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
635 if (animated) {
636 FastBitmapDrawable.State prevState = d.getCurrentState();
637 if (d.animateState(focusState)) {
638 // If the state was updated, then update the view accordingly
639 animate().scaleX(focusState.viewScale)
640 .scaleY(focusState.viewScale)
641 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
642 .setDuration(d.getDurationForStateChange(prevState, focusState))
643 .start();
644 }
645 } else {
646 if (d.setState(focusState)) {
647 // If the state was updated, then update the view accordingly
648 animate().cancel();
649 setScaleX(focusState.viewScale);
650 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700651 }
652 }
653 }
654
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700655 /**
Sunny Goyal3ffa64d2016-07-25 13:54:32 -0700656 * Returns true if the view can show custom shortcuts.
657 */
658 public boolean hasDeepShortcuts() {
659 return !mLauncher.getShortcutIdsForItem((ItemInfo) getTag()).isEmpty();
660 }
661
662 /**
Winsonc0880492015-08-21 11:16:27 -0700663 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
664 */
665 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
666 final FastBitmapDrawable.State toState) {
667 switch (toState) {
668 case NORMAL:
669 switch (fromState) {
670 case FAST_SCROLL_HIGHLIGHTED:
671 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
672 }
673 }
674 return 0;
675 }
676
677 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700678 * Interface to be implemented by the grand parent to allow click shadow effect.
679 */
Winsonc0880492015-08-21 11:16:27 -0700680 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700681 void setPressedIcon(BubbleTextView icon, Bitmap background);
682 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683}