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