blob: 4149ab618a6712e6b1369557f5f2b21e2f4cae3f [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;
Winson Chung3b187b82012-01-30 15:11:08 -080038 private boolean mLockDrawableState = 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 Chung3b187b82012-01-30 15:11:08 -080063 public void lockDrawableState() {
64 mLockDrawableState = true;
65 }
66
Winson Chunge4e50662012-01-23 14:45:13 -080067 public void resetDrawableState() {
Winson Chung3b187b82012-01-30 15:11:08 -080068 mLockDrawableState = false;
Winson Chunge4e50662012-01-23 14:45:13 -080069 post(new Runnable() {
70 @Override
71 public void run() {
72 refreshDrawableState();
73 }
74 });
75 }
76
Michael Jurkae3517d02012-01-12 07:46:24 -080077 protected void drawableStateChanged() {
Michael Jurkae3517d02012-01-12 07:46:24 -080078 super.drawableStateChanged();
Winson Chunge4e50662012-01-23 14:45:13 -080079
80 // We keep in the pressed state until resetDrawableState() is called to reset the press
81 // feedback
82 if (isPressed()) {
83 setAlpha(PRESS_ALPHA);
84 if (mPressedCallback != null) {
85 mPressedCallback.iconPressed(this);
86 }
Winson Chung3b187b82012-01-30 15:11:08 -080087 } else if (!mLockDrawableState) {
Winson Chunge4e50662012-01-23 14:45:13 -080088 setAlpha(1f);
Winson Chunge4e50662012-01-23 14:45:13 -080089 }
Michael Jurkae3517d02012-01-12 07:46:24 -080090 }
Winson Chungb3347bb2010-08-19 14:51:28 -070091}