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.animation.ObjectAnimator;
     20 import android.content.ComponentName;
     21 import android.content.Context;
     22 import android.content.pm.PackageManager;
     23 import android.content.pm.ResolveInfo;
     24 import android.content.res.Resources;
     25 import android.content.res.TypedArray;
     26 import android.graphics.Bitmap;
     27 import android.graphics.Canvas;
     28 import android.graphics.Paint;
     29 import android.os.Handler;
     30 import android.os.HandlerThread;
     31 import android.os.Message;
     32 import android.util.AttributeSet;
     33 import android.view.KeyEvent;
     34 import android.widget.Checkable;
     35 
     36 import com.android.launcher.R;
     37 
     38 
     39 /**
     40  * An icon on a PagedView, specifically for items in the launcher's paged view (with compound
     41  * drawables on the top).
     42  */
     43 public class PagedViewIcon extends CachedTextView implements Checkable {
     44     private static final String TAG = "PagedViewIcon";
     45 
     46     // holographic outline
     47     private final Paint mPaint = new Paint();
     48     private Bitmap mCheckedOutline;
     49     private Bitmap mHolographicOutline;
     50     private Bitmap mIcon;
     51 
     52     private int mAlpha = 255;
     53     private int mHolographicAlpha;
     54 
     55     private boolean mIsChecked;
     56     private ObjectAnimator mCheckedAlphaAnimator;
     57     private float mCheckedAlpha = 1.0f;
     58     private int mCheckedFadeInDuration;
     59     private int mCheckedFadeOutDuration;
     60 
     61     HolographicPagedViewIcon mHolographicOutlineView;
     62     private HolographicOutlineHelper mHolographicOutlineHelper;
     63 
     64     public PagedViewIcon(Context context) {
     65         this(context, null);
     66     }
     67 
     68     public PagedViewIcon(Context context, AttributeSet attrs) {
     69         this(context, attrs, 0);
     70     }
     71 
     72     public PagedViewIcon(Context context, AttributeSet attrs, int defStyle) {
     73         super(context, attrs, defStyle);
     74 
     75         // Set up fade in/out constants
     76         final Resources r = context.getResources();
     77         final int alpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha);
     78         if (alpha > 0) {
     79             mCheckedAlpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha) / 256.0f;
     80             mCheckedFadeInDuration =
     81                 r.getInteger(R.integer.config_dragAppsCustomizeIconFadeInDuration);
     82             mCheckedFadeOutDuration =
     83                 r.getInteger(R.integer.config_dragAppsCustomizeIconFadeOutDuration);
     84         }
     85 
     86         mHolographicOutlineView = new HolographicPagedViewIcon(context, this);
     87     }
     88 
     89     protected HolographicPagedViewIcon getHolographicOutlineView() {
     90         return mHolographicOutlineView;
     91     }
     92 
     93     protected Bitmap getHolographicOutline() {
     94         return mHolographicOutline;
     95     }
     96 
     97     public void applyFromApplicationInfo(ApplicationInfo info, boolean scaleUp,
     98             HolographicOutlineHelper holoOutlineHelper) {
     99         mHolographicOutlineHelper = holoOutlineHelper;
    100         mIcon = info.iconBitmap;
    101         setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(mIcon), null, null);
    102         setText(info.title);
    103         setTag(info);
    104     }
    105 
    106     public void setHolographicOutline(Bitmap holoOutline) {
    107         mHolographicOutline = holoOutline;
    108         getHolographicOutlineView().invalidate();
    109     }
    110 
    111     @Override
    112     public void setAlpha(float alpha) {
    113         final float viewAlpha = HolographicOutlineHelper.viewAlphaInterpolator(alpha);
    114         final float holographicAlpha = HolographicOutlineHelper.highlightAlphaInterpolator(alpha);
    115         int newViewAlpha = (int) (viewAlpha * 255);
    116         int newHolographicAlpha = (int) (holographicAlpha * 255);
    117         if ((mAlpha != newViewAlpha) || (mHolographicAlpha != newHolographicAlpha)) {
    118             mAlpha = newViewAlpha;
    119             mHolographicAlpha = newHolographicAlpha;
    120             super.setAlpha(viewAlpha);
    121         }
    122     }
    123 
    124     public void invalidateCheckedImage() {
    125         if (mCheckedOutline != null) {
    126             mCheckedOutline.recycle();
    127             mCheckedOutline = null;
    128         }
    129     }
    130 
    131     @Override
    132     protected void onDraw(Canvas canvas) {
    133         if (mAlpha > 0) {
    134             super.onDraw(canvas);
    135         }
    136 
    137         Bitmap overlay = null;
    138 
    139         // draw any blended overlays
    140         if (mCheckedOutline != null) {
    141             mPaint.setAlpha(255);
    142             overlay = mCheckedOutline;
    143         }
    144 
    145         if (overlay != null) {
    146             final int offset = getScrollX();
    147             final int compoundPaddingLeft = getCompoundPaddingLeft();
    148             final int compoundPaddingRight = getCompoundPaddingRight();
    149             int hspace = getWidth() - compoundPaddingRight - compoundPaddingLeft;
    150             canvas.drawBitmap(overlay,
    151                     offset + compoundPaddingLeft + (hspace - overlay.getWidth()) / 2,
    152                     mPaddingTop,
    153                     mPaint);
    154         }
    155     }
    156 
    157     @Override
    158     public boolean onKeyDown(int keyCode, KeyEvent event) {
    159         return FocusHelper.handleAppsCustomizeKeyEvent(this, keyCode, event)
    160                 || super.onKeyDown(keyCode, event);
    161     }
    162 
    163     @Override
    164     public boolean onKeyUp(int keyCode, KeyEvent event) {
    165         return FocusHelper.handleAppsCustomizeKeyEvent(this, keyCode, event)
    166                 || super.onKeyUp(keyCode, event);
    167     }
    168 
    169     @Override
    170     public boolean isChecked() {
    171         return mIsChecked;
    172     }
    173 
    174     void setChecked(boolean checked, boolean animate) {
    175         if (mIsChecked != checked) {
    176             mIsChecked = checked;
    177 
    178             float alpha;
    179             int duration;
    180             if (mIsChecked) {
    181                 alpha = mCheckedAlpha;
    182                 duration = mCheckedFadeInDuration;
    183             } else {
    184                 alpha = 1.0f;
    185                 duration = mCheckedFadeOutDuration;
    186             }
    187 
    188             // Initialize the animator
    189             if (mCheckedAlphaAnimator != null) {
    190                 mCheckedAlphaAnimator.cancel();
    191             }
    192             if (animate) {
    193                 mCheckedAlphaAnimator = ObjectAnimator.ofFloat(this, "alpha", getAlpha(), alpha);
    194                 mCheckedAlphaAnimator.setDuration(duration);
    195                 mCheckedAlphaAnimator.start();
    196             } else {
    197                 setAlpha(alpha);
    198             }
    199 
    200             invalidate();
    201         }
    202     }
    203 
    204     @Override
    205     public void setChecked(boolean checked) {
    206         setChecked(checked, true);
    207     }
    208 
    209     @Override
    210     public void toggle() {
    211         setChecked(!mIsChecked);
    212     }
    213 }
    214