blob: c6d5e49960631d2e7ebae7798ab0bfb27950cc37 [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;
Michael Jurka0620bec2010-10-25 17:50:09 -070072 setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
Winson Chung241c3b42010-08-25 16:53:03 -070073 setText(info.title);
74 setTag(info);
75 }
Michael Jurkae3517d02012-01-12 07:46:24 -080076
Winson Chung3b187b82012-01-30 15:11:08 -080077 public void lockDrawableState() {
78 mLockDrawableState = true;
79 }
80
Winson Chunge4e50662012-01-23 14:45:13 -080081 public void resetDrawableState() {
Winson Chung3b187b82012-01-30 15:11:08 -080082 mLockDrawableState = false;
Winson Chunge4e50662012-01-23 14:45:13 -080083 post(new Runnable() {
84 @Override
85 public void run() {
86 refreshDrawableState();
87 }
88 });
89 }
90
Michael Jurkae3517d02012-01-12 07:46:24 -080091 protected void drawableStateChanged() {
Michael Jurkae3517d02012-01-12 07:46:24 -080092 super.drawableStateChanged();
Winson Chunge4e50662012-01-23 14:45:13 -080093
94 // We keep in the pressed state until resetDrawableState() is called to reset the press
95 // feedback
96 if (isPressed()) {
97 setAlpha(PRESS_ALPHA);
98 if (mPressedCallback != null) {
99 mPressedCallback.iconPressed(this);
100 }
Winson Chung3b187b82012-01-30 15:11:08 -0800101 } else if (!mLockDrawableState) {
Winson Chunge4e50662012-01-23 14:45:13 -0800102 setAlpha(1f);
Winson Chunge4e50662012-01-23 14:45:13 -0800103 }
Michael Jurkae3517d02012-01-12 07:46:24 -0800104 }
Winson Chung2d75f122013-09-23 16:53:31 -0700105
106 @Override
107 public void draw(Canvas canvas) {
108 // If text is transparent, don't draw any shadow
109 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
110 getPaint().clearShadowLayer();
111 super.draw(canvas);
112 return;
113 }
114
115 // We enhance the shadow by drawing the shadow twice
116 getPaint().setShadowLayer(BubbleTextView.SHADOW_LARGE_RADIUS, 0.0f,
117 BubbleTextView.SHADOW_Y_OFFSET, BubbleTextView.SHADOW_LARGE_COLOUR);
118 super.draw(canvas);
119 canvas.save(Canvas.CLIP_SAVE_FLAG);
120 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
121 getScrollX() + getWidth(),
122 getScrollY() + getHeight(), Region.Op.INTERSECT);
123 getPaint().setShadowLayer(BubbleTextView.SHADOW_SMALL_RADIUS, 0.0f, 0.0f,
124 BubbleTextView.SHADOW_SMALL_COLOUR);
125 super.draw(canvas);
126 canvas.restore();
127 }
Winson Chungb3347bb2010-08-19 14:51:28 -0700128}