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 | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 21 | import android.util.AttributeSet; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 22 | import android.util.TypedValue; |
Michael Jurka | 4c4c001 | 2011-11-15 13:59:13 -0800 | [diff] [blame] | 23 | import android.widget.TextView; |
Winson Chung | c84001e | 2010-11-09 12:20:57 -0800 | [diff] [blame] | 24 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 25 | /** |
| 26 | * An icon on a PagedView, specifically for items in the launcher's paged view (with compound |
| 27 | * drawables on the top). |
| 28 | */ |
Michael Jurka | 82369a1 | 2012-01-12 08:08:38 -0800 | [diff] [blame] | 29 | public class PagedViewIcon extends TextView { |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 30 | /** A simple callback interface to allow a PagedViewIcon to notify when it has been pressed */ |
| 31 | public static interface PressedCallback { |
| 32 | void iconPressed(PagedViewIcon icon); |
| 33 | } |
| 34 | |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 35 | @SuppressWarnings("unused") |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 36 | private static final String TAG = "PagedViewIcon"; |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 37 | private static final float PRESS_ALPHA = 0.4f; |
| 38 | |
| 39 | private PagedViewIcon.PressedCallback mPressedCallback; |
Winson Chung | 3b187b8 | 2012-01-30 15:11:08 -0800 | [diff] [blame] | 40 | private boolean mLockDrawableState = false; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 41 | |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 42 | private Bitmap mIcon; |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 43 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 44 | public PagedViewIcon(Context context) { |
| 45 | this(context, null); |
| 46 | } |
| 47 | |
| 48 | public PagedViewIcon(Context context, AttributeSet attrs) { |
| 49 | this(context, attrs, 0); |
| 50 | } |
| 51 | |
| 52 | public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) { |
| 53 | super(context, attrs, defStyle); |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 56 | public void onFinishInflate() { |
| 57 | super.onFinishInflate(); |
| 58 | |
| 59 | // Ensure we are using the right text size |
| 60 | LauncherAppState app = LauncherAppState.getInstance(); |
| 61 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 62 | setTextSize(TypedValue.COMPLEX_UNIT_SP, grid.iconTextSize); |
| 63 | } |
| 64 | |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 65 | public void applyFromApplicationInfo(AppInfo info, boolean scaleUp, |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 66 | PagedViewIcon.PressedCallback cb) { |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 67 | mIcon = info.iconBitmap; |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 68 | mPressedCallback = cb; |
Michael Jurka | 0620bec | 2010-10-25 17:50:09 -0700 | [diff] [blame] | 69 | setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 70 | setText(info.title); |
| 71 | setTag(info); |
| 72 | } |
Michael Jurka | e3517d0 | 2012-01-12 07:46:24 -0800 | [diff] [blame] | 73 | |
Winson Chung | 3b187b8 | 2012-01-30 15:11:08 -0800 | [diff] [blame] | 74 | public void lockDrawableState() { |
| 75 | mLockDrawableState = true; |
| 76 | } |
| 77 | |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 78 | public void resetDrawableState() { |
Winson Chung | 3b187b8 | 2012-01-30 15:11:08 -0800 | [diff] [blame] | 79 | mLockDrawableState = false; |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 80 | post(new Runnable() { |
| 81 | @Override |
| 82 | public void run() { |
| 83 | refreshDrawableState(); |
| 84 | } |
| 85 | }); |
| 86 | } |
| 87 | |
Michael Jurka | e3517d0 | 2012-01-12 07:46:24 -0800 | [diff] [blame] | 88 | protected void drawableStateChanged() { |
Michael Jurka | e3517d0 | 2012-01-12 07:46:24 -0800 | [diff] [blame] | 89 | super.drawableStateChanged(); |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 90 | |
| 91 | // We keep in the pressed state until resetDrawableState() is called to reset the press |
| 92 | // feedback |
| 93 | if (isPressed()) { |
| 94 | setAlpha(PRESS_ALPHA); |
| 95 | if (mPressedCallback != null) { |
| 96 | mPressedCallback.iconPressed(this); |
| 97 | } |
Winson Chung | 3b187b8 | 2012-01-30 15:11:08 -0800 | [diff] [blame] | 98 | } else if (!mLockDrawableState) { |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 99 | setAlpha(1f); |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 100 | } |
Michael Jurka | e3517d0 | 2012-01-12 07:46:24 -0800 | [diff] [blame] | 101 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 102 | } |