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
17package com.android.systemui.statusbar;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.drawable.AnimationDrawable;
22import android.graphics.drawable.Drawable;
23import android.util.AttributeSet;
24import android.view.View;
25import android.widget.ImageView;
26import android.widget.RemoteViews.RemoteView;
27
28import com.android.systemui.R;
29
30@RemoteView
31public class AnimatedImageView extends ImageView {
32    private final boolean mHasOverlappingRendering;
33    AnimationDrawable mAnim;
34    boolean mAttached;
35
36    // Tracks the last image that was set, so that we don't refresh the image if it is exactly
37    // the same as the previous one. If this is a resid, we track that. If it's a drawable, we
38    // track the hashcode of the drawable.
39    int mDrawableId;
40
41    public AnimatedImageView(Context context) {
42        this(context, null);
43    }
44
45    public AnimatedImageView(Context context, AttributeSet attrs) {
46        super(context, attrs);
47        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
48                R.styleable.AnimatedImageView, 0, 0);
49
50        try {
51            // Default to true, which is what View.java defaults toA
52            mHasOverlappingRendering = a.getBoolean(
53                    R.styleable.AnimatedImageView_hasOverlappingRendering, true);
54        } finally {
55            a.recycle();
56        }
57    }
58
59    private void updateAnim() {
60        Drawable drawable = getDrawable();
61        if (mAttached && mAnim != null) {
62            mAnim.stop();
63        }
64        if (drawable instanceof AnimationDrawable) {
65            mAnim = (AnimationDrawable) drawable;
66            if (isShown()) {
67                mAnim.start();
68            }
69        } else {
70            mAnim = null;
71        }
72    }
73
74    @Override
75    public void setImageDrawable(Drawable drawable) {
76        if (drawable != null) {
77            if (mDrawableId == drawable.hashCode()) return;
78
79            mDrawableId = drawable.hashCode();
80        } else {
81            mDrawableId = 0;
82        }
83        super.setImageDrawable(drawable);
84        updateAnim();
85    }
86
87    @Override
88    @android.view.RemotableViewMethod
89    public void setImageResource(int resid) {
90        if (mDrawableId == resid) return;
91
92        mDrawableId = resid;
93        super.setImageResource(resid);
94        updateAnim();
95    }
96
97    @Override
98    public void onAttachedToWindow() {
99        super.onAttachedToWindow();
100        mAttached = true;
101        updateAnim();
102    }
103
104    @Override
105    public void onDetachedFromWindow() {
106        super.onDetachedFromWindow();
107        if (mAnim != null) {
108            mAnim.stop();
109        }
110        mAttached = false;
111    }
112
113    @Override
114    protected void onVisibilityChanged(View changedView, int vis) {
115        super.onVisibilityChanged(changedView, vis);
116        if (mAnim != null) {
117            if (isShown()) {
118                mAnim.start();
119            } else {
120                mAnim.stop();
121            }
122        }
123    }
124
125    @Override
126    public boolean hasOverlappingRendering() {
127        return mHasOverlappingRendering;
128    }
129}
130
131