BarTransitions.java revision bd95740648372449a4d5c164d7050eee352d4c24
1e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock/*
2e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock * Copyright (C) 2013 The Android Open Source Project
3e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock *
4e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock * Licensed under the Apache License, Version 2.0 (the "License");
5e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock * you may not use this file except in compliance with the License.
6e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock * You may obtain a copy of the License at
7e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock *
8e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock *      http://www.apache.org/licenses/LICENSE-2.0
9e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock *
10e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock * Unless required by applicable law or agreed to in writing, software
11e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock * distributed under the License is distributed on an "AS IS" BASIS,
12e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock * See the License for the specific language governing permissions and
14e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock * limitations under the License.
15e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock */
16e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock
17e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlockpackage com.android.systemui.statusbar.phone;
18e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock
193b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlockimport android.animation.ArgbEvaluator;
203b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlockimport android.animation.ValueAnimator;
213b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlockimport android.animation.ValueAnimator.AnimatorUpdateListener;
22e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlockimport android.app.ActivityManager;
23e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlockimport android.content.res.Resources;
24e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlockimport android.graphics.drawable.ColorDrawable;
25e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlockimport android.graphics.drawable.Drawable;
26e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlockimport android.graphics.drawable.TransitionDrawable;
2727735a4ba50d42df64eb82acc3f64fa2d68fd505John Spurlockimport android.util.Log;
28e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlockimport android.view.View;
29e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock
30e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlockimport com.android.systemui.R;
31e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock
32e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlockpublic class BarTransitions {
3327735a4ba50d42df64eb82acc3f64fa2d68fd505John Spurlock    private static final boolean DEBUG = false;
34e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    private static final boolean DEBUG_COLORS = false;
35e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock
363b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock    public static final int MODE_OPAQUE = 0;
3789835ddf327ef256b44d91cf1fd1898c0599eb48John Spurlock    public static final int MODE_SEMI_TRANSPARENT = 1;
38bd95740648372449a4d5c164d7050eee352d4c24John Spurlock    public static final int MODE_TRANSLUCENT = 2;
397edfbca5d00cbc376fda790b50a3fedb9c6070abJohn Spurlock    public static final int MODE_LIGHTS_OUT = 3;
407edfbca5d00cbc376fda790b50a3fedb9c6070abJohn Spurlock
41e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    public static final int LIGHTS_IN_DURATION = 250;
42e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    public static final int LIGHTS_OUT_DURATION = 750;
43e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    public static final int BACKGROUND_DURATION = 200;
44e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock
4527735a4ba50d42df64eb82acc3f64fa2d68fd505John Spurlock    private final String mTag;
46e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    private final View mView;
47e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    private final boolean mSupportsTransitions = ActivityManager.isHighEndGfx();
48e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock
49e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    private final int mOpaque;
50e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    private final int mSemiTransparent;
51e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock
52e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock    private int mMode;
53e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    private ValueAnimator mColorDrawableAnimator;
54e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    private boolean mColorDrawableShowing;
55e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock
56e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    private final ColorDrawable mColorDrawable;
57e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    private final TransitionDrawable mTransitionDrawable;
58e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    private final AnimatorUpdateListener mAnimatorListener = new AnimatorUpdateListener() {
593b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        @Override
603b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        public void onAnimationUpdate(ValueAnimator animator) {
61e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            mColorDrawable.setColor((Integer) animator.getAnimatedValue());
623b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        }
633b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock    };
643b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock
657057d2c3a9a88f1d221bc69780385bd20c5b4999John Spurlock    public BarTransitions(View view, int gradientResourceId) {
66e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        mTag = "BarTransitions." + view.getClass().getSimpleName();
67e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        mView = view;
68e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        final Resources res = mView.getContext().getResources();
69e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock
70e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        if (DEBUG_COLORS) {
71e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            mOpaque = 0xff0000ff;
72e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            mSemiTransparent = 0x7f0000ff;
73e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        } else {
74e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            mOpaque = res.getColor(R.drawable.system_bar_background);
75e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            mSemiTransparent = res.getColor(R.color.system_bar_background_semi_transparent);
76e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        }
77e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock
78e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        mColorDrawable = new ColorDrawable(mOpaque);
79e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        mTransitionDrawable = new TransitionDrawable(
807057d2c3a9a88f1d221bc69780385bd20c5b4999John Spurlock                new Drawable[] { res.getDrawable(gradientResourceId), mColorDrawable });
81e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        mTransitionDrawable.setCrossFadeEnabled(true);
82e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        mTransitionDrawable.resetTransition();
83e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        if (mSupportsTransitions) {
84e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            mView.setBackground(mTransitionDrawable);
85e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        }
86e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    }
87e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock
88b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock    public int getMode() {
89b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock        return mMode;
90b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock    }
91b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock
923b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock    public void transitionTo(int mode, boolean animate) {
9389835ddf327ef256b44d91cf1fd1898c0599eb48John Spurlock        if (mMode == mode) return;
9489835ddf327ef256b44d91cf1fd1898c0599eb48John Spurlock        int oldMode = mMode;
95e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock        mMode = mode;
96e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        if (DEBUG) Log.d(mTag, String.format("%s -> %s animate=%s",
97e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock                modeToString(oldMode), modeToString(mode),  animate));
98e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        if (mSupportsTransitions) {
99e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            onTransition(oldMode, mMode, animate);
100e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        }
1013b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock    }
1023b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock
103e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    private Integer getBackgroundColor(int mode) {
104b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock        if (mode == MODE_SEMI_TRANSPARENT) return mSemiTransparent;
105b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock        if (mode == MODE_OPAQUE) return mOpaque;
1067edfbca5d00cbc376fda790b50a3fedb9c6070abJohn Spurlock        if (mode == MODE_LIGHTS_OUT) return mOpaque;
107b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock        return null;
108b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock    }
109b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock
1103b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock    protected void onTransition(int oldMode, int newMode, boolean animate) {
111e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        applyModeBackground(oldMode, newMode, animate);
112e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    }
113e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock
114e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    protected void applyModeBackground(int oldMode, int newMode, boolean animate) {
115e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        if (DEBUG) Log.d(mTag, String.format("applyModeBackground %s animate=%s",
116e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock                modeToString(newMode), animate));
117e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        cancelColorAnimation();
118b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock        Integer oldColor = getBackgroundColor(oldMode);
119b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock        Integer newColor = getBackgroundColor(newMode);
120e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        if (newColor != null) {
121e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            if (animate && oldColor != null && !oldColor.equals(newColor)) {
122e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock                startColorAnimation(oldColor, newColor);
123e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            } else if (!newColor.equals(mColorDrawable.getColor())) {
124e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock                if (DEBUG) Log.d(mTag, String.format("setColor = %08x", newColor));
125e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock                mColorDrawable.setColor(newColor);
126e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            }
127e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        }
128bd95740648372449a4d5c164d7050eee352d4c24John Spurlock        if (newColor == null && mColorDrawableShowing) {
129e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            if (DEBUG) Log.d(mTag, "Hide color layer");
130b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock            if (animate) {
131e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock                mTransitionDrawable.reverseTransition(BACKGROUND_DURATION);
132b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock            } else {
133e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock                mTransitionDrawable.resetTransition();
134b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock            }
135e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            mColorDrawableShowing = false;
136bd95740648372449a4d5c164d7050eee352d4c24John Spurlock        } else if (newColor != null && !mColorDrawableShowing) {
137e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            if (DEBUG) Log.d(mTag, "Show color layer");
138e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            mTransitionDrawable.startTransition(animate ? BACKGROUND_DURATION : 0);
139e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            mColorDrawableShowing = true;
1403b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        }
14189835ddf327ef256b44d91cf1fd1898c0599eb48John Spurlock    }
14289835ddf327ef256b44d91cf1fd1898c0599eb48John Spurlock
143e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    private void startColorAnimation(int from, int to) {
144e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        if (DEBUG) Log.d(mTag, String.format("startColorAnimation %08x -> %08x", from, to));
145e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        mColorDrawableAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), from, to);
146e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        mColorDrawableAnimator.addUpdateListener(mAnimatorListener);
147e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        mColorDrawableAnimator.start();
1485b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock    }
1495b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock
150e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock    private void cancelColorAnimation() {
151e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock        if (mColorDrawableAnimator != null && mColorDrawableAnimator.isStarted()) {
152e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            mColorDrawableAnimator.cancel();
153e631b41d8be2952db48df5838c6fb5d8df4645d1John Spurlock            mColorDrawableAnimator = null;
1545b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock        }
155e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock    }
15627735a4ba50d42df64eb82acc3f64fa2d68fd505John Spurlock
15727735a4ba50d42df64eb82acc3f64fa2d68fd505John Spurlock    public static String modeToString(int mode) {
1583b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        if (mode == MODE_OPAQUE) return "MODE_OPAQUE";
15989835ddf327ef256b44d91cf1fd1898c0599eb48John Spurlock        if (mode == MODE_SEMI_TRANSPARENT) return "MODE_SEMI_TRANSPARENT";
160bd95740648372449a4d5c164d7050eee352d4c24John Spurlock        if (mode == MODE_TRANSLUCENT) return "MODE_TRANSLUCENT";
1617edfbca5d00cbc376fda790b50a3fedb9c6070abJohn Spurlock        if (mode == MODE_LIGHTS_OUT) return "MODE_LIGHTS_OUT";
16227735a4ba50d42df64eb82acc3f64fa2d68fd505John Spurlock        throw new IllegalArgumentException("Unknown mode " + mode);
16327735a4ba50d42df64eb82acc3f64fa2d68fd505John Spurlock    }
164e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock}
165