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 17 package com.android.launcher3; 18 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.Canvas; 22 import android.graphics.Region; 23 import android.graphics.drawable.Drawable; 24 import android.util.AttributeSet; 25 import android.util.TypedValue; 26 import android.widget.TextView; 27 28 /** 29 * An icon on a PagedView, specifically for items in the launcher's paged view (with compound 30 * drawables on the top). 31 */ 32 public class PagedViewIcon extends TextView { 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 38 @SuppressWarnings("unused") 39 private static final String TAG = "PagedViewIcon"; 40 private static final float PRESS_ALPHA = 0.4f; 41 42 private PagedViewIcon.PressedCallback mPressedCallback; 43 private boolean mLockDrawableState = false; 44 45 private Bitmap mIcon; 46 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); 57 } 58 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(); 65 setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.allAppsIconTextSizePx); 66 } 67 68 public void applyFromApplicationInfo(AppInfo info, boolean scaleUp, 69 PagedViewIcon.PressedCallback cb) { 70 LauncherAppState app = LauncherAppState.getInstance(); 71 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); 72 73 mIcon = info.iconBitmap; 74 mPressedCallback = cb; 75 Drawable icon = Utilities.createIconDrawable(mIcon); 76 icon.setBounds(0, 0, grid.allAppsIconSizePx, grid.allAppsIconSizePx); 77 setCompoundDrawables(null, icon, null, null); 78 setCompoundDrawablePadding(grid.iconDrawablePaddingPx); 79 setText(info.title); 80 setTag(info); 81 } 82 83 public void lockDrawableState() { 84 mLockDrawableState = true; 85 } 86 87 public void resetDrawableState() { 88 mLockDrawableState = false; 89 post(new Runnable() { 90 @Override 91 public void run() { 92 refreshDrawableState(); 93 } 94 }); 95 } 96 97 protected void drawableStateChanged() { 98 super.drawableStateChanged(); 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 } 107 } else if (!mLockDrawableState) { 108 setAlpha(1f); 109 } 110 } 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 } 134 } 135