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 | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 21 | import android.content.res.Resources; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 22 | import android.graphics.Bitmap; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 23 | import android.graphics.Canvas; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 24 | import android.graphics.Paint; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 25 | import android.util.AttributeSet; |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 26 | import android.widget.Checkable; |
Michael Jurka | 4c4c001 | 2011-11-15 13:59:13 -0800 | [diff] [blame] | 27 | import android.widget.TextView; |
Winson Chung | c84001e | 2010-11-09 12:20:57 -0800 | [diff] [blame] | 28 | |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 29 | import com.android.launcher.R; |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 30 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * An icon on a PagedView, specifically for items in the launcher's paged view (with compound |
| 34 | * drawables on the top). |
| 35 | */ |
Michael Jurka | 4c4c001 | 2011-11-15 13:59:13 -0800 | [diff] [blame] | 36 | public class PagedViewIcon extends TextView implements Checkable { |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 37 | private static final String TAG = "PagedViewIcon"; |
| 38 | |
| 39 | // holographic outline |
| 40 | private final Paint mPaint = new Paint(); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 41 | private Bitmap mCheckedOutline; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 42 | private Bitmap mHolographicOutline; |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 43 | private Bitmap mIcon; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 44 | |
Winson Chung | 8812703 | 2010-12-13 12:11:33 -0800 | [diff] [blame] | 45 | private int mAlpha = 255; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 46 | private int mHolographicAlpha; |
| 47 | |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 48 | private boolean mIsChecked; |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 49 | private ObjectAnimator mCheckedAlphaAnimator; |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 50 | private float mCheckedAlpha = 1.0f; |
| 51 | private int mCheckedFadeInDuration; |
| 52 | private int mCheckedFadeOutDuration; |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 53 | |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 54 | HolographicPagedViewIcon mHolographicOutlineView; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 55 | private HolographicOutlineHelper mHolographicOutlineHelper; |
Winson Chung | 64a3cd4 | 2010-09-17 16:47:33 -0700 | [diff] [blame] | 56 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 57 | public PagedViewIcon(Context context) { |
| 58 | this(context, null); |
| 59 | } |
| 60 | |
| 61 | public PagedViewIcon(Context context, AttributeSet attrs) { |
| 62 | this(context, attrs, 0); |
| 63 | } |
| 64 | |
| 65 | public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) { |
| 66 | super(context, attrs, defStyle); |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 67 | |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 68 | // Set up fade in/out constants |
| 69 | final Resources r = context.getResources(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 70 | final int alpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha); |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 71 | if (alpha > 0) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 72 | mCheckedAlpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha) / 256.0f; |
| 73 | mCheckedFadeInDuration = |
| 74 | r.getInteger(R.integer.config_dragAppsCustomizeIconFadeInDuration); |
| 75 | mCheckedFadeOutDuration = |
| 76 | r.getInteger(R.integer.config_dragAppsCustomizeIconFadeOutDuration); |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 79 | mHolographicOutlineView = new HolographicPagedViewIcon(context, this); |
| 80 | } |
| 81 | |
| 82 | protected HolographicPagedViewIcon getHolographicOutlineView() { |
| 83 | return mHolographicOutlineView; |
| 84 | } |
| 85 | |
| 86 | protected Bitmap getHolographicOutline() { |
| 87 | return mHolographicOutline; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 90 | public void applyFromApplicationInfo(ApplicationInfo info, boolean scaleUp, |
| 91 | HolographicOutlineHelper holoOutlineHelper) { |
| 92 | mHolographicOutlineHelper = holoOutlineHelper; |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 93 | mIcon = info.iconBitmap; |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 94 | setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 95 | setText(info.title); |
| 96 | setTag(info); |
| 97 | } |
| 98 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 99 | public void setHolographicOutline(Bitmap holoOutline) { |
| 100 | mHolographicOutline = holoOutline; |
| 101 | getHolographicOutlineView().invalidate(); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 104 | @Override |
| 105 | public void setAlpha(float alpha) { |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 106 | final float viewAlpha = HolographicOutlineHelper.viewAlphaInterpolator(alpha); |
| 107 | final float holographicAlpha = HolographicOutlineHelper.highlightAlphaInterpolator(alpha); |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 108 | int newViewAlpha = (int) (viewAlpha * 255); |
| 109 | int newHolographicAlpha = (int) (holographicAlpha * 255); |
| 110 | if ((mAlpha != newViewAlpha) || (mHolographicAlpha != newHolographicAlpha)) { |
| 111 | mAlpha = newViewAlpha; |
| 112 | mHolographicAlpha = newHolographicAlpha; |
| 113 | super.setAlpha(viewAlpha); |
| 114 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 117 | public void invalidateCheckedImage() { |
| 118 | if (mCheckedOutline != null) { |
| 119 | mCheckedOutline.recycle(); |
| 120 | mCheckedOutline = null; |
| 121 | } |
| 122 | } |
| 123 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 124 | @Override |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 125 | protected void onDraw(Canvas canvas) { |
| 126 | if (mAlpha > 0) { |
| 127 | super.onDraw(canvas); |
| 128 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 129 | |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 130 | Bitmap overlay = null; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 131 | |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 132 | // draw any blended overlays |
Winson Chung | 6a70e9f | 2011-05-17 16:24:49 -0700 | [diff] [blame] | 133 | if (mCheckedOutline != null) { |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 134 | mPaint.setAlpha(255); |
| 135 | overlay = mCheckedOutline; |
| 136 | } |
| 137 | |
| 138 | if (overlay != null) { |
Winson Chung | c84001e | 2010-11-09 12:20:57 -0800 | [diff] [blame] | 139 | final int offset = getScrollX(); |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 140 | final int compoundPaddingLeft = getCompoundPaddingLeft(); |
| 141 | final int compoundPaddingRight = getCompoundPaddingRight(); |
| 142 | int hspace = getWidth() - compoundPaddingRight - compoundPaddingLeft; |
| 143 | canvas.drawBitmap(overlay, |
Winson Chung | c84001e | 2010-11-09 12:20:57 -0800 | [diff] [blame] | 144 | offset + compoundPaddingLeft + (hspace - overlay.getWidth()) / 2, |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 145 | mPaddingTop, |
| 146 | mPaint); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 150 | @Override |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 151 | public boolean isChecked() { |
| 152 | return mIsChecked; |
| 153 | } |
| 154 | |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 155 | void setChecked(boolean checked, boolean animate) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 156 | if (mIsChecked != checked) { |
| 157 | mIsChecked = checked; |
| 158 | |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 159 | float alpha; |
| 160 | int duration; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 161 | if (mIsChecked) { |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 162 | alpha = mCheckedAlpha; |
| 163 | duration = mCheckedFadeInDuration; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 164 | } else { |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 165 | alpha = 1.0f; |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 166 | duration = mCheckedFadeOutDuration; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 169 | // Initialize the animator |
| 170 | if (mCheckedAlphaAnimator != null) { |
| 171 | mCheckedAlphaAnimator.cancel(); |
| 172 | } |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 173 | if (animate) { |
| 174 | mCheckedAlphaAnimator = ObjectAnimator.ofFloat(this, "alpha", getAlpha(), alpha); |
| 175 | mCheckedAlphaAnimator.setDuration(duration); |
| 176 | mCheckedAlphaAnimator.start(); |
| 177 | } else { |
| 178 | setAlpha(alpha); |
| 179 | } |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 180 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 181 | invalidate(); |
| 182 | } |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 186 | public void setChecked(boolean checked) { |
| 187 | setChecked(checked, true); |
| 188 | } |
| 189 | |
| 190 | @Override |
Winson Chung | 03c568e | 2010-08-19 17:16:59 -0700 | [diff] [blame] | 191 | public void toggle() { |
| 192 | setChecked(!mIsChecked); |
| 193 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 194 | } |