blob: cc6df599af7b072506b5324f418181be7c220c1e [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;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070043import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070044
Sunny Goyalc469aad2015-10-01 11:24:23 -070045import java.text.NumberFormat;
46
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047/**
48 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
49 * because we want to make the bubble taller than the text and TextView's clip is
50 * too aggressive.
51 */
Winson Chungb1777442015-06-16 13:35:04 -070052public class BubbleTextView extends TextView
53 implements BaseRecyclerViewFastScrollBar.FastScrollFocusableView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070054
Sameer Padala8fd74832014-09-08 16:00:29 -070055 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070056
Sunny Goyal508da152014-08-14 10:53:27 -070057 private static final float SHADOW_LARGE_RADIUS = 4.0f;
58 private static final float SHADOW_SMALL_RADIUS = 1.75f;
59 private static final float SHADOW_Y_OFFSET = 2.0f;
60 private static final int SHADOW_LARGE_COLOUR = 0xDD000000;
61 private static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080062
Sunny Goyaldfaccf62015-05-11 16:30:44 -070063 private static final int DISPLAY_WORKSPACE = 0;
64 private static final int DISPLAY_ALL_APPS = 1;
65
Sunny Goyal53d7ee42015-05-22 12:25:45 -070066 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080067 private Drawable mIcon;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080068 private final Drawable mBackground;
69 private final CheckLongPressHelper mLongPressHelper;
70 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070071 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080072
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080073 private boolean mBackgroundSizeChanged;
74
Sunny Goyal508da152014-08-14 10:53:27 -070075 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080076
Jason Monk02dd7ae2014-04-15 15:23:31 -040077 private float mSlop;
78
Winson Chung93f98ea2015-03-10 16:28:47 -070079 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070080 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080081 private final boolean mLayoutHorizontal;
82 private final int mIconSize;
Sunny Goyal4ffec482016-02-09 11:28:52 -080083 @ViewDebug.ExportedProperty(category = "launcher")
Winson Chungb745afb2015-03-02 11:51:23 -080084 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070085
Sunny Goyal4ffec482016-02-09 11:28:52 -080086 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurkaddd62e92011-02-16 17:49:14 -080087 private boolean mStayPressed;
Sunny Goyal4ffec482016-02-09 11:28:52 -080088 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal508da152014-08-14 10:53:27 -070089 private boolean mIgnorePressedStateChange;
Sunny Goyal4ffec482016-02-09 11:28:52 -080090 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal69b75642015-05-15 17:00:24 -070091 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050092
Sunny Goyal34b65272015-03-11 16:56:52 -070093 private IconLoadRequest mIconLoadRequest;
94
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070096 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 }
98
99 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700100 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 }
102
103 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
104 super(context, attrs, defStyle);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700105 mLauncher = (Launcher) context;
106 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700107
Adam Cohen96bb7982014-07-07 11:58:56 -0700108 TypedArray a = context.obtainStyledAttributes(attrs,
109 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700110 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800111 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700112 mDeferShadowGenerationOnTouch =
113 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700114
115 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
116 int defaultIconSize = grid.iconSizePx;
117 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700118 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700119 } else if (display == DISPLAY_ALL_APPS) {
Sunny Goyal383e0a22015-12-10 12:28:21 -0800120 setTextSize(TypedValue.COMPLEX_UNIT_SP, grid.allAppsIconTextSizeSp);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700121 defaultIconSize = grid.allAppsIconSizePx;
122 }
123
124 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
125 defaultIconSize);
126
Adam Cohen96bb7982014-07-07 11:58:56 -0700127 a.recycle();
128
Sunny Goyal15872da2014-07-08 15:43:54 -0700129 if (mCustomShadowsEnabled) {
130 // Draw the background itself as the parent is drawn twice.
131 mBackground = getBackground();
132 setBackground(null);
133 } else {
134 mBackground = null;
135 }
Winson Chungb745afb2015-03-02 11:51:23 -0800136
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800137 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700138 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800139
140 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
141 if (mCustomShadowsEnabled) {
142 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
143 }
144
145 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 }
147
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700148 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
149 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700150 }
151
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700152 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700153 boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800154 Bitmap b = info.getIcon(iconCache);
155
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700156 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(b);
Kenny Guy44cba692016-01-21 19:50:02 +0000157 if (info.isDisabled()) {
Winsonc0880492015-08-21 11:16:27 -0700158 iconDrawable.setState(FastBitmapDrawable.State.DISABLED);
159 }
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700160 setIcon(iconDrawable, mIconSize);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100161 if (info.contentDescription != null) {
162 setContentDescription(info.contentDescription);
163 }
Sunny Goyal508da152014-08-14 10:53:27 -0700164 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800165 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700166
Sunny Goyal34942622014-08-29 17:20:55 -0700167 if (promiseStateChanged || info.isPromise()) {
168 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500169 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800170 }
171
Sunny Goyal508da152014-08-14 10:53:27 -0700172 public void applyFromApplicationInfo(AppInfo info) {
Kenny Guy44cba692016-01-21 19:50:02 +0000173 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(info.iconBitmap);
174 if (info.isDisabled()) {
175 iconDrawable.setState(FastBitmapDrawable.State.DISABLED);
176 }
177 setIcon(iconDrawable, mIconSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700178 setText(info.title);
179 if (info.contentDescription != null) {
180 setContentDescription(info.contentDescription);
181 }
Winson Chung888b3a12015-03-13 11:14:16 -0700182 // We don't need to check the info since it's not a ShortcutInfo
183 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700184
185 // Verify high res immediately
186 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700187 }
188
Sunny Goyal0e08f162015-05-12 11:32:39 -0700189 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700190 setIcon(mLauncher.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700191 setText(info.title);
192 if (info.contentDescription != null) {
193 setContentDescription(info.contentDescription);
194 }
195 // We don't need to check the info since it's not a ShortcutInfo
196 super.setTag(info);
197
198 // Verify high res immediately
199 verifyHighRes();
200 }
201
Winson Chung7501adf2015-06-02 11:24:28 -0700202 /**
Winson5fbe0742015-09-30 15:33:00 -0700203 * Used for measurement only, sets some dummy values on this view.
204 */
205 public void applyDummyInfo() {
206 ColorDrawable d = new ColorDrawable();
207 setIcon(mLauncher.resizeIconDrawable(d), mIconSize);
208 setText("");
209 }
210
211 /**
Winson Chung7501adf2015-06-02 11:24:28 -0700212 * Overrides the default long press timeout.
213 */
214 public void setLongPressTimeout(int longPressTimeout) {
215 mLongPressHelper.setLongPressTimeout(longPressTimeout);
216 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 @Override
219 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700220 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 mBackgroundSizeChanged = true;
222 }
223 return super.setFrame(left, top, right, bottom);
224 }
225
226 @Override
227 protected boolean verifyDrawable(Drawable who) {
228 return who == mBackground || super.verifyDrawable(who);
229 }
230
231 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700232 public void setTag(Object tag) {
233 if (tag != null) {
234 LauncherModel.checkItemInfo((ItemInfo) tag);
235 }
236 super.setTag(tag);
237 }
238
239 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700240 public void setPressed(boolean pressed) {
241 super.setPressed(pressed);
242
243 if (!mIgnorePressedStateChange) {
244 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800245 }
Sunny Goyal508da152014-08-14 10:53:27 -0700246 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800247
Winson Chungb745afb2015-03-02 11:51:23 -0800248 /** Returns the icon for this view. */
249 public Drawable getIcon() {
250 return mIcon;
251 }
252
253 /** Returns whether the layout is horizontal. */
254 public boolean isLayoutHorizontal() {
255 return mLayoutHorizontal;
256 }
257
Sunny Goyal508da152014-08-14 10:53:27 -0700258 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800259 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700260 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
Kenny Guy44cba692016-01-21 19:50:02 +0000261 if (getTag() instanceof ItemInfo
262 && ((ItemInfo) getTag()).isDisabled()) {
Sunny Goyalcd5b7f92015-10-20 15:17:00 -0700263 d.animateState(FastBitmapDrawable.State.DISABLED);
Kenny Guy44cba692016-01-21 19:50:02 +0000264 } else if (isPressed() || mStayPressed) {
265 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700266 } else {
267 d.animateState(FastBitmapDrawable.State.NORMAL);
268 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800270 }
271
272 @Override
273 public boolean onTouchEvent(MotionEvent event) {
274 // Call the superclass onTouchEvent first, because sometimes it changes the state to
275 // isPressed() on an ACTION_UP
276 boolean result = super.onTouchEvent(event);
277
Mady Melloref044dd2015-06-02 15:35:07 -0700278 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700279 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700280 mLongPressHelper.cancelLongPress();
281 result = true;
282 }
283
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800284 switch (event.getAction()) {
285 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700286 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800287 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
288 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700289 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700290 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800291 }
Winson Chung88f33452012-02-23 15:23:44 -0800292
Mady Melloref044dd2015-06-02 15:35:07 -0700293 // If we're in a stylus button press, don't check for long press.
294 if (!mStylusEventHelper.inStylusButtonPressed()) {
295 mLongPressHelper.postCheckForLongPress();
296 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800297 break;
298 case MotionEvent.ACTION_CANCEL:
299 case MotionEvent.ACTION_UP:
300 // If we've touched down and up on an item, and it's still not "pressed", then
301 // destroy the pressed outline
302 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700303 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800304 }
Winson Chung88f33452012-02-23 15:23:44 -0800305
306 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800307 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400308 case MotionEvent.ACTION_MOVE:
309 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
310 mLongPressHelper.cancelLongPress();
311 }
312 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800313 }
314 return result;
315 }
316
Michael Jurkaddd62e92011-02-16 17:49:14 -0800317 void setStayPressed(boolean stayPressed) {
318 mStayPressed = stayPressed;
319 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700320 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700321 } else {
322 if (mPressedBackground == null) {
323 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
324 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800325 }
Sunny Goyal508da152014-08-14 10:53:27 -0700326
327 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700328 ViewParent parent = getParent();
329 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
330 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
331 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700332 }
333
334 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800335 }
336
Sunny Goyal508da152014-08-14 10:53:27 -0700337 void clearPressedBackground() {
338 setPressed(false);
339 setStayPressed(false);
340 }
341
342 @Override
343 public boolean onKeyDown(int keyCode, KeyEvent event) {
344 if (super.onKeyDown(keyCode, event)) {
345 // Pre-create shadow so show immediately on click.
346 if (mPressedBackground == null) {
347 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700348 }
Sunny Goyal508da152014-08-14 10:53:27 -0700349 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700350 }
Sunny Goyal508da152014-08-14 10:53:27 -0700351 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800352 }
353
Sunny Goyal508da152014-08-14 10:53:27 -0700354 @Override
355 public boolean onKeyUp(int keyCode, KeyEvent event) {
356 // Unlike touch events, keypress event propagate pressed state change immediately,
357 // without waiting for onClickHandler to execute. Disable pressed state changes here
358 // to avoid flickering.
359 mIgnorePressedStateChange = true;
360 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700361
Sunny Goyal508da152014-08-14 10:53:27 -0700362 mPressedBackground = null;
363 mIgnorePressedStateChange = false;
364 updateIconState();
365 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800366 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800367
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 @Override
369 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700370 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700371 super.draw(canvas);
372 return;
373 }
374
Michael Jurkabdb5c532011-02-01 15:05:06 -0800375 final Drawable background = mBackground;
376 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700377 final int scrollX = getScrollX();
378 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800379
Michael Jurkabdb5c532011-02-01 15:05:06 -0800380 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700381 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800382 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800384
385 if ((scrollX | scrollY) == 0) {
386 background.draw(canvas);
387 } else {
388 canvas.translate(scrollX, scrollY);
389 background.draw(canvas);
390 canvas.translate(-scrollX, -scrollY);
391 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800393
394 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800395 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800396 getPaint().clearShadowLayer();
397 super.draw(canvas);
398 return;
399 }
400
Michael Jurkabdb5c532011-02-01 15:05:06 -0800401 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800402 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800403 super.draw(canvas);
404 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700405 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
406 getScrollX() + getWidth(),
407 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800408 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800409 super.draw(canvas);
410 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400412
413 @Override
414 protected void onAttachedToWindow() {
415 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700416
Winson Chung656d11c2010-11-29 17:15:47 -0800417 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700418
Winson Chungb745afb2015-03-02 11:51:23 -0800419 if (mIcon instanceof PreloadIconDrawable) {
420 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700421 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400422 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400423 }
424
425 @Override
426 protected void onDetachedFromWindow() {
427 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800428 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400429 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700430
Adam Cohen477828c2013-09-20 12:05:49 -0700431 @Override
432 public void setTextColor(int color) {
433 mTextColor = color;
434 super.setTextColor(color);
435 }
436
Adam Cohen96bb7982014-07-07 11:58:56 -0700437 @Override
438 public void setTextColor(ColorStateList colors) {
439 mTextColor = colors.getDefaultColor();
440 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700441 }
442
Winson Chung5f8afe62013-08-12 16:19:28 -0700443 public void setTextVisibility(boolean visible) {
444 Resources res = getResources();
445 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700446 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700447 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700448 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700449 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700450 }
451
Winson Chungaffd7b42010-08-20 15:11:56 -0700452 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800453 public void cancelLongPress() {
454 super.cancelLongPress();
455
456 mLongPressHelper.cancelLongPress();
457 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500458
Sunny Goyal34942622014-08-29 17:20:55 -0700459 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700460 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400461 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700462 final boolean isPromise = info.isPromise();
463 final int progressLevel = isPromise ?
464 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
465 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700466
Sunny Goyalc469aad2015-10-01 11:24:23 -0700467 setContentDescription(progressLevel > 0 ?
468 getContext().getString(R.string.app_downloading_title, info.title,
469 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
470 getContext().getString(R.string.app_waiting_download_title, info.title));
471
Winson Chungb745afb2015-03-02 11:51:23 -0800472 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700473 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800474 if (mIcon instanceof PreloadIconDrawable) {
475 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700476 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800477 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700478 setIcon(preloadDrawable, mIconSize);
Sunny Goyale755d462014-07-22 13:48:29 -0700479 }
480
481 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700482 if (promiseStateChanged) {
483 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700484 }
Sunny Goyale755d462014-07-22 13:48:29 -0700485 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400486 }
487 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700488
489 private Theme getPreloaderTheme() {
490 Object tag = getTag();
491 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
492 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
493 : R.style.PreloadIcon;
494 Theme theme = sPreloaderThemes.get(style);
495 if (theme == null) {
496 theme = getResources().newTheme();
497 theme.applyStyle(style, true);
498 sPreloaderThemes.put(style, theme);
499 }
500 return theme;
501 }
Winson Chungb745afb2015-03-02 11:51:23 -0800502
503 /**
504 * Sets the icon for this view based on the layout direction.
505 */
Sunny Goyal70660032015-05-14 00:07:08 -0700506 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700507 private Drawable setIcon(Drawable icon, int iconSize) {
Winson Chungb745afb2015-03-02 11:51:23 -0800508 mIcon = icon;
509 if (iconSize != -1) {
510 mIcon.setBounds(0, 0, iconSize, iconSize);
511 }
512 if (mLayoutHorizontal) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700513 if (Utilities.ATLEAST_JB_MR1) {
Sunny Goyal70660032015-05-14 00:07:08 -0700514 setCompoundDrawablesRelative(mIcon, null, null, null);
515 } else {
516 setCompoundDrawables(mIcon, null, null, null);
517 }
Winson Chungb745afb2015-03-02 11:51:23 -0800518 } else {
Sunny Goyal70660032015-05-14 00:07:08 -0700519 setCompoundDrawables(null, mIcon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800520 }
Winson Chungb745afb2015-03-02 11:51:23 -0800521 return icon;
522 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700523
Sunny Goyal69b75642015-05-15 17:00:24 -0700524 @Override
525 public void requestLayout() {
526 if (!mDisableRelayout) {
527 super.requestLayout();
528 }
529 }
530
Sunny Goyal34b65272015-03-11 16:56:52 -0700531 /**
532 * Applies the item info if it is same as what the view is pointing to currently.
533 */
534 public void reapplyItemInfo(final ItemInfo info) {
535 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700536 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
537 if (mIcon instanceof FastBitmapDrawable) {
538 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
539 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700540 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700541 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700542
Sunny Goyal34b65272015-03-11 16:56:52 -0700543 if (info instanceof AppInfo) {
544 applyFromApplicationInfo((AppInfo) info);
545 } else if (info instanceof ShortcutInfo) {
546 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700547 LauncherAppState.getInstance().getIconCache());
Sunny Goyal317698b2015-07-29 11:45:41 -0700548 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
549 View folderIcon =
550 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
551 if (folderIcon != null) {
552 folderIcon.invalidate();
553 }
554 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700555 } else if (info instanceof PackageItemInfo) {
556 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700557 }
Winsonc0880492015-08-21 11:16:27 -0700558
559 // If we are reapplying over an old icon, then we should update the new icon to the same
560 // state as the old icon
561 if (mIcon instanceof FastBitmapDrawable) {
562 ((FastBitmapDrawable) mIcon).setState(prevState);
563 }
564
Sunny Goyal69b75642015-05-15 17:00:24 -0700565 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700566 }
567 }
568
569 /**
570 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
571 */
572 public void verifyHighRes() {
573 if (mIconLoadRequest != null) {
574 mIconLoadRequest.cancel();
575 mIconLoadRequest = null;
576 }
577 if (getTag() instanceof AppInfo) {
578 AppInfo info = (AppInfo) getTag();
579 if (info.usingLowResIcon) {
580 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
581 .updateIconInBackground(BubbleTextView.this, info);
582 }
583 } else if (getTag() instanceof ShortcutInfo) {
584 ShortcutInfo info = (ShortcutInfo) getTag();
585 if (info.usingLowResIcon) {
586 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
587 .updateIconInBackground(BubbleTextView.this, info);
588 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700589 } else if (getTag() instanceof PackageItemInfo) {
590 PackageItemInfo info = (PackageItemInfo) getTag();
591 if (info.usingLowResIcon) {
592 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
593 .updateIconInBackground(BubbleTextView.this, info);
594 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700595 }
596 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700597
Winson Chungb1777442015-06-16 13:35:04 -0700598 @Override
Winsonc0880492015-08-21 11:16:27 -0700599 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
600 // We can only set the fast scroll focus state on a FastBitmapDrawable
601 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700602 return;
603 }
604
Winsonc0880492015-08-21 11:16:27 -0700605 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
606 if (animated) {
607 FastBitmapDrawable.State prevState = d.getCurrentState();
608 if (d.animateState(focusState)) {
609 // If the state was updated, then update the view accordingly
610 animate().scaleX(focusState.viewScale)
611 .scaleY(focusState.viewScale)
612 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
613 .setDuration(d.getDurationForStateChange(prevState, focusState))
614 .start();
615 }
616 } else {
617 if (d.setState(focusState)) {
618 // If the state was updated, then update the view accordingly
619 animate().cancel();
620 setScaleX(focusState.viewScale);
621 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700622 }
623 }
624 }
625
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700626 /**
Winsonc0880492015-08-21 11:16:27 -0700627 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
628 */
629 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
630 final FastBitmapDrawable.State toState) {
631 switch (toState) {
632 case NORMAL:
633 switch (fromState) {
634 case FAST_SCROLL_HIGHLIGHTED:
635 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
636 }
637 }
638 return 0;
639 }
640
641 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700642 * Interface to be implemented by the grand parent to allow click shadow effect.
643 */
Winsonc0880492015-08-21 11:16:27 -0700644 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700645 void setPressedIcon(BubbleTextView icon, Bitmap background);
646 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647}