Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ValueAnimator; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 22 | import android.annotation.NonNull; |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 23 | import android.content.Context; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 24 | import android.content.res.Configuration; |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 25 | import android.graphics.Canvas; |
| 26 | import android.graphics.Color; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 27 | import android.graphics.Point; |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 28 | import android.graphics.PorterDuff; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 29 | import android.graphics.PorterDuffColorFilter; |
Selim Cinek | 6811d72 | 2016-01-19 17:53:12 -0800 | [diff] [blame] | 30 | import android.graphics.PorterDuffXfermode; |
| 31 | import android.graphics.Rect; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 32 | import android.graphics.drawable.Drawable; |
| 33 | import android.support.v4.graphics.ColorUtils; |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 34 | import android.util.AttributeSet; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 35 | import android.util.Log; |
| 36 | import android.view.Display; |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 37 | import android.view.View; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 38 | import android.view.WindowManager; |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 39 | import android.view.animation.Interpolator; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 40 | |
| 41 | import com.android.internal.annotations.VisibleForTesting; |
Lucas Dupin | e2292a9 | 2017-07-06 14:35:30 -0700 | [diff] [blame] | 42 | import com.android.internal.colorextraction.ColorExtractor; |
| 43 | import com.android.internal.colorextraction.drawable.GradientDrawable; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 44 | import com.android.systemui.Dependency; |
| 45 | import com.android.systemui.statusbar.policy.ConfigurationController; |
| 46 | |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 47 | /** |
| 48 | * A view which can draw a scrim |
| 49 | */ |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 50 | public class ScrimView extends View implements ConfigurationController.ConfigurationListener { |
| 51 | private static final String TAG = "ScrimView"; |
| 52 | private final ColorExtractor.GradientColors mColors; |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 53 | private boolean mDrawAsSrc; |
| 54 | private float mViewAlpha = 1.0f; |
| 55 | private ValueAnimator mAlphaAnimator; |
Selim Cinek | 6811d72 | 2016-01-19 17:53:12 -0800 | [diff] [blame] | 56 | private Rect mExcludedRect = new Rect(); |
| 57 | private boolean mHasExcludedArea; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 58 | private Drawable mDrawable; |
| 59 | private PorterDuffColorFilter mColorFilter; |
| 60 | private int mTintColor; |
| 61 | private ValueAnimator.AnimatorUpdateListener mAlphaUpdateListener = animation -> { |
| 62 | if (mDrawable == null) { |
| 63 | Log.w(TAG, "Trying to animate null drawable"); |
| 64 | return; |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 65 | } |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 66 | mDrawable.setAlpha((int) (255 * (float) animation.getAnimatedValue())); |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 67 | }; |
| 68 | private AnimatorListenerAdapter mClearAnimatorListener = new AnimatorListenerAdapter() { |
| 69 | @Override |
| 70 | public void onAnimationEnd(Animator animation) { |
| 71 | mAlphaAnimator = null; |
| 72 | } |
| 73 | }; |
Selim Cinek | d35c279 | 2016-01-21 13:20:57 -0800 | [diff] [blame] | 74 | private Runnable mChangeRunnable; |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 75 | |
| 76 | public ScrimView(Context context) { |
| 77 | this(context, null); |
| 78 | } |
| 79 | |
| 80 | public ScrimView(Context context, AttributeSet attrs) { |
| 81 | this(context, attrs, 0); |
| 82 | } |
| 83 | |
| 84 | public ScrimView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 85 | this(context, attrs, defStyleAttr, 0); |
| 86 | } |
| 87 | |
| 88 | public ScrimView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 89 | super(context, attrs, defStyleAttr, defStyleRes); |
Anthony Chen | 3cb3ad9 | 2016-12-01 10:58:47 -0800 | [diff] [blame] | 90 | |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 91 | mDrawable = new GradientDrawable(context); |
| 92 | mDrawable.setCallback(this); |
| 93 | mColors = new ColorExtractor.GradientColors(); |
| 94 | updateScreenSize(); |
Lucas Dupin | d2963f3 | 2017-05-26 15:27:36 -0700 | [diff] [blame] | 95 | updateColorWithTint(false); |
Jason Monk | 31bac9c | 2017-07-06 15:06:36 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
| 99 | protected void onAttachedToWindow() { |
| 100 | super.onAttachedToWindow(); |
Anthony Chen | 3cb3ad9 | 2016-12-01 10:58:47 -0800 | [diff] [blame] | 101 | |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 102 | // We need to know about configuration changes to update the gradient size |
| 103 | // since it's independent from view bounds. |
| 104 | ConfigurationController config = Dependency.get(ConfigurationController.class); |
| 105 | config.addCallback(this); |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
Jason Monk | 31bac9c | 2017-07-06 15:06:36 -0400 | [diff] [blame] | 109 | protected void onDetachedFromWindow() { |
| 110 | super.onDetachedFromWindow(); |
| 111 | |
| 112 | ConfigurationController config = Dependency.get(ConfigurationController.class); |
| 113 | config.removeCallback(this); |
| 114 | } |
| 115 | |
| 116 | @Override |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 117 | protected void onDraw(Canvas canvas) { |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 118 | if (mDrawAsSrc || mDrawable.getAlpha() > 0) { |
Selim Cinek | 6811d72 | 2016-01-19 17:53:12 -0800 | [diff] [blame] | 119 | if (!mHasExcludedArea) { |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 120 | mDrawable.draw(canvas); |
Selim Cinek | 6811d72 | 2016-01-19 17:53:12 -0800 | [diff] [blame] | 121 | } else { |
Selim Cinek | 6811d72 | 2016-01-19 17:53:12 -0800 | [diff] [blame] | 122 | if (mExcludedRect.top > 0) { |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 123 | canvas.save(); |
| 124 | canvas.clipRect(0, 0, getWidth(), mExcludedRect.top); |
| 125 | mDrawable.draw(canvas); |
| 126 | canvas.restore(); |
Selim Cinek | 6811d72 | 2016-01-19 17:53:12 -0800 | [diff] [blame] | 127 | } |
Selim Cinek | 0e39fac | 2016-12-06 11:50:55 -0800 | [diff] [blame] | 128 | if (mExcludedRect.left > 0) { |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 129 | canvas.save(); |
| 130 | canvas.clipRect(0, mExcludedRect.top, mExcludedRect.left, |
| 131 | mExcludedRect.bottom); |
| 132 | mDrawable.draw(canvas); |
| 133 | canvas.restore(); |
Selim Cinek | 6811d72 | 2016-01-19 17:53:12 -0800 | [diff] [blame] | 134 | } |
Selim Cinek | 0e39fac | 2016-12-06 11:50:55 -0800 | [diff] [blame] | 135 | if (mExcludedRect.right < getWidth()) { |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 136 | canvas.save(); |
| 137 | canvas.clipRect(mExcludedRect.right, mExcludedRect.top, getWidth(), |
| 138 | mExcludedRect.bottom); |
| 139 | mDrawable.draw(canvas); |
| 140 | canvas.restore(); |
Selim Cinek | 6811d72 | 2016-01-19 17:53:12 -0800 | [diff] [blame] | 141 | } |
| 142 | if (mExcludedRect.bottom < getHeight()) { |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 143 | canvas.save(); |
| 144 | canvas.clipRect(0, mExcludedRect.bottom, getWidth(), getHeight()); |
| 145 | mDrawable.draw(canvas); |
| 146 | canvas.restore(); |
Selim Cinek | 6811d72 | 2016-01-19 17:53:12 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 152 | public void setDrawable(Drawable drawable) { |
| 153 | mDrawable = drawable; |
| 154 | mDrawable.setCallback(this); |
| 155 | mDrawable.setBounds(getLeft(), getTop(), getRight(), getBottom()); |
Anthony Chen | e658cc2 | 2017-04-27 11:17:35 -0700 | [diff] [blame] | 156 | mDrawable.setAlpha((int) (255 * mViewAlpha)); |
| 157 | setDrawAsSrc(mDrawAsSrc); |
| 158 | updateScreenSize(); |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 159 | invalidate(); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void invalidateDrawable(@NonNull Drawable drawable) { |
| 164 | super.invalidateDrawable(drawable); |
| 165 | if (drawable == mDrawable) { |
| 166 | invalidate(); |
| 167 | } |
Selim Cinek | d35c279 | 2016-01-21 13:20:57 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 170 | public void setDrawAsSrc(boolean asSrc) { |
| 171 | mDrawAsSrc = asSrc; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 172 | PorterDuff.Mode mode = asSrc ? PorterDuff.Mode.SRC : PorterDuff.Mode.SRC_OVER; |
| 173 | mDrawable.setXfermode(new PorterDuffXfermode(mode)); |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 176 | @Override |
| 177 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 178 | super.onLayout(changed, left, top, right, bottom); |
| 179 | if (changed) { |
| 180 | mDrawable.setBounds(left, top, right, bottom); |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 181 | invalidate(); |
| 182 | } |
| 183 | } |
| 184 | |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 185 | public void setColors(@NonNull ColorExtractor.GradientColors colors) { |
Lucas Dupin | c41f8fd | 2017-05-03 16:15:22 -0700 | [diff] [blame] | 186 | setColors(colors, false); |
| 187 | } |
| 188 | |
| 189 | public void setColors(@NonNull ColorExtractor.GradientColors colors, boolean animated) { |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 190 | if (colors == null) { |
| 191 | throw new IllegalArgumentException("Colors cannot be null"); |
| 192 | } |
| 193 | if (mColors.equals(colors)) { |
| 194 | return; |
| 195 | } |
| 196 | mColors.set(colors); |
Lucas Dupin | c41f8fd | 2017-05-03 16:15:22 -0700 | [diff] [blame] | 197 | updateColorWithTint(animated); |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @VisibleForTesting |
| 201 | Drawable getDrawable() { |
| 202 | return mDrawable; |
| 203 | } |
| 204 | |
Lucas Dupin | d2963f3 | 2017-05-26 15:27:36 -0700 | [diff] [blame] | 205 | @VisibleForTesting |
| 206 | ColorExtractor.GradientColors getColors() { |
| 207 | return mColors; |
| 208 | } |
| 209 | |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 210 | public void setTint(int color) { |
Lucas Dupin | c41f8fd | 2017-05-03 16:15:22 -0700 | [diff] [blame] | 211 | setTint(color, false); |
| 212 | } |
| 213 | |
| 214 | public void setTint(int color, boolean animated) { |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 215 | if (mTintColor == color) { |
| 216 | return; |
| 217 | } |
| 218 | mTintColor = color; |
Lucas Dupin | c41f8fd | 2017-05-03 16:15:22 -0700 | [diff] [blame] | 219 | updateColorWithTint(animated); |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Lucas Dupin | c41f8fd | 2017-05-03 16:15:22 -0700 | [diff] [blame] | 222 | private void updateColorWithTint(boolean animated) { |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 223 | if (mDrawable instanceof GradientDrawable) { |
| 224 | // Optimization to blend colors and avoid a color filter |
| 225 | GradientDrawable drawable = (GradientDrawable) mDrawable; |
| 226 | float tintAmount = Color.alpha(mTintColor) / 255f; |
| 227 | int mainTinted = ColorUtils.blendARGB(mColors.getMainColor(), mTintColor, |
| 228 | tintAmount); |
| 229 | int secondaryTinted = ColorUtils.blendARGB(mColors.getSecondaryColor(), mTintColor, |
| 230 | tintAmount); |
Lucas Dupin | c41f8fd | 2017-05-03 16:15:22 -0700 | [diff] [blame] | 231 | drawable.setColors(mainTinted, secondaryTinted, animated); |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 232 | } else { |
| 233 | if (mColorFilter == null) { |
| 234 | mColorFilter = new PorterDuffColorFilter(mTintColor, PorterDuff.Mode.SRC_OVER); |
| 235 | } else { |
| 236 | mColorFilter.setColor(mTintColor); |
| 237 | } |
| 238 | mDrawable.setColorFilter(Color.alpha(mTintColor) == 0 ? null : mColorFilter); |
| 239 | mDrawable.invalidateSelf(); |
| 240 | } |
| 241 | |
| 242 | if (mChangeRunnable != null) { |
| 243 | mChangeRunnable.run(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | public int getTint() { |
| 248 | return mTintColor; |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public boolean hasOverlappingRendering() { |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | public void setViewAlpha(float alpha) { |
Selim Cinek | d35c279 | 2016-01-21 13:20:57 -0800 | [diff] [blame] | 257 | if (alpha != mViewAlpha) { |
| 258 | mViewAlpha = alpha; |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 259 | |
| 260 | if (mAlphaAnimator != null) { |
| 261 | mAlphaAnimator.cancel(); |
| 262 | } |
| 263 | |
| 264 | mDrawable.setAlpha((int) (255 * alpha)); |
Selim Cinek | d35c279 | 2016-01-21 13:20:57 -0800 | [diff] [blame] | 265 | if (mChangeRunnable != null) { |
| 266 | mChangeRunnable.run(); |
| 267 | } |
| 268 | } |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Lucas Dupin | a0bf851 | 2017-05-24 17:04:47 -0700 | [diff] [blame] | 271 | public float getViewAlpha() { |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 272 | return mViewAlpha; |
| 273 | } |
| 274 | |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 275 | public void animateViewAlpha(float alpha, long durationOut, Interpolator interpolator) { |
| 276 | if (mAlphaAnimator != null) { |
| 277 | mAlphaAnimator.cancel(); |
| 278 | } |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 279 | mAlphaAnimator = ValueAnimator.ofFloat(getViewAlpha(), alpha); |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 280 | mAlphaAnimator.addUpdateListener(mAlphaUpdateListener); |
| 281 | mAlphaAnimator.addListener(mClearAnimatorListener); |
| 282 | mAlphaAnimator.setInterpolator(interpolator); |
| 283 | mAlphaAnimator.setDuration(durationOut); |
| 284 | mAlphaAnimator.start(); |
| 285 | } |
Selim Cinek | 6811d72 | 2016-01-19 17:53:12 -0800 | [diff] [blame] | 286 | |
| 287 | public void setExcludedArea(Rect area) { |
| 288 | if (area == null) { |
| 289 | mHasExcludedArea = false; |
| 290 | invalidate(); |
| 291 | return; |
| 292 | } |
Jorim Jaggi | 936233c | 2016-01-27 14:16:21 -0800 | [diff] [blame] | 293 | |
| 294 | int left = Math.max(area.left, 0); |
| 295 | int top = Math.max(area.top, 0); |
| 296 | int right = Math.min(area.right, getWidth()); |
| 297 | int bottom = Math.min(area.bottom, getHeight()); |
| 298 | mExcludedRect.set(left, top, right, bottom); |
| 299 | mHasExcludedArea = left < right && top < bottom; |
Selim Cinek | 6811d72 | 2016-01-19 17:53:12 -0800 | [diff] [blame] | 300 | invalidate(); |
| 301 | } |
Selim Cinek | d35c279 | 2016-01-21 13:20:57 -0800 | [diff] [blame] | 302 | |
| 303 | public void setChangeRunnable(Runnable changeRunnable) { |
| 304 | mChangeRunnable = changeRunnable; |
| 305 | } |
Lucas Dupin | 8da8f2e9 | 2017-04-21 14:02:16 -0700 | [diff] [blame] | 306 | |
| 307 | @Override |
| 308 | public void onConfigChanged(Configuration newConfig) { |
| 309 | updateScreenSize(); |
| 310 | } |
| 311 | |
| 312 | private void updateScreenSize() { |
| 313 | if (mDrawable instanceof GradientDrawable) { |
| 314 | WindowManager wm = mContext.getSystemService(WindowManager.class); |
| 315 | if (wm == null) { |
| 316 | Log.w(TAG, "Can't resize gradient drawable to fit the screen"); |
| 317 | return; |
| 318 | } |
| 319 | Display display = wm.getDefaultDisplay(); |
| 320 | if (display != null) { |
| 321 | Point size = new Point(); |
| 322 | display.getRealSize(size); |
| 323 | ((GradientDrawable) mDrawable).setScreenSize(size.x, size.y); |
| 324 | } |
| 325 | } |
| 326 | } |
Selim Cinek | a0fad3b | 2014-09-19 17:20:05 +0200 | [diff] [blame] | 327 | } |