blob: f4e306af3698da19b5a3c6f6efd846292c012598 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal70660032015-05-14 00:07:08 -070019import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080020import android.content.Context;
Adam Cohen96bb7982014-07-07 11:58:56 -070021import android.content.res.ColorStateList;
Winson Chung656d11c2010-11-29 17:15:47 -080022import android.content.res.Resources;
Sunny Goyal95abbb32014-08-04 10:53:22 -070023import android.content.res.Resources.Theme;
Adam Cohen96bb7982014-07-07 11:58:56 -070024import android.content.res.TypedArray;
Michael Jurka67b2f6c2010-11-17 12:33:46 -080025import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.graphics.Canvas;
Michael Jurka137142e2011-01-05 20:57:04 -080027import android.graphics.Region;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.graphics.drawable.Drawable;
Sunny Goyal70660032015-05-14 00:07:08 -070029import android.os.Build;
Winson Chung656d11c2010-11-29 17:15:47 -080030import android.util.AttributeSet;
Sunny Goyal95abbb32014-08-04 10:53:22 -070031import android.util.SparseArray;
Winson Chung5f8afe62013-08-12 16:19:28 -070032import android.util.TypedValue;
Sunny Goyal508da152014-08-14 10:53:27 -070033import android.view.KeyEvent;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080034import android.view.MotionEvent;
Jason Monk02dd7ae2014-04-15 15:23:31 -040035import android.view.ViewConfiguration;
Sunny Goyal4b6eb262015-05-14 19:24:40 -070036import android.view.ViewParent;
Michael Jurkabdb5c532011-02-01 15:05:06 -080037import android.widget.TextView;
Sunny Goyal34b65272015-03-11 16:56:52 -070038import com.android.launcher3.IconCache.IconLoadRequest;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070039import com.android.launcher3.model.PackageItemInfo;
Sunny Goyal34b65272015-03-11 16:56:52 -070040
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041/**
42 * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
43 * because we want to make the bubble taller than the text and TextView's clip is
44 * too aggressive.
45 */
Michael Jurka08ee7702011-08-11 16:53:35 -070046public class BubbleTextView extends TextView {
Sunny Goyal95abbb32014-08-04 10:53:22 -070047
Sameer Padala8fd74832014-09-08 16:00:29 -070048 private static SparseArray<Theme> sPreloaderThemes = new SparseArray<Theme>(2);
Sunny Goyal95abbb32014-08-04 10:53:22 -070049
Sunny Goyal508da152014-08-14 10:53:27 -070050 private static final float SHADOW_LARGE_RADIUS = 4.0f;
51 private static final float SHADOW_SMALL_RADIUS = 1.75f;
52 private static final float SHADOW_Y_OFFSET = 2.0f;
53 private static final int SHADOW_LARGE_COLOUR = 0xDD000000;
54 private static final int SHADOW_SMALL_COLOUR = 0xCC000000;
Winson Chung656d11c2010-11-29 17:15:47 -080055 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
Sunny Goyal53d7ee42015-05-22 12:25:45 -070060 private final Launcher mLauncher;
Winson Chungb745afb2015-03-02 11:51:23 -080061 private Drawable mIcon;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080062 private final Drawable mBackground;
63 private final CheckLongPressHelper mLongPressHelper;
64 private final HolographicOutlineHelper mOutlineHelper;
Mady Melloref044dd2015-06-02 15:35:07 -070065 private final StylusEventHelper mStylusEventHelper;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080066
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080067 private boolean mBackgroundSizeChanged;
68
Sunny Goyal508da152014-08-14 10:53:27 -070069 private Bitmap mPressedBackground;
Michael Jurka38b4f7c2010-12-14 16:46:39 -080070
Jason Monk02dd7ae2014-04-15 15:23:31 -040071 private float mSlop;
72
Winson Chung93f98ea2015-03-10 16:28:47 -070073 private final boolean mDeferShadowGenerationOnTouch;
Sunny Goyal15872da2014-07-08 15:43:54 -070074 private final boolean mCustomShadowsEnabled;
Winson Chungb745afb2015-03-02 11:51:23 -080075 private final boolean mLayoutHorizontal;
76 private final int mIconSize;
Winson Chungb745afb2015-03-02 11:51:23 -080077 private int mTextColor;
Winson Chung5f8afe62013-08-12 16:19:28 -070078
Michael Jurkaddd62e92011-02-16 17:49:14 -080079 private boolean mStayPressed;
Sunny Goyal508da152014-08-14 10:53:27 -070080 private boolean mIgnorePressedStateChange;
Sunny Goyal69b75642015-05-15 17:00:24 -070081 private boolean mDisableRelayout = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050082
Sunny Goyal34b65272015-03-11 16:56:52 -070083 private IconLoadRequest mIconLoadRequest;
84
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 public BubbleTextView(Context context) {
Adam Cohen96bb7982014-07-07 11:58:56 -070086 this(context, null, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 }
88
89 public BubbleTextView(Context context, AttributeSet attrs) {
Adam Cohen96bb7982014-07-07 11:58:56 -070090 this(context, attrs, 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 }
92
93 public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
94 super(context, attrs, defStyle);
Sunny Goyal53d7ee42015-05-22 12:25:45 -070095 mLauncher = (Launcher) context;
96 DeviceProfile grid = mLauncher.getDeviceProfile();
Adam Cohen96bb7982014-07-07 11:58:56 -070097
Adam Cohen96bb7982014-07-07 11:58:56 -070098 TypedArray a = context.obtainStyledAttributes(attrs,
99 R.styleable.BubbleTextView, defStyle, 0);
Adam Cohen96bb7982014-07-07 11:58:56 -0700100 mCustomShadowsEnabled = a.getBoolean(R.styleable.BubbleTextView_customShadows, true);
Winson Chungb745afb2015-03-02 11:51:23 -0800101 mLayoutHorizontal = a.getBoolean(R.styleable.BubbleTextView_layoutHorizontal, false);
Winson Chung93f98ea2015-03-10 16:28:47 -0700102 mDeferShadowGenerationOnTouch =
103 a.getBoolean(R.styleable.BubbleTextView_deferShadowGeneration, false);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700104
105 int display = a.getInteger(R.styleable.BubbleTextView_iconDisplay, DISPLAY_WORKSPACE);
106 int defaultIconSize = grid.iconSizePx;
107 if (display == DISPLAY_WORKSPACE) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700108 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700109 } else if (display == DISPLAY_ALL_APPS) {
Winson Chung44d0aac2015-05-11 18:02:55 -0700110 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700111 defaultIconSize = grid.allAppsIconSizePx;
112 }
113
114 mIconSize = a.getDimensionPixelSize(R.styleable.BubbleTextView_iconSizeOverride,
115 defaultIconSize);
116
Adam Cohen96bb7982014-07-07 11:58:56 -0700117 a.recycle();
118
Sunny Goyal15872da2014-07-08 15:43:54 -0700119 if (mCustomShadowsEnabled) {
120 // Draw the background itself as the parent is drawn twice.
121 mBackground = getBackground();
122 setBackground(null);
123 } else {
124 mBackground = null;
125 }
Winson Chungb745afb2015-03-02 11:51:23 -0800126
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800127 mLongPressHelper = new CheckLongPressHelper(this);
Mady Melloref044dd2015-06-02 15:35:07 -0700128 mStylusEventHelper = new StylusEventHelper(this);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800129
130 mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
131 if (mCustomShadowsEnabled) {
132 setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
133 }
134
135 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 }
137
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700138 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
139 applyFromShortcutInfo(info, iconCache, false);
Winson Chung5f8afe62013-08-12 16:19:28 -0700140 }
141
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700142 public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700143 boolean promiseStateChanged) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800144 Bitmap b = info.getIcon(iconCache);
145
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700146 FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(b);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700147 iconDrawable.setGhostModeEnabled(info.isDisabled != 0);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700148
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700149 setIcon(iconDrawable, mIconSize);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100150 if (info.contentDescription != null) {
151 setContentDescription(info.contentDescription);
152 }
Sunny Goyal508da152014-08-14 10:53:27 -0700153 setText(info.title);
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800154 setTag(info);
Sunny Goyal34846382014-07-09 00:09:28 -0700155
Sunny Goyal34942622014-08-29 17:20:55 -0700156 if (promiseStateChanged || info.isPromise()) {
157 applyState(promiseStateChanged);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500158 }
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800159 }
160
Sunny Goyal508da152014-08-14 10:53:27 -0700161 public void applyFromApplicationInfo(AppInfo info) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700162 setIcon(mLauncher.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal508da152014-08-14 10:53:27 -0700163 setText(info.title);
164 if (info.contentDescription != null) {
165 setContentDescription(info.contentDescription);
166 }
Winson Chung888b3a12015-03-13 11:14:16 -0700167 // We don't need to check the info since it's not a ShortcutInfo
168 super.setTag(info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700169
170 // Verify high res immediately
171 verifyHighRes();
Sunny Goyal508da152014-08-14 10:53:27 -0700172 }
173
Sunny Goyal0e08f162015-05-12 11:32:39 -0700174 public void applyFromPackageItemInfo(PackageItemInfo info) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700175 setIcon(mLauncher.createIconDrawable(info.iconBitmap), mIconSize);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700176 setText(info.title);
177 if (info.contentDescription != null) {
178 setContentDescription(info.contentDescription);
179 }
180 // We don't need to check the info since it's not a ShortcutInfo
181 super.setTag(info);
182
183 // Verify high res immediately
184 verifyHighRes();
185 }
186
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 @Override
189 protected boolean setFrame(int left, int top, int right, int bottom) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700190 if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 mBackgroundSizeChanged = true;
192 }
193 return super.setFrame(left, top, right, bottom);
194 }
195
196 @Override
197 protected boolean verifyDrawable(Drawable who) {
198 return who == mBackground || super.verifyDrawable(who);
199 }
200
201 @Override
Michael Jurka816474f2012-06-25 14:49:02 -0700202 public void setTag(Object tag) {
203 if (tag != null) {
204 LauncherModel.checkItemInfo((ItemInfo) tag);
205 }
206 super.setTag(tag);
207 }
208
209 @Override
Sunny Goyal508da152014-08-14 10:53:27 -0700210 public void setPressed(boolean pressed) {
211 super.setPressed(pressed);
212
213 if (!mIgnorePressedStateChange) {
214 updateIconState();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800215 }
Sunny Goyal508da152014-08-14 10:53:27 -0700216 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800217
Winson Chungb745afb2015-03-02 11:51:23 -0800218 /** Returns the icon for this view. */
219 public Drawable getIcon() {
220 return mIcon;
221 }
222
223 /** Returns whether the layout is horizontal. */
224 public boolean isLayoutHorizontal() {
225 return mLayoutHorizontal;
226 }
227
Sunny Goyal508da152014-08-14 10:53:27 -0700228 private void updateIconState() {
Winson Chungb745afb2015-03-02 11:51:23 -0800229 if (mIcon instanceof FastBitmapDrawable) {
230 ((FastBitmapDrawable) mIcon).setPressed(isPressed() || mStayPressed);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800232 }
233
234 @Override
235 public boolean onTouchEvent(MotionEvent event) {
236 // Call the superclass onTouchEvent first, because sometimes it changes the state to
237 // isPressed() on an ACTION_UP
238 boolean result = super.onTouchEvent(event);
239
Mady Melloref044dd2015-06-02 15:35:07 -0700240 // Check for a stylus button press, if it occurs cancel any long press checks.
241 if (mStylusEventHelper.checkAndPerformStylusEvent(event)) {
242 mLongPressHelper.cancelLongPress();
243 result = true;
244 }
245
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800246 switch (event.getAction()) {
247 case MotionEvent.ACTION_DOWN:
Sunny Goyal508da152014-08-14 10:53:27 -0700248 // So that the pressed outline is visible immediately on setStayPressed(),
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800249 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
250 // to create it)
Winson Chung93f98ea2015-03-10 16:28:47 -0700251 if (!mDeferShadowGenerationOnTouch && mPressedBackground == null) {
Sunny Goyal508da152014-08-14 10:53:27 -0700252 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800253 }
Winson Chung88f33452012-02-23 15:23:44 -0800254
Mady Melloref044dd2015-06-02 15:35:07 -0700255 // If we're in a stylus button press, don't check for long press.
256 if (!mStylusEventHelper.inStylusButtonPressed()) {
257 mLongPressHelper.postCheckForLongPress();
258 }
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800259 break;
260 case MotionEvent.ACTION_CANCEL:
261 case MotionEvent.ACTION_UP:
262 // If we've touched down and up on an item, and it's still not "pressed", then
263 // destroy the pressed outline
264 if (!isPressed()) {
Sunny Goyal508da152014-08-14 10:53:27 -0700265 mPressedBackground = null;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800266 }
Winson Chung88f33452012-02-23 15:23:44 -0800267
268 mLongPressHelper.cancelLongPress();
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800269 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400270 case MotionEvent.ACTION_MOVE:
271 if (!Utilities.pointInView(this, event.getX(), event.getY(), mSlop)) {
272 mLongPressHelper.cancelLongPress();
273 }
274 break;
Michael Jurka38b4f7c2010-12-14 16:46:39 -0800275 }
276 return result;
277 }
278
Michael Jurkaddd62e92011-02-16 17:49:14 -0800279 void setStayPressed(boolean stayPressed) {
280 mStayPressed = stayPressed;
281 if (!stayPressed) {
Sunny Goyal508da152014-08-14 10:53:27 -0700282 mPressedBackground = null;
Winson Chung93f98ea2015-03-10 16:28:47 -0700283 } else {
284 if (mPressedBackground == null) {
285 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
286 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800287 }
Sunny Goyal508da152014-08-14 10:53:27 -0700288
289 // Only show the shadow effect when persistent pressed state is set.
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700290 ViewParent parent = getParent();
291 if (parent != null && parent.getParent() instanceof BubbleTextShadowHandler) {
292 ((BubbleTextShadowHandler) parent.getParent()).setPressedIcon(
293 this, mPressedBackground);
Sunny Goyal508da152014-08-14 10:53:27 -0700294 }
295
296 updateIconState();
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800297 }
298
Sunny Goyal508da152014-08-14 10:53:27 -0700299 void clearPressedBackground() {
300 setPressed(false);
301 setStayPressed(false);
302 }
303
304 @Override
305 public boolean onKeyDown(int keyCode, KeyEvent event) {
306 if (super.onKeyDown(keyCode, event)) {
307 // Pre-create shadow so show immediately on click.
308 if (mPressedBackground == null) {
309 mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700310 }
Sunny Goyal508da152014-08-14 10:53:27 -0700311 return true;
Patrick Dubroyd69e1132011-03-15 10:29:01 -0700312 }
Sunny Goyal508da152014-08-14 10:53:27 -0700313 return false;
Patrick Dubroy3499d8c2011-03-10 17:17:23 -0800314 }
315
Sunny Goyal508da152014-08-14 10:53:27 -0700316 @Override
317 public boolean onKeyUp(int keyCode, KeyEvent event) {
318 // Unlike touch events, keypress event propagate pressed state change immediately,
319 // without waiting for onClickHandler to execute. Disable pressed state changes here
320 // to avoid flickering.
321 mIgnorePressedStateChange = true;
322 boolean result = super.onKeyUp(keyCode, event);
Winson Chung1e9cbfe2011-09-30 16:52:26 -0700323
Sunny Goyal508da152014-08-14 10:53:27 -0700324 mPressedBackground = null;
325 mIgnorePressedStateChange = false;
326 updateIconState();
327 return result;
Michael Jurkaddd62e92011-02-16 17:49:14 -0800328 }
Patrick Dubroya017c032011-03-09 15:58:32 -0800329
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 @Override
331 public void draw(Canvas canvas) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700332 if (!mCustomShadowsEnabled) {
Adam Cohen477828c2013-09-20 12:05:49 -0700333 super.draw(canvas);
334 return;
335 }
336
Michael Jurkabdb5c532011-02-01 15:05:06 -0800337 final Drawable background = mBackground;
338 if (background != null) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700339 final int scrollX = getScrollX();
340 final int scrollY = getScrollY();
Winson Chung88127032010-12-13 12:11:33 -0800341
Michael Jurkabdb5c532011-02-01 15:05:06 -0800342 if (mBackgroundSizeChanged) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700343 background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop());
Michael Jurkabdb5c532011-02-01 15:05:06 -0800344 mBackgroundSizeChanged = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 }
Michael Jurkabdb5c532011-02-01 15:05:06 -0800346
347 if ((scrollX | scrollY) == 0) {
348 background.draw(canvas);
349 } else {
350 canvas.translate(scrollX, scrollY);
351 background.draw(canvas);
352 canvas.translate(-scrollX, -scrollY);
353 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800355
356 // If text is transparent, don't draw any shadow
Andrew Flynnbc239a12012-03-06 11:39:49 -0800357 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800358 getPaint().clearShadowLayer();
359 super.draw(canvas);
360 return;
361 }
362
Michael Jurkabdb5c532011-02-01 15:05:06 -0800363 // We enhance the shadow by drawing the shadow twice
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800364 getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800365 super.draw(canvas);
366 canvas.save(Canvas.CLIP_SAVE_FLAG);
Michael Jurka8b805b12012-04-18 14:23:14 -0700367 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
368 getScrollX() + getWidth(),
369 getScrollY() + getHeight(), Region.Op.INTERSECT);
Michael Jurkae7e3f6c2011-02-01 21:08:29 -0800370 getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
Michael Jurkabdb5c532011-02-01 15:05:06 -0800371 super.draw(canvas);
372 canvas.restore();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400374
375 @Override
376 protected void onAttachedToWindow() {
377 super.onAttachedToWindow();
Sunny Goyal95abbb32014-08-04 10:53:22 -0700378
Winson Chung656d11c2010-11-29 17:15:47 -0800379 if (mBackground != null) mBackground.setCallback(this);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700380
Winson Chungb745afb2015-03-02 11:51:23 -0800381 if (mIcon instanceof PreloadIconDrawable) {
382 ((PreloadIconDrawable) mIcon).applyPreloaderTheme(getPreloaderTheme());
Sunny Goyal95abbb32014-08-04 10:53:22 -0700383 }
Jason Monk02dd7ae2014-04-15 15:23:31 -0400384 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400385 }
386
387 @Override
388 protected void onDetachedFromWindow() {
389 super.onDetachedFromWindow();
Winson Chung656d11c2010-11-29 17:15:47 -0800390 if (mBackground != null) mBackground.setCallback(null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400391 }
Winson Chungaffd7b42010-08-20 15:11:56 -0700392
Adam Cohen477828c2013-09-20 12:05:49 -0700393 @Override
394 public void setTextColor(int color) {
395 mTextColor = color;
396 super.setTextColor(color);
397 }
398
Adam Cohen96bb7982014-07-07 11:58:56 -0700399 @Override
400 public void setTextColor(ColorStateList colors) {
401 mTextColor = colors.getDefaultColor();
402 super.setTextColor(colors);
Adam Cohen477828c2013-09-20 12:05:49 -0700403 }
404
Winson Chung5f8afe62013-08-12 16:19:28 -0700405 public void setTextVisibility(boolean visible) {
406 Resources res = getResources();
407 if (visible) {
Adam Cohen477828c2013-09-20 12:05:49 -0700408 super.setTextColor(mTextColor);
Winson Chung5f8afe62013-08-12 16:19:28 -0700409 } else {
Adam Cohen477828c2013-09-20 12:05:49 -0700410 super.setTextColor(res.getColor(android.R.color.transparent));
Winson Chung5f8afe62013-08-12 16:19:28 -0700411 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700412 }
413
Winson Chungaffd7b42010-08-20 15:11:56 -0700414 @Override
Winson Chung88f33452012-02-23 15:23:44 -0800415 public void cancelLongPress() {
416 super.cancelLongPress();
417
418 mLongPressHelper.cancelLongPress();
419 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500420
Sunny Goyal34942622014-08-29 17:20:55 -0700421 public void applyState(boolean promiseStateChanged) {
Sunny Goyale755d462014-07-22 13:48:29 -0700422 if (getTag() instanceof ShortcutInfo) {
Chris Wren40c5ed32014-06-24 18:24:23 -0400423 ShortcutInfo info = (ShortcutInfo) getTag();
Sunny Goyal34942622014-08-29 17:20:55 -0700424 final boolean isPromise = info.isPromise();
425 final int progressLevel = isPromise ?
426 ((info.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE) ?
427 info.getInstallProgress() : 0)) : 100;
Sunny Goyale755d462014-07-22 13:48:29 -0700428
Winson Chungb745afb2015-03-02 11:51:23 -0800429 if (mIcon != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700430 final PreloadIconDrawable preloadDrawable;
Winson Chungb745afb2015-03-02 11:51:23 -0800431 if (mIcon instanceof PreloadIconDrawable) {
432 preloadDrawable = (PreloadIconDrawable) mIcon;
Sunny Goyale755d462014-07-22 13:48:29 -0700433 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800434 preloadDrawable = new PreloadIconDrawable(mIcon, getPreloaderTheme());
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700435 setIcon(preloadDrawable, mIconSize);
Sunny Goyale755d462014-07-22 13:48:29 -0700436 }
437
438 preloadDrawable.setLevel(progressLevel);
Sunny Goyal34942622014-08-29 17:20:55 -0700439 if (promiseStateChanged) {
440 preloadDrawable.maybePerformFinishedAnimation();
Sunny Goyale755d462014-07-22 13:48:29 -0700441 }
Sunny Goyale755d462014-07-22 13:48:29 -0700442 }
Chris Wren40c5ed32014-06-24 18:24:23 -0400443 }
444 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700445
446 private Theme getPreloaderTheme() {
447 Object tag = getTag();
448 int style = ((tag != null) && (tag instanceof ShortcutInfo) &&
449 (((ShortcutInfo) tag).container >= 0)) ? R.style.PreloadIcon_Folder
450 : R.style.PreloadIcon;
451 Theme theme = sPreloaderThemes.get(style);
452 if (theme == null) {
453 theme = getResources().newTheme();
454 theme.applyStyle(style, true);
455 sPreloaderThemes.put(style, theme);
456 }
457 return theme;
458 }
Winson Chungb745afb2015-03-02 11:51:23 -0800459
460 /**
461 * Sets the icon for this view based on the layout direction.
462 */
Sunny Goyal70660032015-05-14 00:07:08 -0700463 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700464 private Drawable setIcon(Drawable icon, int iconSize) {
Winson Chungb745afb2015-03-02 11:51:23 -0800465 mIcon = icon;
466 if (iconSize != -1) {
467 mIcon.setBounds(0, 0, iconSize, iconSize);
468 }
469 if (mLayoutHorizontal) {
Sunny Goyal70660032015-05-14 00:07:08 -0700470 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
471 setCompoundDrawablesRelative(mIcon, null, null, null);
472 } else {
473 setCompoundDrawables(mIcon, null, null, null);
474 }
Winson Chungb745afb2015-03-02 11:51:23 -0800475 } else {
Sunny Goyal70660032015-05-14 00:07:08 -0700476 setCompoundDrawables(null, mIcon, null, null);
Winson Chungb745afb2015-03-02 11:51:23 -0800477 }
Winson Chungb745afb2015-03-02 11:51:23 -0800478 return icon;
479 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700480
Sunny Goyal69b75642015-05-15 17:00:24 -0700481 @Override
482 public void requestLayout() {
483 if (!mDisableRelayout) {
484 super.requestLayout();
485 }
486 }
487
Sunny Goyal34b65272015-03-11 16:56:52 -0700488 /**
489 * Applies the item info if it is same as what the view is pointing to currently.
490 */
491 public void reapplyItemInfo(final ItemInfo info) {
492 if (getTag() == info) {
493 mIconLoadRequest = null;
Sunny Goyal69b75642015-05-15 17:00:24 -0700494 mDisableRelayout = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700495 if (info instanceof AppInfo) {
496 applyFromApplicationInfo((AppInfo) info);
497 } else if (info instanceof ShortcutInfo) {
498 applyFromShortcutInfo((ShortcutInfo) info,
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700499 LauncherAppState.getInstance().getIconCache());
Sunny Goyal0e08f162015-05-12 11:32:39 -0700500 } else if (info instanceof PackageItemInfo) {
501 applyFromPackageItemInfo((PackageItemInfo) info);
Sunny Goyal34b65272015-03-11 16:56:52 -0700502 }
Sunny Goyal69b75642015-05-15 17:00:24 -0700503 mDisableRelayout = false;
Sunny Goyal34b65272015-03-11 16:56:52 -0700504 }
505 }
506
507 /**
508 * Verifies that the current icon is high-res otherwise posts a request to load the icon.
509 */
510 public void verifyHighRes() {
511 if (mIconLoadRequest != null) {
512 mIconLoadRequest.cancel();
513 mIconLoadRequest = null;
514 }
515 if (getTag() instanceof AppInfo) {
516 AppInfo info = (AppInfo) getTag();
517 if (info.usingLowResIcon) {
518 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
519 .updateIconInBackground(BubbleTextView.this, info);
520 }
521 } else if (getTag() instanceof ShortcutInfo) {
522 ShortcutInfo info = (ShortcutInfo) getTag();
523 if (info.usingLowResIcon) {
524 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
525 .updateIconInBackground(BubbleTextView.this, info);
526 }
Sunny Goyal0e08f162015-05-12 11:32:39 -0700527 } else if (getTag() instanceof PackageItemInfo) {
528 PackageItemInfo info = (PackageItemInfo) getTag();
529 if (info.usingLowResIcon) {
530 mIconLoadRequest = LauncherAppState.getInstance().getIconCache()
531 .updateIconInBackground(BubbleTextView.this, info);
532 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700533 }
534 }
Sunny Goyal4b6eb262015-05-14 19:24:40 -0700535
536 /**
537 * Interface to be implemented by the grand parent to allow click shadow effect.
538 */
539 public static interface BubbleTextShadowHandler {
540 void setPressedIcon(BubbleTextView icon, Bitmap background);
541 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542}