blob: af10f189c6f2aac1940464be437e3eb6dcb762cc [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 Chungb3347bb2010-08-19 14:51:28 -070020import android.content.Context;
Winson Chung59e1f9a2010-12-21 11:31:54 -080021import android.content.res.Resources;
Winson Chungb3347bb2010-08-19 14:51:28 -070022import android.graphics.Bitmap;
Winson Chungb3347bb2010-08-19 14:51:28 -070023import android.graphics.Canvas;
Winson Chungb3347bb2010-08-19 14:51:28 -070024import android.graphics.Paint;
Winson Chungb3347bb2010-08-19 14:51:28 -070025import android.util.AttributeSet;
Winson Chung03c568e2010-08-19 17:16:59 -070026import android.widget.Checkable;
Michael Jurka4c4c0012011-11-15 13:59:13 -080027import android.widget.TextView;
Winson Chungc84001e2010-11-09 12:20:57 -080028
Winson Chung63257c12011-05-05 17:06:13 -070029import com.android.launcher.R;
Winson Chung241c3b42010-08-25 16:53:03 -070030
Winson Chungb3347bb2010-08-19 14:51:28 -070031
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 Jurka4c4c0012011-11-15 13:59:13 -080036public class PagedViewIcon extends TextView implements Checkable {
Winson Chungb3347bb2010-08-19 14:51:28 -070037 private static final String TAG = "PagedViewIcon";
38
39 // holographic outline
40 private final Paint mPaint = new Paint();
Winson Chung5f2aa4e2010-08-20 14:49:25 -070041 private Bitmap mCheckedOutline;
Winson Chungb3347bb2010-08-19 14:51:28 -070042 private Bitmap mHolographicOutline;
Michael Jurka0620bec2010-10-25 17:50:09 -070043 private Bitmap mIcon;
Winson Chungb3347bb2010-08-19 14:51:28 -070044
Winson Chung88127032010-12-13 12:11:33 -080045 private int mAlpha = 255;
Winson Chungb3347bb2010-08-19 14:51:28 -070046 private int mHolographicAlpha;
47
Winson Chung03c568e2010-08-19 17:16:59 -070048 private boolean mIsChecked;
Winson Chungcd4bc492010-12-09 18:52:32 -080049 private ObjectAnimator mCheckedAlphaAnimator;
Winson Chung59e1f9a2010-12-21 11:31:54 -080050 private float mCheckedAlpha = 1.0f;
51 private int mCheckedFadeInDuration;
52 private int mCheckedFadeOutDuration;
Winson Chung03c568e2010-08-19 17:16:59 -070053
Michael Jurka8245a862011-02-01 17:53:59 -080054 HolographicPagedViewIcon mHolographicOutlineView;
Winson Chungb44b5242011-06-13 11:32:14 -070055 private HolographicOutlineHelper mHolographicOutlineHelper;
Winson Chung64a3cd42010-09-17 16:47:33 -070056
Winson Chungb3347bb2010-08-19 14:51:28 -070057 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 Chung29d6fea2010-12-01 15:47:31 -080067
Winson Chung59e1f9a2010-12-21 11:31:54 -080068 // Set up fade in/out constants
69 final Resources r = context.getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -070070 final int alpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha);
Winson Chung59e1f9a2010-12-21 11:31:54 -080071 if (alpha > 0) {
Winson Chung785d2eb2011-04-14 16:08:02 -070072 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 Chung59e1f9a2010-12-21 11:31:54 -080077 }
78
Michael Jurka8245a862011-02-01 17:53:59 -080079 mHolographicOutlineView = new HolographicPagedViewIcon(context, this);
80 }
81
82 protected HolographicPagedViewIcon getHolographicOutlineView() {
83 return mHolographicOutlineView;
84 }
85
86 protected Bitmap getHolographicOutline() {
87 return mHolographicOutline;
Winson Chungb3347bb2010-08-19 14:51:28 -070088 }
89
Winson Chungb44b5242011-06-13 11:32:14 -070090 public void applyFromApplicationInfo(ApplicationInfo info, boolean scaleUp,
91 HolographicOutlineHelper holoOutlineHelper) {
92 mHolographicOutlineHelper = holoOutlineHelper;
Michael Jurkac9a96192010-11-01 11:52:08 -070093 mIcon = info.iconBitmap;
Michael Jurka0620bec2010-10-25 17:50:09 -070094 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -070095 setText(info.title);
96 setTag(info);
97 }
98
Winson Chungb44b5242011-06-13 11:32:14 -070099 public void setHolographicOutline(Bitmap holoOutline) {
100 mHolographicOutline = holoOutline;
101 getHolographicOutlineView().invalidate();
Winson Chung241c3b42010-08-25 16:53:03 -0700102 }
103
Winson Chungb3347bb2010-08-19 14:51:28 -0700104 @Override
105 public void setAlpha(float alpha) {
Winson Chungc3eecff2011-07-11 17:44:15 -0700106 final float viewAlpha = HolographicOutlineHelper.viewAlphaInterpolator(alpha);
107 final float holographicAlpha = HolographicOutlineHelper.highlightAlphaInterpolator(alpha);
Winson Chunge22a8e92010-11-12 13:40:58 -0800108 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 Chungb3347bb2010-08-19 14:51:28 -0700115 }
116
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700117 public void invalidateCheckedImage() {
118 if (mCheckedOutline != null) {
119 mCheckedOutline.recycle();
120 mCheckedOutline = null;
121 }
122 }
123
Winson Chungb3347bb2010-08-19 14:51:28 -0700124 @Override
Michael Jurka0620bec2010-10-25 17:50:09 -0700125 protected void onDraw(Canvas canvas) {
126 if (mAlpha > 0) {
127 super.onDraw(canvas);
128 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700129
Michael Jurka0620bec2010-10-25 17:50:09 -0700130 Bitmap overlay = null;
Winson Chungb3347bb2010-08-19 14:51:28 -0700131
Michael Jurka0620bec2010-10-25 17:50:09 -0700132 // draw any blended overlays
Winson Chung6a70e9f2011-05-17 16:24:49 -0700133 if (mCheckedOutline != null) {
Michael Jurka0620bec2010-10-25 17:50:09 -0700134 mPaint.setAlpha(255);
135 overlay = mCheckedOutline;
136 }
137
138 if (overlay != null) {
Winson Chungc84001e2010-11-09 12:20:57 -0800139 final int offset = getScrollX();
Michael Jurka0620bec2010-10-25 17:50:09 -0700140 final int compoundPaddingLeft = getCompoundPaddingLeft();
141 final int compoundPaddingRight = getCompoundPaddingRight();
142 int hspace = getWidth() - compoundPaddingRight - compoundPaddingLeft;
143 canvas.drawBitmap(overlay,
Winson Chungc84001e2010-11-09 12:20:57 -0800144 offset + compoundPaddingLeft + (hspace - overlay.getWidth()) / 2,
Michael Jurka0620bec2010-10-25 17:50:09 -0700145 mPaddingTop,
146 mPaint);
Winson Chungb3347bb2010-08-19 14:51:28 -0700147 }
148 }
149
Winson Chungb3347bb2010-08-19 14:51:28 -0700150 @Override
Winson Chung03c568e2010-08-19 17:16:59 -0700151 public boolean isChecked() {
152 return mIsChecked;
153 }
154
Patrick Dubroy5f445422011-02-18 14:35:21 -0800155 void setChecked(boolean checked, boolean animate) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700156 if (mIsChecked != checked) {
157 mIsChecked = checked;
158
Winson Chungcd4bc492010-12-09 18:52:32 -0800159 float alpha;
160 int duration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700161 if (mIsChecked) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800162 alpha = mCheckedAlpha;
163 duration = mCheckedFadeInDuration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700164 } else {
Winson Chungcd4bc492010-12-09 18:52:32 -0800165 alpha = 1.0f;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800166 duration = mCheckedFadeOutDuration;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700167 }
168
Winson Chungcd4bc492010-12-09 18:52:32 -0800169 // Initialize the animator
170 if (mCheckedAlphaAnimator != null) {
171 mCheckedAlphaAnimator.cancel();
172 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800173 if (animate) {
174 mCheckedAlphaAnimator = ObjectAnimator.ofFloat(this, "alpha", getAlpha(), alpha);
175 mCheckedAlphaAnimator.setDuration(duration);
176 mCheckedAlphaAnimator.start();
177 } else {
178 setAlpha(alpha);
179 }
Winson Chungcd4bc492010-12-09 18:52:32 -0800180
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700181 invalidate();
182 }
Winson Chung03c568e2010-08-19 17:16:59 -0700183 }
184
185 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800186 public void setChecked(boolean checked) {
187 setChecked(checked, true);
188 }
189
190 @Override
Winson Chung03c568e2010-08-19 17:16:59 -0700191 public void toggle() {
192 setChecked(!mIsChecked);
193 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700194}