BarTransitions.java revision 3b139a9ed06fdaad694ca97b8f99e7038007a054
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 private final View mTarget; 41 private final int mOpaque; 42 private final int mSemiTransparent; 43 44 protected Drawable mTransparent; 45 private int mMode; 46 47 private final AnimatorUpdateListener mBackgroundColorListener = new AnimatorUpdateListener() { 48 @Override 49 public void onAnimationUpdate(ValueAnimator animator) { 50 mTarget.setBackgroundColor((Integer) animator.getAnimatedValue()); 51 } 52 }; 53 54 public BarTransitions(Context context, View target) { 55 mTag = "BarTransitions." + target.getClass().getSimpleName(); 56 mTarget = target; 57 final Resources res = context.getResources(); 58 mOpaque = res.getColor(R.drawable.status_bar_background); 59 mSemiTransparent = res.getColor(R.color.status_bar_background_semi_transparent); 60 } 61 62 public void setTransparent(Drawable transparent) { 63 mTransparent = transparent; 64 if (mMode == MODE_TRANSPARENT) { 65 transitionTo(MODE_TRANSPARENT); 66 } 67 } 68 69 public void transitionTo(int mode) { 70 transitionTo(mode, false); 71 } 72 73 public void transitionTo(int mode, boolean animate) { 74 if (mMode == mode) return; 75 int oldMode = mMode; 76 mMode = mode; 77 if (!ActivityManager.isHighEndGfx()) return; 78 if (DEBUG) Log.d(mTag, modeToString(oldMode) + " -> " + modeToString(mode)); 79 onTransition(oldMode, mMode, animate); 80 } 81 82 protected void onTransition(int oldMode, int newMode, boolean animate) { 83 if (animate && oldMode == MODE_SEMI_TRANSPARENT && newMode == MODE_OPAQUE) { 84 startColorAnimation(mSemiTransparent, mOpaque); 85 } else if (animate && oldMode == MODE_OPAQUE && newMode == MODE_SEMI_TRANSPARENT) { 86 startColorAnimation(mOpaque, mSemiTransparent); 87 } else if (newMode == MODE_OPAQUE || newMode == MODE_SEMI_TRANSPARENT) { 88 mTarget.setBackgroundColor(newMode == MODE_OPAQUE ? mOpaque : mSemiTransparent); 89 } else { 90 mTarget.setBackground(newMode == MODE_TRANSPARENT? mTransparent 91 : newMode == MODE_SEMI_TRANSPARENT ? new ColorDrawable(mSemiTransparent) 92 : new ColorDrawable(mOpaque)); 93 } 94 } 95 96 private void startColorAnimation(int from, int to) { 97 ValueAnimator anim = ValueAnimator.ofObject(new ArgbEvaluator(), from, to); 98 anim.addUpdateListener(mBackgroundColorListener); 99 anim.start(); 100 } 101 102 public static String modeToString(int mode) { 103 if (mode == MODE_OPAQUE) return "MODE_OPAQUE"; 104 if (mode == MODE_SEMI_TRANSPARENT) return "MODE_SEMI_TRANSPARENT"; 105 if (mode == MODE_TRANSPARENT) return "MODE_TRANSPARENT"; 106 throw new IllegalArgumentException("Unknown mode " + mode); 107 } 108} 109