blob: 6a13f3d696b920f3bb2a6294f391bfb47f791bfb [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;
Jason Monk02dd7ae2014-04-15 15:23:31 -040035import android.view.ViewConfiguration;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070036import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080037import android.widget.TextView;
Sunny Goyal34b65272015-03-11 16:56:52 -070038import com.android.launcher3.IconCache.IconLoadRequest;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070039import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070040
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041/**
42 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
43 * because we want to make the bubble taller than the text and TextView's clip is
44 * too aggressive.
45 */
Michael Jurka08ee7702011-08-11 16:53:35 -070046public class BubbleTextView extends TextView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070047
Sameer Padala8fd74832014-09-08 16:00:29 -070048 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070049
Sunny Goyal508da152014-08-14 10:53:27 -070050 private static final float SHADOW_LARGE_RADIUS = 4.0f;
51 private static final float SHADOW_SMALL_RADIUS = 1.75f;
52 private static final float SHADOW_Y_OFFSET = 2.0f;
53 private static final int SHADOW_LARGE_COLOUR = 0xDD000000;
54 private static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080055 static final float PADDING_V = 3.0f;
56
Sunny Goyaldfaccf62015-05-11 16:30:44 -070057 private static final int DISPLAY_WORKSPACE = 0;
58 private static final int DISPLAY_ALL_APPS = 1;
59
Sunny Goyal53d7ee42015-05-22 12:25:45 -070060 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080061 private Drawable mIcon;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080062 private final Drawable mBackground;
63 private final CheckLongPressHelper mLongPressHelper;
64 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070065 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080066
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080067 private boolean mBackgroundSizeChanged;
68
Sunny Goyal508da152014-08-14 10:53:27 -070069 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080070
Jason Monk02dd7ae2014-04-15 15:23:31 -040071 private float mSlop;
72
Winson Chung93f98ea2015-03-10 16:28:47 -070073 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070074 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080075 private final boolean mLayoutHorizontal;
76 private final int mIconSize;
Winson Chungb745afb2015-03-02 11:51:23 -080077 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070078
Michael Jurkaddd62e92011-02-16 17:49:14 -080079 private boolean mStayPressed;
Sunny Goyal508da152014-08-14 10:53:27 -070080 private boolean mIgnorePressedStateChange;
Sunny Goyal69b75642015-05-15 17:00:24 -070081 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050082
Sunny Goyal34b65272015-03-11 16:56:52 -070083 private IconLoadRequest mIconLoadRequest;
84
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070086 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 }
88
89 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -070090 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 }
92
93 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
94 super(context, attrs, defStyle);
Sunny Goyal53d7ee42015-05-22 12:25:45 -070095 mLauncher = (Launcher) context;
96 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -070097
Adam Cohen96bb7982014-07-07 11:58:56 -070098 TypedArray a = context.obtainStyledAttributes(attrs,
99 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700100 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800101 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700102 mDeferShadowGenerationOnTouch =
103 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700104
105 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
106 int defaultIconSize = grid.iconSizePx;
107 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700108 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700109 } else if (display == DISPLAY_ALL_APPS) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700110 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700111 defaultIconSize = grid.allAppsIconSizePx;
112 }
113
114 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
115 defaultIconSize);
116
Adam Cohen96bb7982014-07-07 11:58:56 -0700117 a.recycle();
118
Sunny Goyal15872da2014-07-08 15:43:54 -0700119 if (mCustomShadowsEnabled) {
120 // Draw the background itself as the parent is drawn twice.
121 mBackground = getBackground();
122 setBackground(null);
123 } else {
124 mBackground = null;
125 }
Winson Chungb745afb2015-03-02 11:51:23 -0800126
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800127 mLongPressHelper = new CheckLongPressHelper(this);
Mady Melloref044dd2015-06-02 15:35:07 -0700128 mStylusEventHelper = new StylusEventHelper(this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800129
130 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
131 if (mCustomShadowsEnabled) {
132 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
133 }
134
135 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 }
137
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700138 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
139 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700140 }
141
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700142 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700143 boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800144 Bitmap b = info.getIcon(iconCache);
145
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700146 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(b);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700147 iconDrawable.setGhostModeEnabled(info.isDisabled != 0);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700148
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700149 setIcon(iconDrawable, mIconSize);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100150 if (info.contentDescription != null) {
151 setContentDescription(info.contentDescription);
152 }
Sunny Goyal508da152014-08-14 10:53:27 -0700153 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800154 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700155
Sunny Goyal34942622014-08-29 17:20:55 -0700156 if (promiseStateChanged || info.isPromise()) {
157 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500158 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800159 }
160
Sunny Goyal508da152014-08-14 10:53:27 -0700161 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700162 setIcon(mLauncher.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700163 setText(info.title);
164 if (info.contentDescription != null) {
165 setContentDescription(info.contentDescription);
166 }
Winson Chung888b3a12015-03-13 11:14:16 -0700167 // We don't need to check the info since it's not a ShortcutInfo
168 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700169
170 // Verify high res immediately
171 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700172 }
173
Sunny Goyal0e08f162015-05-12 11:32:39 -0700174 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700175 setIcon(mLauncher.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700176 setText(info.title);
177 if (info.contentDescription != null) {
178 setContentDescription(info.contentDescription);
179 }
180 // We don't need to check the info since it's not a ShortcutInfo
181 super.setTag(info);
182
183 // Verify high res immediately
184 verifyHighRes();
185 }
186
Winson Chung7501adf2015-06-02 11:24:28 -0700187 /**
188 * Overrides the default long press timeout.
189 */
190 public void setLongPressTimeout(int longPressTimeout) {
191 mLongPressHelper.setLongPressTimeout(longPressTimeout);
192 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 @Override
195 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700196 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 mBackgroundSizeChanged = true;
198 }
199 return super.setFrame(left, top, right, bottom);
200 }
201
202 @Override
203 protected boolean verifyDrawable(Drawable who) {
204 return who == mBackground || super.verifyDrawable(who);
205 }
206
207 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700208 public void setTag(Object tag) {
209 if (tag != null) {
210 LauncherModel.checkItemInfo((ItemInfo) tag);
211 }
212 super.setTag(tag);
213 }
214
215 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700216 public void setPressed(boolean pressed) {
217 super.setPressed(pressed);
218
219 if (!mIgnorePressedStateChange) {
220 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800221 }
Sunny Goyal508da152014-08-14 10:53:27 -0700222 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800223
Winson Chungb745afb2015-03-02 11:51:23 -0800224 /** Returns the icon for this view. */
225 public Drawable getIcon() {
226 return mIcon;
227 }
228
229 /** Returns whether the layout is horizontal. */
230 public boolean isLayoutHorizontal() {
231 return mLayoutHorizontal;
232 }
233
Sunny Goyal508da152014-08-14 10:53:27 -0700234 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800235 if (mIcon instanceof FastBitmapDrawable) {
236 ((FastBitmapDrawable) mIcon).setPressed(isPressed() || mStayPressed);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800238 }
239
240 @Override
241 public boolean onTouchEvent(MotionEvent event) {
242 // Call the superclass onTouchEvent first, because sometimes it changes the state to
243 // isPressed() on an ACTION_UP
244 boolean result = super.onTouchEvent(event);
245
Mady Melloref044dd2015-06-02 15:35:07 -0700246 // Check for a stylus button press, if it occurs cancel any long press checks.
247 if (mStylusEventHelper.checkAndPerformStylusEvent(event)) {
248 mLongPressHelper.cancelLongPress();
249 result = true;
250 }
251
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800252 switch (event.getAction()) {
253 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700254 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800255 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
256 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700257 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700258 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800259 }
Winson Chung88f33452012-02-23 15:23:44 -0800260
Mady Melloref044dd2015-06-02 15:35:07 -0700261 // If we're in a stylus button press, don't check for long press.
262 if (!mStylusEventHelper.inStylusButtonPressed()) {
263 mLongPressHelper.postCheckForLongPress();
264 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800265 break;
266 case MotionEvent.ACTION_CANCEL:
267 case MotionEvent.ACTION_UP:
268 // If we've touched down and up on an item, and it's still not "pressed", then
269 // destroy the pressed outline
270 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700271 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800272 }
Winson Chung88f33452012-02-23 15:23:44 -0800273
274 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800275 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400276 case MotionEvent.ACTION_MOVE:
277 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
278 mLongPressHelper.cancelLongPress();
279 }
280 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800281 }
282 return result;
283 }
284
Michael Jurkaddd62e92011-02-16 17:49:14 -0800285 void setStayPressed(boolean stayPressed) {
286 mStayPressed = stayPressed;
287 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700288 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700289 } else {
290 if (mPressedBackground == null) {
291 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
292 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800293 }
Sunny Goyal508da152014-08-14 10:53:27 -0700294
295 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700296 ViewParent parent = getParent();
297 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
298 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
299 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700300 }
301
302 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800303 }
304
Sunny Goyal508da152014-08-14 10:53:27 -0700305 void clearPressedBackground() {
306 setPressed(false);
307 setStayPressed(false);
308 }
309
310 @Override
311 public boolean onKeyDown(int keyCode, KeyEvent event) {
312 if (super.onKeyDown(keyCode, event)) {
313 // Pre-create shadow so show immediately on click.
314 if (mPressedBackground == null) {
315 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700316 }
Sunny Goyal508da152014-08-14 10:53:27 -0700317 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700318 }
Sunny Goyal508da152014-08-14 10:53:27 -0700319 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800320 }
321
Sunny Goyal508da152014-08-14 10:53:27 -0700322 @Override
323 public boolean onKeyUp(int keyCode, KeyEvent event) {
324 // Unlike touch events, keypress event propagate pressed state change immediately,
325 // without waiting for onClickHandler to execute. Disable pressed state changes here
326 // to avoid flickering.
327 mIgnorePressedStateChange = true;
328 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700329
Sunny Goyal508da152014-08-14 10:53:27 -0700330 mPressedBackground = null;
331 mIgnorePressedStateChange = false;
332 updateIconState();
333 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800334 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800335
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 @Override
337 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700338 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700339 super.draw(canvas);
340 return;
341 }
342
Michael Jurkabdb5c532011-02-01 15:05:06 -0800343 final Drawable background = mBackground;
344 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700345 final int scrollX = getScrollX();
346 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800347
Michael Jurkabdb5c532011-02-01 15:05:06 -0800348 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700349 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800350 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800352
353 if ((scrollX | scrollY) == 0) {
354 background.draw(canvas);
355 } else {
356 canvas.translate(scrollX, scrollY);
357 background.draw(canvas);
358 canvas.translate(-scrollX, -scrollY);
359 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800361
362 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800363 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800364 getPaint().clearShadowLayer();
365 super.draw(canvas);
366 return;
367 }
368
Michael Jurkabdb5c532011-02-01 15:05:06 -0800369 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800370 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800371 super.draw(canvas);
372 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700373 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
374 getScrollX() + getWidth(),
375 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800376 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800377 super.draw(canvas);
378 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380
381 @Override
382 protected void onAttachedToWindow() {
383 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700384
Winson Chung656d11c2010-11-29 17:15:47 -0800385 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700386
Winson Chungb745afb2015-03-02 11:51:23 -0800387 if (mIcon instanceof PreloadIconDrawable) {
388 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700389 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400390 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400391 }
392
393 @Override
394 protected void onDetachedFromWindow() {
395 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800396 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400397 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700398
Adam Cohen477828c2013-09-20 12:05:49 -0700399 @Override
400 public void setTextColor(int color) {
401 mTextColor = color;
402 super.setTextColor(color);
403 }
404
Adam Cohen96bb7982014-07-07 11:58:56 -0700405 @Override
406 public void setTextColor(ColorStateList colors) {
407 mTextColor = colors.getDefaultColor();
408 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700409 }
410
Winson Chung5f8afe62013-08-12 16:19:28 -0700411 public void setTextVisibility(boolean visible) {
412 Resources res = getResources();
413 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700414 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700415 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700416 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700417 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700418 }
419
Winson Chungaffd7b42010-08-20 15:11:56 -0700420 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800421 public void cancelLongPress() {
422 super.cancelLongPress();
423
424 mLongPressHelper.cancelLongPress();
425 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500426
Sunny Goyal34942622014-08-29 17:20:55 -0700427 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700428 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400429 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700430 final boolean isPromise = info.isPromise();
431 final int progressLevel = isPromise ?
432 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
433 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700434
Winson Chungb745afb2015-03-02 11:51:23 -0800435 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700436 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800437 if (mIcon instanceof PreloadIconDrawable) {
438 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700439 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800440 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700441 setIcon(preloadDrawable, mIconSize);
Sunny Goyale755d462014-07-22 13:48:29 -0700442 }
443
444 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700445 if (promiseStateChanged) {
446 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700447 }
Sunny Goyale755d462014-07-22 13:48:29 -0700448 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400449 }
450 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700451
452 private Theme getPreloaderTheme() {
453 Object tag = getTag();
454 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
455 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
456 : R.style.PreloadIcon;
457 Theme theme = sPreloaderThemes.get(style);
458 if (theme == null) {
459 theme = getResources().newTheme();
460 theme.applyStyle(style, true);
461 sPreloaderThemes.put(style, theme);
462 }
463 return theme;
464 }
Winson Chungb745afb2015-03-02 11:51:23 -0800465
466 /**
467 * Sets the icon for this view based on the layout direction.
468 */
Sunny Goyal70660032015-05-14 00:07:08 -0700469 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700470 private Drawable setIcon(Drawable icon, int iconSize) {
Winson Chungb745afb2015-03-02 11:51:23 -0800471 mIcon = icon;
472 if (iconSize != -1) {
473 mIcon.setBounds(0, 0, iconSize, iconSize);
474 }
475 if (mLayoutHorizontal) {
Sunny Goyal70660032015-05-14 00:07:08 -0700476 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
477 setCompoundDrawablesRelative(mIcon, null, null, null);
478 } else {
479 setCompoundDrawables(mIcon, null, null, null);
480 }
Winson Chungb745afb2015-03-02 11:51:23 -0800481 } else {
Sunny Goyal70660032015-05-14 00:07:08 -0700482 setCompoundDrawables(null, mIcon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800483 }
Winson Chungb745afb2015-03-02 11:51:23 -0800484 return icon;
485 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700486
Sunny Goyal69b75642015-05-15 17:00:24 -0700487 @Override
488 public void requestLayout() {
489 if (!mDisableRelayout) {
490 super.requestLayout();
491 }
492 }
493
Sunny Goyal34b65272015-03-11 16:56:52 -0700494 /**
495 * Applies the item info if it is same as what the view is pointing to currently.
496 */
497 public void reapplyItemInfo(final ItemInfo info) {
498 if (getTag() == info) {
499 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700500 mDisableRelayout = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700501 if (info instanceof AppInfo) {
502 applyFromApplicationInfo((AppInfo) info);
503 } else if (info instanceof ShortcutInfo) {
504 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700505 LauncherAppState.getInstance().getIconCache());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700506 } else if (info instanceof PackageItemInfo) {
507 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700508 }
Sunny Goyal69b75642015-05-15 17:00:24 -0700509 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700510 }
511 }
512
513 /**
514 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
515 */
516 public void verifyHighRes() {
517 if (mIconLoadRequest != null) {
518 mIconLoadRequest.cancel();
519 mIconLoadRequest = null;
520 }
521 if (getTag() instanceof AppInfo) {
522 AppInfo info = (AppInfo) getTag();
523 if (info.usingLowResIcon) {
524 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
525 .updateIconInBackground(BubbleTextView.this, info);
526 }
527 } else if (getTag() instanceof ShortcutInfo) {
528 ShortcutInfo info = (ShortcutInfo) getTag();
529 if (info.usingLowResIcon) {
530 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
531 .updateIconInBackground(BubbleTextView.this, info);
532 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700533 } else if (getTag() instanceof PackageItemInfo) {
534 PackageItemInfo info = (PackageItemInfo) getTag();
535 if (info.usingLowResIcon) {
536 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
537 .updateIconInBackground(BubbleTextView.this, info);
538 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700539 }
540 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700541
542 /**
543 * Interface to be implemented by the grand parent to allow click shadow effect.
544 */
545 public static interface BubbleTextShadowHandler {
546 void setPressedIcon(BubbleTextView icon, Bitmap background);
547 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548}