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