blob: 5dc3b1239568a649f38b23fc74e1cebbe8ee8b44 [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;
Michael Jurkabdb5c532011-02-01 15:05:06 -080036import android.widget.TextView;
Romain Guyedcce092010-03-04 13:03:17 -080037
Sunny Goyal34b65272015-03-11 16:56:52 -070038import com.android.launcher3.IconCache.IconLoadRequest;
Sunny Goyal0e08f162015-05-12 11:32:39 -070039import com.android.launcher3.widget.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070040
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041/**
42 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
43 * because we want to make the bubble taller than the text and TextView's clip is
44 * too aggressive.
45 */
Michael Jurka08ee7702011-08-11 16:53:35 -070046public class BubbleTextView extends TextView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070047
Sameer Padala8fd74832014-09-08 16:00:29 -070048 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070049
Sunny Goyal508da152014-08-14 10:53:27 -070050 private static final float SHADOW_LARGE_RADIUS = 4.0f;
51 private static final float SHADOW_SMALL_RADIUS = 1.75f;
52 private static final float SHADOW_Y_OFFSET = 2.0f;
53 private static final int SHADOW_LARGE_COLOUR = 0xDD000000;
54 private static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080055 static final float PADDING_V = 3.0f;
56
Sunny Goyaldfaccf62015-05-11 16:30:44 -070057 private static final int DISPLAY_WORKSPACE = 0;
58 private static final int DISPLAY_ALL_APPS = 1;
59
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;
64
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080065 private boolean mBackgroundSizeChanged;
66
Sunny Goyal508da152014-08-14 10:53:27 -070067 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080068
Jason Monk02dd7ae2014-04-15 15:23:31 -040069 private float mSlop;
70
Winson Chung93f98ea2015-03-10 16:28:47 -070071 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070072 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080073 private final boolean mLayoutHorizontal;
74 private final int mIconSize;
Winson Chungb745afb2015-03-02 11:51:23 -080075 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070076
Michael Jurkaddd62e92011-02-16 17:49:14 -080077 private boolean mStayPressed;
Sunny Goyal508da152014-08-14 10:53:27 -070078 private boolean mIgnorePressedStateChange;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050079
Sunny Goyal34b65272015-03-11 16:56:52 -070080 private IconLoadRequest mIconLoadRequest;
81
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070083 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 }
85
86 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -070087 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 }
89
90 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
91 super(context, attrs, defStyle);
Winson Chungb745afb2015-03-02 11:51:23 -080092 LauncherAppState app = LauncherAppState.getInstance();
93 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -070094
Adam Cohen96bb7982014-07-07 11:58:56 -070095 TypedArray a = context.obtainStyledAttributes(attrs,
96 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -070097 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -080098 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -070099 mDeferShadowGenerationOnTouch =
100 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700101
102 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
103 int defaultIconSize = grid.iconSizePx;
104 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700105 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700106 } else if (display == DISPLAY_ALL_APPS) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700107 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700108 defaultIconSize = grid.allAppsIconSizePx;
109 }
110
111 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
112 defaultIconSize);
113
Adam Cohen96bb7982014-07-07 11:58:56 -0700114 a.recycle();
115
Sunny Goyal15872da2014-07-08 15:43:54 -0700116 if (mCustomShadowsEnabled) {
117 // Draw the background itself as the parent is drawn twice.
118 mBackground = getBackground();
119 setBackground(null);
120 } else {
121 mBackground = null;
122 }
Winson Chungb745afb2015-03-02 11:51:23 -0800123
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800124 mLongPressHelper = new CheckLongPressHelper(this);
125
126 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
127 if (mCustomShadowsEnabled) {
128 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
129 }
130
131 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 }
133
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700134 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
135 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700136 }
137
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700138 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700139 boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800140 Bitmap b = info.getIcon(iconCache);
141
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700142 FastBitmapDrawable iconDrawable = Utilities.createIconDrawable(b);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700143 iconDrawable.setGhostModeEnabled(info.isDisabled != 0);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700144
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700145 setIcon(iconDrawable, mIconSize);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100146 if (info.contentDescription != null) {
147 setContentDescription(info.contentDescription);
148 }
Sunny Goyal508da152014-08-14 10:53:27 -0700149 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800150 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700151
Sunny Goyal34942622014-08-29 17:20:55 -0700152 if (promiseStateChanged || info.isPromise()) {
153 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500154 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800155 }
156
Sunny Goyal508da152014-08-14 10:53:27 -0700157 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700158 setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700159 setText(info.title);
160 if (info.contentDescription != null) {
161 setContentDescription(info.contentDescription);
162 }
Winson Chung888b3a12015-03-13 11:14:16 -0700163 // We don't need to check the info since it's not a ShortcutInfo
164 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700165
166 // Verify high res immediately
167 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700168 }
169
Sunny Goyal0e08f162015-05-12 11:32:39 -0700170 public void applyFromPackageItemInfo(PackageItemInfo info) {
171 setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize);
172 setText(info.title);
173 if (info.contentDescription != null) {
174 setContentDescription(info.contentDescription);
175 }
176 // We don't need to check the info since it's not a ShortcutInfo
177 super.setTag(info);
178
179 // Verify high res immediately
180 verifyHighRes();
181 }
182
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 @Override
185 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700186 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 mBackgroundSizeChanged = true;
188 }
189 return super.setFrame(left, top, right, bottom);
190 }
191
192 @Override
193 protected boolean verifyDrawable(Drawable who) {
194 return who == mBackground || super.verifyDrawable(who);
195 }
196
197 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700198 public void setTag(Object tag) {
199 if (tag != null) {
200 LauncherModel.checkItemInfo((ItemInfo) tag);
201 }
202 super.setTag(tag);
203 }
204
205 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700206 public void setPressed(boolean pressed) {
207 super.setPressed(pressed);
208
209 if (!mIgnorePressedStateChange) {
210 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800211 }
Sunny Goyal508da152014-08-14 10:53:27 -0700212 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800213
Winson Chungb745afb2015-03-02 11:51:23 -0800214 /** Returns the icon for this view. */
215 public Drawable getIcon() {
216 return mIcon;
217 }
218
219 /** Returns whether the layout is horizontal. */
220 public boolean isLayoutHorizontal() {
221 return mLayoutHorizontal;
222 }
223
Sunny Goyal508da152014-08-14 10:53:27 -0700224 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800225 if (mIcon instanceof FastBitmapDrawable) {
226 ((FastBitmapDrawable) mIcon).setPressed(isPressed() || mStayPressed);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800228 }
229
230 @Override
231 public boolean onTouchEvent(MotionEvent event) {
232 // Call the superclass onTouchEvent first, because sometimes it changes the state to
233 // isPressed() on an ACTION_UP
234 boolean result = super.onTouchEvent(event);
235
236 switch (event.getAction()) {
237 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700238 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800239 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
240 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700241 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700242 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800243 }
Winson Chung88f33452012-02-23 15:23:44 -0800244
245 mLongPressHelper.postCheckForLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800246 break;
247 case MotionEvent.ACTION_CANCEL:
248 case MotionEvent.ACTION_UP:
249 // If we've touched down and up on an item, and it's still not "pressed", then
250 // destroy the pressed outline
251 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700252 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800253 }
Winson Chung88f33452012-02-23 15:23:44 -0800254
255 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800256 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400257 case MotionEvent.ACTION_MOVE:
258 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
259 mLongPressHelper.cancelLongPress();
260 }
261 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800262 }
263 return result;
264 }
265
Michael Jurkaddd62e92011-02-16 17:49:14 -0800266 void setStayPressed(boolean stayPressed) {
267 mStayPressed = stayPressed;
268 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700269 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700270 } else {
271 if (mPressedBackground == null) {
272 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
273 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800274 }
Sunny Goyal508da152014-08-14 10:53:27 -0700275
276 // Only show the shadow effect when persistent pressed state is set.
277 if (getParent() instanceof ShortcutAndWidgetContainer) {
278 CellLayout layout = (CellLayout) getParent().getParent();
Sunny Goyal4fe5a372015-05-14 19:55:10 -0700279 layout.setPressedIcon(this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700280 }
281
282 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800283 }
284
Sunny Goyal508da152014-08-14 10:53:27 -0700285 void clearPressedBackground() {
286 setPressed(false);
287 setStayPressed(false);
288 }
289
290 @Override
291 public boolean onKeyDown(int keyCode, KeyEvent event) {
292 if (super.onKeyDown(keyCode, event)) {
293 // Pre-create shadow so show immediately on click.
294 if (mPressedBackground == null) {
295 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700296 }
Sunny Goyal508da152014-08-14 10:53:27 -0700297 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700298 }
Sunny Goyal508da152014-08-14 10:53:27 -0700299 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800300 }
301
Sunny Goyal508da152014-08-14 10:53:27 -0700302 @Override
303 public boolean onKeyUp(int keyCode, KeyEvent event) {
304 // Unlike touch events, keypress event propagate pressed state change immediately,
305 // without waiting for onClickHandler to execute. Disable pressed state changes here
306 // to avoid flickering.
307 mIgnorePressedStateChange = true;
308 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700309
Sunny Goyal508da152014-08-14 10:53:27 -0700310 mPressedBackground = null;
311 mIgnorePressedStateChange = false;
312 updateIconState();
313 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800314 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800315
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 @Override
317 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700318 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700319 super.draw(canvas);
320 return;
321 }
322
Michael Jurkabdb5c532011-02-01 15:05:06 -0800323 final Drawable background = mBackground;
324 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700325 final int scrollX = getScrollX();
326 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800327
Michael Jurkabdb5c532011-02-01 15:05:06 -0800328 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700329 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800330 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800332
333 if ((scrollX | scrollY) == 0) {
334 background.draw(canvas);
335 } else {
336 canvas.translate(scrollX, scrollY);
337 background.draw(canvas);
338 canvas.translate(-scrollX, -scrollY);
339 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800341
342 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800343 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800344 getPaint().clearShadowLayer();
345 super.draw(canvas);
346 return;
347 }
348
Michael Jurkabdb5c532011-02-01 15:05:06 -0800349 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800350 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800351 super.draw(canvas);
352 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700353 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
354 getScrollX() + getWidth(),
355 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800356 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800357 super.draw(canvas);
358 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400360
361 @Override
362 protected void onAttachedToWindow() {
363 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700364
Winson Chung656d11c2010-11-29 17:15:47 -0800365 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700366
Winson Chungb745afb2015-03-02 11:51:23 -0800367 if (mIcon instanceof PreloadIconDrawable) {
368 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700369 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400370 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400371 }
372
373 @Override
374 protected void onDetachedFromWindow() {
375 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800376 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400377 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700378
Adam Cohen477828c2013-09-20 12:05:49 -0700379 @Override
380 public void setTextColor(int color) {
381 mTextColor = color;
382 super.setTextColor(color);
383 }
384
Adam Cohen96bb7982014-07-07 11:58:56 -0700385 @Override
386 public void setTextColor(ColorStateList colors) {
387 mTextColor = colors.getDefaultColor();
388 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700389 }
390
Winson Chung5f8afe62013-08-12 16:19:28 -0700391 public void setTextVisibility(boolean visible) {
392 Resources res = getResources();
393 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700394 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700395 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700396 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700397 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700398 }
399
Winson Chungaffd7b42010-08-20 15:11:56 -0700400 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800401 public void cancelLongPress() {
402 super.cancelLongPress();
403
404 mLongPressHelper.cancelLongPress();
405 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500406
Sunny Goyal34942622014-08-29 17:20:55 -0700407 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700408 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400409 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700410 final boolean isPromise = info.isPromise();
411 final int progressLevel = isPromise ?
412 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
413 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700414
Winson Chungb745afb2015-03-02 11:51:23 -0800415 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700416 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800417 if (mIcon instanceof PreloadIconDrawable) {
418 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700419 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800420 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700421 setIcon(preloadDrawable, mIconSize);
Sunny Goyale755d462014-07-22 13:48:29 -0700422 }
423
424 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700425 if (promiseStateChanged) {
426 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700427 }
Sunny Goyale755d462014-07-22 13:48:29 -0700428 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400429 }
430 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700431
432 private Theme getPreloaderTheme() {
433 Object tag = getTag();
434 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
435 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
436 : R.style.PreloadIcon;
437 Theme theme = sPreloaderThemes.get(style);
438 if (theme == null) {
439 theme = getResources().newTheme();
440 theme.applyStyle(style, true);
441 sPreloaderThemes.put(style, theme);
442 }
443 return theme;
444 }
Winson Chungb745afb2015-03-02 11:51:23 -0800445
446 /**
447 * Sets the icon for this view based on the layout direction.
448 */
Sunny Goyal70660032015-05-14 00:07:08 -0700449 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700450 private Drawable setIcon(Drawable icon, int iconSize) {
Winson Chungb745afb2015-03-02 11:51:23 -0800451 mIcon = icon;
452 if (iconSize != -1) {
453 mIcon.setBounds(0, 0, iconSize, iconSize);
454 }
455 if (mLayoutHorizontal) {
Sunny Goyal70660032015-05-14 00:07:08 -0700456 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
457 setCompoundDrawablesRelative(mIcon, null, null, null);
458 } else {
459 setCompoundDrawables(mIcon, null, null, null);
460 }
Winson Chungb745afb2015-03-02 11:51:23 -0800461 } else {
Sunny Goyal70660032015-05-14 00:07:08 -0700462 setCompoundDrawables(null, mIcon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800463 }
Winson Chungb745afb2015-03-02 11:51:23 -0800464 return icon;
465 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700466
467 /**
468 * Applies the item info if it is same as what the view is pointing to currently.
469 */
470 public void reapplyItemInfo(final ItemInfo info) {
471 if (getTag() == info) {
472 mIconLoadRequest = null;
473 if (info instanceof AppInfo) {
474 applyFromApplicationInfo((AppInfo) info);
475 } else if (info instanceof ShortcutInfo) {
476 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700477 LauncherAppState.getInstance().getIconCache());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700478 } else if (info instanceof PackageItemInfo) {
479 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700480 }
481 }
482 }
483
484 /**
485 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
486 */
487 public void verifyHighRes() {
488 if (mIconLoadRequest != null) {
489 mIconLoadRequest.cancel();
490 mIconLoadRequest = null;
491 }
492 if (getTag() instanceof AppInfo) {
493 AppInfo info = (AppInfo) getTag();
494 if (info.usingLowResIcon) {
495 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
496 .updateIconInBackground(BubbleTextView.this, info);
497 }
498 } else if (getTag() instanceof ShortcutInfo) {
499 ShortcutInfo info = (ShortcutInfo) getTag();
500 if (info.usingLowResIcon) {
501 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
502 .updateIconInBackground(BubbleTextView.this, info);
503 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700504 } else if (getTag() instanceof PackageItemInfo) {
505 PackageItemInfo info = (PackageItemInfo) getTag();
506 if (info.usingLowResIcon) {
507 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
508 .updateIconInBackground(BubbleTextView.this, info);
509 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700510 }
511 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512}