blob: a6a1f4e3f62bb283576eed8600b05a49dd754a54 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.graphics.drawable.Drawable;
Sunny Goyal70660032015-05-14 00:07:08 -070029import android.os.Build;
Winson Chung656d11c2010-11-29 17:15:47 -080030import android.util.AttributeSet;
Sunny Goyal95abbb32014-08-04 10:53:22 -070031import android.util.SparseArray;
Winson Chung5f8afe62013-08-12 16:19:28 -070032import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070033import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080034import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070035import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040036import android.view.ViewConfiguration;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070037import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080038import android.widget.TextView;
Sunny Goyal317698b2015-07-29 11:45:41 -070039
Sunny Goyal34b65272015-03-11 16:56:52 -070040import com.android.launcher3.IconCache.IconLoadRequest;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070041import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070042
Sunny Goyalc469aad2015-10-01 11:24:23 -070043import java.text.NumberFormat;
44
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045/**
46 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
47 * because we want to make the bubble taller than the text and TextView's clip is
48 * too aggressive.
49 */
Winson Chungb1777442015-06-16 13:35:04 -070050public class BubbleTextView extends TextView
51 implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070052
Sameer Padala8fd74832014-09-08 16:00:29 -070053 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070054
Sunny Goyal508da152014-08-14 10:53:27 -070055 private static final float SHADOW_LARGE_RADIUS = 4.0f;
56 private static final float SHADOW_SMALL_RADIUS = 1.75f;
57 private static final float SHADOW_Y_OFFSET = 2.0f;
58 private static final int SHADOW_LARGE_COLOUR = 0xDD000000;
59 private static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080060
Sunny Goyaldfaccf62015-05-11 16:30:44 -070061 private static final int DISPLAY_WORKSPACE = 0;
62 private static final int DISPLAY_ALL_APPS = 1;
63
Sunny Goyal53d7ee42015-05-22 12:25:45 -070064 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080065 private Drawable mIcon;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080066 private final Drawable mBackground;
67 private final CheckLongPressHelper mLongPressHelper;
68 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070069 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080070
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080071 private boolean mBackgroundSizeChanged;
72
Sunny Goyal508da152014-08-14 10:53:27 -070073 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080074
Jason Monk02dd7ae2014-04-15 15:23:31 -040075 private float mSlop;
76
Winson Chung93f98ea2015-03-10 16:28:47 -070077 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070078 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080079 private final boolean mLayoutHorizontal;
80 private final int mIconSize;
Winson Chungb745afb2015-03-02 11:51:23 -080081 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070082
Michael Jurkaddd62e92011-02-16 17:49:14 -080083 private boolean mStayPressed;
Sunny Goyal508da152014-08-14 10:53:27 -070084 private boolean mIgnorePressedStateChange;
Sunny Goyal69b75642015-05-15 17:00:24 -070085 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050086
Sunny Goyal34b65272015-03-11 16:56:52 -070087 private IconLoadRequest mIconLoadRequest;
88
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070090 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 }
92
93 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -070094 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 }
96
97 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
98 super(context, attrs, defStyle);
Sunny Goyal53d7ee42015-05-22 12:25:45 -070099 mLauncher = (Launcher) context;
100 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700101
Adam Cohen96bb7982014-07-07 11:58:56 -0700102 TypedArray a = context.obtainStyledAttributes(attrs,
103 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700104 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800105 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700106 mDeferShadowGenerationOnTouch =
107 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700108
109 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
110 int defaultIconSize = grid.iconSizePx;
111 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700112 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700113 } else if (display == DISPLAY_ALL_APPS) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700114 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700115 defaultIconSize = grid.allAppsIconSizePx;
116 }
117
118 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
119 defaultIconSize);
120
Adam Cohen96bb7982014-07-07 11:58:56 -0700121 a.recycle();
122
Sunny Goyal15872da2014-07-08 15:43:54 -0700123 if (mCustomShadowsEnabled) {
124 // Draw the background itself as the parent is drawn twice.
125 mBackground = getBackground();
126 setBackground(null);
127 } else {
128 mBackground = null;
129 }
Winson Chungb745afb2015-03-02 11:51:23 -0800130
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800131 mLongPressHelper = new CheckLongPressHelper(this);
Mady Melloref044dd2015-06-02 15:35:07 -0700132 mStylusEventHelper = new StylusEventHelper(this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800133
134 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
135 if (mCustomShadowsEnabled) {
136 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
137 }
138
139 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 }
141
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700142 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
143 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700144 }
145
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700146 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700147 boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800148 Bitmap b = info.getIcon(iconCache);
149
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700150 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(b);
Winsonc0880492015-08-21 11:16:27 -0700151 if (info.isDisabled != 0) {
152 iconDrawable.setState(FastBitmapDrawable.State.DISABLED);
153 }
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700154 setIcon(iconDrawable, mIconSize);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100155 if (info.contentDescription != null) {
156 setContentDescription(info.contentDescription);
157 }
Sunny Goyal508da152014-08-14 10:53:27 -0700158 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800159 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700160
Sunny Goyal34942622014-08-29 17:20:55 -0700161 if (promiseStateChanged || info.isPromise()) {
162 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500163 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800164 }
165
Sunny Goyal508da152014-08-14 10:53:27 -0700166 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700167 setIcon(mLauncher.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700168 setText(info.title);
169 if (info.contentDescription != null) {
170 setContentDescription(info.contentDescription);
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 Goyal53d7ee42015-05-22 12:25:45 -0700180 setIcon(mLauncher.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700181 setText(info.title);
182 if (info.contentDescription != null) {
183 setContentDescription(info.contentDescription);
184 }
185 // We don't need to check the info since it's not a ShortcutInfo
186 super.setTag(info);
187
188 // Verify high res immediately
189 verifyHighRes();
190 }
191
Winson Chung7501adf2015-06-02 11:24:28 -0700192 /**
193 * Overrides the default long press timeout.
194 */
195 public void setLongPressTimeout(int longPressTimeout) {
196 mLongPressHelper.setLongPressTimeout(longPressTimeout);
197 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 @Override
200 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700201 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 mBackgroundSizeChanged = true;
203 }
204 return super.setFrame(left, top, right, bottom);
205 }
206
207 @Override
208 protected boolean verifyDrawable(Drawable who) {
209 return who == mBackground || super.verifyDrawable(who);
210 }
211
212 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700213 public void setTag(Object tag) {
214 if (tag != null) {
215 LauncherModel.checkItemInfo((ItemInfo) tag);
216 }
217 super.setTag(tag);
218 }
219
220 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700221 public void setPressed(boolean pressed) {
222 super.setPressed(pressed);
223
224 if (!mIgnorePressedStateChange) {
225 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800226 }
Sunny Goyal508da152014-08-14 10:53:27 -0700227 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800228
Winson Chungb745afb2015-03-02 11:51:23 -0800229 /** Returns the icon for this view. */
230 public Drawable getIcon() {
231 return mIcon;
232 }
233
234 /** Returns whether the layout is horizontal. */
235 public boolean isLayoutHorizontal() {
236 return mLayoutHorizontal;
237 }
238
Sunny Goyal508da152014-08-14 10:53:27 -0700239 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800240 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700241 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
242 if (isPressed() || mStayPressed) {
243 d.animateState(FastBitmapDrawable.State.PRESSED);
244 } else {
245 d.animateState(FastBitmapDrawable.State.NORMAL);
246 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800248 }
249
250 @Override
251 public boolean onTouchEvent(MotionEvent event) {
252 // Call the superclass onTouchEvent first, because sometimes it changes the state to
253 // isPressed() on an ACTION_UP
254 boolean result = super.onTouchEvent(event);
255
Mady Melloref044dd2015-06-02 15:35:07 -0700256 // Check for a stylus button press, if it occurs cancel any long press checks.
257 if (mStylusEventHelper.checkAndPerformStylusEvent(event)) {
258 mLongPressHelper.cancelLongPress();
259 result = true;
260 }
261
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800262 switch (event.getAction()) {
263 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700264 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800265 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
266 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700267 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700268 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800269 }
Winson Chung88f33452012-02-23 15:23:44 -0800270
Mady Melloref044dd2015-06-02 15:35:07 -0700271 // If we're in a stylus button press, don't check for long press.
272 if (!mStylusEventHelper.inStylusButtonPressed()) {
273 mLongPressHelper.postCheckForLongPress();
274 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800275 break;
276 case MotionEvent.ACTION_CANCEL:
277 case MotionEvent.ACTION_UP:
278 // If we've touched down and up on an item, and it's still not "pressed", then
279 // destroy the pressed outline
280 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700281 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800282 }
Winson Chung88f33452012-02-23 15:23:44 -0800283
284 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800285 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400286 case MotionEvent.ACTION_MOVE:
287 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
288 mLongPressHelper.cancelLongPress();
289 }
290 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800291 }
292 return result;
293 }
294
Michael Jurkaddd62e92011-02-16 17:49:14 -0800295 void setStayPressed(boolean stayPressed) {
296 mStayPressed = stayPressed;
297 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700298 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700299 } else {
300 if (mPressedBackground == null) {
301 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
302 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800303 }
Sunny Goyal508da152014-08-14 10:53:27 -0700304
305 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700306 ViewParent parent = getParent();
307 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
308 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
309 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700310 }
311
312 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800313 }
314
Sunny Goyal508da152014-08-14 10:53:27 -0700315 void clearPressedBackground() {
316 setPressed(false);
317 setStayPressed(false);
318 }
319
320 @Override
321 public boolean onKeyDown(int keyCode, KeyEvent event) {
322 if (super.onKeyDown(keyCode, event)) {
323 // Pre-create shadow so show immediately on click.
324 if (mPressedBackground == null) {
325 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700326 }
Sunny Goyal508da152014-08-14 10:53:27 -0700327 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700328 }
Sunny Goyal508da152014-08-14 10:53:27 -0700329 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800330 }
331
Sunny Goyal508da152014-08-14 10:53:27 -0700332 @Override
333 public boolean onKeyUp(int keyCode, KeyEvent event) {
334 // Unlike touch events, keypress event propagate pressed state change immediately,
335 // without waiting for onClickHandler to execute. Disable pressed state changes here
336 // to avoid flickering.
337 mIgnorePressedStateChange = true;
338 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700339
Sunny Goyal508da152014-08-14 10:53:27 -0700340 mPressedBackground = null;
341 mIgnorePressedStateChange = false;
342 updateIconState();
343 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800344 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800345
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 @Override
347 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700348 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700349 super.draw(canvas);
350 return;
351 }
352
Michael Jurkabdb5c532011-02-01 15:05:06 -0800353 final Drawable background = mBackground;
354 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700355 final int scrollX = getScrollX();
356 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800357
Michael Jurkabdb5c532011-02-01 15:05:06 -0800358 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700359 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800360 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800362
363 if ((scrollX | scrollY) == 0) {
364 background.draw(canvas);
365 } else {
366 canvas.translate(scrollX, scrollY);
367 background.draw(canvas);
368 canvas.translate(-scrollX, -scrollY);
369 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800371
372 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800373 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800374 getPaint().clearShadowLayer();
375 super.draw(canvas);
376 return;
377 }
378
Michael Jurkabdb5c532011-02-01 15:05:06 -0800379 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800380 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800381 super.draw(canvas);
382 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700383 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
384 getScrollX() + getWidth(),
385 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800386 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800387 super.draw(canvas);
388 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400390
391 @Override
392 protected void onAttachedToWindow() {
393 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700394
Winson Chung656d11c2010-11-29 17:15:47 -0800395 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700396
Winson Chungb745afb2015-03-02 11:51:23 -0800397 if (mIcon instanceof PreloadIconDrawable) {
398 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700399 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400400 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400401 }
402
403 @Override
404 protected void onDetachedFromWindow() {
405 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800406 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400407 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700408
Adam Cohen477828c2013-09-20 12:05:49 -0700409 @Override
410 public void setTextColor(int color) {
411 mTextColor = color;
412 super.setTextColor(color);
413 }
414
Adam Cohen96bb7982014-07-07 11:58:56 -0700415 @Override
416 public void setTextColor(ColorStateList colors) {
417 mTextColor = colors.getDefaultColor();
418 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700419 }
420
Winson Chung5f8afe62013-08-12 16:19:28 -0700421 public void setTextVisibility(boolean visible) {
422 Resources res = getResources();
423 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700424 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700425 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700426 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700427 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700428 }
429
Winson Chungaffd7b42010-08-20 15:11:56 -0700430 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800431 public void cancelLongPress() {
432 super.cancelLongPress();
433
434 mLongPressHelper.cancelLongPress();
435 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500436
Sunny Goyal34942622014-08-29 17:20:55 -0700437 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700438 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400439 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700440 final boolean isPromise = info.isPromise();
441 final int progressLevel = isPromise ?
442 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
443 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700444
Sunny Goyalc469aad2015-10-01 11:24:23 -0700445 setContentDescription(progressLevel > 0 ?
446 getContext().getString(R.string.app_downloading_title, info.title,
447 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
448 getContext().getString(R.string.app_waiting_download_title, info.title));
449
Winson Chungb745afb2015-03-02 11:51:23 -0800450 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700451 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800452 if (mIcon instanceof PreloadIconDrawable) {
453 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700454 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800455 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700456 setIcon(preloadDrawable, mIconSize);
Sunny Goyale755d462014-07-22 13:48:29 -0700457 }
458
459 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700460 if (promiseStateChanged) {
461 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700462 }
Sunny Goyale755d462014-07-22 13:48:29 -0700463 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400464 }
465 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700466
467 private Theme getPreloaderTheme() {
468 Object tag = getTag();
469 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
470 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
471 : R.style.PreloadIcon;
472 Theme theme = sPreloaderThemes.get(style);
473 if (theme == null) {
474 theme = getResources().newTheme();
475 theme.applyStyle(style, true);
476 sPreloaderThemes.put(style, theme);
477 }
478 return theme;
479 }
Winson Chungb745afb2015-03-02 11:51:23 -0800480
481 /**
482 * Sets the icon for this view based on the layout direction.
483 */
Sunny Goyal70660032015-05-14 00:07:08 -0700484 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700485 private Drawable setIcon(Drawable icon, int iconSize) {
Winson Chungb745afb2015-03-02 11:51:23 -0800486 mIcon = icon;
487 if (iconSize != -1) {
488 mIcon.setBounds(0, 0, iconSize, iconSize);
489 }
490 if (mLayoutHorizontal) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700491 if (Utilities.ATLEAST_JB_MR1) {
Sunny Goyal70660032015-05-14 00:07:08 -0700492 setCompoundDrawablesRelative(mIcon, null, null, null);
493 } else {
494 setCompoundDrawables(mIcon, null, null, null);
495 }
Winson Chungb745afb2015-03-02 11:51:23 -0800496 } else {
Sunny Goyal70660032015-05-14 00:07:08 -0700497 setCompoundDrawables(null, mIcon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800498 }
Winson Chungb745afb2015-03-02 11:51:23 -0800499 return icon;
500 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700501
Sunny Goyal69b75642015-05-15 17:00:24 -0700502 @Override
503 public void requestLayout() {
504 if (!mDisableRelayout) {
505 super.requestLayout();
506 }
507 }
508
Sunny Goyal34b65272015-03-11 16:56:52 -0700509 /**
510 * Applies the item info if it is same as what the view is pointing to currently.
511 */
512 public void reapplyItemInfo(final ItemInfo info) {
513 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700514 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
515 if (mIcon instanceof FastBitmapDrawable) {
516 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
517 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700518 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700519 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700520
Sunny Goyal34b65272015-03-11 16:56:52 -0700521 if (info instanceof AppInfo) {
522 applyFromApplicationInfo((AppInfo) info);
523 } else if (info instanceof ShortcutInfo) {
524 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700525 LauncherAppState.getInstance().getIconCache());
Sunny Goyal317698b2015-07-29 11:45:41 -0700526 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
527 View folderIcon =
528 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
529 if (folderIcon != null) {
530 folderIcon.invalidate();
531 }
532 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700533 } else if (info instanceof PackageItemInfo) {
534 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700535 }
Winsonc0880492015-08-21 11:16:27 -0700536
537 // If we are reapplying over an old icon, then we should update the new icon to the same
538 // state as the old icon
539 if (mIcon instanceof FastBitmapDrawable) {
540 ((FastBitmapDrawable) mIcon).setState(prevState);
541 }
542
Sunny Goyal69b75642015-05-15 17:00:24 -0700543 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700544 }
545 }
546
547 /**
548 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
549 */
550 public void verifyHighRes() {
551 if (mIconLoadRequest != null) {
552 mIconLoadRequest.cancel();
553 mIconLoadRequest = null;
554 }
555 if (getTag() instanceof AppInfo) {
556 AppInfo info = (AppInfo) getTag();
557 if (info.usingLowResIcon) {
558 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
559 .updateIconInBackground(BubbleTextView.this, info);
560 }
561 } else if (getTag() instanceof ShortcutInfo) {
562 ShortcutInfo info = (ShortcutInfo) getTag();
563 if (info.usingLowResIcon) {
564 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
565 .updateIconInBackground(BubbleTextView.this, info);
566 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700567 } else if (getTag() instanceof PackageItemInfo) {
568 PackageItemInfo info = (PackageItemInfo) getTag();
569 if (info.usingLowResIcon) {
570 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
571 .updateIconInBackground(BubbleTextView.this, info);
572 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700573 }
574 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700575
Winson Chungb1777442015-06-16 13:35:04 -0700576 @Override
Winsonc0880492015-08-21 11:16:27 -0700577 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
578 // We can only set the fast scroll focus state on a FastBitmapDrawable
579 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700580 return;
581 }
582
Winsonc0880492015-08-21 11:16:27 -0700583 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
584 if (animated) {
585 FastBitmapDrawable.State prevState = d.getCurrentState();
586 if (d.animateState(focusState)) {
587 // If the state was updated, then update the view accordingly
588 animate().scaleX(focusState.viewScale)
589 .scaleY(focusState.viewScale)
590 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
591 .setDuration(d.getDurationForStateChange(prevState, focusState))
592 .start();
593 }
594 } else {
595 if (d.setState(focusState)) {
596 // If the state was updated, then update the view accordingly
597 animate().cancel();
598 setScaleX(focusState.viewScale);
599 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700600 }
601 }
602 }
603
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700604 /**
Winsonc0880492015-08-21 11:16:27 -0700605 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
606 */
607 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
608 final FastBitmapDrawable.State toState) {
609 switch (toState) {
610 case NORMAL:
611 switch (fromState) {
612 case FAST_SCROLL_HIGHLIGHTED:
613 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
614 }
615 }
616 return 0;
617 }
618
619 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700620 * Interface to be implemented by the grand parent to allow click shadow effect.
621 */
Winsonc0880492015-08-21 11:16:27 -0700622 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700623 void setPressedIcon(BubbleTextView icon, Bitmap background);
624 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625}