Home | History | Annotate | Download | only in statusbar
      1 /*
      2  * Copyright (C) 2008 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.systemui.statusbar;
     18 
     19 import android.content.Context;
     20 import android.graphics.drawable.AnimationDrawable;
     21 import android.graphics.drawable.Drawable;
     22 import android.util.AttributeSet;
     23 import android.view.View;
     24 import android.widget.ImageView;
     25 import android.widget.RemoteViews.RemoteView;
     26 
     27 @RemoteView
     28 public class AnimatedImageView extends AlphaOptimizedImageView {
     29     AnimationDrawable mAnim;
     30     boolean mAttached;
     31 
     32     // Tracks the last image that was set, so that we don't refresh the image if it is exactly
     33     // the same as the previous one. If this is a resid, we track that. If it's a drawable, we
     34     // track the hashcode of the drawable.
     35     int mDrawableId;
     36 
     37     public AnimatedImageView(Context context) {
     38         super(context);
     39     }
     40 
     41     public AnimatedImageView(Context context, AttributeSet attrs) {
     42         super(context, attrs);
     43     }
     44 
     45     private void updateAnim() {
     46         Drawable drawable = getDrawable();
     47         if (mAttached && mAnim != null) {
     48             mAnim.stop();
     49         }
     50         if (drawable instanceof AnimationDrawable) {
     51             mAnim = (AnimationDrawable) drawable;
     52             if (isShown()) {
     53                 mAnim.start();
     54             }
     55         } else {
     56             mAnim = null;
     57         }
     58     }
     59 
     60     @Override
     61     public void setImageDrawable(Drawable drawable) {
     62         if (drawable != null) {
     63             if (mDrawableId == drawable.hashCode()) return;
     64 
     65             mDrawableId = drawable.hashCode();
     66         } else {
     67             mDrawableId = 0;
     68         }
     69         super.setImageDrawable(drawable);
     70         updateAnim();
     71     }
     72 
     73     @Override
     74     @android.view.RemotableViewMethod
     75     public void setImageResource(int resid) {
     76         if (mDrawableId == resid) return;
     77 
     78         mDrawableId = resid;
     79         super.setImageResource(resid);
     80         updateAnim();
     81     }
     82 
     83     @Override
     84     public void onAttachedToWindow() {
     85         super.onAttachedToWindow();
     86         mAttached = true;
     87         updateAnim();
     88     }
     89 
     90     @Override
     91     public void onDetachedFromWindow() {
     92         super.onDetachedFromWindow();
     93         if (mAnim != null) {
     94             mAnim.stop();
     95         }
     96         mAttached = false;
     97     }
     98 
     99     @Override
    100     protected void onVisibilityChanged(View changedView, int vis) {
    101         super.onVisibilityChanged(changedView, vis);
    102         if (mAnim != null) {
    103             if (isShown()) {
    104                 mAnim.start();
    105             } else {
    106                 mAnim.stop();
    107             }
    108         }
    109     }
    110 }
    111 
    112