BarTransitions.java revision d4e6575c4f664e0d42d9306c9762d96533df429e
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 transitionTo(int mode, boolean animate) { 68 if (mMode == mode) return; 69 int oldMode = mMode; 70 mMode = mode; 71 if (!ActivityManager.isHighEndGfx()) return; 72 if (DEBUG) Log.d(mTag, modeToString(oldMode) + " -> " + modeToString(mode)); 73 onTransition(oldMode, mMode, animate); 74 } 75 76 protected Integer getBackgroundColor(int mode) { 77 if (mode == MODE_SEMI_TRANSPARENT) return mSemiTransparent; 78 if (mode == MODE_OPAQUE) return mOpaque; 79 return null; 80 } 81 82 protected void onTransition(int oldMode, int newMode, boolean animate) { 83 cancelBackgroundColorAnimation(); 84 Integer oldColor = getBackgroundColor(oldMode); 85 Integer newColor = getBackgroundColor(newMode); 86 if (oldColor != null && newColor != null) { 87 if (animate) { 88 startBackgroundColorAnimation(oldColor, newColor); 89 } else { 90 mTarget.setBackgroundColor(newColor); 91 } 92 } else { 93 mTarget.setBackground(newMode == MODE_TRANSPARENT ? mTransparent 94 : newMode == MODE_SEMI_TRANSPARENT ? new ColorDrawable(mSemiTransparent) 95 : new ColorDrawable(mOpaque)); 96 } 97 } 98 99 private void startBackgroundColorAnimation(int from, int to) { 100 mBackgroundColorAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), from, to); 101 mBackgroundColorAnimator.addUpdateListener(mBackgroundColorListener); 102 mBackgroundColorAnimator.start(); 103 } 104 105 private void cancelBackgroundColorAnimation() { 106 if (mBackgroundColorAnimator != null && mBackgroundColorAnimator.isStarted()) { 107 mBackgroundColorAnimator.cancel(); 108 mBackgroundColorAnimator = null; 109 } 110 } 111 112 public static String modeToString(int mode) { 113 if (mode == MODE_OPAQUE) return "MODE_OPAQUE"; 114 if (mode == MODE_SEMI_TRANSPARENT) return "MODE_SEMI_TRANSPARENT"; 115 if (mode == MODE_TRANSPARENT) return "MODE_TRANSPARENT"; 116 throw new IllegalArgumentException("Unknown mode " + mode); 117 } 118} 119