Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 20 | import android.graphics.Bitmap; |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 21 | import android.graphics.Canvas; |
| 22 | import android.graphics.Region; |
Winson Chung | 67ca7e4 | 2013-10-31 16:53:19 -0700 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 25 | import android.util.TypedValue; |
Michael Jurka | 4c4c001 | 2011-11-15 13:59:13 -0800 | [diff] [blame] | 26 | import android.widget.TextView; |
Winson Chung | c84001e | 2010-11-09 12:20:57 -0800 | [diff] [blame] | 27 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 28 | /** |
| 29 | * An icon on a PagedView, specifically for items in the launcher's paged view (with compound |
| 30 | * drawables on the top). |
| 31 | */ |
Michael Jurka | 82369a1 | 2012-01-12 08:08:38 -0800 | [diff] [blame] | 32 | public class PagedViewIcon extends TextView { |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 33 | /** 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 Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 38 | @SuppressWarnings("unused") |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 39 | private static final String TAG = "PagedViewIcon"; |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 40 | private static final float PRESS_ALPHA = 0.4f; |
| 41 | |
| 42 | private PagedViewIcon.PressedCallback mPressedCallback; |
Winson Chung | 3b187b8 | 2012-01-30 15:11:08 -0800 | [diff] [blame] | 43 | private boolean mLockDrawableState = false; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 44 | |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 45 | private Bitmap mIcon; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 46 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 47 | 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 Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 59 | 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 Chung | 67ca7e4 | 2013-10-31 16:53:19 -0700 | [diff] [blame] | 65 | setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 68 | public void applyFromApplicationInfo(AppInfo info, boolean scaleUp, |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 69 | PagedViewIcon.PressedCallback cb) { |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 70 | LauncherAppState app = LauncherAppState.getInstance(); |
| 71 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 72 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 73 | mIcon = info.iconBitmap; |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 74 | mPressedCallback = cb; |
Winson Chung | 67ca7e4 | 2013-10-31 16:53:19 -0700 | [diff] [blame] | 75 | Drawable icon = Utilities.createIconDrawable(mIcon); |
| 76 | icon.setBounds(0, 0, grid.allAppsIconSizePx, grid.allAppsIconSizePx); |
| 77 | setCompoundDrawables(null, icon, null, null); |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 78 | setCompoundDrawablePadding(grid.iconDrawablePaddingPx); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 79 | setText(info.title); |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 80 | if (info.contentDescription != null) { |
| 81 | setContentDescription(info.contentDescription); |
| 82 | } |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 83 | setTag(info); |
| 84 | } |
Michael Jurka | e3517d0 | 2012-01-12 07:46:24 -0800 | [diff] [blame] | 85 | |
Winson Chung | 3b187b8 | 2012-01-30 15:11:08 -0800 | [diff] [blame] | 86 | public void lockDrawableState() { |
| 87 | mLockDrawableState = true; |
| 88 | } |
| 89 | |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 90 | public void resetDrawableState() { |
Winson Chung | 3b187b8 | 2012-01-30 15:11:08 -0800 | [diff] [blame] | 91 | mLockDrawableState = false; |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 92 | post(new Runnable() { |
| 93 | @Override |
| 94 | public void run() { |
| 95 | refreshDrawableState(); |
| 96 | } |
| 97 | }); |
| 98 | } |
| 99 | |
Michael Jurka | e3517d0 | 2012-01-12 07:46:24 -0800 | [diff] [blame] | 100 | protected void drawableStateChanged() { |
Michael Jurka | e3517d0 | 2012-01-12 07:46:24 -0800 | [diff] [blame] | 101 | super.drawableStateChanged(); |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 102 | |
| 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 Chung | 3b187b8 | 2012-01-30 15:11:08 -0800 | [diff] [blame] | 110 | } else if (!mLockDrawableState) { |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 111 | setAlpha(1f); |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 112 | } |
Michael Jurka | e3517d0 | 2012-01-12 07:46:24 -0800 | [diff] [blame] | 113 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 114 | } |