Home | History | Annotate | Download | only in launcher2
      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.launcher2;
     18 
     19 import android.content.Context;
     20 import android.graphics.Bitmap;
     21 import android.util.AttributeSet;
     22 import android.widget.TextView;
     23 
     24 /**
     25  * An icon on a PagedView, specifically for items in the launcher's paged view (with compound
     26  * drawables on the top).
     27  */
     28 public class PagedViewIcon extends TextView {
     29     /** A simple callback interface to allow a PagedViewIcon to notify when it has been pressed */
     30     public static interface PressedCallback {
     31         void iconPressed(PagedViewIcon icon);
     32     }
     33 
     34     @SuppressWarnings("unused")
     35     private static final String TAG = "PagedViewIcon";
     36     private static final float PRESS_ALPHA = 0.4f;
     37 
     38     private PagedViewIcon.PressedCallback mPressedCallback;
     39     private boolean mLockDrawableState = false;
     40 
     41     private Bitmap mIcon;
     42 
     43     public PagedViewIcon(Context context) {
     44         this(context, null);
     45     }
     46 
     47     public PagedViewIcon(Context context, AttributeSet attrs) {
     48         this(context, attrs, 0);
     49     }
     50 
     51     public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) {
     52         super(context, attrs, defStyle);
     53     }
     54 
     55     public void applyFromApplicationInfo(ApplicationInfo info, boolean scaleUp,
     56             PagedViewIcon.PressedCallback cb) {
     57         mIcon = info.iconBitmap;
     58         mPressedCallback = cb;
     59         setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
     60         setText(info.title);
     61         if (info.contentDescription != null) {
     62             setContentDescription(info.contentDescription);
     63         }
     64         setTag(info);
     65     }
     66 
     67     public void lockDrawableState() {
     68         mLockDrawableState = true;
     69     }
     70 
     71     public void resetDrawableState() {
     72         mLockDrawableState = false;
     73         post(new Runnable() {
     74             @Override
     75             public void run() {
     76                 refreshDrawableState();
     77             }
     78         });
     79     }
     80 
     81     protected void drawableStateChanged() {
     82         super.drawableStateChanged();
     83 
     84         // We keep in the pressed state until resetDrawableState() is called to reset the press
     85         // feedback
     86         if (isPressed()) {
     87             setAlpha(PRESS_ALPHA);
     88             if (mPressedCallback != null) {
     89                 mPressedCallback.iconPressed(this);
     90             }
     91         } else if (!mLockDrawableState) {
     92             setAlpha(1f);
     93         }
     94     }
     95 }
     96