blob: d2aa31f86a2417d3da3d533ba1e24c93bdcd7ff4 [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
Michael Jurka3a9fced2012-04-13 14:44:29 -070034 @SuppressWarnings("unused")
Winson Chungb3347bb2010-08-19 14:51:28 -070035 private static final String TAG = "PagedViewIcon";
Winson Chunge4e50662012-01-23 14:45:13 -080036 private static final float PRESS_ALPHA = 0.4f;
37
38 private PagedViewIcon.PressedCallback mPressedCallback;
Winson Chung3b187b82012-01-30 15:11:08 -080039 private boolean mLockDrawableState = false;
Winson Chungb3347bb2010-08-19 14:51:28 -070040
Michael Jurka0620bec2010-10-25 17:50:09 -070041 private Bitmap mIcon;
Winson Chungb3347bb2010-08-19 14:51:28 -070042
Winson Chungb3347bb2010-08-19 14:51:28 -070043 public PagedViewIcon(Context context) {
44 this(context, null);
45 }
46
47 public PagedViewIcon(Context context, AttributeSet attrs) {
48 this(context, attrs, 0);
49 }
50
51 public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) {
52 super(context, attrs, defStyle);
Michael Jurka8245a862011-02-01 17:53:59 -080053 }
54
Winson Chunge4e50662012-01-23 14:45:13 -080055 public void applyFromApplicationInfo(ApplicationInfo info, boolean scaleUp,
56 PagedViewIcon.PressedCallback cb) {
Michael Jurkac9a96192010-11-01 11:52:08 -070057 mIcon = info.iconBitmap;
Winson Chunge4e50662012-01-23 14:45:13 -080058 mPressedCallback = cb;
Michael Jurka0620bec2010-10-25 17:50:09 -070059 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -070060 setText(info.title);
61 setTag(info);
62 }
Michael Jurkae3517d02012-01-12 07:46:24 -080063
Winson Chung3b187b82012-01-30 15:11:08 -080064 public void lockDrawableState() {
65 mLockDrawableState = true;
66 }
67
Winson Chunge4e50662012-01-23 14:45:13 -080068 public void resetDrawableState() {
Winson Chung3b187b82012-01-30 15:11:08 -080069 mLockDrawableState = false;
Winson Chunge4e50662012-01-23 14:45:13 -080070 post(new Runnable() {
71 @Override
72 public void run() {
73 refreshDrawableState();
74 }
75 });
76 }
77
Michael Jurkae3517d02012-01-12 07:46:24 -080078 protected void drawableStateChanged() {
Michael Jurkae3517d02012-01-12 07:46:24 -080079 super.drawableStateChanged();
Winson Chunge4e50662012-01-23 14:45:13 -080080
81 // We keep in the pressed state until resetDrawableState() is called to reset the press
82 // feedback
83 if (isPressed()) {
84 setAlpha(PRESS_ALPHA);
85 if (mPressedCallback != null) {
86 mPressedCallback.iconPressed(this);
87 }
Winson Chung3b187b82012-01-30 15:11:08 -080088 } else if (!mLockDrawableState) {
Winson Chunge4e50662012-01-23 14:45:13 -080089 setAlpha(1f);
Winson Chunge4e50662012-01-23 14:45:13 -080090 }
Michael Jurkae3517d02012-01-12 07:46:24 -080091 }
Winson Chungb3347bb2010-08-19 14:51:28 -070092}