BarTransitions.java revision b77edbfdab54531023c8bbea7d89b6cefc42096c
1/*
2 * Copyright (C) 2013 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.phone;
18
19import android.animation.ArgbEvaluator;
20import android.animation.ValueAnimator;
21import android.animation.ValueAnimator.AnimatorUpdateListener;
22import android.app.ActivityManager;
23import android.content.Context;
24import android.content.res.Resources;
25import android.graphics.drawable.ColorDrawable;
26import android.graphics.drawable.Drawable;
27import android.util.Log;
28import android.view.View;
29
30import com.android.systemui.R;
31
32public class BarTransitions {
33    private static final boolean DEBUG = false;
34
35    public static final int MODE_OPAQUE = 0;
36    public static final int MODE_SEMI_TRANSPARENT = 1;
37    public static final int MODE_TRANSPARENT = 2;
38
39    private final String mTag;
40    protected final View mTarget;
41    protected final int mOpaque;
42    protected final int mSemiTransparent;
43
44    protected Drawable mTransparent;
45    private int mMode;
46    private ValueAnimator mBackgroundColorAnimator;
47
48    private final AnimatorUpdateListener mBackgroundColorListener = new AnimatorUpdateListener() {
49        @Override
50        public void onAnimationUpdate(ValueAnimator animator) {
51            mTarget.setBackgroundColor((Integer) animator.getAnimatedValue());
52        }
53    };
54
55    public BarTransitions(Context context, View target) {
56        mTag = "BarTransitions." + target.getClass().getSimpleName();
57        mTarget = target;
58        final Resources res = context.getResources();
59        mOpaque = res.getColor(R.drawable.status_bar_background);
60        mSemiTransparent = res.getColor(R.color.status_bar_background_semi_transparent);
61    }
62
63    public int getMode() {
64        return mMode;
65    }
66
67    public void setTransparent(Drawable transparent) {
68        mTransparent = transparent;
69        if (mMode == MODE_TRANSPARENT) {
70            transitionTo(MODE_TRANSPARENT);
71        }
72    }
73
74    public void transitionTo(int mode) {
75        transitionTo(mode, false);
76    }
77
78    public void transitionTo(int mode, boolean animate) {
79        if (mMode == mode) return;
80        int oldMode = mMode;
81        mMode = mode;
82        if (!ActivityManager.isHighEndGfx()) return;
83        if (DEBUG) Log.d(mTag, modeToString(oldMode) + " -> " + modeToString(mode));
84        onTransition(oldMode, mMode, animate);
85    }
86
87    protected Integer getBackgroundColor(int mode) {
88        if (mode == MODE_SEMI_TRANSPARENT) return mSemiTransparent;
89        if (mode == MODE_OPAQUE) return mOpaque;
90        return null;
91    }
92
93    protected void onTransition(int oldMode, int newMode, boolean animate) {
94        cancelBackgroundColorAnimation();
95        Integer oldColor = getBackgroundColor(oldMode);
96        Integer newColor = getBackgroundColor(newMode);
97        if (oldColor != null && newColor != null) {
98            if (animate) {
99                startBackgroundColorAnimation(oldColor, newColor);
100            } else {
101                mTarget.setBackgroundColor(newColor);
102            }
103        } else {
104            mTarget.setBackground(newMode == MODE_TRANSPARENT ? mTransparent
105                    : newMode == MODE_SEMI_TRANSPARENT ? new ColorDrawable(mSemiTransparent)
106                    : new ColorDrawable(mOpaque));
107        }
108    }
109
110    private void startBackgroundColorAnimation(int from, int to) {
111        mBackgroundColorAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), from, to);
112        mBackgroundColorAnimator.addUpdateListener(mBackgroundColorListener);
113        mBackgroundColorAnimator.start();
114    }
115
116    private void cancelBackgroundColorAnimation() {
117        if (mBackgroundColorAnimator != null && mBackgroundColorAnimator.isStarted()) {
118            mBackgroundColorAnimator.cancel();
119            mBackgroundColorAnimator = null;
120        }
121    }
122
123    public static String modeToString(int mode) {
124        if (mode == MODE_OPAQUE) return "MODE_OPAQUE";
125        if (mode == MODE_SEMI_TRANSPARENT) return "MODE_SEMI_TRANSPARENT";
126        if (mode == MODE_TRANSPARENT) return "MODE_TRANSPARENT";
127        throw new IllegalArgumentException("Unknown mode " + mode);
128    }
129}
130