BarTransitions.java revision b77edbfdab54531023c8bbea7d89b6cefc42096c
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.Context;
24e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlockimport android.content.res.Resources;
25e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlockimport android.graphics.drawable.ColorDrawable;
26e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlockimport android.graphics.drawable.Drawable;
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;
34e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock
353b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock    public static final int MODE_OPAQUE = 0;
3689835ddf327ef256b44d91cf1fd1898c0599eb48John Spurlock    public static final int MODE_SEMI_TRANSPARENT = 1;
37e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock    public static final int MODE_TRANSPARENT = 2;
38e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock
3927735a4ba50d42df64eb82acc3f64fa2d68fd505John Spurlock    private final String mTag;
405b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock    protected final View mTarget;
415b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock    protected final int mOpaque;
425b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock    protected final int mSemiTransparent;
43e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock
4489835ddf327ef256b44d91cf1fd1898c0599eb48John Spurlock    protected Drawable mTransparent;
45e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock    private int mMode;
465b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock    private ValueAnimator mBackgroundColorAnimator;
47e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock
483b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock    private final AnimatorUpdateListener mBackgroundColorListener = new AnimatorUpdateListener() {
493b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        @Override
503b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        public void onAnimationUpdate(ValueAnimator animator) {
513b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock            mTarget.setBackgroundColor((Integer) animator.getAnimatedValue());
523b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        }
533b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock    };
543b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock
5589835ddf327ef256b44d91cf1fd1898c0599eb48John Spurlock    public BarTransitions(Context context, View target) {
5627735a4ba50d42df64eb82acc3f64fa2d68fd505John Spurlock        mTag = "BarTransitions." + target.getClass().getSimpleName();
57e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock        mTarget = target;
58e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock        final Resources res = context.getResources();
593b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        mOpaque = res.getColor(R.drawable.status_bar_background);
603b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        mSemiTransparent = res.getColor(R.color.status_bar_background_semi_transparent);
61e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock    }
62e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock
63b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock    public int getMode() {
64b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock        return mMode;
65b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock    }
66b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock
67e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock    public void setTransparent(Drawable transparent) {
68e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock        mTransparent = transparent;
69e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock        if (mMode == MODE_TRANSPARENT) {
70e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock            transitionTo(MODE_TRANSPARENT);
71e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock        }
72e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock    }
73e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock
74e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock    public void transitionTo(int mode) {
753b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        transitionTo(mode, false);
763b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock    }
773b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock
783b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock    public void transitionTo(int mode, boolean animate) {
7989835ddf327ef256b44d91cf1fd1898c0599eb48John Spurlock        if (mMode == mode) return;
8089835ddf327ef256b44d91cf1fd1898c0599eb48John Spurlock        int oldMode = mMode;
81e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock        mMode = mode;
82e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock        if (!ActivityManager.isHighEndGfx()) return;
833b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        if (DEBUG) Log.d(mTag, modeToString(oldMode) + " -> " + modeToString(mode));
843b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        onTransition(oldMode, mMode, animate);
853b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock    }
863b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock
87b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock    protected Integer getBackgroundColor(int mode) {
88b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock        if (mode == MODE_SEMI_TRANSPARENT) return mSemiTransparent;
89b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock        if (mode == MODE_OPAQUE) return mOpaque;
90b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock        return null;
91b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock    }
92b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock
933b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock    protected void onTransition(int oldMode, int newMode, boolean animate) {
945b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock        cancelBackgroundColorAnimation();
95b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock        Integer oldColor = getBackgroundColor(oldMode);
96b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock        Integer newColor = getBackgroundColor(newMode);
97b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock        if (oldColor != null && newColor != null) {
98b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock            if (animate) {
99b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock                startBackgroundColorAnimation(oldColor, newColor);
100b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock            } else {
101b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock                mTarget.setBackgroundColor(newColor);
102b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock            }
1033b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        } else {
104b77edbfdab54531023c8bbea7d89b6cefc42096cJohn Spurlock            mTarget.setBackground(newMode == MODE_TRANSPARENT ? mTransparent
1053b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock                    : newMode == MODE_SEMI_TRANSPARENT ? new ColorDrawable(mSemiTransparent)
1063b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock                    : new ColorDrawable(mOpaque));
1073b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        }
10889835ddf327ef256b44d91cf1fd1898c0599eb48John Spurlock    }
10989835ddf327ef256b44d91cf1fd1898c0599eb48John Spurlock
1105b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock    private void startBackgroundColorAnimation(int from, int to) {
1115b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock        mBackgroundColorAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), from, to);
1125b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock        mBackgroundColorAnimator.addUpdateListener(mBackgroundColorListener);
1135b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock        mBackgroundColorAnimator.start();
1145b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock    }
1155b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock
1165b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock    private void cancelBackgroundColorAnimation() {
1175b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock        if (mBackgroundColorAnimator != null && mBackgroundColorAnimator.isStarted()) {
1185b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock            mBackgroundColorAnimator.cancel();
1195b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock            mBackgroundColorAnimator = null;
1205b9145bf990a9bbf4fdef1739e61ff8c70ec868fJohn Spurlock        }
121e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock    }
12227735a4ba50d42df64eb82acc3f64fa2d68fd505John Spurlock
12327735a4ba50d42df64eb82acc3f64fa2d68fd505John Spurlock    public static String modeToString(int mode) {
1243b139a9ed06fdaad694ca97b8f99e7038007a054John Spurlock        if (mode == MODE_OPAQUE) return "MODE_OPAQUE";
12589835ddf327ef256b44d91cf1fd1898c0599eb48John Spurlock        if (mode == MODE_SEMI_TRANSPARENT) return "MODE_SEMI_TRANSPARENT";
12627735a4ba50d42df64eb82acc3f64fa2d68fd505John Spurlock        if (mode == MODE_TRANSPARENT) return "MODE_TRANSPARENT";
12727735a4ba50d42df64eb82acc3f64fa2d68fd505John Spurlock        throw new IllegalArgumentException("Unknown mode " + mode);
12827735a4ba50d42df64eb82acc3f64fa2d68fd505John Spurlock    }
129e932e30eadb5978591316ffe3d21d60604c0cd91John Spurlock}
130