blob: 00f9321c652b8b64830c9f5a4f16577ac27b41c9 [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
19import android.content.Context;
Winson Chungb3347bb2010-08-19 14:51:28 -070020import android.graphics.Bitmap;
Winson Chungb3347bb2010-08-19 14:51:28 -070021import android.util.AttributeSet;
Michael Jurka4c4c0012011-11-15 13:59:13 -080022import android.widget.TextView;
Winson Chungc84001e2010-11-09 12:20:57 -080023
Winson Chungb3347bb2010-08-19 14:51:28 -070024/**
25 * An icon on a PagedView, specifically for items in the launcher's paged view (with compound
26 * drawables on the top).
27 */
Michael Jurka82369a12012-01-12 08:08:38 -080028public class PagedViewIcon extends TextView {
Winson Chunge4e50662012-01-23 14:45:13 -080029 /** A simple callback interface to allow a PagedViewIcon to notify when it has been pressed */
30 public static interface PressedCallback {
31 void iconPressed(PagedViewIcon icon);
32 }
33
Winson Chungb3347bb2010-08-19 14:51:28 -070034 private static final String TAG = "PagedViewIcon";
Winson Chunge4e50662012-01-23 14:45:13 -080035 private static final float PRESS_ALPHA = 0.4f;
36
37 private PagedViewIcon.PressedCallback mPressedCallback;
38 private boolean mResetDrawableState = false;
Winson Chungb3347bb2010-08-19 14:51:28 -070039
Michael Jurka0620bec2010-10-25 17:50:09 -070040 private Bitmap mIcon;
Winson Chungb3347bb2010-08-19 14:51:28 -070041
Winson Chungb3347bb2010-08-19 14:51:28 -070042 public PagedViewIcon(Context context) {
43 this(context, null);
44 }
45
46 public PagedViewIcon(Context context, AttributeSet attrs) {
47 this(context, attrs, 0);
48 }
49
50 public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) {
51 super(context, attrs, defStyle);
Michael Jurka8245a862011-02-01 17:53:59 -080052 }
53
Winson Chunge4e50662012-01-23 14:45:13 -080054 public void applyFromApplicationInfo(ApplicationInfo info, boolean scaleUp,
55 PagedViewIcon.PressedCallback cb) {
Michael Jurkac9a96192010-11-01 11:52:08 -070056 mIcon = info.iconBitmap;
Winson Chunge4e50662012-01-23 14:45:13 -080057 mPressedCallback = cb;
Michael Jurka0620bec2010-10-25 17:50:09 -070058 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -070059 setText(info.title);
60 setTag(info);
61 }
Michael Jurkae3517d02012-01-12 07:46:24 -080062
Winson Chunge4e50662012-01-23 14:45:13 -080063 public void resetDrawableState() {
64 mResetDrawableState = true;
65 post(new Runnable() {
66 @Override
67 public void run() {
68 refreshDrawableState();
69 }
70 });
71 }
72
Michael Jurkae3517d02012-01-12 07:46:24 -080073 protected void drawableStateChanged() {
Michael Jurkae3517d02012-01-12 07:46:24 -080074 super.drawableStateChanged();
Winson Chunge4e50662012-01-23 14:45:13 -080075
76 // We keep in the pressed state until resetDrawableState() is called to reset the press
77 // feedback
78 if (isPressed()) {
79 setAlpha(PRESS_ALPHA);
80 if (mPressedCallback != null) {
81 mPressedCallback.iconPressed(this);
82 }
83 } else if (mResetDrawableState) {
84 setAlpha(1f);
85 mResetDrawableState = false;
86 }
Michael Jurkae3517d02012-01-12 07:46:24 -080087 }
Winson Chungb3347bb2010-08-19 14:51:28 -070088}