blob: e819d5edf0b09b7bd5b23468b038cef9d919b9a3 [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 Chung2d75f122013-09-23 16:53:31 -070021import android.graphics.Canvas;
22import android.graphics.Region;
Winson Chung67ca7e42013-10-31 16:53:19 -070023import android.graphics.drawable.Drawable;
Winson Chungb3347bb2010-08-19 14:51:28 -070024import android.util.AttributeSet;
Winson Chungc58497e2013-09-03 17:48:37 -070025import android.util.TypedValue;
Michael Jurka4c4c0012011-11-15 13:59:13 -080026import android.widget.TextView;
Winson Chungc84001e2010-11-09 12:20:57 -080027
Winson Chungb3347bb2010-08-19 14:51:28 -070028/**
29 * An icon on a PagedView, specifically for items in the launcher's paged view (with compound
30 * drawables on the top).
31 */
Michael Jurka82369a12012-01-12 08:08:38 -080032public class PagedViewIcon extends TextView {
Winson Chunge4e50662012-01-23 14:45:13 -080033 /** A simple callback interface to allow a PagedViewIcon to notify when it has been pressed */
34 public static interface PressedCallback {
35 void iconPressed(PagedViewIcon icon);
36 }
37
Michael Jurka3a9fced2012-04-13 14:44:29 -070038 @SuppressWarnings("unused")
Winson Chungb3347bb2010-08-19 14:51:28 -070039 private static final String TAG = "PagedViewIcon";
Winson Chunge4e50662012-01-23 14:45:13 -080040 private static final float PRESS_ALPHA = 0.4f;
41
42 private PagedViewIcon.PressedCallback mPressedCallback;
Winson Chung3b187b82012-01-30 15:11:08 -080043 private boolean mLockDrawableState = false;
Winson Chungb3347bb2010-08-19 14:51:28 -070044
Michael Jurka0620bec2010-10-25 17:50:09 -070045 private Bitmap mIcon;
Winson Chungb3347bb2010-08-19 14:51:28 -070046
Winson Chungb3347bb2010-08-19 14:51:28 -070047 public PagedViewIcon(Context context) {
48 this(context, null);
49 }
50
51 public PagedViewIcon(Context context, AttributeSet attrs) {
52 this(context, attrs, 0);
53 }
54
55 public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) {
56 super(context, attrs, defStyle);
Michael Jurka8245a862011-02-01 17:53:59 -080057 }
58
Winson Chungc58497e2013-09-03 17:48:37 -070059 public void onFinishInflate() {
60 super.onFinishInflate();
61
62 // Ensure we are using the right text size
63 LauncherAppState app = LauncherAppState.getInstance();
64 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung67ca7e42013-10-31 16:53:19 -070065 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx);
Winson Chungc58497e2013-09-03 17:48:37 -070066 }
67
Michael Jurkaeadbfc52013-09-04 00:45:37 +020068 public void applyFromApplicationInfo(AppInfo info, boolean scaleUp,
Winson Chunge4e50662012-01-23 14:45:13 -080069 PagedViewIcon.PressedCallback cb) {
Winson Chung6e1c0d32013-10-25 15:24:24 -070070 LauncherAppState app = LauncherAppState.getInstance();
71 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
72
Michael Jurkac9a96192010-11-01 11:52:08 -070073 mIcon = info.iconBitmap;
Winson Chunge4e50662012-01-23 14:45:13 -080074 mPressedCallback = cb;
Winson Chung67ca7e42013-10-31 16:53:19 -070075 Drawable icon = Utilities.createIconDrawable(mIcon);
76 icon.setBounds(0, 0, grid.allAppsIconSizePx, grid.allAppsIconSizePx);
77 setCompoundDrawables(null, icon, null, null);
Winson Chung6e1c0d32013-10-25 15:24:24 -070078 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Winson Chung241c3b42010-08-25 16:53:03 -070079 setText(info.title);
Kenny Guyc2bd8102014-06-30 12:30:31 +010080 if (info.contentDescription != null) {
81 setContentDescription(info.contentDescription);
82 }
Winson Chung241c3b42010-08-25 16:53:03 -070083 setTag(info);
84 }
Michael Jurkae3517d02012-01-12 07:46:24 -080085
Winson Chung3b187b82012-01-30 15:11:08 -080086 public void lockDrawableState() {
87 mLockDrawableState = true;
88 }
89
Winson Chunge4e50662012-01-23 14:45:13 -080090 public void resetDrawableState() {
Winson Chung3b187b82012-01-30 15:11:08 -080091 mLockDrawableState = false;
Winson Chunge4e50662012-01-23 14:45:13 -080092 post(new Runnable() {
93 @Override
94 public void run() {
95 refreshDrawableState();
96 }
97 });
98 }
99
Michael Jurkae3517d02012-01-12 07:46:24 -0800100 protected void drawableStateChanged() {
Michael Jurkae3517d02012-01-12 07:46:24 -0800101 super.drawableStateChanged();
Winson Chunge4e50662012-01-23 14:45:13 -0800102
103 // We keep in the pressed state until resetDrawableState() is called to reset the press
104 // feedback
105 if (isPressed()) {
106 setAlpha(PRESS_ALPHA);
107 if (mPressedCallback != null) {
108 mPressedCallback.iconPressed(this);
109 }
Winson Chung3b187b82012-01-30 15:11:08 -0800110 } else if (!mLockDrawableState) {
Winson Chunge4e50662012-01-23 14:45:13 -0800111 setAlpha(1f);
Winson Chunge4e50662012-01-23 14:45:13 -0800112 }
Michael Jurkae3517d02012-01-12 07:46:24 -0800113 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700114}