blob: fa9ec5a15c84aaec91bb66fdf1b1d0697a8069c9 [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chungb3347bb2010-08-19 14:51:28 -070018
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;
Winson Chungc58497e2013-09-03 17:48:37 -070022import android.util.TypedValue;
Michael Jurka4c4c0012011-11-15 13:59:13 -080023import android.widget.TextView;
Winson Chungc84001e2010-11-09 12:20:57 -080024
Winson Chungb3347bb2010-08-19 14:51:28 -070025/**
26 * An icon on a PagedView, specifically for items in the launcher's paged view (with compound
27 * drawables on the top).
28 */
Michael Jurka82369a12012-01-12 08:08:38 -080029public class PagedViewIcon extends TextView {
Winson Chunge4e50662012-01-23 14:45:13 -080030 /** A simple callback interface to allow a PagedViewIcon to notify when it has been pressed */
31 public static interface PressedCallback {
32 void iconPressed(PagedViewIcon icon);
33 }
34
Michael Jurka3a9fced2012-04-13 14:44:29 -070035 @SuppressWarnings("unused")
Winson Chungb3347bb2010-08-19 14:51:28 -070036 private static final String TAG = "PagedViewIcon";
Winson Chunge4e50662012-01-23 14:45:13 -080037 private static final float PRESS_ALPHA = 0.4f;
38
39 private PagedViewIcon.PressedCallback mPressedCallback;
Winson Chung3b187b82012-01-30 15:11:08 -080040 private boolean mLockDrawableState = false;
Winson Chungb3347bb2010-08-19 14:51:28 -070041
Michael Jurka0620bec2010-10-25 17:50:09 -070042 private Bitmap mIcon;
Winson Chungb3347bb2010-08-19 14:51:28 -070043
Winson Chungb3347bb2010-08-19 14:51:28 -070044 public PagedViewIcon(Context context) {
45 this(context, null);
46 }
47
48 public PagedViewIcon(Context context, AttributeSet attrs) {
49 this(context, attrs, 0);
50 }
51
52 public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) {
53 super(context, attrs, defStyle);
Michael Jurka8245a862011-02-01 17:53:59 -080054 }
55
Winson Chungc58497e2013-09-03 17:48:37 -070056 public void onFinishInflate() {
57 super.onFinishInflate();
58
59 // Ensure we are using the right text size
60 LauncherAppState app = LauncherAppState.getInstance();
61 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
62 setTextSize(TypedValue.COMPLEX_UNIT_SP, grid.iconTextSize);
63 }
64
Michael Jurkaeadbfc52013-09-04 00:45:37 +020065 public void applyFromApplicationInfo(AppInfo info, boolean scaleUp,
Winson Chunge4e50662012-01-23 14:45:13 -080066 PagedViewIcon.PressedCallback cb) {
Michael Jurkac9a96192010-11-01 11:52:08 -070067 mIcon = info.iconBitmap;
Winson Chunge4e50662012-01-23 14:45:13 -080068 mPressedCallback = cb;
Michael Jurka0620bec2010-10-25 17:50:09 -070069 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -070070 setText(info.title);
71 setTag(info);
72 }
Michael Jurkae3517d02012-01-12 07:46:24 -080073
Winson Chung3b187b82012-01-30 15:11:08 -080074 public void lockDrawableState() {
75 mLockDrawableState = true;
76 }
77
Winson Chunge4e50662012-01-23 14:45:13 -080078 public void resetDrawableState() {
Winson Chung3b187b82012-01-30 15:11:08 -080079 mLockDrawableState = false;
Winson Chunge4e50662012-01-23 14:45:13 -080080 post(new Runnable() {
81 @Override
82 public void run() {
83 refreshDrawableState();
84 }
85 });
86 }
87
Michael Jurkae3517d02012-01-12 07:46:24 -080088 protected void drawableStateChanged() {
Michael Jurkae3517d02012-01-12 07:46:24 -080089 super.drawableStateChanged();
Winson Chunge4e50662012-01-23 14:45:13 -080090
91 // We keep in the pressed state until resetDrawableState() is called to reset the press
92 // feedback
93 if (isPressed()) {
94 setAlpha(PRESS_ALPHA);
95 if (mPressedCallback != null) {
96 mPressedCallback.iconPressed(this);
97 }
Winson Chung3b187b82012-01-30 15:11:08 -080098 } else if (!mLockDrawableState) {
Winson Chunge4e50662012-01-23 14:45:13 -080099 setAlpha(1f);
Winson Chunge4e50662012-01-23 14:45:13 -0800100 }
Michael Jurkae3517d02012-01-12 07:46:24 -0800101 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700102}