BarTransitions.java revision 4fa7892f35268ff7d591928ac9f5a26c267031e3
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 boolean HIGH_END = ActivityManager.isHighEndGfx(); 41 42 public static final int MODE_OPAQUE = 0; 43 public static final int MODE_SEMI_TRANSPARENT = 1; 44 public static final int MODE_TRANSLUCENT = 2; 45 public static final int MODE_LIGHTS_OUT = 3; 46 public static final int MODE_TRANSPARENT = 4; 47 public static final int MODE_WARNING = 5; 48 public static final int MODE_LIGHTS_OUT_TRANSPARENT = 6; 49 50 public static final int LIGHTS_IN_DURATION = 250; 51 public static final int LIGHTS_OUT_DURATION = 750; 52 public static final int BACKGROUND_DURATION = 200; 53 54 private final String mTag; 55 private final View mView; 56 private final BarBackgroundDrawable mBarBackground; 57 58 private int mMode; 59 60 public BarTransitions(View view, int gradientResourceId) { 61 mTag = "BarTransitions." + view.getClass().getSimpleName(); 62 mView = view; 63 mBarBackground = new BarBackgroundDrawable(mView.getContext(), gradientResourceId); 64 if (HIGH_END) { 65 mView.setBackground(mBarBackground); 66 } 67 } 68 69 public int getMode() { 70 return mMode; 71 } 72 73 public void transitionTo(int mode, boolean animate) { 74 // low-end devices do not support translucent modes, fallback to opaque 75 if (!HIGH_END && (mode == MODE_SEMI_TRANSPARENT || mode == MODE_TRANSLUCENT 76 || mode == MODE_TRANSPARENT)) { 77 mode = MODE_OPAQUE; 78 } 79 if (!HIGH_END && (mode == MODE_LIGHTS_OUT_TRANSPARENT)) { 80 mode = MODE_LIGHTS_OUT; 81 } 82 if (mMode == mode) return; 83 int oldMode = mMode; 84 mMode = mode; 85 if (DEBUG) Log.d(mTag, String.format("%s -> %s animate=%s", 86 modeToString(oldMode), modeToString(mode), animate)); 87 onTransition(oldMode, mMode, animate); 88 } 89 90 protected void onTransition(int oldMode, int newMode, boolean animate) { 91 if (HIGH_END) { 92 applyModeBackground(oldMode, newMode, animate); 93 } 94 } 95 96 protected void applyModeBackground(int oldMode, int newMode, boolean animate) { 97 if (DEBUG) Log.d(mTag, String.format("applyModeBackground oldMode=%s newMode=%s animate=%s", 98 modeToString(oldMode), modeToString(newMode), animate)); 99 mBarBackground.applyModeBackground(oldMode, newMode, animate); 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_TRANSLUCENT) return "MODE_TRANSLUCENT"; 106 if (mode == MODE_LIGHTS_OUT) return "MODE_LIGHTS_OUT"; 107 if (mode == MODE_TRANSPARENT) return "MODE_TRANSPARENT"; 108 if (mode == MODE_WARNING) return "MODE_WARNING"; 109 if (mode == MODE_LIGHTS_OUT_TRANSPARENT) return "MODE_LIGHTS_OUT_TRANSPARENT"; 110 throw new IllegalArgumentException("Unknown mode " + mode); 111 } 112 113 public void finishAnimations() { 114 mBarBackground.finishAnimation(); 115 } 116 117 protected boolean isLightsOut(int mode) { 118 return mode == MODE_LIGHTS_OUT || mode == MODE_LIGHTS_OUT_TRANSPARENT; 119 } 120 121 private static class BarBackgroundDrawable extends Drawable { 122 private final int mOpaque; 123 private final int mSemiTransparent; 124 private final int mTransparent; 125 private final int mWarning; 126 private final Drawable mGradient; 127 private final TimeInterpolator mInterpolator; 128 129 private int mMode = -1; 130 private boolean mAnimating; 131 private long mStartTime; 132 private long mEndTime; 133 134 private int mGradientAlpha; 135 private int mColor; 136 137 private int mGradientAlphaStart; 138 private int mColorStart; 139 140 public BarBackgroundDrawable(Context context, int gradientResourceId) { 141 final Resources res = context.getResources(); 142 if (DEBUG_COLORS) { 143 mOpaque = 0xff0000ff; 144 mSemiTransparent = 0x7f0000ff; 145 mTransparent = 0x2f0000ff; 146 mWarning = 0xffff0000; 147 } else { 148 mOpaque = context.getColor(R.color.system_bar_background_opaque); 149 mSemiTransparent = context.getColor( 150 com.android.internal.R.color.system_bar_background_semi_transparent); 151 mTransparent = context.getColor(R.color.system_bar_background_transparent); 152 mWarning = context.getColor(com.android.internal.R.color.battery_saver_mode_color); 153 } 154 mGradient = context.getDrawable(gradientResourceId); 155 mInterpolator = new LinearInterpolator(); 156 } 157 158 @Override 159 public void setAlpha(int alpha) { 160 // noop 161 } 162 163 @Override 164 public void setColorFilter(ColorFilter colorFilter) { 165 // noop 166 } 167 168 @Override 169 protected void onBoundsChange(Rect bounds) { 170 super.onBoundsChange(bounds); 171 mGradient.setBounds(bounds); 172 } 173 174 public void applyModeBackground(int oldMode, int newMode, boolean animate) { 175 if (mMode == newMode) return; 176 mMode = newMode; 177 mAnimating = animate; 178 if (animate) { 179 long now = SystemClock.elapsedRealtime(); 180 mStartTime = now; 181 mEndTime = now + BACKGROUND_DURATION; 182 mGradientAlphaStart = mGradientAlpha; 183 mColorStart = mColor; 184 } 185 invalidateSelf(); 186 } 187 188 @Override 189 public int getOpacity() { 190 return PixelFormat.TRANSLUCENT; 191 } 192 193 public void finishAnimation() { 194 if (mAnimating) { 195 mAnimating = false; 196 invalidateSelf(); 197 } 198 } 199 200 @Override 201 public void draw(Canvas canvas) { 202 int targetGradientAlpha = 0, targetColor = 0; 203 if (mMode == MODE_WARNING) { 204 targetColor = mWarning; 205 } else if (mMode == MODE_TRANSLUCENT) { 206 targetColor = mSemiTransparent; 207 } else if (mMode == MODE_SEMI_TRANSPARENT) { 208 targetColor = mSemiTransparent; 209 } else if (mMode == MODE_TRANSPARENT || mMode == MODE_LIGHTS_OUT_TRANSPARENT) { 210 targetColor = mTransparent; 211 } else { 212 targetColor = mOpaque; 213 } 214 if (!mAnimating) { 215 mColor = targetColor; 216 mGradientAlpha = targetGradientAlpha; 217 } else { 218 final long now = SystemClock.elapsedRealtime(); 219 if (now >= mEndTime) { 220 mAnimating = false; 221 mColor = targetColor; 222 mGradientAlpha = targetGradientAlpha; 223 } else { 224 final float t = (now - mStartTime) / (float)(mEndTime - mStartTime); 225 final float v = Math.max(0, Math.min(mInterpolator.getInterpolation(t), 1)); 226 mGradientAlpha = (int)(v * targetGradientAlpha + mGradientAlphaStart * (1 - v)); 227 mColor = Color.argb( 228 (int)(v * Color.alpha(targetColor) + Color.alpha(mColorStart) * (1 - v)), 229 (int)(v * Color.red(targetColor) + Color.red(mColorStart) * (1 - v)), 230 (int)(v * Color.green(targetColor) + Color.green(mColorStart) * (1 - v)), 231 (int)(v * Color.blue(targetColor) + Color.blue(mColorStart) * (1 - v))); 232 } 233 } 234 if (mGradientAlpha > 0) { 235 mGradient.setAlpha(mGradientAlpha); 236 mGradient.draw(canvas); 237 } 238 if (Color.alpha(mColor) > 0) { 239 canvas.drawColor(mColor); 240 } 241 if (mAnimating) { 242 invalidateSelf(); // keep going 243 } 244 } 245 } 246} 247