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); |
| 80 | setTag(info); |
| 81 | } |
Michael Jurka | e3517d0 | 2012-01-12 07:46:24 -0800 | [diff] [blame] | 82 | |
Winson Chung | 3b187b8 | 2012-01-30 15:11:08 -0800 | [diff] [blame] | 83 | public void lockDrawableState() { |
| 84 | mLockDrawableState = true; |
| 85 | } |
| 86 | |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 87 | public void resetDrawableState() { |
Winson Chung | 3b187b8 | 2012-01-30 15:11:08 -0800 | [diff] [blame] | 88 | mLockDrawableState = false; |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 89 | post(new Runnable() { |
| 90 | @Override |
| 91 | public void run() { |
| 92 | refreshDrawableState(); |
| 93 | } |
| 94 | }); |
| 95 | } |
| 96 | |
Michael Jurka | e3517d0 | 2012-01-12 07:46:24 -0800 | [diff] [blame] | 97 | protected void drawableStateChanged() { |
Michael Jurka | e3517d0 | 2012-01-12 07:46:24 -0800 | [diff] [blame] | 98 | super.drawableStateChanged(); |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 99 | |
| 100 | // We keep in the pressed state until resetDrawableState() is called to reset the press |
| 101 | // feedback |
| 102 | if (isPressed()) { |
| 103 | setAlpha(PRESS_ALPHA); |
| 104 | if (mPressedCallback != null) { |
| 105 | mPressedCallback.iconPressed(this); |
| 106 | } |
Winson Chung | 3b187b8 | 2012-01-30 15:11:08 -0800 | [diff] [blame] | 107 | } else if (!mLockDrawableState) { |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 108 | setAlpha(1f); |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 109 | } |
Michael Jurka | e3517d0 | 2012-01-12 07:46:24 -0800 | [diff] [blame] | 110 | } |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 111 | |
| 112 | @Override |
| 113 | public void draw(Canvas canvas) { |
| 114 | // If text is transparent, don't draw any shadow |
| 115 | if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) { |
| 116 | getPaint().clearShadowLayer(); |
| 117 | super.draw(canvas); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | // We enhance the shadow by drawing the shadow twice |
| 122 | getPaint().setShadowLayer(BubbleTextView.SHADOW_LARGE_RADIUS, 0.0f, |
| 123 | BubbleTextView.SHADOW_Y_OFFSET, BubbleTextView.SHADOW_LARGE_COLOUR); |
| 124 | super.draw(canvas); |
| 125 | canvas.save(Canvas.CLIP_SAVE_FLAG); |
| 126 | canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(), |
| 127 | getScrollX() + getWidth(), |
| 128 | getScrollY() + getHeight(), Region.Op.INTERSECT); |
| 129 | getPaint().setShadowLayer(BubbleTextView.SHADOW_SMALL_RADIUS, 0.0f, 0.0f, |
| 130 | BubbleTextView.SHADOW_SMALL_COLOUR); |
| 131 | super.draw(canvas); |
| 132 | canvas.restore(); |
| 133 | } |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 134 | } |