blob: ca60d5cbdb0392666a82b2a4448cd81c515651a5 [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;
Michael Jurka137142e2011-01-05 20:57:04 -080027import android.graphics.Region;
Winson5fbe0742015-09-30 15:33:00 -070028import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.graphics.drawable.Drawable;
Sunny Goyal70660032015-05-14 00:07:08 -070030import android.os.Build;
Winson Chung656d11c2010-11-29 17:15:47 -080031import android.util.AttributeSet;
Sunny Goyal95abbb32014-08-04 10:53:22 -070032import android.util.SparseArray;
Winson Chung5f8afe62013-08-12 16:19:28 -070033import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070034import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080035import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070036import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040037import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080038import android.view.ViewDebug;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070039import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080040import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070041
Sunny Goyal34b65272015-03-11 16:56:52 -070042import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal26119432016-02-18 22:09:23 +000043import com.android.launcher3.folder.FolderIcon;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070044import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070045
Sunny Goyalc469aad2015-10-01 11:24:23 -070046import java.text.NumberFormat;
47
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048/**
49 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
50 * because we want to make the bubble taller than the text and TextView's clip is
51 * too aggressive.
52 */
Winson Chungb1777442015-06-16 13:35:04 -070053public class BubbleTextView extends TextView
54 implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070055
Sameer Padala8fd74832014-09-08 16:00:29 -070056 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070057
Sunny Goyal508da152014-08-14 10:53:27 -070058 private static final float SHADOW_LARGE_RADIUS = 4.0f;
59 private static final float SHADOW_SMALL_RADIUS = 1.75f;
60 private static final float SHADOW_Y_OFFSET = 2.0f;
61 private static final int SHADOW_LARGE_COLOUR = 0xDD000000;
62 private static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080063
Sunny Goyaldfaccf62015-05-11 16:30:44 -070064 private static final int DISPLAY_WORKSPACE = 0;
65 private static final int DISPLAY_ALL_APPS = 1;
66
Sunny Goyal53d7ee42015-05-22 12:25:45 -070067 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080068 private Drawable mIcon;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080069 private final Drawable mBackground;
Tony Wickham1bce7fd2016-04-28 17:39:03 -070070 private OnLongClickListener mOnLongClickListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080071 private final CheckLongPressHelper mLongPressHelper;
72 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070073 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080074
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080075 private boolean mBackgroundSizeChanged;
76
Sunny Goyal508da152014-08-14 10:53:27 -070077 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080078
Jason Monk02dd7ae2014-04-15 15:23:31 -040079 private float mSlop;
80
Winson Chung93f98ea2015-03-10 16:28:47 -070081 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070082 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080083 private final boolean mLayoutHorizontal;
84 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080085 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080086 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070087
Sunny Goyal4ffec482016-02-09 11:28:52 -080088 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -080089 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -080090 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -070091 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -080092 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -070093 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050094
Sunny Goyal34b65272015-03-11 16:56:52 -070095 private IconLoadRequest mIconLoadRequest;
96
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070098 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 }
100
101 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700102 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 }
104
105 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
106 super(context, attrs, defStyle);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700107 mLauncher = Launcher.getLauncher(context);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700108 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700109
Adam Cohen96bb7982014-07-07 11:58:56 -0700110 TypedArray a = context.obtainStyledAttributes(attrs,
111 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700112 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800113 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700114 mDeferShadowGenerationOnTouch =
115 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700116
117 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
118 int defaultIconSize = grid.iconSizePx;
119 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700120 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700121 } else if (display == DISPLAY_ALL_APPS) {
Sunny Goyal383e0a22015-12-10 12:28:21 -0800122 setTextSize(TypedValue.COMPLEX_UNIT_SP, grid.allAppsIconTextSizeSp);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700123 defaultIconSize = grid.allAppsIconSizePx;
124 }
125
126 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
127 defaultIconSize);
128
Adam Cohen96bb7982014-07-07 11:58:56 -0700129 a.recycle();
130
Sunny Goyal15872da2014-07-08 15:43:54 -0700131 if (mCustomShadowsEnabled) {
132 // Draw the background itself as the parent is drawn twice.
133 mBackground = getBackground();
134 setBackground(null);
135 } else {
136 mBackground = null;
137 }
Winson Chungb745afb2015-03-02 11:51:23 -0800138
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800139 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700140 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800141
142 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
143 if (mCustomShadowsEnabled) {
144 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
145 }
146
Sunny Goyalae502842016-06-17 08:43:56 -0700147 setAccessibilityDelegate(mLauncher.getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 }
149
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700150 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
151 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700152 }
153
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700154 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700155 boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800156 Bitmap b = info.getIcon(iconCache);
157
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700158 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(b);
Kenny Guy44cba692016-01-21 19:50:02 +0000159 if (info.isDisabled()) {
Winsonc0880492015-08-21 11:16:27 -0700160 iconDrawable.setState(FastBitmapDrawable.State.DISABLED);
161 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700162 setIcon(iconDrawable);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100163 if (info.contentDescription != null) {
164 setContentDescription(info.contentDescription);
165 }
Sunny Goyal508da152014-08-14 10:53:27 -0700166 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800167 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700168
Sunny Goyal34942622014-08-29 17:20:55 -0700169 if (promiseStateChanged || info.isPromise()) {
170 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500171 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800172 }
173
Sunny Goyal508da152014-08-14 10:53:27 -0700174 public void applyFromApplicationInfo(AppInfo info) {
Kenny Guy44cba692016-01-21 19:50:02 +0000175 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(info.iconBitmap);
176 if (info.isDisabled()) {
177 iconDrawable.setState(FastBitmapDrawable.State.DISABLED);
178 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700179 setIcon(iconDrawable);
Sunny Goyal508da152014-08-14 10:53:27 -0700180 setText(info.title);
181 if (info.contentDescription != null) {
182 setContentDescription(info.contentDescription);
183 }
Winson Chung888b3a12015-03-13 11:14:16 -0700184 // We don't need to check the info since it's not a ShortcutInfo
185 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700186
187 // Verify high res immediately
188 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700189 }
190
Sunny Goyal0e08f162015-05-12 11:32:39 -0700191 public void applyFromPackageItemInfo(PackageItemInfo info) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700192 setIcon(mLauncher.createIconDrawable(info.iconBitmap));
Sunny Goyal0e08f162015-05-12 11:32:39 -0700193 setText(info.title);
194 if (info.contentDescription != null) {
195 setContentDescription(info.contentDescription);
196 }
197 // We don't need to check the info since it's not a ShortcutInfo
198 super.setTag(info);
199
200 // Verify high res immediately
201 verifyHighRes();
202 }
203
Winson Chung7501adf2015-06-02 11:24:28 -0700204 /**
Winson5fbe0742015-09-30 15:33:00 -0700205 * Used for measurement only, sets some dummy values on this view.
206 */
207 public void applyDummyInfo() {
208 ColorDrawable d = new ColorDrawable();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700209 setIcon(mLauncher.resizeIconDrawable(d));
Winson5fbe0742015-09-30 15:33:00 -0700210 setText("");
211 }
212
213 /**
Winson Chung7501adf2015-06-02 11:24:28 -0700214 * Overrides the default long press timeout.
215 */
216 public void setLongPressTimeout(int longPressTimeout) {
217 mLongPressHelper.setLongPressTimeout(longPressTimeout);
218 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700219
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 @Override
221 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700222 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 mBackgroundSizeChanged = true;
224 }
225 return super.setFrame(left, top, right, bottom);
226 }
227
228 @Override
229 protected boolean verifyDrawable(Drawable who) {
230 return who == mBackground || super.verifyDrawable(who);
231 }
232
233 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700234 public void setTag(Object tag) {
235 if (tag != null) {
236 LauncherModel.checkItemInfo((ItemInfo) tag);
237 }
238 super.setTag(tag);
239 }
240
241 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700242 public void setPressed(boolean pressed) {
243 super.setPressed(pressed);
244
245 if (!mIgnorePressedStateChange) {
246 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800247 }
Sunny Goyal508da152014-08-14 10:53:27 -0700248 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800249
Winson Chungb745afb2015-03-02 11:51:23 -0800250 /** Returns the icon for this view. */
251 public Drawable getIcon() {
252 return mIcon;
253 }
254
255 /** Returns whether the layout is horizontal. */
256 public boolean isLayoutHorizontal() {
257 return mLayoutHorizontal;
258 }
259
Sunny Goyal508da152014-08-14 10:53:27 -0700260 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800261 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700262 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
Kenny Guy44cba692016-01-21 19:50:02 +0000263 if (getTag() instanceof ItemInfo
264 && ((ItemInfo) getTag()).isDisabled()) {
Sunny Goyalcd5b7f92015-10-20 15:17:00 -0700265 d.animateState(FastBitmapDrawable.State.DISABLED);
Kenny Guy44cba692016-01-21 19:50:02 +0000266 } else if (isPressed() || mStayPressed) {
267 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700268 } else {
269 d.animateState(FastBitmapDrawable.State.NORMAL);
270 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800272 }
273
274 @Override
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700275 public void setOnLongClickListener(OnLongClickListener l) {
276 super.setOnLongClickListener(l);
277 mOnLongClickListener = l;
278 }
279
280 public OnLongClickListener getOnLongClickListener() {
281 return mOnLongClickListener;
282 }
283
284 @Override
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800285 public boolean onTouchEvent(MotionEvent event) {
286 // Call the superclass onTouchEvent first, because sometimes it changes the state to
287 // isPressed() on an ACTION_UP
288 boolean result = super.onTouchEvent(event);
289
Mady Melloref044dd2015-06-02 15:35:07 -0700290 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700291 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700292 mLongPressHelper.cancelLongPress();
293 result = true;
294 }
295
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800296 switch (event.getAction()) {
297 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700298 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800299 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
300 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700301 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700302 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800303 }
Winson Chung88f33452012-02-23 15:23:44 -0800304
Mady Melloref044dd2015-06-02 15:35:07 -0700305 // If we're in a stylus button press, don't check for long press.
306 if (!mStylusEventHelper.inStylusButtonPressed()) {
307 mLongPressHelper.postCheckForLongPress();
308 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800309 break;
310 case MotionEvent.ACTION_CANCEL:
311 case MotionEvent.ACTION_UP:
312 // If we've touched down and up on an item, and it's still not "pressed", then
313 // destroy the pressed outline
314 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700315 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800316 }
Winson Chung88f33452012-02-23 15:23:44 -0800317
318 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800319 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400320 case MotionEvent.ACTION_MOVE:
321 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
322 mLongPressHelper.cancelLongPress();
323 }
324 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800325 }
326 return result;
327 }
328
Michael Jurkaddd62e92011-02-16 17:49:14 -0800329 void setStayPressed(boolean stayPressed) {
330 mStayPressed = stayPressed;
331 if (!stayPressed) {
Sunny Goyal6c651132016-02-22 16:10:44 -0800332 HolographicOutlineHelper.obtain(getContext()).recycleShadowBitmap(mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700333 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700334 } else {
335 if (mPressedBackground == null) {
336 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
337 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800338 }
Sunny Goyal508da152014-08-14 10:53:27 -0700339
340 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700341 ViewParent parent = getParent();
342 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
343 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
344 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700345 }
346
347 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800348 }
349
Sunny Goyal508da152014-08-14 10:53:27 -0700350 void clearPressedBackground() {
351 setPressed(false);
352 setStayPressed(false);
353 }
354
355 @Override
356 public boolean onKeyDown(int keyCode, KeyEvent event) {
357 if (super.onKeyDown(keyCode, event)) {
358 // Pre-create shadow so show immediately on click.
359 if (mPressedBackground == null) {
360 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700361 }
Sunny Goyal508da152014-08-14 10:53:27 -0700362 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700363 }
Sunny Goyal508da152014-08-14 10:53:27 -0700364 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800365 }
366
Sunny Goyal508da152014-08-14 10:53:27 -0700367 @Override
368 public boolean onKeyUp(int keyCode, KeyEvent event) {
369 // Unlike touch events, keypress event propagate pressed state change immediately,
370 // without waiting for onClickHandler to execute. Disable pressed state changes here
371 // to avoid flickering.
372 mIgnorePressedStateChange = true;
373 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700374
Sunny Goyal508da152014-08-14 10:53:27 -0700375 mPressedBackground = null;
376 mIgnorePressedStateChange = false;
377 updateIconState();
378 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800379 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800380
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 @Override
382 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700383 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700384 super.draw(canvas);
385 return;
386 }
387
Michael Jurkabdb5c532011-02-01 15:05:06 -0800388 final Drawable background = mBackground;
389 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700390 final int scrollX = getScrollX();
391 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800392
Michael Jurkabdb5c532011-02-01 15:05:06 -0800393 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700394 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800395 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800397
398 if ((scrollX | scrollY) == 0) {
399 background.draw(canvas);
400 } else {
401 canvas.translate(scrollX, scrollY);
402 background.draw(canvas);
403 canvas.translate(-scrollX, -scrollY);
404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800406
407 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800408 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800409 getPaint().clearShadowLayer();
410 super.draw(canvas);
411 return;
412 }
413
Michael Jurkabdb5c532011-02-01 15:05:06 -0800414 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800415 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
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);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800421 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
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
439 protected void onDetachedFromWindow() {
440 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800441 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400442 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700443
Adam Cohen477828c2013-09-20 12:05:49 -0700444 @Override
445 public void setTextColor(int color) {
446 mTextColor = color;
447 super.setTextColor(color);
448 }
449
Adam Cohen96bb7982014-07-07 11:58:56 -0700450 @Override
451 public void setTextColor(ColorStateList colors) {
452 mTextColor = colors.getDefaultColor();
453 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700454 }
455
Winson Chung5f8afe62013-08-12 16:19:28 -0700456 public void setTextVisibility(boolean visible) {
457 Resources res = getResources();
458 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700459 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700460 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700461 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700462 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700463 }
464
Winson Chungaffd7b42010-08-20 15:11:56 -0700465 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800466 public void cancelLongPress() {
467 super.cancelLongPress();
468
469 mLongPressHelper.cancelLongPress();
470 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500471
Sunny Goyal34942622014-08-29 17:20:55 -0700472 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700473 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400474 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700475 final boolean isPromise = info.isPromise();
476 final int progressLevel = isPromise ?
477 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
478 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700479
Sunny Goyalc469aad2015-10-01 11:24:23 -0700480 setContentDescription(progressLevel > 0 ?
481 getContext().getString(R.string.app_downloading_title, info.title,
482 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
483 getContext().getString(R.string.app_waiting_download_title, info.title));
484
Winson Chungb745afb2015-03-02 11:51:23 -0800485 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700486 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800487 if (mIcon instanceof PreloadIconDrawable) {
488 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700489 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800490 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700491 setIcon(preloadDrawable);
Sunny Goyale755d462014-07-22 13:48:29 -0700492 }
493
494 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700495 if (promiseStateChanged) {
496 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700497 }
Sunny Goyale755d462014-07-22 13:48:29 -0700498 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400499 }
500 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700501
502 private Theme getPreloaderTheme() {
503 Object tag = getTag();
504 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
505 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
506 : R.style.PreloadIcon;
507 Theme theme = sPreloaderThemes.get(style);
508 if (theme == null) {
509 theme = getResources().newTheme();
510 theme.applyStyle(style, true);
511 sPreloaderThemes.put(style, theme);
512 }
513 return theme;
514 }
Winson Chungb745afb2015-03-02 11:51:23 -0800515
516 /**
517 * Sets the icon for this view based on the layout direction.
518 */
Sunny Goyal70660032015-05-14 00:07:08 -0700519 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700520 public void setIcon(Drawable icon) {
Winson Chungb745afb2015-03-02 11:51:23 -0800521 mIcon = icon;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700522 if (mIconSize != -1) {
523 mIcon.setBounds(0, 0, mIconSize, mIconSize);
Winson Chungb745afb2015-03-02 11:51:23 -0800524 }
525 if (mLayoutHorizontal) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700526 if (Utilities.ATLEAST_JB_MR1) {
Sunny Goyal70660032015-05-14 00:07:08 -0700527 setCompoundDrawablesRelative(mIcon, null, null, null);
528 } else {
529 setCompoundDrawables(mIcon, null, null, null);
530 }
Winson Chungb745afb2015-03-02 11:51:23 -0800531 } else {
Sunny Goyal70660032015-05-14 00:07:08 -0700532 setCompoundDrawables(null, mIcon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800533 }
Winson Chungb745afb2015-03-02 11:51:23 -0800534 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700535
Sunny Goyal69b75642015-05-15 17:00:24 -0700536 @Override
537 public void requestLayout() {
538 if (!mDisableRelayout) {
539 super.requestLayout();
540 }
541 }
542
Sunny Goyal34b65272015-03-11 16:56:52 -0700543 /**
544 * Applies the item info if it is same as what the view is pointing to currently.
545 */
546 public void reapplyItemInfo(final ItemInfo info) {
547 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700548 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
549 if (mIcon instanceof FastBitmapDrawable) {
550 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
551 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700552 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700553 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700554
Sunny Goyal34b65272015-03-11 16:56:52 -0700555 if (info instanceof AppInfo) {
556 applyFromApplicationInfo((AppInfo) info);
557 } else if (info instanceof ShortcutInfo) {
558 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700559 LauncherAppState.getInstance().getIconCache());
Sunny Goyal317698b2015-07-29 11:45:41 -0700560 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
561 View folderIcon =
562 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
563 if (folderIcon != null) {
564 folderIcon.invalidate();
565 }
566 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700567 } else if (info instanceof PackageItemInfo) {
568 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700569 }
Winsonc0880492015-08-21 11:16:27 -0700570
571 // If we are reapplying over an old icon, then we should update the new icon to the same
572 // state as the old icon
573 if (mIcon instanceof FastBitmapDrawable) {
574 ((FastBitmapDrawable) mIcon).setState(prevState);
575 }
576
Sunny Goyal69b75642015-05-15 17:00:24 -0700577 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700578 }
579 }
580
581 /**
582 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
583 */
584 public void verifyHighRes() {
585 if (mIconLoadRequest != null) {
586 mIconLoadRequest.cancel();
587 mIconLoadRequest = null;
588 }
589 if (getTag() instanceof AppInfo) {
590 AppInfo info = (AppInfo) getTag();
591 if (info.usingLowResIcon) {
592 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
593 .updateIconInBackground(BubbleTextView.this, info);
594 }
595 } else if (getTag() instanceof ShortcutInfo) {
596 ShortcutInfo info = (ShortcutInfo) getTag();
597 if (info.usingLowResIcon) {
598 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
599 .updateIconInBackground(BubbleTextView.this, info);
600 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700601 } else if (getTag() instanceof PackageItemInfo) {
602 PackageItemInfo info = (PackageItemInfo) getTag();
603 if (info.usingLowResIcon) {
604 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
605 .updateIconInBackground(BubbleTextView.this, info);
606 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700607 }
608 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700609
Winson Chungb1777442015-06-16 13:35:04 -0700610 @Override
Winsonc0880492015-08-21 11:16:27 -0700611 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
612 // We can only set the fast scroll focus state on a FastBitmapDrawable
613 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700614 return;
615 }
616
Winsonc0880492015-08-21 11:16:27 -0700617 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
618 if (animated) {
619 FastBitmapDrawable.State prevState = d.getCurrentState();
620 if (d.animateState(focusState)) {
621 // If the state was updated, then update the view accordingly
622 animate().scaleX(focusState.viewScale)
623 .scaleY(focusState.viewScale)
624 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
625 .setDuration(d.getDurationForStateChange(prevState, focusState))
626 .start();
627 }
628 } else {
629 if (d.setState(focusState)) {
630 // If the state was updated, then update the view accordingly
631 animate().cancel();
632 setScaleX(focusState.viewScale);
633 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700634 }
635 }
636 }
637
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700638 /**
Winsonc0880492015-08-21 11:16:27 -0700639 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
640 */
641 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
642 final FastBitmapDrawable.State toState) {
643 switch (toState) {
644 case NORMAL:
645 switch (fromState) {
646 case FAST_SCROLL_HIGHLIGHTED:
647 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
648 }
649 }
650 return 0;
651 }
652
653 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700654 * Interface to be implemented by the grand parent to allow click shadow effect.
655 */
Winsonc0880492015-08-21 11:16:27 -0700656 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700657 void setPressedIcon(BubbleTextView icon, Bitmap background);
658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659}