BarTransitions.java revision 42197263bc08843ad18a8660887ef540c630c232
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.TimeInterpolator; 20import android.app.ActivityManager; 21import android.content.Context; 22import android.content.res.Resources; 23import android.graphics.Canvas; 24import android.graphics.Color; 25import android.graphics.ColorFilter; 26import android.graphics.PixelFormat; 27import android.graphics.Rect; 28import android.graphics.drawable.Drawable; 29import android.os.SystemClock; 30import android.util.Log; 31import android.view.View; 32import android.view.animation.LinearInterpolator; 33 34import com.android.systemui.R; 35 36public class BarTransitions { 37 private static final boolean DEBUG = false; 38 private static final boolean DEBUG_COLORS = false; 39 40 public static final int MODE_OPAQUE = 0; 41 public static final int MODE_SEMI_TRANSPARENT = 1; 42 public static final int MODE_TRANSLUCENT = 2; 43 public static final int MODE_LIGHTS_OUT = 3; 44 45 public static final int LIGHTS_IN_DURATION = 250; 46 public static final int LIGHTS_OUT_DURATION = 750; 47 public static final int BACKGROUND_DURATION = 200; 48 49 private final String mTag; 50 private final View mView; 51 private final boolean mSupportsTransitions = ActivityManager.isHighEndGfx(); 52 private final BarBackgroundDrawable mBarBackground; 53 54 private int mMode; 55 56 public BarTransitions(View view, int gradientResourceId) { 57 mTag = "BarTransitions." + view.getClass().getSimpleName(); 58 mView = view; 59 mBarBackground = new BarBackgroundDrawable(mView.getContext(), gradientResourceId); 60 if (mSupportsTransitions) { 61 mView.setBackground(mBarBackground); 62 } 63 } 64 65 public int getMode() { 66 return mMode; 67 } 68 69 public void transitionTo(int mode, boolean animate) { 70 if (mMode == mode) return; 71 int oldMode = mMode; 72 mMode = mode; 73 if (DEBUG) Log.d(mTag, String.format("%s -> %s animate=%s", 74 modeToString(oldMode), modeToString(mode), animate)); 75 if (mSupportsTransitions) { 76 onTransition(oldMode, mMode, animate); 77 } 78 } 79 80 protected void onTransition(int oldMode, int newMode, boolean animate) { 81 applyModeBackground(oldMode, newMode, animate); 82 } 83 84 protected void applyModeBackground(int oldMode, int newMode, boolean animate) { 85 if (DEBUG) Log.d(mTag, String.format("applyModeBackground oldMode=%s newMode=%s animate=%s", 86 modeToString(oldMode), modeToString(newMode), animate)); 87 mBarBackground.applyModeBackground(oldMode, newMode, animate); 88 } 89 90 public static String modeToString(int mode) { 91 if (mode == MODE_OPAQUE) return "MODE_OPAQUE"; 92 if (mode == MODE_SEMI_TRANSPARENT) return "MODE_SEMI_TRANSPARENT"; 93 if (mode == MODE_TRANSLUCENT) return "MODE_TRANSLUCENT"; 94 if (mode == MODE_LIGHTS_OUT) return "MODE_LIGHTS_OUT"; 95 throw new IllegalArgumentException("Unknown mode " + mode); 96 } 97 98 public void finishAnimations() { 99 mBarBackground.finishAnimation(); 100 } 101 102 private static class BarBackgroundDrawable extends Drawable { 103 private final int mOpaque; 104 private final int mSemiTransparent; 105 private final Drawable mGradient; 106 private final TimeInterpolator mInterpolator; 107 108 private int mMode = -1; 109 private boolean mAnimating; 110 private long mStartTime; 111 private long mEndTime; 112 113 private int mGradientAlpha; 114 private int mColor; 115 116 private int mGradientAlphaStart; 117 private int mColorStart; 118 119 public BarBackgroundDrawable(Context context, int gradientResourceId) { 120 final Resources res = context.getResources(); 121 if (DEBUG_COLORS) { 122 mOpaque = 0xff0000ff; 123 mSemiTransparent = 0x7f0000ff; 124 } else { 125 mOpaque = res.getColor(R.color.system_bar_background_opaque); 126 mSemiTransparent = res.getColor(R.color.system_bar_background_semi_transparent); 127 } 128 mGradient = res.getDrawable(gradientResourceId); 129 mInterpolator = new LinearInterpolator(); 130 } 131 132 @Override 133 public void setAlpha(int alpha) { 134 // noop 135 } 136 137 @Override 138 public void setColorFilter(ColorFilter cf) { 139 // noop 140 } 141 142 @Override 143 protected void onBoundsChange(Rect bounds) { 144 super.onBoundsChange(bounds); 145 mGradient.setBounds(bounds); 146 } 147 148 public void applyModeBackground(int oldMode, int newMode, boolean animate) { 149 if (mMode == newMode) return; 150 mMode = newMode; 151 mAnimating = animate; 152 if (animate) { 153 long now = SystemClock.elapsedRealtime(); 154 mStartTime = now; 155 mEndTime = now + BACKGROUND_DURATION; 156 mGradientAlphaStart = mGradientAlpha; 157 mColorStart = mColor; 158 } 159 invalidateSelf(); 160 } 161 162 @Override 163 public int getOpacity() { 164 return PixelFormat.TRANSLUCENT; 165 } 166 167 public void finishAnimation() { 168 if (mAnimating) { 169 mAnimating = false; 170 invalidateSelf(); 171 } 172 } 173 174 @Override 175 public void draw(Canvas canvas) { 176 int targetGradientAlpha = 0, targetColor = 0; 177 if (mMode == MODE_TRANSLUCENT) { 178 targetGradientAlpha = 0xff; 179 } else if (mMode == MODE_SEMI_TRANSPARENT) { 180 targetColor = mSemiTransparent; 181 } else { 182 targetColor = mOpaque; 183 } 184 if (!mAnimating) { 185 mColor = targetColor; 186 mGradientAlpha = targetGradientAlpha; 187 } else { 188 final long now = SystemClock.elapsedRealtime(); 189 if (now >= mEndTime) { 190 mAnimating = false; 191 mColor = targetColor; 192 mGradientAlpha = targetGradientAlpha; 193 } else { 194 final float t = (now - mStartTime) / (float)(mEndTime - mStartTime); 195 final float v = Math.max(0, Math.min(mInterpolator.getInterpolation(t), 1)); 196 mGradientAlpha = (int)(v * targetGradientAlpha + mGradientAlphaStart * (1 - v)); 197 mColor = Color.argb( 198 (int)(v * Color.alpha(targetColor) + Color.alpha(mColorStart) * (1 - v)), 199 (int)(v * Color.red(targetColor) + Color.red(mColorStart) * (1 - v)), 200 (int)(v * Color.green(targetColor) + Color.green(mColorStart) * (1 - v)), 201 (int)(v * Color.blue(targetColor) + Color.blue(mColorStart) * (1 - v))); 202 } 203 } 204 if (mGradientAlpha > 0) { 205 mGradient.setAlpha(mGradientAlpha); 206 mGradient.draw(canvas); 207 } 208 if (Color.alpha(mColor) > 0) { 209 canvas.drawColor(mColor); 210 } 211 if (mAnimating) { 212 invalidateSelf(); // keep going 213 } 214 } 215 } 216} 217