blob: 00ec4c2ffc7e888db5481ef682d11127b16bcfc0 [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 Goyal508da152014-08-14 10:53:27 -070059 private static final float SHADOW_LARGE_RADIUS = 4.0f;
60 private static final float SHADOW_SMALL_RADIUS = 1.75f;
61 private static final float SHADOW_Y_OFFSET = 2.0f;
62 private static final int SHADOW_LARGE_COLOUR = 0xDD000000;
63 private static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080064
Sunny Goyaldfaccf62015-05-11 16:30:44 -070065 private static final int DISPLAY_WORKSPACE = 0;
66 private static final int DISPLAY_ALL_APPS = 1;
67
Sunny Goyal53d7ee42015-05-22 12:25:45 -070068 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080069 private Drawable mIcon;
Winson1f064272016-07-18 17:18:02 -070070 private final boolean mCenterVertically;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080071 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070072 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080073 private final CheckLongPressHelper mLongPressHelper;
74 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070075 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080076
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080077 private boolean mBackgroundSizeChanged;
78
Sunny Goyal508da152014-08-14 10:53:27 -070079 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080080
Jason Monk02dd7ae2014-04-15 15:23:31 -040081 private float mSlop;
82
Winson Chung93f98ea2015-03-10 16:28:47 -070083 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070084 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080085 private final boolean mLayoutHorizontal;
86 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080087 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080088 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070089
Sunny Goyal4ffec482016-02-09 11:28:52 -080090 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -080091 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -080092 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -070093 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -080094 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -070095 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050096
Sunny Goyal34b65272015-03-11 16:56:52 -070097 private IconLoadRequest mIconLoadRequest;
98
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700100 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 }
102
103 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700104 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 }
106
107 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
108 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700109 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700110 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700111
Adam Cohen96bb7982014-07-07 11:58:56 -0700112 TypedArray a = context.obtainStyledAttributes(attrs,
113 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700114 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800115 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700116 mDeferShadowGenerationOnTouch =
117 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700118
119 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
120 int defaultIconSize = grid.iconSizePx;
121 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700122 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700123 } else if (display == DISPLAY_ALL_APPS) {
Winson1f064272016-07-18 17:18:02 -0700124 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
125 setCompoundDrawablePadding(grid.allAppsIconDrawablePaddingPx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700126 defaultIconSize = grid.allAppsIconSizePx;
127 }
Winson1f064272016-07-18 17:18:02 -0700128 mCenterVertically = a.getBoolean(R.styleable.BubbleTextView_centerVertically, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700129
130 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
131 defaultIconSize);
132
Adam Cohen96bb7982014-07-07 11:58:56 -0700133 a.recycle();
134
Sunny Goyal15872da2014-07-08 15:43:54 -0700135 if (mCustomShadowsEnabled) {
136 // Draw the background itself as the parent is drawn twice.
137 mBackground = getBackground();
138 setBackground(null);
139 } else {
140 mBackground = null;
141 }
Winson Chungb745afb2015-03-02 11:51:23 -0800142
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800143 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700144 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800145
146 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
147 if (mCustomShadowsEnabled) {
148 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
149 }
150
Sunny Goyalae502842016-06-17 08:43:56 -0700151 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 }
153
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700154 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
155 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700156 }
157
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700158 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700159 boolean promiseStateChanged) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700160 applyIconAndLabel(info.getIcon(iconCache), info);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800161 setTag(info);
Sunny Goyal34942622014-08-29 17:20:55 -0700162 if (promiseStateChanged || info.isPromise()) {
163 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500164 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800165 }
166
Sunny Goyal508da152014-08-14 10:53:27 -0700167 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700168 applyIconAndLabel(info.iconBitmap, info);
169
Winson Chung888b3a12015-03-13 11:14:16 -0700170 // We don't need to check the info since it's not a ShortcutInfo
171 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700172
173 // Verify high res immediately
174 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700175 }
176
Sunny Goyal0e08f162015-05-12 11:32:39 -0700177 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyalf4204382016-07-13 10:46:07 -0700178 applyIconAndLabel(info.iconBitmap, info);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700179 // We don't need to check the info since it's not a ShortcutInfo
180 super.setTag(info);
181
182 // Verify high res immediately
183 verifyHighRes();
184 }
185
Sunny Goyalf4204382016-07-13 10:46:07 -0700186 private void applyIconAndLabel(Bitmap icon, ItemInfo info) {
187 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(icon);
188 if (info.isDisabled()) {
189 iconDrawable.setState(FastBitmapDrawable.State.DISABLED);
190 }
191 setIcon(iconDrawable);
192 setText(info.title);
193 if (info.contentDescription != null) {
194 setContentDescription(info.isDisabled()
195 ? getContext().getString(R.string.disabled_app_label, info.contentDescription)
196 : info.contentDescription);
197 }
198 }
199
Winson Chung7501adf2015-06-02 11:24:28 -0700200 /**
Winson5fbe0742015-09-30 15:33:00 -0700201 * Used for measurement only, sets some dummy values on this view.
202 */
203 public void applyDummyInfo() {
204 ColorDrawable d = new ColorDrawable();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700205 setIcon(mLauncher.resizeIconDrawable(d));
Winson5fbe0742015-09-30 15:33:00 -0700206 setText("");
207 }
208
209 /**
Winson Chung7501adf2015-06-02 11:24:28 -0700210 * Overrides the default long press timeout.
211 */
212 public void setLongPressTimeout(int longPressTimeout) {
213 mLongPressHelper.setLongPressTimeout(longPressTimeout);
214 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 @Override
217 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700218 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 mBackgroundSizeChanged = true;
220 }
221 return super.setFrame(left, top, right, bottom);
222 }
223
224 @Override
225 protected boolean verifyDrawable(Drawable who) {
226 return who == mBackground || super.verifyDrawable(who);
227 }
228
229 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700230 public void setTag(Object tag) {
231 if (tag != null) {
232 LauncherModel.checkItemInfo((ItemInfo) tag);
233 }
234 super.setTag(tag);
235 }
236
237 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700238 public void setPressed(boolean pressed) {
239 super.setPressed(pressed);
240
241 if (!mIgnorePressedStateChange) {
242 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800243 }
Sunny Goyal508da152014-08-14 10:53:27 -0700244 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800245
Winson Chungb745afb2015-03-02 11:51:23 -0800246 /** Returns the icon for this view. */
247 public Drawable getIcon() {
248 return mIcon;
249 }
250
251 /** Returns whether the layout is horizontal. */
252 public boolean isLayoutHorizontal() {
253 return mLayoutHorizontal;
254 }
255
Sunny Goyal508da152014-08-14 10:53:27 -0700256 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800257 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700258 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
Kenny Guy44cba692016-01-21 19:50:02 +0000259 if (getTag() instanceof ItemInfo
260 && ((ItemInfo) getTag()).isDisabled()) {
Sunny Goyalcd5b7f92015-10-20 15:17:00 -0700261 d.animateState(FastBitmapDrawable.State.DISABLED);
Kenny Guy44cba692016-01-21 19:50:02 +0000262 } else if (isPressed() || mStayPressed) {
263 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700264 } else {
265 d.animateState(FastBitmapDrawable.State.NORMAL);
266 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800268 }
269
270 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700271 public void setOnLongClickListener(OnLongClickListener l) {
272 super.setOnLongClickListener(l);
273 mOnLongClickListener = l;
274 }
275
276 public OnLongClickListener getOnLongClickListener() {
277 return mOnLongClickListener;
278 }
279
280 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800281 public boolean onTouchEvent(MotionEvent event) {
282 // Call the superclass onTouchEvent first, because sometimes it changes the state to
283 // isPressed() on an ACTION_UP
284 boolean result = super.onTouchEvent(event);
285
Mady Melloref044dd2015-06-02 15:35:07 -0700286 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700287 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700288 mLongPressHelper.cancelLongPress();
289 result = true;
290 }
291
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800292 switch (event.getAction()) {
293 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700294 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800295 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
296 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700297 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700298 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800299 }
Winson Chung88f33452012-02-23 15:23:44 -0800300
Mady Melloref044dd2015-06-02 15:35:07 -0700301 // If we're in a stylus button press, don't check for long press.
302 if (!mStylusEventHelper.inStylusButtonPressed()) {
303 mLongPressHelper.postCheckForLongPress();
304 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800305 break;
306 case MotionEvent.ACTION_CANCEL:
307 case MotionEvent.ACTION_UP:
308 // If we've touched down and up on an item, and it's still not "pressed", then
309 // destroy the pressed outline
310 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700311 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800312 }
Winson Chung88f33452012-02-23 15:23:44 -0800313
314 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800315 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400316 case MotionEvent.ACTION_MOVE:
317 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
318 mLongPressHelper.cancelLongPress();
319 }
320 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800321 }
322 return result;
323 }
324
Michael Jurkaddd62e92011-02-16 17:49:14 -0800325 void setStayPressed(boolean stayPressed) {
326 mStayPressed = stayPressed;
327 if (!stayPressed) {
Sunny Goyal6c651132016-02-22 16:10:44 -0800328 HolographicOutlineHelper.obtain(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700329 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700330 } else {
331 if (mPressedBackground == null) {
332 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
333 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800334 }
Sunny Goyal508da152014-08-14 10:53:27 -0700335
336 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700337 ViewParent parent = getParent();
338 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
339 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
340 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700341 }
342
343 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800344 }
345
Sunny Goyal508da152014-08-14 10:53:27 -0700346 void clearPressedBackground() {
347 setPressed(false);
348 setStayPressed(false);
349 }
350
351 @Override
352 public boolean onKeyDown(int keyCode, KeyEvent event) {
353 if (super.onKeyDown(keyCode, event)) {
354 // Pre-create shadow so show immediately on click.
355 if (mPressedBackground == null) {
356 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700357 }
Sunny Goyal508da152014-08-14 10:53:27 -0700358 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700359 }
Sunny Goyal508da152014-08-14 10:53:27 -0700360 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800361 }
362
Sunny Goyal508da152014-08-14 10:53:27 -0700363 @Override
364 public boolean onKeyUp(int keyCode, KeyEvent event) {
365 // Unlike touch events, keypress event propagate pressed state change immediately,
366 // without waiting for onClickHandler to execute. Disable pressed state changes here
367 // to avoid flickering.
368 mIgnorePressedStateChange = true;
369 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700370
Sunny Goyal508da152014-08-14 10:53:27 -0700371 mPressedBackground = null;
372 mIgnorePressedStateChange = false;
373 updateIconState();
374 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800375 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800376
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 @Override
378 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700379 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700380 super.draw(canvas);
381 return;
382 }
383
Michael Jurkabdb5c532011-02-01 15:05:06 -0800384 final Drawable background = mBackground;
385 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700386 final int scrollX = getScrollX();
387 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800388
Michael Jurkabdb5c532011-02-01 15:05:06 -0800389 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700390 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800391 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800393
394 if ((scrollX | scrollY) == 0) {
395 background.draw(canvas);
396 } else {
397 canvas.translate(scrollX, scrollY);
398 background.draw(canvas);
399 canvas.translate(-scrollX, -scrollY);
400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800402
403 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800404 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800405 getPaint().clearShadowLayer();
406 super.draw(canvas);
407 return;
408 }
409
Michael Jurkabdb5c532011-02-01 15:05:06 -0800410 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800411 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800412 super.draw(canvas);
413 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700414 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
415 getScrollX() + getWidth(),
416 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800417 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800418 super.draw(canvas);
419 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400421
422 @Override
423 protected void onAttachedToWindow() {
424 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700425
Winson Chung656d11c2010-11-29 17:15:47 -0800426 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700427
Winson Chungb745afb2015-03-02 11:51:23 -0800428 if (mIcon instanceof PreloadIconDrawable) {
429 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700430 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400431 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400432 }
433
434 @Override
Winson1f064272016-07-18 17:18:02 -0700435 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
436 if (mCenterVertically) {
437 Paint.FontMetrics fm = getPaint().getFontMetrics();
438 int cellHeightPx = mIconSize + getCompoundDrawablePadding() +
439 (int) Math.ceil(fm.bottom - fm.top);
440 int height = MeasureSpec.getSize(heightMeasureSpec);
441 setPadding(getPaddingLeft(), (height - cellHeightPx) / 2, getPaddingRight(),
442 getPaddingBottom());
443 }
444 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
445 }
446
447 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -0400448 protected void onDetachedFromWindow() {
449 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800450 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400451 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700452
Adam Cohen477828c2013-09-20 12:05:49 -0700453 @Override
454 public void setTextColor(int color) {
455 mTextColor = color;
456 super.setTextColor(color);
457 }
458
Adam Cohen96bb7982014-07-07 11:58:56 -0700459 @Override
460 public void setTextColor(ColorStateList colors) {
461 mTextColor = colors.getDefaultColor();
462 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700463 }
464
Winson Chung5f8afe62013-08-12 16:19:28 -0700465 public void setTextVisibility(boolean visible) {
466 Resources res = getResources();
467 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700468 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700469 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700470 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700471 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700472 }
473
Winson Chungaffd7b42010-08-20 15:11:56 -0700474 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800475 public void cancelLongPress() {
476 super.cancelLongPress();
477
478 mLongPressHelper.cancelLongPress();
479 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500480
Sunny Goyal34942622014-08-29 17:20:55 -0700481 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700482 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400483 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700484 final boolean isPromise = info.isPromise();
485 final int progressLevel = isPromise ?
486 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
487 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700488
Sunny Goyalc469aad2015-10-01 11:24:23 -0700489 setContentDescription(progressLevel > 0 ?
490 getContext().getString(R.string.app_downloading_title, info.title,
491 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
492 getContext().getString(R.string.app_waiting_download_title, info.title));
493
Winson Chungb745afb2015-03-02 11:51:23 -0800494 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700495 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800496 if (mIcon instanceof PreloadIconDrawable) {
497 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700498 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800499 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700500 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700501 }
502
503 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700504 if (promiseStateChanged) {
505 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700506 }
Sunny Goyale755d462014-07-22 13:48:29 -0700507 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400508 }
509 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700510
511 private Theme getPreloaderTheme() {
512 Object tag = getTag();
513 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
514 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
515 : R.style.PreloadIcon;
516 Theme theme = sPreloaderThemes.get(style);
517 if (theme == null) {
518 theme = getResources().newTheme();
519 theme.applyStyle(style, true);
520 sPreloaderThemes.put(style, theme);
521 }
522 return theme;
523 }
Winson Chungb745afb2015-03-02 11:51:23 -0800524
525 /**
526 * Sets the icon for this view based on the layout direction.
527 */
Sunny Goyal70660032015-05-14 00:07:08 -0700528 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700529 public void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800530 mIcon = icon;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700531 if (mIconSize != -1) {
532 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800533 }
534 if (mLayoutHorizontal) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700535 if (Utilities.ATLEAST_JB_MR1) {
Sunny Goyal70660032015-05-14 00:07:08 -0700536 setCompoundDrawablesRelative(mIcon, null, null, null);
537 } else {
538 setCompoundDrawables(mIcon, null, null, null);
539 }
Winson Chungb745afb2015-03-02 11:51:23 -0800540 } else {
Sunny Goyal70660032015-05-14 00:07:08 -0700541 setCompoundDrawables(null, mIcon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800542 }
Winson Chungb745afb2015-03-02 11:51:23 -0800543 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700544
Sunny Goyal69b75642015-05-15 17:00:24 -0700545 @Override
546 public void requestLayout() {
547 if (!mDisableRelayout) {
548 super.requestLayout();
549 }
550 }
551
Sunny Goyal34b65272015-03-11 16:56:52 -0700552 /**
553 * Applies the item info if it is same as what the view is pointing to currently.
554 */
555 public void reapplyItemInfo(final ItemInfo info) {
556 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700557 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
558 if (mIcon instanceof FastBitmapDrawable) {
559 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
560 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700561 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700562 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700563
Sunny Goyal34b65272015-03-11 16:56:52 -0700564 if (info instanceof AppInfo) {
565 applyFromApplicationInfo((AppInfo) info);
566 } else if (info instanceof ShortcutInfo) {
567 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700568 LauncherAppState.getInstance().getIconCache());
Sunny Goyal317698b2015-07-29 11:45:41 -0700569 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
570 View folderIcon =
571 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
572 if (folderIcon != null) {
573 folderIcon.invalidate();
574 }
575 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700576 } else if (info instanceof PackageItemInfo) {
577 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700578 }
Winsonc0880492015-08-21 11:16:27 -0700579
580 // If we are reapplying over an old icon, then we should update the new icon to the same
581 // state as the old icon
582 if (mIcon instanceof FastBitmapDrawable) {
583 ((FastBitmapDrawable) mIcon).setState(prevState);
584 }
585
Sunny Goyal69b75642015-05-15 17:00:24 -0700586 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700587 }
588 }
589
590 /**
591 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
592 */
593 public void verifyHighRes() {
594 if (mIconLoadRequest != null) {
595 mIconLoadRequest.cancel();
596 mIconLoadRequest = null;
597 }
598 if (getTag() instanceof AppInfo) {
599 AppInfo info = (AppInfo) getTag();
600 if (info.usingLowResIcon) {
601 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
602 .updateIconInBackground(BubbleTextView.this, info);
603 }
604 } else if (getTag() instanceof ShortcutInfo) {
605 ShortcutInfo info = (ShortcutInfo) getTag();
606 if (info.usingLowResIcon) {
607 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
608 .updateIconInBackground(BubbleTextView.this, info);
609 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700610 } else if (getTag() instanceof PackageItemInfo) {
611 PackageItemInfo info = (PackageItemInfo) getTag();
612 if (info.usingLowResIcon) {
613 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
614 .updateIconInBackground(BubbleTextView.this, info);
615 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700616 }
617 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700618
Winson Chungb1777442015-06-16 13:35:04 -0700619 @Override
Winsonc0880492015-08-21 11:16:27 -0700620 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
621 // We can only set the fast scroll focus state on a FastBitmapDrawable
622 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700623 return;
624 }
625
Winsonc0880492015-08-21 11:16:27 -0700626 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
627 if (animated) {
628 FastBitmapDrawable.State prevState = d.getCurrentState();
629 if (d.animateState(focusState)) {
630 // If the state was updated, then update the view accordingly
631 animate().scaleX(focusState.viewScale)
632 .scaleY(focusState.viewScale)
633 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
634 .setDuration(d.getDurationForStateChange(prevState, focusState))
635 .start();
636 }
637 } else {
638 if (d.setState(focusState)) {
639 // If the state was updated, then update the view accordingly
640 animate().cancel();
641 setScaleX(focusState.viewScale);
642 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700643 }
644 }
645 }
646
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700647 /**
Winsonc0880492015-08-21 11:16:27 -0700648 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
649 */
650 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
651 final FastBitmapDrawable.State toState) {
652 switch (toState) {
653 case NORMAL:
654 switch (fromState) {
655 case FAST_SCROLL_HIGHLIGHTED:
656 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
657 }
658 }
659 return 0;
660 }
661
662 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700663 * Interface to be implemented by the grand parent to allow click shadow effect.
664 */
Winsonc0880492015-08-21 11:16:27 -0700665 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700666 void setPressedIcon(BubbleTextView icon, Bitmap background);
667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668}