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         setTag(info);
     62     }
     63 
     64     public void lockDrawableState() {
     65         mLockDrawableState = true;
     66     }
     67 
     68     public void resetDrawableState() {
     69         mLockDrawableState = false;
     70         post(new Runnable() {
     71             @Override
     72             public void run() {
     73                 refreshDrawableState();
     74             }
     75         });
     76     }
     77 
     78     protected void drawableStateChanged() {
     79         super.drawableStateChanged();
     80 
     81         // We keep in the pressed state until resetDrawableState() is called to reset the press
     82         // feedback
     83         if (isPressed()) {
     84             setAlpha(PRESS_ALPHA);
     85             if (mPressedCallback != null) {
     86                 mPressedCallback.iconPressed(this);
     87             }
     88         } else if (!mLockDrawableState) {
     89             setAlpha(1f);
     90         }
     91     }
     92 }
     93