BarTransitions.java revision c18010f6720f606003cde3cd376ddacaca30f6e5
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.TimeInterpolator;
20import android.app.ActivityManager;
21import android.content.Context;
22import android.content.res.Resources;
23import android.graphics.Canvas;
24import android.graphics.Color;
25import android.graphics.ColorFilter;
26import android.graphics.PixelFormat;
27import android.graphics.Rect;
28import android.graphics.drawable.Drawable;
29import android.os.SystemClock;
30import android.util.Log;
31import android.view.View;
32import android.view.animation.LinearInterpolator;
33
34import com.android.systemui.R;
35import com.android.systemui.statusbar.Interpolators;
36
37public class BarTransitions {
38    private static final boolean DEBUG = false;
39    private static final boolean DEBUG_COLORS = false;
40
41    public static final boolean HIGH_END = ActivityManager.isHighEndGfx();
42
43    public static final int MODE_OPAQUE = 0;
44    public static final int MODE_SEMI_TRANSPARENT = 1;
45    public static final int MODE_TRANSLUCENT = 2;
46    public static final int MODE_LIGHTS_OUT = 3;
47    public static final int MODE_TRANSPARENT = 4;
48    public static final int MODE_WARNING = 5;
49    public static final int MODE_LIGHTS_OUT_TRANSPARENT = 6;
50
51    public static final int LIGHTS_IN_DURATION = 250;
52    public static final int LIGHTS_OUT_DURATION = 750;
53    public static final int BACKGROUND_DURATION = 200;
54
55    private final String mTag;
56    private final View mView;
57    private final BarBackgroundDrawable mBarBackground;
58
59    private int mMode;
60
61    public BarTransitions(View view, int gradientResourceId) {
62        mTag = "BarTransitions." + view.getClass().getSimpleName();
63        mView = view;
64        mBarBackground = new BarBackgroundDrawable(mView.getContext(), gradientResourceId);
65        if (HIGH_END) {
66            mView.setBackground(mBarBackground);
67        }
68    }
69
70    public int getMode() {
71        return mMode;
72    }
73
74    public void transitionTo(int mode, boolean animate) {
75        // low-end devices do not support translucent modes, fallback to opaque
76        if (!HIGH_END && (mode == MODE_SEMI_TRANSPARENT || mode == MODE_TRANSLUCENT
77                || mode == MODE_TRANSPARENT)) {
78            mode = MODE_OPAQUE;
79        }
80        if (!HIGH_END && (mode == MODE_LIGHTS_OUT_TRANSPARENT)) {
81            mode = MODE_LIGHTS_OUT;
82        }
83        if (mMode == mode) return;
84        int oldMode = mMode;
85        mMode = mode;
86        if (DEBUG) Log.d(mTag, String.format("%s -> %s animate=%s",
87                modeToString(oldMode), modeToString(mode),  animate));
88        onTransition(oldMode, mMode, animate);
89    }
90
91    protected void onTransition(int oldMode, int newMode, boolean animate) {
92        if (HIGH_END) {
93            applyModeBackground(oldMode, newMode, animate);
94        }
95    }
96
97    protected void applyModeBackground(int oldMode, int newMode, boolean animate) {
98        if (DEBUG) Log.d(mTag, String.format("applyModeBackground oldMode=%s newMode=%s animate=%s",
99                modeToString(oldMode), modeToString(newMode), animate));
100        mBarBackground.applyModeBackground(oldMode, newMode, animate);
101    }
102
103    public static String modeToString(int mode) {
104        if (mode == MODE_OPAQUE) return "MODE_OPAQUE";
105        if (mode == MODE_SEMI_TRANSPARENT) return "MODE_SEMI_TRANSPARENT";
106        if (mode == MODE_TRANSLUCENT) return "MODE_TRANSLUCENT";
107        if (mode == MODE_LIGHTS_OUT) return "MODE_LIGHTS_OUT";
108        if (mode == MODE_TRANSPARENT) return "MODE_TRANSPARENT";
109        if (mode == MODE_WARNING) return "MODE_WARNING";
110        if (mode == MODE_LIGHTS_OUT_TRANSPARENT) return "MODE_LIGHTS_OUT_TRANSPARENT";
111        throw new IllegalArgumentException("Unknown mode " + mode);
112    }
113
114    public void finishAnimations() {
115        mBarBackground.finishAnimation();
116    }
117
118    protected boolean isLightsOut(int mode) {
119        return mode == MODE_LIGHTS_OUT || mode == MODE_LIGHTS_OUT_TRANSPARENT;
120    }
121
122    private static class BarBackgroundDrawable extends Drawable {
123        private final int mOpaque;
124        private final int mSemiTransparent;
125        private final int mTransparent;
126        private final int mWarning;
127        private final Drawable mGradient;
128
129        private int mMode = -1;
130        private boolean mAnimating;
131        private long mStartTime;
132        private long mEndTime;
133
134        private int mGradientAlpha;
135        private int mColor;
136
137        private int mGradientAlphaStart;
138        private int mColorStart;
139
140        public BarBackgroundDrawable(Context context, int gradientResourceId) {
141            final Resources res = context.getResources();
142            if (DEBUG_COLORS) {
143                mOpaque = 0xff0000ff;
144                mSemiTransparent = 0x7f0000ff;
145                mTransparent = 0x2f0000ff;
146                mWarning = 0xffff0000;
147            } else {
148                mOpaque = context.getColor(R.color.system_bar_background_opaque);
149                mSemiTransparent = context.getColor(
150                        com.android.internal.R.color.system_bar_background_semi_transparent);
151                mTransparent = context.getColor(R.color.system_bar_background_transparent);
152                mWarning = context.getColor(com.android.internal.R.color.battery_saver_mode_color);
153            }
154            mGradient = context.getDrawable(gradientResourceId);
155        }
156
157        @Override
158        public void setAlpha(int alpha) {
159            // noop
160        }
161
162        @Override
163        public void setColorFilter(ColorFilter colorFilter) {
164            // noop
165        }
166
167        @Override
168        protected void onBoundsChange(Rect bounds) {
169            super.onBoundsChange(bounds);
170            mGradient.setBounds(bounds);
171        }
172
173        public void applyModeBackground(int oldMode, int newMode, boolean animate) {
174            if (mMode == newMode) return;
175            mMode = newMode;
176            mAnimating = animate;
177            if (animate) {
178                long now = SystemClock.elapsedRealtime();
179                mStartTime = now;
180                mEndTime = now + BACKGROUND_DURATION;
181                mGradientAlphaStart = mGradientAlpha;
182                mColorStart = mColor;
183            }
184            invalidateSelf();
185        }
186
187        @Override
188        public int getOpacity() {
189            return PixelFormat.TRANSLUCENT;
190        }
191
192        public void finishAnimation() {
193            if (mAnimating) {
194                mAnimating = false;
195                invalidateSelf();
196            }
197        }
198
199        @Override
200        public void draw(Canvas canvas) {
201            int targetGradientAlpha = 0, targetColor = 0;
202            if (mMode == MODE_WARNING) {
203                targetColor = mWarning;
204            } else if (mMode == MODE_TRANSLUCENT) {
205                targetColor = mSemiTransparent;
206            } else if (mMode == MODE_SEMI_TRANSPARENT) {
207                targetColor = mSemiTransparent;
208            } else if (mMode == MODE_TRANSPARENT || mMode == MODE_LIGHTS_OUT_TRANSPARENT) {
209                targetColor = mTransparent;
210            } else {
211                targetColor = mOpaque;
212            }
213            if (!mAnimating) {
214                mColor = targetColor;
215                mGradientAlpha = targetGradientAlpha;
216            } else {
217                final long now = SystemClock.elapsedRealtime();
218                if (now >= mEndTime) {
219                    mAnimating = false;
220                    mColor = targetColor;
221                    mGradientAlpha = targetGradientAlpha;
222                } else {
223                    final float t = (now - mStartTime) / (float)(mEndTime - mStartTime);
224                    final float v = Math.max(0, Math.min(
225                            Interpolators.LINEAR.getInterpolation(t), 1));
226                    mGradientAlpha = (int)(v * targetGradientAlpha + mGradientAlphaStart * (1 - v));
227                    mColor = Color.argb(
228                          (int)(v * Color.alpha(targetColor) + Color.alpha(mColorStart) * (1 - v)),
229                          (int)(v * Color.red(targetColor) + Color.red(mColorStart) * (1 - v)),
230                          (int)(v * Color.green(targetColor) + Color.green(mColorStart) * (1 - v)),
231                          (int)(v * Color.blue(targetColor) + Color.blue(mColorStart) * (1 - v)));
232                }
233            }
234            if (mGradientAlpha > 0) {
235                mGradient.setAlpha(mGradientAlpha);
236                mGradient.draw(canvas);
237            }
238            if (Color.alpha(mColor) > 0) {
239                canvas.drawColor(mColor);
240            }
241            if (mAnimating) {
242                invalidateSelf();  // keep going
243            }
244        }
245    }
246}
247