blob: 3ae978d19943ea9fdd080b471913834b8a6b4c8a [file] [log] [blame]
Winson Chungb3347bb2010-08-19 14:51:28 -07001/*
2 * Copyright (C) 2010 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
17package com.android.launcher2;
18
Winson Chungcd4bc492010-12-09 18:52:32 -080019import android.animation.ObjectAnimator;
Winson Chung4dbea792011-05-05 14:21:32 -070020import android.content.ComponentName;
Winson Chungb3347bb2010-08-19 14:51:28 -070021import android.content.Context;
Winson Chung241c3b42010-08-25 16:53:03 -070022import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
Winson Chung59e1f9a2010-12-21 11:31:54 -080024import android.content.res.Resources;
Winson Chung64a3cd42010-09-17 16:47:33 -070025import android.content.res.TypedArray;
Winson Chungb3347bb2010-08-19 14:51:28 -070026import android.graphics.Bitmap;
Winson Chungb3347bb2010-08-19 14:51:28 -070027import android.graphics.Canvas;
Winson Chungb3347bb2010-08-19 14:51:28 -070028import android.graphics.Paint;
Michael Jurka0620bec2010-10-25 17:50:09 -070029import android.os.Handler;
30import android.os.HandlerThread;
31import android.os.Message;
Winson Chungb3347bb2010-08-19 14:51:28 -070032import android.util.AttributeSet;
Winson Chung97d85d22011-04-13 11:27:36 -070033import android.view.KeyEvent;
Winson Chung03c568e2010-08-19 17:16:59 -070034import android.widget.Checkable;
Winson Chungc84001e2010-11-09 12:20:57 -080035
Winson Chung63257c12011-05-05 17:06:13 -070036import com.android.launcher.R;
Winson Chung241c3b42010-08-25 16:53:03 -070037
Winson Chungb3347bb2010-08-19 14:51:28 -070038
39/**
40 * An icon on a PagedView, specifically for items in the launcher's paged view (with compound
41 * drawables on the top).
42 */
Michael Jurkac0759f52011-02-03 16:47:14 -080043public class PagedViewIcon extends CachedTextView implements Checkable {
Winson Chungb3347bb2010-08-19 14:51:28 -070044 private static final String TAG = "PagedViewIcon";
45
46 // holographic outline
47 private final Paint mPaint = new Paint();
Winson Chung5f2aa4e2010-08-20 14:49:25 -070048 private Bitmap mCheckedOutline;
Winson Chungb3347bb2010-08-19 14:51:28 -070049 private Bitmap mHolographicOutline;
Michael Jurka0620bec2010-10-25 17:50:09 -070050 private Bitmap mIcon;
Winson Chungb3347bb2010-08-19 14:51:28 -070051
Winson Chung88127032010-12-13 12:11:33 -080052 private int mAlpha = 255;
Winson Chungb3347bb2010-08-19 14:51:28 -070053 private int mHolographicAlpha;
54
Winson Chung03c568e2010-08-19 17:16:59 -070055 private boolean mIsChecked;
Winson Chungcd4bc492010-12-09 18:52:32 -080056 private ObjectAnimator mCheckedAlphaAnimator;
Winson Chung59e1f9a2010-12-21 11:31:54 -080057 private float mCheckedAlpha = 1.0f;
58 private int mCheckedFadeInDuration;
59 private int mCheckedFadeOutDuration;
Winson Chung03c568e2010-08-19 17:16:59 -070060
Michael Jurka8245a862011-02-01 17:53:59 -080061 HolographicPagedViewIcon mHolographicOutlineView;
Winson Chungb44b5242011-06-13 11:32:14 -070062 private HolographicOutlineHelper mHolographicOutlineHelper;
Winson Chung64a3cd42010-09-17 16:47:33 -070063
Winson Chungb3347bb2010-08-19 14:51:28 -070064 public PagedViewIcon(Context context) {
65 this(context, null);
66 }
67
68 public PagedViewIcon(Context context, AttributeSet attrs) {
69 this(context, attrs, 0);
70 }
71
72 public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) {
73 super(context, attrs, defStyle);
Winson Chung29d6fea2010-12-01 15:47:31 -080074
Winson Chung59e1f9a2010-12-21 11:31:54 -080075 // Set up fade in/out constants
76 final Resources r = context.getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -070077 final int alpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha);
Winson Chung59e1f9a2010-12-21 11:31:54 -080078 if (alpha > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -070079 mCheckedAlpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha) / 256.0f;
80 mCheckedFadeInDuration =
81 r.getInteger(R.integer.config_dragAppsCustomizeIconFadeInDuration);
82 mCheckedFadeOutDuration =
83 r.getInteger(R.integer.config_dragAppsCustomizeIconFadeOutDuration);
Winson Chung59e1f9a2010-12-21 11:31:54 -080084 }
85
Michael Jurka8245a862011-02-01 17:53:59 -080086 mHolographicOutlineView = new HolographicPagedViewIcon(context, this);
87 }
88
89 protected HolographicPagedViewIcon getHolographicOutlineView() {
90 return mHolographicOutlineView;
91 }
92
93 protected Bitmap getHolographicOutline() {
94 return mHolographicOutline;
Winson Chungb3347bb2010-08-19 14:51:28 -070095 }
96
Winson Chungb44b5242011-06-13 11:32:14 -070097 public void applyFromApplicationInfo(ApplicationInfo info, boolean scaleUp,
98 HolographicOutlineHelper holoOutlineHelper) {
99 mHolographicOutlineHelper = holoOutlineHelper;
Michael Jurkac9a96192010-11-01 11:52:08 -0700100 mIcon = info.iconBitmap;
Michael Jurka0620bec2010-10-25 17:50:09 -0700101 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -0700102 setText(info.title);
103 setTag(info);
104 }
105
Winson Chungb44b5242011-06-13 11:32:14 -0700106 public void setHolographicOutline(Bitmap holoOutline) {
107 mHolographicOutline = holoOutline;
108 getHolographicOutlineView().invalidate();
Winson Chung241c3b42010-08-25 16:53:03 -0700109 }
110
Winson Chungb3347bb2010-08-19 14:51:28 -0700111 @Override
112 public void setAlpha(float alpha) {
Winson Chungc3eecff2011-07-11 17:44:15 -0700113 final float viewAlpha = HolographicOutlineHelper.viewAlphaInterpolator(alpha);
114 final float holographicAlpha = HolographicOutlineHelper.highlightAlphaInterpolator(alpha);
Winson Chunge22a8e92010-11-12 13:40:58 -0800115 int newViewAlpha = (int) (viewAlpha * 255);
116 int newHolographicAlpha = (int) (holographicAlpha * 255);
117 if ((mAlpha != newViewAlpha) || (mHolographicAlpha != newHolographicAlpha)) {
118 mAlpha = newViewAlpha;
119 mHolographicAlpha = newHolographicAlpha;
120 super.setAlpha(viewAlpha);
121 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700122 }
123
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700124 public void invalidateCheckedImage() {
125 if (mCheckedOutline != null) {
126 mCheckedOutline.recycle();
127 mCheckedOutline = null;
128 }
129 }
130
Winson Chungb3347bb2010-08-19 14:51:28 -0700131 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700132 protected void onDraw(Canvas canvas) {
133 if (mAlpha > 0) {
134 super.onDraw(canvas);
135 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700136
Michael Jurka0620bec2010-10-25 17:50:09 -0700137 Bitmap overlay = null;
Winson Chungb3347bb2010-08-19 14:51:28 -0700138
Michael Jurka0620bec2010-10-25 17:50:09 -0700139 // draw any blended overlays
Winson Chung6a70e9f2011-05-17 16:24:49 -0700140 if (mCheckedOutline != null) {
Michael Jurka0620bec2010-10-25 17:50:09 -0700141 mPaint.setAlpha(255);
142 overlay = mCheckedOutline;
143 }
144
145 if (overlay != null) {
Winson Chungc84001e2010-11-09 12:20:57 -0800146 final int offset = getScrollX();
Michael Jurka0620bec2010-10-25 17:50:09 -0700147 final int compoundPaddingLeft = getCompoundPaddingLeft();
148 final int compoundPaddingRight = getCompoundPaddingRight();
149 int hspace = getWidth() - compoundPaddingRight - compoundPaddingLeft;
150 canvas.drawBitmap(overlay,
Winson Chungc84001e2010-11-09 12:20:57 -0800151 offset + compoundPaddingLeft + (hspace - overlay.getWidth()) / 2,
Michael Jurka0620bec2010-10-25 17:50:09 -0700152 mPaddingTop,
153 mPaint);
Winson Chungb3347bb2010-08-19 14:51:28 -0700154 }
155 }
156
Winson Chungb3347bb2010-08-19 14:51:28 -0700157 @Override
Winson Chung97d85d22011-04-13 11:27:36 -0700158 public boolean onKeyDown(int keyCode, KeyEvent event) {
159 return FocusHelper.handlePagedViewIconKeyEvent(this, keyCode, event)
160 || super.onKeyDown(keyCode, event);
161 }
162
163 @Override
164 public boolean onKeyUp(int keyCode, KeyEvent event) {
165 return FocusHelper.handlePagedViewIconKeyEvent(this, keyCode, event)
166 || super.onKeyUp(keyCode, event);
167 }
168
169 @Override
Winson Chung03c568e2010-08-19 17:16:59 -0700170 public boolean isChecked() {
171 return mIsChecked;
172 }
173
Patrick Dubroy5f445422011-02-18 14:35:21 -0800174 void setChecked(boolean checked, boolean animate) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700175 if (mIsChecked != checked) {
176 mIsChecked = checked;
177
Winson Chungcd4bc492010-12-09 18:52:32 -0800178 float alpha;
179 int duration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700180 if (mIsChecked) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800181 alpha = mCheckedAlpha;
182 duration = mCheckedFadeInDuration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700183 } else {
Winson Chungcd4bc492010-12-09 18:52:32 -0800184 alpha = 1.0f;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800185 duration = mCheckedFadeOutDuration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700186 }
187
Winson Chungcd4bc492010-12-09 18:52:32 -0800188 // Initialize the animator
189 if (mCheckedAlphaAnimator != null) {
190 mCheckedAlphaAnimator.cancel();
191 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800192 if (animate) {
193 mCheckedAlphaAnimator = ObjectAnimator.ofFloat(this, "alpha", getAlpha(), alpha);
194 mCheckedAlphaAnimator.setDuration(duration);
195 mCheckedAlphaAnimator.start();
196 } else {
197 setAlpha(alpha);
198 }
Winson Chungcd4bc492010-12-09 18:52:32 -0800199
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700200 invalidate();
201 }
Winson Chung03c568e2010-08-19 17:16:59 -0700202 }
203
204 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800205 public void setChecked(boolean checked) {
206 setChecked(checked, true);
207 }
208
209 @Override
Winson Chung03c568e2010-08-19 17:16:59 -0700210 public void toggle() {
211 setChecked(!mIsChecked);
212 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700213}