blob: 6c13b4a9b56aa01ce6531860685599f9a2fe9083 [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
Sunny Goyaldfaccf62015-05-11 16:30:44 -070056 private static final int DISPLAY_WORKSPACE = 0;
57 private static final int DISPLAY_ALL_APPS = 1;
58
Sunny Goyal53d7ee42015-05-22 12:25:45 -070059 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080060 private Drawable mIcon;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080061 private final Drawable mBackground;
62 private final CheckLongPressHelper mLongPressHelper;
63 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070064 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080065
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080066 private boolean mBackgroundSizeChanged;
67
Sunny Goyal508da152014-08-14 10:53:27 -070068 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080069
Jason Monk02dd7ae2014-04-15 15:23:31 -040070 private float mSlop;
71
Winson Chung93f98ea2015-03-10 16:28:47 -070072 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070073 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080074 private final boolean mLayoutHorizontal;
75 private final int mIconSize;
Winson Chungb745afb2015-03-02 11:51:23 -080076 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070077
Michael Jurkaddd62e92011-02-16 17:49:14 -080078 private boolean mStayPressed;
Sunny Goyal508da152014-08-14 10:53:27 -070079 private boolean mIgnorePressedStateChange;
Sunny Goyal69b75642015-05-15 17:00:24 -070080 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050081
Sunny Goyal34b65272015-03-11 16:56:52 -070082 private IconLoadRequest mIconLoadRequest;
83
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070085 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 }
87
88 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -070089 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 }
91
92 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
93 super(context, attrs, defStyle);
Sunny Goyal53d7ee42015-05-22 12:25:45 -070094 mLauncher = (Launcher) context;
95 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -070096
Adam Cohen96bb7982014-07-07 11:58:56 -070097 TypedArray a = context.obtainStyledAttributes(attrs,
98 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -070099 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800100 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700101 mDeferShadowGenerationOnTouch =
102 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700103
104 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
105 int defaultIconSize = grid.iconSizePx;
106 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700107 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700108 } else if (display == DISPLAY_ALL_APPS) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700109 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700110 defaultIconSize = grid.allAppsIconSizePx;
111 }
112
113 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
114 defaultIconSize);
115
Adam Cohen96bb7982014-07-07 11:58:56 -0700116 a.recycle();
117
Sunny Goyal15872da2014-07-08 15:43:54 -0700118 if (mCustomShadowsEnabled) {
119 // Draw the background itself as the parent is drawn twice.
120 mBackground = getBackground();
121 setBackground(null);
122 } else {
123 mBackground = null;
124 }
Winson Chungb745afb2015-03-02 11:51:23 -0800125
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800126 mLongPressHelper = new CheckLongPressHelper(this);
Mady Melloref044dd2015-06-02 15:35:07 -0700127 mStylusEventHelper = new StylusEventHelper(this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800128
129 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
130 if (mCustomShadowsEnabled) {
131 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
132 }
133
134 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 }
136
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700137 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
138 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700139 }
140
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700141 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700142 boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800143 Bitmap b = info.getIcon(iconCache);
144
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700145 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(b);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700146 iconDrawable.setGhostModeEnabled(info.isDisabled != 0);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700147
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700148 setIcon(iconDrawable, mIconSize);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100149 if (info.contentDescription != null) {
150 setContentDescription(info.contentDescription);
151 }
Sunny Goyal508da152014-08-14 10:53:27 -0700152 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800153 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700154
Sunny Goyal34942622014-08-29 17:20:55 -0700155 if (promiseStateChanged || info.isPromise()) {
156 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500157 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800158 }
159
Sunny Goyal508da152014-08-14 10:53:27 -0700160 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700161 setIcon(mLauncher.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700162 setText(info.title);
163 if (info.contentDescription != null) {
164 setContentDescription(info.contentDescription);
165 }
Winson Chung888b3a12015-03-13 11:14:16 -0700166 // We don't need to check the info since it's not a ShortcutInfo
167 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700168
169 // Verify high res immediately
170 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700171 }
172
Sunny Goyal0e08f162015-05-12 11:32:39 -0700173 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700174 setIcon(mLauncher.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700175 setText(info.title);
176 if (info.contentDescription != null) {
177 setContentDescription(info.contentDescription);
178 }
179 // We don't need to check the info since it's not a ShortcutInfo
180 super.setTag(info);
181
182 // Verify high res immediately
183 verifyHighRes();
184 }
185
Winson Chung7501adf2015-06-02 11:24:28 -0700186 /**
187 * Overrides the default long press timeout.
188 */
189 public void setLongPressTimeout(int longPressTimeout) {
190 mLongPressHelper.setLongPressTimeout(longPressTimeout);
191 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 @Override
194 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700195 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 mBackgroundSizeChanged = true;
197 }
198 return super.setFrame(left, top, right, bottom);
199 }
200
201 @Override
202 protected boolean verifyDrawable(Drawable who) {
203 return who == mBackground || super.verifyDrawable(who);
204 }
205
206 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700207 public void setTag(Object tag) {
208 if (tag != null) {
209 LauncherModel.checkItemInfo((ItemInfo) tag);
210 }
211 super.setTag(tag);
212 }
213
214 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700215 public void setPressed(boolean pressed) {
216 super.setPressed(pressed);
217
218 if (!mIgnorePressedStateChange) {
219 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800220 }
Sunny Goyal508da152014-08-14 10:53:27 -0700221 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800222
Winson Chungb745afb2015-03-02 11:51:23 -0800223 /** Returns the icon for this view. */
224 public Drawable getIcon() {
225 return mIcon;
226 }
227
228 /** Returns whether the layout is horizontal. */
229 public boolean isLayoutHorizontal() {
230 return mLayoutHorizontal;
231 }
232
Sunny Goyal508da152014-08-14 10:53:27 -0700233 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800234 if (mIcon instanceof FastBitmapDrawable) {
235 ((FastBitmapDrawable) mIcon).setPressed(isPressed() || mStayPressed);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800237 }
238
239 @Override
240 public boolean onTouchEvent(MotionEvent event) {
241 // Call the superclass onTouchEvent first, because sometimes it changes the state to
242 // isPressed() on an ACTION_UP
243 boolean result = super.onTouchEvent(event);
244
Mady Melloref044dd2015-06-02 15:35:07 -0700245 // Check for a stylus button press, if it occurs cancel any long press checks.
246 if (mStylusEventHelper.checkAndPerformStylusEvent(event)) {
247 mLongPressHelper.cancelLongPress();
248 result = true;
249 }
250
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800251 switch (event.getAction()) {
252 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700253 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800254 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
255 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700256 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700257 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800258 }
Winson Chung88f33452012-02-23 15:23:44 -0800259
Mady Melloref044dd2015-06-02 15:35:07 -0700260 // If we're in a stylus button press, don't check for long press.
261 if (!mStylusEventHelper.inStylusButtonPressed()) {
262 mLongPressHelper.postCheckForLongPress();
263 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800264 break;
265 case MotionEvent.ACTION_CANCEL:
266 case MotionEvent.ACTION_UP:
267 // If we've touched down and up on an item, and it's still not "pressed", then
268 // destroy the pressed outline
269 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700270 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800271 }
Winson Chung88f33452012-02-23 15:23:44 -0800272
273 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800274 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400275 case MotionEvent.ACTION_MOVE:
276 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
277 mLongPressHelper.cancelLongPress();
278 }
279 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800280 }
281 return result;
282 }
283
Michael Jurkaddd62e92011-02-16 17:49:14 -0800284 void setStayPressed(boolean stayPressed) {
285 mStayPressed = stayPressed;
286 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700287 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700288 } else {
289 if (mPressedBackground == null) {
290 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
291 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800292 }
Sunny Goyal508da152014-08-14 10:53:27 -0700293
294 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700295 ViewParent parent = getParent();
296 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
297 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
298 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700299 }
300
301 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800302 }
303
Sunny Goyal508da152014-08-14 10:53:27 -0700304 void clearPressedBackground() {
305 setPressed(false);
306 setStayPressed(false);
307 }
308
309 @Override
310 public boolean onKeyDown(int keyCode, KeyEvent event) {
311 if (super.onKeyDown(keyCode, event)) {
312 // Pre-create shadow so show immediately on click.
313 if (mPressedBackground == null) {
314 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700315 }
Sunny Goyal508da152014-08-14 10:53:27 -0700316 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700317 }
Sunny Goyal508da152014-08-14 10:53:27 -0700318 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800319 }
320
Sunny Goyal508da152014-08-14 10:53:27 -0700321 @Override
322 public boolean onKeyUp(int keyCode, KeyEvent event) {
323 // Unlike touch events, keypress event propagate pressed state change immediately,
324 // without waiting for onClickHandler to execute. Disable pressed state changes here
325 // to avoid flickering.
326 mIgnorePressedStateChange = true;
327 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700328
Sunny Goyal508da152014-08-14 10:53:27 -0700329 mPressedBackground = null;
330 mIgnorePressedStateChange = false;
331 updateIconState();
332 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800333 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800334
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 @Override
336 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700337 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700338 super.draw(canvas);
339 return;
340 }
341
Michael Jurkabdb5c532011-02-01 15:05:06 -0800342 final Drawable background = mBackground;
343 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700344 final int scrollX = getScrollX();
345 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800346
Michael Jurkabdb5c532011-02-01 15:05:06 -0800347 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700348 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800349 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800351
352 if ((scrollX | scrollY) == 0) {
353 background.draw(canvas);
354 } else {
355 canvas.translate(scrollX, scrollY);
356 background.draw(canvas);
357 canvas.translate(-scrollX, -scrollY);
358 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800360
361 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800362 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800363 getPaint().clearShadowLayer();
364 super.draw(canvas);
365 return;
366 }
367
Michael Jurkabdb5c532011-02-01 15:05:06 -0800368 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800369 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800370 super.draw(canvas);
371 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700372 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
373 getScrollX() + getWidth(),
374 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800375 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800376 super.draw(canvas);
377 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379
380 @Override
381 protected void onAttachedToWindow() {
382 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700383
Winson Chung656d11c2010-11-29 17:15:47 -0800384 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700385
Winson Chungb745afb2015-03-02 11:51:23 -0800386 if (mIcon instanceof PreloadIconDrawable) {
387 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700388 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400389 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400390 }
391
392 @Override
393 protected void onDetachedFromWindow() {
394 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800395 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400396 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700397
Adam Cohen477828c2013-09-20 12:05:49 -0700398 @Override
399 public void setTextColor(int color) {
400 mTextColor = color;
401 super.setTextColor(color);
402 }
403
Adam Cohen96bb7982014-07-07 11:58:56 -0700404 @Override
405 public void setTextColor(ColorStateList colors) {
406 mTextColor = colors.getDefaultColor();
407 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700408 }
409
Winson Chung5f8afe62013-08-12 16:19:28 -0700410 public void setTextVisibility(boolean visible) {
411 Resources res = getResources();
412 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700413 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700414 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700415 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700416 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700417 }
418
Winson Chungaffd7b42010-08-20 15:11:56 -0700419 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800420 public void cancelLongPress() {
421 super.cancelLongPress();
422
423 mLongPressHelper.cancelLongPress();
424 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500425
Sunny Goyal34942622014-08-29 17:20:55 -0700426 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700427 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400428 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700429 final boolean isPromise = info.isPromise();
430 final int progressLevel = isPromise ?
431 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
432 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700433
Winson Chungb745afb2015-03-02 11:51:23 -0800434 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700435 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800436 if (mIcon instanceof PreloadIconDrawable) {
437 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700438 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800439 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700440 setIcon(preloadDrawable, mIconSize);
Sunny Goyale755d462014-07-22 13:48:29 -0700441 }
442
443 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700444 if (promiseStateChanged) {
445 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700446 }
Sunny Goyale755d462014-07-22 13:48:29 -0700447 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400448 }
449 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700450
451 private Theme getPreloaderTheme() {
452 Object tag = getTag();
453 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
454 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
455 : R.style.PreloadIcon;
456 Theme theme = sPreloaderThemes.get(style);
457 if (theme == null) {
458 theme = getResources().newTheme();
459 theme.applyStyle(style, true);
460 sPreloaderThemes.put(style, theme);
461 }
462 return theme;
463 }
Winson Chungb745afb2015-03-02 11:51:23 -0800464
465 /**
466 * Sets the icon for this view based on the layout direction.
467 */
Sunny Goyal70660032015-05-14 00:07:08 -0700468 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700469 private Drawable setIcon(Drawable icon, int iconSize) {
Winson Chungb745afb2015-03-02 11:51:23 -0800470 mIcon = icon;
471 if (iconSize != -1) {
472 mIcon.setBounds(0, 0, iconSize, iconSize);
473 }
474 if (mLayoutHorizontal) {
Sunny Goyal70660032015-05-14 00:07:08 -0700475 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
476 setCompoundDrawablesRelative(mIcon, null, null, null);
477 } else {
478 setCompoundDrawables(mIcon, null, null, null);
479 }
Winson Chungb745afb2015-03-02 11:51:23 -0800480 } else {
Sunny Goyal70660032015-05-14 00:07:08 -0700481 setCompoundDrawables(null, mIcon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800482 }
Winson Chungb745afb2015-03-02 11:51:23 -0800483 return icon;
484 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700485
Sunny Goyal69b75642015-05-15 17:00:24 -0700486 @Override
487 public void requestLayout() {
488 if (!mDisableRelayout) {
489 super.requestLayout();
490 }
491 }
492
Sunny Goyal34b65272015-03-11 16:56:52 -0700493 /**
494 * Applies the item info if it is same as what the view is pointing to currently.
495 */
496 public void reapplyItemInfo(final ItemInfo info) {
497 if (getTag() == info) {
498 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700499 mDisableRelayout = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700500 if (info instanceof AppInfo) {
501 applyFromApplicationInfo((AppInfo) info);
502 } else if (info instanceof ShortcutInfo) {
503 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700504 LauncherAppState.getInstance().getIconCache());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700505 } else if (info instanceof PackageItemInfo) {
506 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700507 }
Sunny Goyal69b75642015-05-15 17:00:24 -0700508 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700509 }
510 }
511
512 /**
513 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
514 */
515 public void verifyHighRes() {
516 if (mIconLoadRequest != null) {
517 mIconLoadRequest.cancel();
518 mIconLoadRequest = null;
519 }
520 if (getTag() instanceof AppInfo) {
521 AppInfo info = (AppInfo) getTag();
522 if (info.usingLowResIcon) {
523 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
524 .updateIconInBackground(BubbleTextView.this, info);
525 }
526 } else if (getTag() instanceof ShortcutInfo) {
527 ShortcutInfo info = (ShortcutInfo) getTag();
528 if (info.usingLowResIcon) {
529 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
530 .updateIconInBackground(BubbleTextView.this, info);
531 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700532 } else if (getTag() instanceof PackageItemInfo) {
533 PackageItemInfo info = (PackageItemInfo) getTag();
534 if (info.usingLowResIcon) {
535 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
536 .updateIconInBackground(BubbleTextView.this, info);
537 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700538 }
539 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700540
541 /**
542 * Interface to be implemented by the grand parent to allow click shadow effect.
543 */
544 public static interface BubbleTextShadowHandler {
545 void setPressedIcon(BubbleTextView icon, Bitmap background);
546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547}