blob: 865c0b2860d68cc3856f219dfd7ba8e8d41e6e41 [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();
Winson Chung6e1c0d32013-10-25 15:24:24 -070065 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.iconTextSizePx);
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 Chung54000492013-10-14 16:29:29 -070075 setCompoundDrawables(null, Utilities.createIconDrawable(mIcon),
Winson Chung0dbd7342013-10-13 22:46:20 -070076 null, null);
Winson Chung6e1c0d32013-10-25 15:24:24 -070077 setCompoundDrawablePadding(grid.iconDrawablePaddingPx);
Winson Chung241c3b42010-08-25 16:53:03 -070078 setText(info.title);
79 setTag(info);
80 }
Michael Jurkae3517d02012-01-12 07:46:24 -080081
Winson Chung3b187b82012-01-30 15:11:08 -080082 public void lockDrawableState() {
83 mLockDrawableState = true;
84 }
85
Winson Chunge4e50662012-01-23 14:45:13 -080086 public void resetDrawableState() {
Winson Chung3b187b82012-01-30 15:11:08 -080087 mLockDrawableState = false;
Winson Chunge4e50662012-01-23 14:45:13 -080088 post(new Runnable() {
89 @Override
90 public void run() {
91 refreshDrawableState();
92 }
93 });
94 }
95
Michael Jurkae3517d02012-01-12 07:46:24 -080096 protected void drawableStateChanged() {
Michael Jurkae3517d02012-01-12 07:46:24 -080097 super.drawableStateChanged();
Winson Chunge4e50662012-01-23 14:45:13 -080098
99 // We keep in the pressed state until resetDrawableState() is called to reset the press
100 // feedback
101 if (isPressed()) {
102 setAlpha(PRESS_ALPHA);
103 if (mPressedCallback != null) {
104 mPressedCallback.iconPressed(this);
105 }
Winson Chung3b187b82012-01-30 15:11:08 -0800106 } else if (!mLockDrawableState) {
Winson Chunge4e50662012-01-23 14:45:13 -0800107 setAlpha(1f);
Winson Chunge4e50662012-01-23 14:45:13 -0800108 }
Michael Jurkae3517d02012-01-12 07:46:24 -0800109 }
Winson Chung2d75f122013-09-23 16:53:31 -0700110
111 @Override
112 public void draw(Canvas canvas) {
113 // If text is transparent, don't draw any shadow
114 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
115 getPaint().clearShadowLayer();
116 super.draw(canvas);
117 return;
118 }
119
120 // We enhance the shadow by drawing the shadow twice
121 getPaint().setShadowLayer(BubbleTextView.SHADOW_LARGE_RADIUS, 0.0f,
122 BubbleTextView.SHADOW_Y_OFFSET, BubbleTextView.SHADOW_LARGE_COLOUR);
123 super.draw(canvas);
124 canvas.save(Canvas.CLIP_SAVE_FLAG);
125 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
126 getScrollX() + getWidth(),
127 getScrollY() + getHeight(), Region.Op.INTERSECT);
128 getPaint().setShadowLayer(BubbleTextView.SHADOW_SMALL_RADIUS, 0.0f, 0.0f,
129 BubbleTextView.SHADOW_SMALL_COLOUR);
130 super.draw(canvas);
131 canvas.restore();
132 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700133}