blob: 8842d5cdd64d1bd8dc28f80571bbb13946287921 [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.BitmapDrawable;
29import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.drawable.Drawable;
Sunny Goyal70660032015-05-14 00:07:08 -070031import android.os.Build;
Winson Chung656d11c2010-11-29 17:15:47 -080032import android.util.AttributeSet;
Sunny Goyal95abbb32014-08-04 10:53:22 -070033import android.util.SparseArray;
Winson Chung5f8afe62013-08-12 16:19:28 -070034import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070035import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080036import android.view.MotionEvent;
Sunny Goyal317698b2015-07-29 11:45:41 -070037import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040038import android.view.ViewConfiguration;
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;
Winson Chungb745afb2015-03-02 11:51:23 -080083 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070084
Michael Jurkaddd62e92011-02-16 17:49:14 -080085 private boolean mStayPressed;
Sunny Goyal508da152014-08-14 10:53:27 -070086 private boolean mIgnorePressedStateChange;
Sunny Goyal69b75642015-05-15 17:00:24 -070087 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050088
Sunny Goyal34b65272015-03-11 16:56:52 -070089 private IconLoadRequest mIconLoadRequest;
90
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070092 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 }
94
95 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -070096 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 }
98
99 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
100 super(context, attrs, defStyle);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700101 mLauncher = (Launcher) context;
102 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -0700103
Adam Cohen96bb7982014-07-07 11:58:56 -0700104 TypedArray a = context.obtainStyledAttributes(attrs,
105 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700106 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800107 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700108 mDeferShadowGenerationOnTouch =
109 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700110
111 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
112 int defaultIconSize = grid.iconSizePx;
113 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700114 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700115 } else if (display == DISPLAY_ALL_APPS) {
Sunny Goyal383e0a22015-12-10 12:28:21 -0800116 setTextSize(TypedValue.COMPLEX_UNIT_SP, grid.allAppsIconTextSizeSp);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700117 defaultIconSize = grid.allAppsIconSizePx;
118 }
119
120 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
121 defaultIconSize);
122
Adam Cohen96bb7982014-07-07 11:58:56 -0700123 a.recycle();
124
Sunny Goyal15872da2014-07-08 15:43:54 -0700125 if (mCustomShadowsEnabled) {
126 // Draw the background itself as the parent is drawn twice.
127 mBackground = getBackground();
128 setBackground(null);
129 } else {
130 mBackground = null;
131 }
Winson Chungb745afb2015-03-02 11:51:23 -0800132
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800133 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellorbb835202015-07-15 16:34:34 -0700134 mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800135
136 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
137 if (mCustomShadowsEnabled) {
138 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
139 }
140
141 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 }
143
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700144 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
145 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700146 }
147
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700148 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700149 boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800150 Bitmap b = info.getIcon(iconCache);
151
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700152 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(b);
Kenny Guy44cba692016-01-21 19:50:02 +0000153 if (info.isDisabled()) {
Winsonc0880492015-08-21 11:16:27 -0700154 iconDrawable.setState(FastBitmapDrawable.State.DISABLED);
155 }
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700156 setIcon(iconDrawable, mIconSize);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100157 if (info.contentDescription != null) {
158 setContentDescription(info.contentDescription);
159 }
Sunny Goyal508da152014-08-14 10:53:27 -0700160 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800161 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700162
Sunny Goyal34942622014-08-29 17:20:55 -0700163 if (promiseStateChanged || info.isPromise()) {
164 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500165 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800166 }
167
Sunny Goyal508da152014-08-14 10:53:27 -0700168 public void applyFromApplicationInfo(AppInfo info) {
Kenny Guy44cba692016-01-21 19:50:02 +0000169 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(info.iconBitmap);
170 if (info.isDisabled()) {
171 iconDrawable.setState(FastBitmapDrawable.State.DISABLED);
172 }
173 setIcon(iconDrawable, mIconSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700174 setText(info.title);
175 if (info.contentDescription != null) {
176 setContentDescription(info.contentDescription);
177 }
Winson Chung888b3a12015-03-13 11:14:16 -0700178 // We don't need to check the info since it's not a ShortcutInfo
179 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700180
181 // Verify high res immediately
182 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700183 }
184
Sunny Goyal0e08f162015-05-12 11:32:39 -0700185 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700186 setIcon(mLauncher.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700187 setText(info.title);
188 if (info.contentDescription != null) {
189 setContentDescription(info.contentDescription);
190 }
191 // We don't need to check the info since it's not a ShortcutInfo
192 super.setTag(info);
193
194 // Verify high res immediately
195 verifyHighRes();
196 }
197
Winson Chung7501adf2015-06-02 11:24:28 -0700198 /**
Winson5fbe0742015-09-30 15:33:00 -0700199 * Used for measurement only, sets some dummy values on this view.
200 */
201 public void applyDummyInfo() {
202 ColorDrawable d = new ColorDrawable();
203 setIcon(mLauncher.resizeIconDrawable(d), mIconSize);
204 setText("");
205 }
206
207 /**
Winson Chung7501adf2015-06-02 11:24:28 -0700208 * Overrides the default long press timeout.
209 */
210 public void setLongPressTimeout(int longPressTimeout) {
211 mLongPressHelper.setLongPressTimeout(longPressTimeout);
212 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 @Override
215 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700216 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 mBackgroundSizeChanged = true;
218 }
219 return super.setFrame(left, top, right, bottom);
220 }
221
222 @Override
223 protected boolean verifyDrawable(Drawable who) {
224 return who == mBackground || super.verifyDrawable(who);
225 }
226
227 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700228 public void setTag(Object tag) {
229 if (tag != null) {
230 LauncherModel.checkItemInfo((ItemInfo) tag);
231 }
232 super.setTag(tag);
233 }
234
235 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700236 public void setPressed(boolean pressed) {
237 super.setPressed(pressed);
238
239 if (!mIgnorePressedStateChange) {
240 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800241 }
Sunny Goyal508da152014-08-14 10:53:27 -0700242 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800243
Winson Chungb745afb2015-03-02 11:51:23 -0800244 /** Returns the icon for this view. */
245 public Drawable getIcon() {
246 return mIcon;
247 }
248
249 /** Returns whether the layout is horizontal. */
250 public boolean isLayoutHorizontal() {
251 return mLayoutHorizontal;
252 }
253
Sunny Goyal508da152014-08-14 10:53:27 -0700254 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800255 if (mIcon instanceof FastBitmapDrawable) {
Winsonc0880492015-08-21 11:16:27 -0700256 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
Kenny Guy44cba692016-01-21 19:50:02 +0000257 if (getTag() instanceof ItemInfo
258 && ((ItemInfo) getTag()).isDisabled()) {
Sunny Goyalcd5b7f92015-10-20 15:17:00 -0700259 d.animateState(FastBitmapDrawable.State.DISABLED);
Kenny Guy44cba692016-01-21 19:50:02 +0000260 } else if (isPressed() || mStayPressed) {
261 d.animateState(FastBitmapDrawable.State.PRESSED);
Winsonc0880492015-08-21 11:16:27 -0700262 } else {
263 d.animateState(FastBitmapDrawable.State.NORMAL);
264 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800266 }
267
268 @Override
269 public boolean onTouchEvent(MotionEvent event) {
270 // Call the superclass onTouchEvent first, because sometimes it changes the state to
271 // isPressed() on an ACTION_UP
272 boolean result = super.onTouchEvent(event);
273
Mady Melloref044dd2015-06-02 15:35:07 -0700274 // Check for a stylus button press, if it occurs cancel any long press checks.
Mady Mellorbb835202015-07-15 16:34:34 -0700275 if (mStylusEventHelper.onMotionEvent(event)) {
Mady Melloref044dd2015-06-02 15:35:07 -0700276 mLongPressHelper.cancelLongPress();
277 result = true;
278 }
279
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800280 switch (event.getAction()) {
281 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700282 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800283 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
284 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700285 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700286 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800287 }
Winson Chung88f33452012-02-23 15:23:44 -0800288
Mady Melloref044dd2015-06-02 15:35:07 -0700289 // If we're in a stylus button press, don't check for long press.
290 if (!mStylusEventHelper.inStylusButtonPressed()) {
291 mLongPressHelper.postCheckForLongPress();
292 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800293 break;
294 case MotionEvent.ACTION_CANCEL:
295 case MotionEvent.ACTION_UP:
296 // If we've touched down and up on an item, and it's still not "pressed", then
297 // destroy the pressed outline
298 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700299 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800300 }
Winson Chung88f33452012-02-23 15:23:44 -0800301
302 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800303 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400304 case MotionEvent.ACTION_MOVE:
305 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
306 mLongPressHelper.cancelLongPress();
307 }
308 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800309 }
310 return result;
311 }
312
Michael Jurkaddd62e92011-02-16 17:49:14 -0800313 void setStayPressed(boolean stayPressed) {
314 mStayPressed = stayPressed;
315 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700316 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700317 } else {
318 if (mPressedBackground == null) {
319 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
320 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800321 }
Sunny Goyal508da152014-08-14 10:53:27 -0700322
323 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700324 ViewParent parent = getParent();
325 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
326 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
327 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700328 }
329
330 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800331 }
332
Sunny Goyal508da152014-08-14 10:53:27 -0700333 void clearPressedBackground() {
334 setPressed(false);
335 setStayPressed(false);
336 }
337
338 @Override
339 public boolean onKeyDown(int keyCode, KeyEvent event) {
340 if (super.onKeyDown(keyCode, event)) {
341 // Pre-create shadow so show immediately on click.
342 if (mPressedBackground == null) {
343 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700344 }
Sunny Goyal508da152014-08-14 10:53:27 -0700345 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700346 }
Sunny Goyal508da152014-08-14 10:53:27 -0700347 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800348 }
349
Sunny Goyal508da152014-08-14 10:53:27 -0700350 @Override
351 public boolean onKeyUp(int keyCode, KeyEvent event) {
352 // Unlike touch events, keypress event propagate pressed state change immediately,
353 // without waiting for onClickHandler to execute. Disable pressed state changes here
354 // to avoid flickering.
355 mIgnorePressedStateChange = true;
356 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700357
Sunny Goyal508da152014-08-14 10:53:27 -0700358 mPressedBackground = null;
359 mIgnorePressedStateChange = false;
360 updateIconState();
361 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800362 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800363
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 @Override
365 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700366 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700367 super.draw(canvas);
368 return;
369 }
370
Michael Jurkabdb5c532011-02-01 15:05:06 -0800371 final Drawable background = mBackground;
372 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700373 final int scrollX = getScrollX();
374 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800375
Michael Jurkabdb5c532011-02-01 15:05:06 -0800376 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700377 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800378 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800380
381 if ((scrollX | scrollY) == 0) {
382 background.draw(canvas);
383 } else {
384 canvas.translate(scrollX, scrollY);
385 background.draw(canvas);
386 canvas.translate(-scrollX, -scrollY);
387 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800389
390 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800391 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800392 getPaint().clearShadowLayer();
393 super.draw(canvas);
394 return;
395 }
396
Michael Jurkabdb5c532011-02-01 15:05:06 -0800397 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800398 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800399 super.draw(canvas);
400 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700401 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
402 getScrollX() + getWidth(),
403 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800404 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800405 super.draw(canvas);
406 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400408
409 @Override
410 protected void onAttachedToWindow() {
411 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700412
Winson Chung656d11c2010-11-29 17:15:47 -0800413 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700414
Winson Chungb745afb2015-03-02 11:51:23 -0800415 if (mIcon instanceof PreloadIconDrawable) {
416 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700417 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400418 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400419 }
420
421 @Override
422 protected void onDetachedFromWindow() {
423 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800424 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400425 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700426
Adam Cohen477828c2013-09-20 12:05:49 -0700427 @Override
428 public void setTextColor(int color) {
429 mTextColor = color;
430 super.setTextColor(color);
431 }
432
Adam Cohen96bb7982014-07-07 11:58:56 -0700433 @Override
434 public void setTextColor(ColorStateList colors) {
435 mTextColor = colors.getDefaultColor();
436 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700437 }
438
Winson Chung5f8afe62013-08-12 16:19:28 -0700439 public void setTextVisibility(boolean visible) {
440 Resources res = getResources();
441 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700442 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700443 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700444 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700445 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700446 }
447
Winson Chungaffd7b42010-08-20 15:11:56 -0700448 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800449 public void cancelLongPress() {
450 super.cancelLongPress();
451
452 mLongPressHelper.cancelLongPress();
453 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500454
Sunny Goyal34942622014-08-29 17:20:55 -0700455 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700456 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400457 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700458 final boolean isPromise = info.isPromise();
459 final int progressLevel = isPromise ?
460 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
461 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700462
Sunny Goyalc469aad2015-10-01 11:24:23 -0700463 setContentDescription(progressLevel > 0 ?
464 getContext().getString(R.string.app_downloading_title, info.title,
465 NumberFormat.getPercentInstance().format(progressLevel * 0.01)) :
466 getContext().getString(R.string.app_waiting_download_title, info.title));
467
Winson Chungb745afb2015-03-02 11:51:23 -0800468 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700469 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800470 if (mIcon instanceof PreloadIconDrawable) {
471 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700472 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800473 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700474 setIcon(preloadDrawable, mIconSize);
Sunny Goyale755d462014-07-22 13:48:29 -0700475 }
476
477 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700478 if (promiseStateChanged) {
479 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700480 }
Sunny Goyale755d462014-07-22 13:48:29 -0700481 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400482 }
483 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700484
485 private Theme getPreloaderTheme() {
486 Object tag = getTag();
487 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
488 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
489 : R.style.PreloadIcon;
490 Theme theme = sPreloaderThemes.get(style);
491 if (theme == null) {
492 theme = getResources().newTheme();
493 theme.applyStyle(style, true);
494 sPreloaderThemes.put(style, theme);
495 }
496 return theme;
497 }
Winson Chungb745afb2015-03-02 11:51:23 -0800498
499 /**
500 * Sets the icon for this view based on the layout direction.
501 */
Sunny Goyal70660032015-05-14 00:07:08 -0700502 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700503 private Drawable setIcon(Drawable icon, int iconSize) {
Winson Chungb745afb2015-03-02 11:51:23 -0800504 mIcon = icon;
505 if (iconSize != -1) {
506 mIcon.setBounds(0, 0, iconSize, iconSize);
507 }
508 if (mLayoutHorizontal) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700509 if (Utilities.ATLEAST_JB_MR1) {
Sunny Goyal70660032015-05-14 00:07:08 -0700510 setCompoundDrawablesRelative(mIcon, null, null, null);
511 } else {
512 setCompoundDrawables(mIcon, null, null, null);
513 }
Winson Chungb745afb2015-03-02 11:51:23 -0800514 } else {
Sunny Goyal70660032015-05-14 00:07:08 -0700515 setCompoundDrawables(null, mIcon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800516 }
Winson Chungb745afb2015-03-02 11:51:23 -0800517 return icon;
518 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700519
Sunny Goyal69b75642015-05-15 17:00:24 -0700520 @Override
521 public void requestLayout() {
522 if (!mDisableRelayout) {
523 super.requestLayout();
524 }
525 }
526
Sunny Goyal34b65272015-03-11 16:56:52 -0700527 /**
528 * Applies the item info if it is same as what the view is pointing to currently.
529 */
530 public void reapplyItemInfo(final ItemInfo info) {
531 if (getTag() == info) {
Winsonc0880492015-08-21 11:16:27 -0700532 FastBitmapDrawable.State prevState = FastBitmapDrawable.State.NORMAL;
533 if (mIcon instanceof FastBitmapDrawable) {
534 prevState = ((FastBitmapDrawable) mIcon).getCurrentState();
535 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700536 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700537 mDisableRelayout = true;
Winsonc0880492015-08-21 11:16:27 -0700538
Sunny Goyal34b65272015-03-11 16:56:52 -0700539 if (info instanceof AppInfo) {
540 applyFromApplicationInfo((AppInfo) info);
541 } else if (info instanceof ShortcutInfo) {
542 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700543 LauncherAppState.getInstance().getIconCache());
Sunny Goyal317698b2015-07-29 11:45:41 -0700544 if ((info.rank < FolderIcon.NUM_ITEMS_IN_PREVIEW) && (info.container >= 0)) {
545 View folderIcon =
546 mLauncher.getWorkspace().getHomescreenIconByItemId(info.container);
547 if (folderIcon != null) {
548 folderIcon.invalidate();
549 }
550 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700551 } else if (info instanceof PackageItemInfo) {
552 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700553 }
Winsonc0880492015-08-21 11:16:27 -0700554
555 // If we are reapplying over an old icon, then we should update the new icon to the same
556 // state as the old icon
557 if (mIcon instanceof FastBitmapDrawable) {
558 ((FastBitmapDrawable) mIcon).setState(prevState);
559 }
560
Sunny Goyal69b75642015-05-15 17:00:24 -0700561 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700562 }
563 }
564
565 /**
566 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
567 */
568 public void verifyHighRes() {
569 if (mIconLoadRequest != null) {
570 mIconLoadRequest.cancel();
571 mIconLoadRequest = null;
572 }
573 if (getTag() instanceof AppInfo) {
574 AppInfo info = (AppInfo) getTag();
575 if (info.usingLowResIcon) {
576 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
577 .updateIconInBackground(BubbleTextView.this, info);
578 }
579 } else if (getTag() instanceof ShortcutInfo) {
580 ShortcutInfo info = (ShortcutInfo) getTag();
581 if (info.usingLowResIcon) {
582 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
583 .updateIconInBackground(BubbleTextView.this, info);
584 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700585 } else if (getTag() instanceof PackageItemInfo) {
586 PackageItemInfo info = (PackageItemInfo) getTag();
587 if (info.usingLowResIcon) {
588 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
589 .updateIconInBackground(BubbleTextView.this, info);
590 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700591 }
592 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700593
Winson Chungb1777442015-06-16 13:35:04 -0700594 @Override
Winsonc0880492015-08-21 11:16:27 -0700595 public void setFastScrollFocusState(final FastBitmapDrawable.State focusState, boolean animated) {
596 // We can only set the fast scroll focus state on a FastBitmapDrawable
597 if (!(mIcon instanceof FastBitmapDrawable)) {
Winson Chungb1777442015-06-16 13:35:04 -0700598 return;
599 }
600
Winsonc0880492015-08-21 11:16:27 -0700601 FastBitmapDrawable d = (FastBitmapDrawable) mIcon;
602 if (animated) {
603 FastBitmapDrawable.State prevState = d.getCurrentState();
604 if (d.animateState(focusState)) {
605 // If the state was updated, then update the view accordingly
606 animate().scaleX(focusState.viewScale)
607 .scaleY(focusState.viewScale)
608 .setStartDelay(getStartDelayForStateChange(prevState, focusState))
609 .setDuration(d.getDurationForStateChange(prevState, focusState))
610 .start();
611 }
612 } else {
613 if (d.setState(focusState)) {
614 // If the state was updated, then update the view accordingly
615 animate().cancel();
616 setScaleX(focusState.viewScale);
617 setScaleY(focusState.viewScale);
Winson Chungb1777442015-06-16 13:35:04 -0700618 }
619 }
620 }
621
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700622 /**
Winsonc0880492015-08-21 11:16:27 -0700623 * Returns the start delay when animating between certain {@link FastBitmapDrawable} states.
624 */
625 private static int getStartDelayForStateChange(final FastBitmapDrawable.State fromState,
626 final FastBitmapDrawable.State toState) {
627 switch (toState) {
628 case NORMAL:
629 switch (fromState) {
630 case FAST_SCROLL_HIGHLIGHTED:
631 return FastBitmapDrawable.FAST_SCROLL_INACTIVE_DURATION / 4;
632 }
633 }
634 return 0;
635 }
636
637 /**
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700638 * Interface to be implemented by the grand parent to allow click shadow effect.
639 */
Winsonc0880492015-08-21 11:16:27 -0700640 public interface BubbleTextShadowHandler {
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700641 void setPressedIcon(BubbleTextView icon, Bitmap background);
642 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643}