blob: 8bfe42d249a7d1be8633c92919a96d38bbafd669 [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;
23import android.graphics.Region.Op;
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();
65 setTextSize(TypedValue.COMPLEX_UNIT_SP, grid.iconTextSize);
66 }
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) {
Michael Jurkac9a96192010-11-01 11:52:08 -070070 mIcon = info.iconBitmap;
Winson Chunge4e50662012-01-23 14:45:13 -080071 mPressedCallback = cb;
Winson Chung54000492013-10-14 16:29:29 -070072 setCompoundDrawables(null, Utilities.createIconDrawable(mIcon),
Winson Chung0dbd7342013-10-13 22:46:20 -070073 null, null);
Winson Chung241c3b42010-08-25 16:53:03 -070074 setText(info.title);
75 setTag(info);
76 }
Michael Jurkae3517d02012-01-12 07:46:24 -080077
Winson Chung3b187b82012-01-30 15:11:08 -080078 public void lockDrawableState() {
79 mLockDrawableState = true;
80 }
81
Winson Chunge4e50662012-01-23 14:45:13 -080082 public void resetDrawableState() {
Winson Chung3b187b82012-01-30 15:11:08 -080083 mLockDrawableState = false;
Winson Chunge4e50662012-01-23 14:45:13 -080084 post(new Runnable() {
85 @Override
86 public void run() {
87 refreshDrawableState();
88 }
89 });
90 }
91
Michael Jurkae3517d02012-01-12 07:46:24 -080092 protected void drawableStateChanged() {
Michael Jurkae3517d02012-01-12 07:46:24 -080093 super.drawableStateChanged();
Winson Chunge4e50662012-01-23 14:45:13 -080094
95 // We keep in the pressed state until resetDrawableState() is called to reset the press
96 // feedback
97 if (isPressed()) {
98 setAlpha(PRESS_ALPHA);
99 if (mPressedCallback != null) {
100 mPressedCallback.iconPressed(this);
101 }
Winson Chung3b187b82012-01-30 15:11:08 -0800102 } else if (!mLockDrawableState) {
Winson Chunge4e50662012-01-23 14:45:13 -0800103 setAlpha(1f);
Winson Chunge4e50662012-01-23 14:45:13 -0800104 }
Michael Jurkae3517d02012-01-12 07:46:24 -0800105 }
Winson Chung2d75f122013-09-23 16:53:31 -0700106
107 @Override
108 public void draw(Canvas canvas) {
109 // If text is transparent, don't draw any shadow
110 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
111 getPaint().clearShadowLayer();
112 super.draw(canvas);
113 return;
114 }
115
116 // We enhance the shadow by drawing the shadow twice
117 getPaint().setShadowLayer(BubbleTextView.SHADOW_LARGE_RADIUS, 0.0f,
118 BubbleTextView.SHADOW_Y_OFFSET, BubbleTextView.SHADOW_LARGE_COLOUR);
119 super.draw(canvas);
120 canvas.save(Canvas.CLIP_SAVE_FLAG);
121 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
122 getScrollX() + getWidth(),
123 getScrollY() + getHeight(), Region.Op.INTERSECT);
124 getPaint().setShadowLayer(BubbleTextView.SHADOW_SMALL_RADIUS, 0.0f, 0.0f,
125 BubbleTextView.SHADOW_SMALL_COLOUR);
126 super.draw(canvas);
127 canvas.restore();
128 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700129}