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.Region.Op; 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_SP, grid.iconTextSize); 66 } 67 68 public void applyFromApplicationInfo(AppInfo info, boolean scaleUp, 69 PagedViewIcon.PressedCallback cb) { 70 mIcon = info.iconBitmap; 71 mPressedCallback = cb; 72 setCompoundDrawables(null, Utilities.createIconDrawable(mIcon), 73 null, null); 74 setText(info.title); 75 setTag(info); 76 } 77 78 public void lockDrawableState() { 79 mLockDrawableState = true; 80 } 81 82 public void resetDrawableState() { 83 mLockDrawableState = false; 84 post(new Runnable() { 85 @Override 86 public void run() { 87 refreshDrawableState(); 88 } 89 }); 90 } 91 92 protected void drawableStateChanged() { 93 super.drawableStateChanged(); 94 95 // We keep in the pressed state until resetDrawableState() is called to reset the press 96 // feedback 97 if (isPressed()) { 98 setAlpha(PRESS_ALPHA); 99 if (mPressedCallback != null) { 100 mPressedCallback.iconPressed(this); 101 } 102 } else if (!mLockDrawableState) { 103 setAlpha(1f); 104 } 105 } 106 107 @Override 108 public void draw(Canvas canvas) { 109 // If text is transparent, don't draw any shadow 110 if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) { 111 getPaint().clearShadowLayer(); 112 super.draw(canvas); 113 return; 114 } 115 116 // We enhance the shadow by drawing the shadow twice 117 getPaint().setShadowLayer(BubbleTextView.SHADOW_LARGE_RADIUS, 0.0f, 118 BubbleTextView.SHADOW_Y_OFFSET, BubbleTextView.SHADOW_LARGE_COLOUR); 119 super.draw(canvas); 120 canvas.save(Canvas.CLIP_SAVE_FLAG); 121 canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(), 122 getScrollX() + getWidth(), 123 getScrollY() + getHeight(), Region.Op.INTERSECT); 124 getPaint().setShadowLayer(BubbleTextView.SHADOW_SMALL_RADIUS, 0.0f, 0.0f, 125 BubbleTextView.SHADOW_SMALL_COLOUR); 126 super.draw(canvas); 127 canvas.restore(); 128 } 129 } 130