Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 19 | import android.animation.ObjectAnimator; |
Winson Chung | 4dbea79 | 2011-05-05 14:21:32 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 22 | import android.content.pm.PackageManager; |
| 23 | import android.content.pm.ResolveInfo; |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 24 | import android.content.res.Resources; |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 25 | import android.content.res.TypedArray; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 26 | import android.graphics.Bitmap; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 27 | import android.graphics.Canvas; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 28 | import android.graphics.Paint; |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 29 | import android.os.Handler; |
| 30 | import android.os.HandlerThread; |
| 31 | import android.os.Message; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 32 | import android.util.AttributeSet; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 33 | import android.view.KeyEvent; |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 34 | import android.widget.Checkable; |
Winson Chung | c84001e | 2010-11-09 12:20:57 -0800 | [diff] [blame] | 35 | |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 36 | import com.android.launcher.R; |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 37 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 38 | |
| 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 Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 43 | public class PagedViewIcon extends CachedTextView implements Checkable { |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 44 | private static final String TAG = "PagedViewIcon"; |
| 45 | |
| 46 | // holographic outline |
| 47 | private final Paint mPaint = new Paint(); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 48 | private Bitmap mCheckedOutline; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 49 | private Bitmap mHolographicOutline; |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 50 | private Bitmap mIcon; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 51 | |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 52 | private int mAlpha = 255; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 53 | private int mHolographicAlpha; |
| 54 | |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 55 | private boolean mIsChecked; |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 56 | private ObjectAnimator mCheckedAlphaAnimator; |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 57 | private float mCheckedAlpha = 1.0f; |
| 58 | private int mCheckedFadeInDuration; |
| 59 | private int mCheckedFadeOutDuration; |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 60 | |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 61 | HolographicPagedViewIcon mHolographicOutlineView; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 62 | private HolographicOutlineHelper mHolographicOutlineHelper; |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 63 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 64 | 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 Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 74 | |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 75 | // Set up fade in/out constants |
| 76 | final Resources r = context.getResources(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 77 | final int alpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha); |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 78 | if (alpha > 0) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 79 | 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 Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 86 | mHolographicOutlineView = new HolographicPagedViewIcon(context, this); |
| 87 | } |
| 88 | |
| 89 | protected HolographicPagedViewIcon getHolographicOutlineView() { |
| 90 | return mHolographicOutlineView; |
| 91 | } |
| 92 | |
| 93 | protected Bitmap getHolographicOutline() { |
| 94 | return mHolographicOutline; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 97 | public void applyFromApplicationInfo(ApplicationInfo info, boolean scaleUp, |
| 98 | HolographicOutlineHelper holoOutlineHelper) { |
| 99 | mHolographicOutlineHelper = holoOutlineHelper; |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 100 | mIcon = info.iconBitmap; |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 101 | setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 102 | setText(info.title); |
| 103 | setTag(info); |
| 104 | } |
| 105 | |
| 106 | public void applyFromResolveInfo(ResolveInfo info, PackageManager packageManager, |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 107 | IconCache modelIconCache, HolographicOutlineHelper holoOutlineHelper) { |
| 108 | mHolographicOutlineHelper = holoOutlineHelper; |
Winson Chung | 4dbea79 | 2011-05-05 14:21:32 -0700 | [diff] [blame] | 109 | ComponentName cn = new ComponentName(info.activityInfo.packageName, info.activityInfo.name); |
| 110 | mIcon = modelIconCache.getIcon(cn, info); |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 111 | setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 112 | setText(info.loadLabel(packageManager)); |
| 113 | setTag(info); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 114 | } |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 115 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 116 | public void setHolographicOutline(Bitmap holoOutline) { |
| 117 | mHolographicOutline = holoOutline; |
| 118 | getHolographicOutlineView().invalidate(); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 121 | @Override |
| 122 | public void setAlpha(float alpha) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 123 | final float viewAlpha = mHolographicOutlineHelper.viewAlphaInterpolator(alpha); |
| 124 | final float holographicAlpha = mHolographicOutlineHelper.highlightAlphaInterpolator(alpha); |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 125 | int newViewAlpha = (int) (viewAlpha * 255); |
| 126 | int newHolographicAlpha = (int) (holographicAlpha * 255); |
| 127 | if ((mAlpha != newViewAlpha) || (mHolographicAlpha != newHolographicAlpha)) { |
| 128 | mAlpha = newViewAlpha; |
| 129 | mHolographicAlpha = newHolographicAlpha; |
| 130 | super.setAlpha(viewAlpha); |
| 131 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 134 | public void invalidateCheckedImage() { |
| 135 | if (mCheckedOutline != null) { |
| 136 | mCheckedOutline.recycle(); |
| 137 | mCheckedOutline = null; |
| 138 | } |
| 139 | } |
| 140 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 141 | @Override |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 142 | protected void onDraw(Canvas canvas) { |
| 143 | if (mAlpha > 0) { |
| 144 | super.onDraw(canvas); |
| 145 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 146 | |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 147 | Bitmap overlay = null; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 148 | |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 149 | // draw any blended overlays |
Winson Chung | 6a70e9f | 2011-05-17 16:24:49 -0700 | [diff] [blame] | 150 | if (mCheckedOutline != null) { |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 151 | mPaint.setAlpha(255); |
| 152 | overlay = mCheckedOutline; |
| 153 | } |
| 154 | |
| 155 | if (overlay != null) { |
Winson Chung | c84001e | 2010-11-09 12:20:57 -0800 | [diff] [blame] | 156 | final int offset = getScrollX(); |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 157 | final int compoundPaddingLeft = getCompoundPaddingLeft(); |
| 158 | final int compoundPaddingRight = getCompoundPaddingRight(); |
| 159 | int hspace = getWidth() - compoundPaddingRight - compoundPaddingLeft; |
| 160 | canvas.drawBitmap(overlay, |
Winson Chung | c84001e | 2010-11-09 12:20:57 -0800 | [diff] [blame] | 161 | offset + compoundPaddingLeft + (hspace - overlay.getWidth()) / 2, |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 162 | mPaddingTop, |
| 163 | mPaint); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 167 | @Override |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 168 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 169 | return FocusHelper.handlePagedViewIconKeyEvent(this, keyCode, event) |
| 170 | || super.onKeyDown(keyCode, event); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 175 | return FocusHelper.handlePagedViewIconKeyEvent(this, keyCode, event) |
| 176 | || super.onKeyUp(keyCode, event); |
| 177 | } |
| 178 | |
| 179 | @Override |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 180 | public boolean isChecked() { |
| 181 | return mIsChecked; |
| 182 | } |
| 183 | |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 184 | void setChecked(boolean checked, boolean animate) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 185 | if (mIsChecked != checked) { |
| 186 | mIsChecked = checked; |
| 187 | |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 188 | float alpha; |
| 189 | int duration; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 190 | if (mIsChecked) { |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 191 | alpha = mCheckedAlpha; |
| 192 | duration = mCheckedFadeInDuration; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 193 | } else { |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 194 | alpha = 1.0f; |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 195 | duration = mCheckedFadeOutDuration; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 198 | // Initialize the animator |
| 199 | if (mCheckedAlphaAnimator != null) { |
| 200 | mCheckedAlphaAnimator.cancel(); |
| 201 | } |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 202 | if (animate) { |
| 203 | mCheckedAlphaAnimator = ObjectAnimator.ofFloat(this, "alpha", getAlpha(), alpha); |
| 204 | mCheckedAlphaAnimator.setDuration(duration); |
| 205 | mCheckedAlphaAnimator.start(); |
| 206 | } else { |
| 207 | setAlpha(alpha); |
| 208 | } |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 209 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 210 | invalidate(); |
| 211 | } |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | @Override |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 215 | public void setChecked(boolean checked) { |
| 216 | setChecked(checked, true); |
| 217 | } |
| 218 | |
| 219 | @Override |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 220 | public void toggle() { |
| 221 | setChecked(!mIsChecked); |
| 222 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 223 | } |