1503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato/*
2503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato * Copyright (C) 2008 The Android Open Source Project
3503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato *
4503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato * Licensed under the Apache License, Version 2.0 (the "License");
5503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato * you may not use this file except in compliance with the License.
6503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato * You may obtain a copy of the License at
7503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato *
8503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato *      http://www.apache.org/licenses/LICENSE-2.0
9503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato *
10503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato * Unless required by applicable law or agreed to in writing, software
11503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato * distributed under the License is distributed on an "AS IS" BASIS,
12503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato * See the License for the specific language governing permissions and
14503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato * limitations under the License.
15503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato */
16503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato
1779de0c550037a5328bbc7f4fddaf02f192a5c283Joe Onoratopackage com.android.systemui.statusbar;
18503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato
19503007dd023668b1e45de948d3673e594d7f5a82Joe Onoratoimport android.content.Context;
20b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinekimport android.content.res.TypedArray;
21503007dd023668b1e45de948d3673e594d7f5a82Joe Onoratoimport android.graphics.drawable.AnimationDrawable;
22503007dd023668b1e45de948d3673e594d7f5a82Joe Onoratoimport android.graphics.drawable.Drawable;
23503007dd023668b1e45de948d3673e594d7f5a82Joe Onoratoimport android.util.AttributeSet;
241e7277e20465be39e30d7e84192c40c4c7b55f89Joe Onoratoimport android.view.View;
25b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinekimport android.widget.ImageView;
26503007dd023668b1e45de948d3673e594d7f5a82Joe Onoratoimport android.widget.RemoteViews.RemoteView;
27503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato
28b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinekimport com.android.systemui.R;
29b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek
30503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato@RemoteView
31b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinekpublic class AnimatedImageView extends ImageView {
32b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek    private final boolean mHasOverlappingRendering;
33503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    AnimationDrawable mAnim;
34503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    boolean mAttached;
35503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato
360f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn    // Tracks the last image that was set, so that we don't refresh the image if it is exactly
370f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn    // the same as the previous one. If this is a resid, we track that. If it's a drawable, we
380f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn    // track the hashcode of the drawable.
390f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn    int mDrawableId;
400f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn
41503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    public AnimatedImageView(Context context) {
42b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek        this(context, null);
43503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    }
44503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato
45503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    public AnimatedImageView(Context context, AttributeSet attrs) {
46503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        super(context, attrs);
47b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
48b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek                R.styleable.AnimatedImageView, 0, 0);
49b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek
50b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek        try {
51b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek            // Default to true, which is what View.java defaults toA
52b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek            mHasOverlappingRendering = a.getBoolean(
53b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek                    R.styleable.AnimatedImageView_hasOverlappingRendering, true);
54b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek        } finally {
55b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek            a.recycle();
56b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek        }
57503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    }
58503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato
59503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    private void updateAnim() {
60180979f76b0c99cd7053a44692f6408721b74bceJohn Spurlock        Drawable drawable = getDrawable();
61503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        if (mAttached && mAnim != null) {
62503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato            mAnim.stop();
63503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        }
64503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        if (drawable instanceof AnimationDrawable) {
650f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn            mAnim = (AnimationDrawable) drawable;
661e7277e20465be39e30d7e84192c40c4c7b55f89Joe Onorato            if (isShown()) {
67503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato                mAnim.start();
68503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato            }
69503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        } else {
70503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato            mAnim = null;
71503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        }
72503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    }
73503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato
74503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    @Override
75503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    public void setImageDrawable(Drawable drawable) {
760f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn        if (drawable != null) {
770f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn            if (mDrawableId == drawable.hashCode()) return;
780f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn
790f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn            mDrawableId = drawable.hashCode();
800f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn        } else {
810f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn            mDrawableId = 0;
820f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn        }
83503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        super.setImageDrawable(drawable);
84503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        updateAnim();
85503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    }
86503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato
87503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    @Override
88503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    @android.view.RemotableViewMethod
89503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    public void setImageResource(int resid) {
900f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn        if (mDrawableId == resid) return;
910f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn
920f27a3f0d7048140fac2329c714af2bfcf4218d6Andrew Flynn        mDrawableId = resid;
93503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        super.setImageResource(resid);
94503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        updateAnim();
95503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    }
96503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato
97503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    @Override
98503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    public void onAttachedToWindow() {
99503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        super.onAttachedToWindow();
100503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        mAttached = true;
101258384aff260270f58bf397329c26b637dd03766Daniel Bateman        updateAnim();
102503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    }
103503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato
104503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    @Override
105503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    public void onDetachedFromWindow() {
106503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        super.onDetachedFromWindow();
107503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        if (mAnim != null) {
108503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato            mAnim.stop();
109503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        }
110503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato        mAttached = false;
111503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato    }
1121e7277e20465be39e30d7e84192c40c4c7b55f89Joe Onorato
1131e7277e20465be39e30d7e84192c40c4c7b55f89Joe Onorato    @Override
1141e7277e20465be39e30d7e84192c40c4c7b55f89Joe Onorato    protected void onVisibilityChanged(View changedView, int vis) {
1151e7277e20465be39e30d7e84192c40c4c7b55f89Joe Onorato        super.onVisibilityChanged(changedView, vis);
1161e7277e20465be39e30d7e84192c40c4c7b55f89Joe Onorato        if (mAnim != null) {
1171e7277e20465be39e30d7e84192c40c4c7b55f89Joe Onorato            if (isShown()) {
1181e7277e20465be39e30d7e84192c40c4c7b55f89Joe Onorato                mAnim.start();
1191e7277e20465be39e30d7e84192c40c4c7b55f89Joe Onorato            } else {
1201e7277e20465be39e30d7e84192c40c4c7b55f89Joe Onorato                mAnim.stop();
1211e7277e20465be39e30d7e84192c40c4c7b55f89Joe Onorato            }
1221e7277e20465be39e30d7e84192c40c4c7b55f89Joe Onorato        }
1231e7277e20465be39e30d7e84192c40c4c7b55f89Joe Onorato    }
124b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek
125b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek    @Override
126b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek    public boolean hasOverlappingRendering() {
127b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek        return mHasOverlappingRendering;
128b20b75ca5f1ebe893777355907886a1516df00b5Selim Cinek    }
129503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato}
130503007dd023668b1e45de948d3673e594d7f5a82Joe Onorato
131