blob: 798eec8e70f6b4d311a9a4b69a4ec03d6be2ea7c [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 Goyal69b75642015-05-15 17:00:24 -070031import android.util.Log;
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;
Jason Monk02dd7ae2014-04-15 15:23:31 -040036import android.view.ViewConfiguration;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070037import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080038import android.widget.TextView;
Romain Guyedcce092010-03-04 13:03:17 -080039
Sunny Goyal34b65272015-03-11 16:56:52 -070040import com.android.launcher3.IconCache.IconLoadRequest;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070041import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070042
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043/**
44 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
45 * because we want to make the bubble taller than the text and TextView's clip is
46 * too aggressive.
47 */
Michael Jurka08ee7702011-08-11 16:53:35 -070048public class BubbleTextView extends TextView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070049
Sameer Padala8fd74832014-09-08 16:00:29 -070050 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070051
Sunny Goyal508da152014-08-14 10:53:27 -070052 private static final float SHADOW_LARGE_RADIUS = 4.0f;
53 private static final float SHADOW_SMALL_RADIUS = 1.75f;
54 private static final float SHADOW_Y_OFFSET = 2.0f;
55 private static final int SHADOW_LARGE_COLOUR = 0xDD000000;
56 private static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080057 static final float PADDING_V = 3.0f;
58
Sunny Goyaldfaccf62015-05-11 16:30:44 -070059 private static final int DISPLAY_WORKSPACE = 0;
60 private static final int DISPLAY_ALL_APPS = 1;
61
Winson Chungb745afb2015-03-02 11:51:23 -080062 private Drawable mIcon;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080063 private final Drawable mBackground;
64 private final CheckLongPressHelper mLongPressHelper;
65 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070066 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080067
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080068 private boolean mBackgroundSizeChanged;
69
Sunny Goyal508da152014-08-14 10:53:27 -070070 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080071
Jason Monk02dd7ae2014-04-15 15:23:31 -040072 private float mSlop;
73
Winson Chung93f98ea2015-03-10 16:28:47 -070074 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070075 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080076 private final boolean mLayoutHorizontal;
77 private final int mIconSize;
Winson Chungb745afb2015-03-02 11:51:23 -080078 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070079
Michael Jurkaddd62e92011-02-16 17:49:14 -080080 private boolean mStayPressed;
Sunny Goyal508da152014-08-14 10:53:27 -070081 private boolean mIgnorePressedStateChange;
Sunny Goyal69b75642015-05-15 17:00:24 -070082 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050083
Sunny Goyal34b65272015-03-11 16:56:52 -070084 private IconLoadRequest mIconLoadRequest;
85
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070087 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 }
89
90 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -070091 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 }
93
94 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
95 super(context, attrs, defStyle);
Winson Chungb745afb2015-03-02 11:51:23 -080096 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -070097 DeviceProfile grid = ((Launcher) context).getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -070098
Adam Cohen96bb7982014-07-07 11:58:56 -070099 TypedArray a = context.obtainStyledAttributes(attrs,
100 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700101 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800102 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700103 mDeferShadowGenerationOnTouch =
104 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700105
106 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
107 int defaultIconSize = grid.iconSizePx;
108 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700109 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700110 } else if (display == DISPLAY_ALL_APPS) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700111 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700112 defaultIconSize = grid.allAppsIconSizePx;
113 }
114
115 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
116 defaultIconSize);
117
Adam Cohen96bb7982014-07-07 11:58:56 -0700118 a.recycle();
119
Sunny Goyal15872da2014-07-08 15:43:54 -0700120 if (mCustomShadowsEnabled) {
121 // Draw the background itself as the parent is drawn twice.
122 mBackground = getBackground();
123 setBackground(null);
124 } else {
125 mBackground = null;
126 }
Winson Chungb745afb2015-03-02 11:51:23 -0800127
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800128 mLongPressHelper = new CheckLongPressHelper(this);
Mady Melloref044dd2015-06-02 15:35:07 -0700129 mStylusEventHelper = new StylusEventHelper(this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800130
131 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
132 if (mCustomShadowsEnabled) {
133 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
134 }
135
136 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 }
138
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700139 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
140 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700141 }
142
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700143 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700144 boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800145 Bitmap b = info.getIcon(iconCache);
146
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700147 FastBitmapDrawable iconDrawable = Utilities.createIconDrawable(b);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700148 iconDrawable.setGhostModeEnabled(info.isDisabled != 0);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700149
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700150 setIcon(iconDrawable, mIconSize);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100151 if (info.contentDescription != null) {
152 setContentDescription(info.contentDescription);
153 }
Sunny Goyal508da152014-08-14 10:53:27 -0700154 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800155 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700156
Sunny Goyal34942622014-08-29 17:20:55 -0700157 if (promiseStateChanged || info.isPromise()) {
158 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500159 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800160 }
161
Sunny Goyal508da152014-08-14 10:53:27 -0700162 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700163 setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700164 setText(info.title);
165 if (info.contentDescription != null) {
166 setContentDescription(info.contentDescription);
167 }
Winson Chung888b3a12015-03-13 11:14:16 -0700168 // We don't need to check the info since it's not a ShortcutInfo
169 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700170
171 // Verify high res immediately
172 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700173 }
174
Sunny Goyal0e08f162015-05-12 11:32:39 -0700175 public void applyFromPackageItemInfo(PackageItemInfo info) {
176 setIcon(Utilities.createIconDrawable(info.iconBitmap), mIconSize);
177 setText(info.title);
178 if (info.contentDescription != null) {
179 setContentDescription(info.contentDescription);
180 }
181 // We don't need to check the info since it's not a ShortcutInfo
182 super.setTag(info);
183
184 // Verify high res immediately
185 verifyHighRes();
186 }
187
188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 @Override
190 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700191 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 mBackgroundSizeChanged = true;
193 }
194 return super.setFrame(left, top, right, bottom);
195 }
196
197 @Override
198 protected boolean verifyDrawable(Drawable who) {
199 return who == mBackground || super.verifyDrawable(who);
200 }
201
202 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700203 public void setTag(Object tag) {
204 if (tag != null) {
205 LauncherModel.checkItemInfo((ItemInfo) tag);
206 }
207 super.setTag(tag);
208 }
209
210 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700211 public void setPressed(boolean pressed) {
212 super.setPressed(pressed);
213
214 if (!mIgnorePressedStateChange) {
215 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800216 }
Sunny Goyal508da152014-08-14 10:53:27 -0700217 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800218
Winson Chungb745afb2015-03-02 11:51:23 -0800219 /** Returns the icon for this view. */
220 public Drawable getIcon() {
221 return mIcon;
222 }
223
224 /** Returns whether the layout is horizontal. */
225 public boolean isLayoutHorizontal() {
226 return mLayoutHorizontal;
227 }
228
Sunny Goyal508da152014-08-14 10:53:27 -0700229 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800230 if (mIcon instanceof FastBitmapDrawable) {
231 ((FastBitmapDrawable) mIcon).setPressed(isPressed() || mStayPressed);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800233 }
234
235 @Override
236 public boolean onTouchEvent(MotionEvent event) {
237 // Call the superclass onTouchEvent first, because sometimes it changes the state to
238 // isPressed() on an ACTION_UP
239 boolean result = super.onTouchEvent(event);
240
Mady Melloref044dd2015-06-02 15:35:07 -0700241 // Check for a stylus button press, if it occurs cancel any long press checks.
242 if (mStylusEventHelper.checkAndPerformStylusEvent(event)) {
243 mLongPressHelper.cancelLongPress();
244 result = true;
245 }
246
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800247 switch (event.getAction()) {
248 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700249 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800250 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
251 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700252 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700253 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800254 }
Winson Chung88f33452012-02-23 15:23:44 -0800255
Mady Melloref044dd2015-06-02 15:35:07 -0700256 // If we're in a stylus button press, don't check for long press.
257 if (!mStylusEventHelper.inStylusButtonPressed()) {
258 mLongPressHelper.postCheckForLongPress();
259 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800260 break;
261 case MotionEvent.ACTION_CANCEL:
262 case MotionEvent.ACTION_UP:
263 // If we've touched down and up on an item, and it's still not "pressed", then
264 // destroy the pressed outline
265 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700266 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800267 }
Winson Chung88f33452012-02-23 15:23:44 -0800268
269 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800270 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400271 case MotionEvent.ACTION_MOVE:
272 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
273 mLongPressHelper.cancelLongPress();
274 }
275 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800276 }
277 return result;
278 }
279
Michael Jurkaddd62e92011-02-16 17:49:14 -0800280 void setStayPressed(boolean stayPressed) {
281 mStayPressed = stayPressed;
282 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700283 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700284 } else {
285 if (mPressedBackground == null) {
286 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
287 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800288 }
Sunny Goyal508da152014-08-14 10:53:27 -0700289
290 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700291 ViewParent parent = getParent();
292 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
293 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
294 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700295 }
296
297 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800298 }
299
Sunny Goyal508da152014-08-14 10:53:27 -0700300 void clearPressedBackground() {
301 setPressed(false);
302 setStayPressed(false);
303 }
304
305 @Override
306 public boolean onKeyDown(int keyCode, KeyEvent event) {
307 if (super.onKeyDown(keyCode, event)) {
308 // Pre-create shadow so show immediately on click.
309 if (mPressedBackground == null) {
310 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700311 }
Sunny Goyal508da152014-08-14 10:53:27 -0700312 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700313 }
Sunny Goyal508da152014-08-14 10:53:27 -0700314 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800315 }
316
Sunny Goyal508da152014-08-14 10:53:27 -0700317 @Override
318 public boolean onKeyUp(int keyCode, KeyEvent event) {
319 // Unlike touch events, keypress event propagate pressed state change immediately,
320 // without waiting for onClickHandler to execute. Disable pressed state changes here
321 // to avoid flickering.
322 mIgnorePressedStateChange = true;
323 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700324
Sunny Goyal508da152014-08-14 10:53:27 -0700325 mPressedBackground = null;
326 mIgnorePressedStateChange = false;
327 updateIconState();
328 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800329 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800330
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 @Override
332 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700333 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700334 super.draw(canvas);
335 return;
336 }
337
Michael Jurkabdb5c532011-02-01 15:05:06 -0800338 final Drawable background = mBackground;
339 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700340 final int scrollX = getScrollX();
341 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800342
Michael Jurkabdb5c532011-02-01 15:05:06 -0800343 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700344 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800345 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800347
348 if ((scrollX | scrollY) == 0) {
349 background.draw(canvas);
350 } else {
351 canvas.translate(scrollX, scrollY);
352 background.draw(canvas);
353 canvas.translate(-scrollX, -scrollY);
354 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800356
357 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800358 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800359 getPaint().clearShadowLayer();
360 super.draw(canvas);
361 return;
362 }
363
Michael Jurkabdb5c532011-02-01 15:05:06 -0800364 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800365 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800366 super.draw(canvas);
367 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700368 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
369 getScrollX() + getWidth(),
370 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800371 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800372 super.draw(canvas);
373 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400375
376 @Override
377 protected void onAttachedToWindow() {
378 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700379
Winson Chung656d11c2010-11-29 17:15:47 -0800380 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700381
Winson Chungb745afb2015-03-02 11:51:23 -0800382 if (mIcon instanceof PreloadIconDrawable) {
383 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700384 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400385 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400386 }
387
388 @Override
389 protected void onDetachedFromWindow() {
390 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800391 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400392 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700393
Adam Cohen477828c2013-09-20 12:05:49 -0700394 @Override
395 public void setTextColor(int color) {
396 mTextColor = color;
397 super.setTextColor(color);
398 }
399
Adam Cohen96bb7982014-07-07 11:58:56 -0700400 @Override
401 public void setTextColor(ColorStateList colors) {
402 mTextColor = colors.getDefaultColor();
403 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700404 }
405
Winson Chung5f8afe62013-08-12 16:19:28 -0700406 public void setTextVisibility(boolean visible) {
407 Resources res = getResources();
408 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700409 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700410 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700411 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700412 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700413 }
414
Winson Chungaffd7b42010-08-20 15:11:56 -0700415 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800416 public void cancelLongPress() {
417 super.cancelLongPress();
418
419 mLongPressHelper.cancelLongPress();
420 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500421
Sunny Goyal34942622014-08-29 17:20:55 -0700422 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700423 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400424 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700425 final boolean isPromise = info.isPromise();
426 final int progressLevel = isPromise ?
427 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
428 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700429
Winson Chungb745afb2015-03-02 11:51:23 -0800430 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700431 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800432 if (mIcon instanceof PreloadIconDrawable) {
433 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700434 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800435 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700436 setIcon(preloadDrawable, mIconSize);
Sunny Goyale755d462014-07-22 13:48:29 -0700437 }
438
439 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700440 if (promiseStateChanged) {
441 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700442 }
Sunny Goyale755d462014-07-22 13:48:29 -0700443 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400444 }
445 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700446
447 private Theme getPreloaderTheme() {
448 Object tag = getTag();
449 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
450 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
451 : R.style.PreloadIcon;
452 Theme theme = sPreloaderThemes.get(style);
453 if (theme == null) {
454 theme = getResources().newTheme();
455 theme.applyStyle(style, true);
456 sPreloaderThemes.put(style, theme);
457 }
458 return theme;
459 }
Winson Chungb745afb2015-03-02 11:51:23 -0800460
461 /**
462 * Sets the icon for this view based on the layout direction.
463 */
Sunny Goyal70660032015-05-14 00:07:08 -0700464 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700465 private Drawable setIcon(Drawable icon, int iconSize) {
Winson Chungb745afb2015-03-02 11:51:23 -0800466 mIcon = icon;
467 if (iconSize != -1) {
468 mIcon.setBounds(0, 0, iconSize, iconSize);
469 }
470 if (mLayoutHorizontal) {
Sunny Goyal70660032015-05-14 00:07:08 -0700471 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
472 setCompoundDrawablesRelative(mIcon, null, null, null);
473 } else {
474 setCompoundDrawables(mIcon, null, null, null);
475 }
Winson Chungb745afb2015-03-02 11:51:23 -0800476 } else {
Sunny Goyal70660032015-05-14 00:07:08 -0700477 setCompoundDrawables(null, mIcon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800478 }
Winson Chungb745afb2015-03-02 11:51:23 -0800479 return icon;
480 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700481
Sunny Goyal69b75642015-05-15 17:00:24 -0700482 @Override
483 public void requestLayout() {
484 if (!mDisableRelayout) {
485 super.requestLayout();
486 }
487 }
488
Sunny Goyal34b65272015-03-11 16:56:52 -0700489 /**
490 * Applies the item info if it is same as what the view is pointing to currently.
491 */
492 public void reapplyItemInfo(final ItemInfo info) {
493 if (getTag() == info) {
494 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700495 mDisableRelayout = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700496 if (info instanceof AppInfo) {
497 applyFromApplicationInfo((AppInfo) info);
498 } else if (info instanceof ShortcutInfo) {
499 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700500 LauncherAppState.getInstance().getIconCache());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700501 } else if (info instanceof PackageItemInfo) {
502 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700503 }
Sunny Goyal69b75642015-05-15 17:00:24 -0700504 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700505 }
506 }
507
508 /**
509 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
510 */
511 public void verifyHighRes() {
512 if (mIconLoadRequest != null) {
513 mIconLoadRequest.cancel();
514 mIconLoadRequest = null;
515 }
516 if (getTag() instanceof AppInfo) {
517 AppInfo info = (AppInfo) getTag();
518 if (info.usingLowResIcon) {
519 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
520 .updateIconInBackground(BubbleTextView.this, info);
521 }
522 } else if (getTag() instanceof ShortcutInfo) {
523 ShortcutInfo info = (ShortcutInfo) getTag();
524 if (info.usingLowResIcon) {
525 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
526 .updateIconInBackground(BubbleTextView.this, info);
527 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700528 } else if (getTag() instanceof PackageItemInfo) {
529 PackageItemInfo info = (PackageItemInfo) getTag();
530 if (info.usingLowResIcon) {
531 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
532 .updateIconInBackground(BubbleTextView.this, info);
533 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700534 }
535 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700536
537 /**
538 * Interface to be implemented by the grand parent to allow click shadow effect.
539 */
540 public static interface BubbleTextShadowHandler {
541 void setPressedIcon(BubbleTextView icon, Bitmap background);
542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543}