John Spurlock | e932e30 | 2013-08-12 10:16:29 -0400 | [diff] [blame] | 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 | |
| 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
| 19 | import android.app.ActivityManager; |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 20 | import android.content.Context; |
John Spurlock | e932e30 | 2013-08-12 10:16:29 -0400 | [diff] [blame] | 21 | import android.content.res.Resources; |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 22 | import android.graphics.Canvas; |
| 23 | import android.graphics.Color; |
| 24 | import android.graphics.ColorFilter; |
Yorke Lee | 1b268cb | 2016-02-25 17:58:33 -0800 | [diff] [blame] | 25 | import android.graphics.Paint; |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 26 | import android.graphics.PixelFormat; |
Yorke Lee | 1b268cb | 2016-02-25 17:58:33 -0800 | [diff] [blame] | 27 | import android.graphics.PorterDuff; |
| 28 | import android.graphics.PorterDuffColorFilter; |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 29 | import android.graphics.Rect; |
Yorke Lee | 1b268cb | 2016-02-25 17:58:33 -0800 | [diff] [blame] | 30 | import android.graphics.PorterDuff.Mode; |
John Spurlock | e932e30 | 2013-08-12 10:16:29 -0400 | [diff] [blame] | 31 | import android.graphics.drawable.Drawable; |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 32 | import android.os.SystemClock; |
John Spurlock | 27735a4 | 2013-08-14 17:57:38 -0400 | [diff] [blame] | 33 | import android.util.Log; |
John Spurlock | e932e30 | 2013-08-12 10:16:29 -0400 | [diff] [blame] | 34 | import android.view.View; |
| 35 | |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 36 | import com.android.systemui.Interpolators; |
John Spurlock | e932e30 | 2013-08-12 10:16:29 -0400 | [diff] [blame] | 37 | import com.android.systemui.R; |
| 38 | |
| 39 | public class BarTransitions { |
John Spurlock | 27735a4 | 2013-08-14 17:57:38 -0400 | [diff] [blame] | 40 | private static final boolean DEBUG = false; |
John Spurlock | e631b41 | 2013-09-18 16:33:57 -0400 | [diff] [blame] | 41 | private static final boolean DEBUG_COLORS = false; |
John Spurlock | e932e30 | 2013-08-12 10:16:29 -0400 | [diff] [blame] | 42 | |
John Spurlock | fd0e6c0 | 2014-01-14 14:51:23 -0500 | [diff] [blame] | 43 | public static final boolean HIGH_END = ActivityManager.isHighEndGfx(); |
| 44 | |
John Spurlock | 3b139a9 | 2013-08-17 17:18:08 -0400 | [diff] [blame] | 45 | public static final int MODE_OPAQUE = 0; |
John Spurlock | 89835dd | 2013-08-16 15:06:51 -0400 | [diff] [blame] | 46 | public static final int MODE_SEMI_TRANSPARENT = 1; |
John Spurlock | bd95740 | 2013-10-03 11:38:39 -0400 | [diff] [blame] | 47 | public static final int MODE_TRANSLUCENT = 2; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 48 | public static final int MODE_LIGHTS_OUT = 3; |
Adrian Roos | ea56251 | 2014-05-05 13:33:03 +0200 | [diff] [blame] | 49 | public static final int MODE_TRANSPARENT = 4; |
John Spurlock | 0ff62e0 | 2014-07-22 16:15:08 -0400 | [diff] [blame] | 50 | public static final int MODE_WARNING = 5; |
Adrian Roos | c0f0a74 | 2014-10-28 16:39:56 +0100 | [diff] [blame] | 51 | public static final int MODE_LIGHTS_OUT_TRANSPARENT = 6; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 52 | |
John Spurlock | e631b41 | 2013-09-18 16:33:57 -0400 | [diff] [blame] | 53 | public static final int LIGHTS_IN_DURATION = 250; |
| 54 | public static final int LIGHTS_OUT_DURATION = 750; |
| 55 | public static final int BACKGROUND_DURATION = 200; |
John Spurlock | e932e30 | 2013-08-12 10:16:29 -0400 | [diff] [blame] | 56 | |
John Spurlock | 27735a4 | 2013-08-14 17:57:38 -0400 | [diff] [blame] | 57 | private final String mTag; |
John Spurlock | e631b41 | 2013-09-18 16:33:57 -0400 | [diff] [blame] | 58 | private final View mView; |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 59 | private final BarBackgroundDrawable mBarBackground; |
John Spurlock | e631b41 | 2013-09-18 16:33:57 -0400 | [diff] [blame] | 60 | |
John Spurlock | e932e30 | 2013-08-12 10:16:29 -0400 | [diff] [blame] | 61 | private int mMode; |
Yorke Lee | 1b268cb | 2016-02-25 17:58:33 -0800 | [diff] [blame] | 62 | private boolean mAlwaysOpaque = false; |
John Spurlock | 3b139a9 | 2013-08-17 17:18:08 -0400 | [diff] [blame] | 63 | |
John Spurlock | 7057d2c | 2013-10-02 10:00:37 -0400 | [diff] [blame] | 64 | public BarTransitions(View view, int gradientResourceId) { |
John Spurlock | e631b41 | 2013-09-18 16:33:57 -0400 | [diff] [blame] | 65 | mTag = "BarTransitions." + view.getClass().getSimpleName(); |
| 66 | mView = view; |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 67 | mBarBackground = new BarBackgroundDrawable(mView.getContext(), gradientResourceId); |
John Spurlock | fd0e6c0 | 2014-01-14 14:51:23 -0500 | [diff] [blame] | 68 | if (HIGH_END) { |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 69 | mView.setBackground(mBarBackground); |
John Spurlock | e631b41 | 2013-09-18 16:33:57 -0400 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
John Spurlock | b77edbf | 2013-08-21 21:04:12 -0400 | [diff] [blame] | 73 | public int getMode() { |
| 74 | return mMode; |
| 75 | } |
| 76 | |
Yorke Lee | 1b268cb | 2016-02-25 17:58:33 -0800 | [diff] [blame] | 77 | /** |
| 78 | * @param alwaysOpaque if {@code true}, the bar's background will always be opaque, regardless |
| 79 | * of what mode it is currently set to. |
| 80 | */ |
| 81 | public void setAlwaysOpaque(boolean alwaysOpaque) { |
| 82 | mAlwaysOpaque = alwaysOpaque; |
| 83 | } |
| 84 | |
| 85 | public boolean isAlwaysOpaque() { |
| 86 | // Low-end devices do not support translucent modes, fallback to opaque |
| 87 | return !HIGH_END || mAlwaysOpaque; |
| 88 | } |
| 89 | |
John Spurlock | 3b139a9 | 2013-08-17 17:18:08 -0400 | [diff] [blame] | 90 | public void transitionTo(int mode, boolean animate) { |
Yorke Lee | 1b268cb | 2016-02-25 17:58:33 -0800 | [diff] [blame] | 91 | if (isAlwaysOpaque() && (mode == MODE_SEMI_TRANSPARENT || mode == MODE_TRANSLUCENT |
Adrian Roos | ea56251 | 2014-05-05 13:33:03 +0200 | [diff] [blame] | 92 | || mode == MODE_TRANSPARENT)) { |
John Spurlock | fd0e6c0 | 2014-01-14 14:51:23 -0500 | [diff] [blame] | 93 | mode = MODE_OPAQUE; |
| 94 | } |
Yorke Lee | 1b268cb | 2016-02-25 17:58:33 -0800 | [diff] [blame] | 95 | if (isAlwaysOpaque() && (mode == MODE_LIGHTS_OUT_TRANSPARENT)) { |
Adrian Roos | c0f0a74 | 2014-10-28 16:39:56 +0100 | [diff] [blame] | 96 | mode = MODE_LIGHTS_OUT; |
| 97 | } |
John Spurlock | 89835dd | 2013-08-16 15:06:51 -0400 | [diff] [blame] | 98 | if (mMode == mode) return; |
| 99 | int oldMode = mMode; |
John Spurlock | e932e30 | 2013-08-12 10:16:29 -0400 | [diff] [blame] | 100 | mMode = mode; |
John Spurlock | e631b41 | 2013-09-18 16:33:57 -0400 | [diff] [blame] | 101 | if (DEBUG) Log.d(mTag, String.format("%s -> %s animate=%s", |
| 102 | modeToString(oldMode), modeToString(mode), animate)); |
John Spurlock | fd0e6c0 | 2014-01-14 14:51:23 -0500 | [diff] [blame] | 103 | onTransition(oldMode, mMode, animate); |
John Spurlock | 89835dd | 2013-08-16 15:06:51 -0400 | [diff] [blame] | 104 | } |
| 105 | |
John Spurlock | 3b139a9 | 2013-08-17 17:18:08 -0400 | [diff] [blame] | 106 | protected void onTransition(int oldMode, int newMode, boolean animate) { |
John Spurlock | fd0e6c0 | 2014-01-14 14:51:23 -0500 | [diff] [blame] | 107 | if (HIGH_END) { |
| 108 | applyModeBackground(oldMode, newMode, animate); |
| 109 | } |
John Spurlock | e631b41 | 2013-09-18 16:33:57 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | protected void applyModeBackground(int oldMode, int newMode, boolean animate) { |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 113 | if (DEBUG) Log.d(mTag, String.format("applyModeBackground oldMode=%s newMode=%s animate=%s", |
| 114 | modeToString(oldMode), modeToString(newMode), animate)); |
| 115 | mBarBackground.applyModeBackground(oldMode, newMode, animate); |
John Spurlock | e932e30 | 2013-08-12 10:16:29 -0400 | [diff] [blame] | 116 | } |
John Spurlock | 27735a4 | 2013-08-14 17:57:38 -0400 | [diff] [blame] | 117 | |
| 118 | public static String modeToString(int mode) { |
John Spurlock | 3b139a9 | 2013-08-17 17:18:08 -0400 | [diff] [blame] | 119 | if (mode == MODE_OPAQUE) return "MODE_OPAQUE"; |
John Spurlock | 89835dd | 2013-08-16 15:06:51 -0400 | [diff] [blame] | 120 | if (mode == MODE_SEMI_TRANSPARENT) return "MODE_SEMI_TRANSPARENT"; |
John Spurlock | bd95740 | 2013-10-03 11:38:39 -0400 | [diff] [blame] | 121 | if (mode == MODE_TRANSLUCENT) return "MODE_TRANSLUCENT"; |
John Spurlock | 7edfbca | 2013-09-14 11:58:55 -0400 | [diff] [blame] | 122 | if (mode == MODE_LIGHTS_OUT) return "MODE_LIGHTS_OUT"; |
Adrian Roos | ea56251 | 2014-05-05 13:33:03 +0200 | [diff] [blame] | 123 | if (mode == MODE_TRANSPARENT) return "MODE_TRANSPARENT"; |
John Spurlock | 0ff62e0 | 2014-07-22 16:15:08 -0400 | [diff] [blame] | 124 | if (mode == MODE_WARNING) return "MODE_WARNING"; |
Adrian Roos | c0f0a74 | 2014-10-28 16:39:56 +0100 | [diff] [blame] | 125 | if (mode == MODE_LIGHTS_OUT_TRANSPARENT) return "MODE_LIGHTS_OUT_TRANSPARENT"; |
John Spurlock | 27735a4 | 2013-08-14 17:57:38 -0400 | [diff] [blame] | 126 | throw new IllegalArgumentException("Unknown mode " + mode); |
| 127 | } |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 128 | |
| 129 | public void finishAnimations() { |
| 130 | mBarBackground.finishAnimation(); |
| 131 | } |
| 132 | |
Adrian Roos | c0f0a74 | 2014-10-28 16:39:56 +0100 | [diff] [blame] | 133 | protected boolean isLightsOut(int mode) { |
| 134 | return mode == MODE_LIGHTS_OUT || mode == MODE_LIGHTS_OUT_TRANSPARENT; |
| 135 | } |
| 136 | |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 137 | private static class BarBackgroundDrawable extends Drawable { |
| 138 | private final int mOpaque; |
| 139 | private final int mSemiTransparent; |
Adrian Roos | ea56251 | 2014-05-05 13:33:03 +0200 | [diff] [blame] | 140 | private final int mTransparent; |
John Spurlock | 0ff62e0 | 2014-07-22 16:15:08 -0400 | [diff] [blame] | 141 | private final int mWarning; |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 142 | private final Drawable mGradient; |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 143 | |
| 144 | private int mMode = -1; |
| 145 | private boolean mAnimating; |
| 146 | private long mStartTime; |
| 147 | private long mEndTime; |
| 148 | |
| 149 | private int mGradientAlpha; |
| 150 | private int mColor; |
Yorke Lee | 1b268cb | 2016-02-25 17:58:33 -0800 | [diff] [blame] | 151 | private PorterDuffColorFilter mTintFilter; |
| 152 | private Paint mPaint = new Paint(); |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 153 | |
| 154 | private int mGradientAlphaStart; |
| 155 | private int mColorStart; |
| 156 | |
Yorke Lee | 1b268cb | 2016-02-25 17:58:33 -0800 | [diff] [blame] | 157 | |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 158 | public BarBackgroundDrawable(Context context, int gradientResourceId) { |
| 159 | final Resources res = context.getResources(); |
| 160 | if (DEBUG_COLORS) { |
| 161 | mOpaque = 0xff0000ff; |
| 162 | mSemiTransparent = 0x7f0000ff; |
Adrian Roos | ea56251 | 2014-05-05 13:33:03 +0200 | [diff] [blame] | 163 | mTransparent = 0x2f0000ff; |
John Spurlock | 0ff62e0 | 2014-07-22 16:15:08 -0400 | [diff] [blame] | 164 | mWarning = 0xffff0000; |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 165 | } else { |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 166 | mOpaque = context.getColor(R.color.system_bar_background_opaque); |
Jorim Jaggi | 4fa7892 | 2015-11-30 17:13:56 -0800 | [diff] [blame] | 167 | mSemiTransparent = context.getColor( |
| 168 | com.android.internal.R.color.system_bar_background_semi_transparent); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 169 | mTransparent = context.getColor(R.color.system_bar_background_transparent); |
| 170 | mWarning = context.getColor(com.android.internal.R.color.battery_saver_mode_color); |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 171 | } |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 172 | mGradient = context.getDrawable(gradientResourceId); |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void setAlpha(int alpha) { |
| 177 | // noop |
| 178 | } |
| 179 | |
| 180 | @Override |
Chris Craik | bd3bfc5 | 2015-03-02 10:43:29 -0800 | [diff] [blame] | 181 | public void setColorFilter(ColorFilter colorFilter) { |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 182 | // noop |
| 183 | } |
| 184 | |
| 185 | @Override |
Yorke Lee | 1b268cb | 2016-02-25 17:58:33 -0800 | [diff] [blame] | 186 | public void setTint(int color) { |
| 187 | if (mTintFilter == null) { |
| 188 | mTintFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN); |
| 189 | } else { |
| 190 | mTintFilter.setColor(color); |
| 191 | } |
| 192 | invalidateSelf(); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void setTintMode(Mode tintMode) { |
| 197 | if (mTintFilter == null) { |
| 198 | mTintFilter = new PorterDuffColorFilter(0, tintMode); |
| 199 | } else { |
| 200 | mTintFilter.setMode(tintMode); |
| 201 | } |
| 202 | invalidateSelf(); |
| 203 | } |
| 204 | |
| 205 | @Override |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 206 | protected void onBoundsChange(Rect bounds) { |
| 207 | super.onBoundsChange(bounds); |
| 208 | mGradient.setBounds(bounds); |
| 209 | } |
| 210 | |
| 211 | public void applyModeBackground(int oldMode, int newMode, boolean animate) { |
| 212 | if (mMode == newMode) return; |
| 213 | mMode = newMode; |
| 214 | mAnimating = animate; |
| 215 | if (animate) { |
| 216 | long now = SystemClock.elapsedRealtime(); |
| 217 | mStartTime = now; |
| 218 | mEndTime = now + BACKGROUND_DURATION; |
| 219 | mGradientAlphaStart = mGradientAlpha; |
| 220 | mColorStart = mColor; |
| 221 | } |
| 222 | invalidateSelf(); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public int getOpacity() { |
| 227 | return PixelFormat.TRANSLUCENT; |
| 228 | } |
| 229 | |
| 230 | public void finishAnimation() { |
| 231 | if (mAnimating) { |
| 232 | mAnimating = false; |
| 233 | invalidateSelf(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public void draw(Canvas canvas) { |
| 239 | int targetGradientAlpha = 0, targetColor = 0; |
John Spurlock | 0ff62e0 | 2014-07-22 16:15:08 -0400 | [diff] [blame] | 240 | if (mMode == MODE_WARNING) { |
| 241 | targetColor = mWarning; |
| 242 | } else if (mMode == MODE_TRANSLUCENT) { |
Adrian Roos | ea56251 | 2014-05-05 13:33:03 +0200 | [diff] [blame] | 243 | targetColor = mSemiTransparent; |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 244 | } else if (mMode == MODE_SEMI_TRANSPARENT) { |
| 245 | targetColor = mSemiTransparent; |
Adrian Roos | c0f0a74 | 2014-10-28 16:39:56 +0100 | [diff] [blame] | 246 | } else if (mMode == MODE_TRANSPARENT || mMode == MODE_LIGHTS_OUT_TRANSPARENT) { |
Adrian Roos | ea56251 | 2014-05-05 13:33:03 +0200 | [diff] [blame] | 247 | targetColor = mTransparent; |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 248 | } else { |
| 249 | targetColor = mOpaque; |
| 250 | } |
Yorke Lee | 1b268cb | 2016-02-25 17:58:33 -0800 | [diff] [blame] | 251 | |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 252 | if (!mAnimating) { |
| 253 | mColor = targetColor; |
| 254 | mGradientAlpha = targetGradientAlpha; |
| 255 | } else { |
| 256 | final long now = SystemClock.elapsedRealtime(); |
| 257 | if (now >= mEndTime) { |
| 258 | mAnimating = false; |
| 259 | mColor = targetColor; |
| 260 | mGradientAlpha = targetGradientAlpha; |
| 261 | } else { |
| 262 | final float t = (now - mStartTime) / (float)(mEndTime - mStartTime); |
Selim Cinek | c18010f | 2016-01-20 13:41:30 -0800 | [diff] [blame] | 263 | final float v = Math.max(0, Math.min( |
| 264 | Interpolators.LINEAR.getInterpolation(t), 1)); |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 265 | mGradientAlpha = (int)(v * targetGradientAlpha + mGradientAlphaStart * (1 - v)); |
| 266 | mColor = Color.argb( |
| 267 | (int)(v * Color.alpha(targetColor) + Color.alpha(mColorStart) * (1 - v)), |
| 268 | (int)(v * Color.red(targetColor) + Color.red(mColorStart) * (1 - v)), |
| 269 | (int)(v * Color.green(targetColor) + Color.green(mColorStart) * (1 - v)), |
| 270 | (int)(v * Color.blue(targetColor) + Color.blue(mColorStart) * (1 - v))); |
| 271 | } |
| 272 | } |
| 273 | if (mGradientAlpha > 0) { |
| 274 | mGradient.setAlpha(mGradientAlpha); |
| 275 | mGradient.draw(canvas); |
| 276 | } |
| 277 | if (Color.alpha(mColor) > 0) { |
Yorke Lee | 1b268cb | 2016-02-25 17:58:33 -0800 | [diff] [blame] | 278 | mPaint.setColor(mColor); |
| 279 | if (mTintFilter != null) { |
| 280 | mPaint.setColorFilter(mTintFilter); |
| 281 | } |
| 282 | canvas.drawPaint(mPaint); |
John Spurlock | 4219726 | 2013-10-21 09:32:25 -0400 | [diff] [blame] | 283 | } |
| 284 | if (mAnimating) { |
| 285 | invalidateSelf(); // keep going |
| 286 | } |
| 287 | } |
| 288 | } |
John Spurlock | e932e30 | 2013-08-12 10:16:29 -0400 | [diff] [blame] | 289 | } |