Adrian Roos | 7bcf6d3 | 2017-04-04 16:44:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.notification; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ValueAnimator; |
| 22 | import android.graphics.ColorMatrix; |
| 23 | import android.graphics.ColorMatrixColorFilter; |
Selim Cinek | 653c139 | 2018-05-15 13:02:42 -0700 | [diff] [blame] | 24 | import android.view.View; |
Adrian Roos | 7bcf6d3 | 2017-04-04 16:44:25 -0700 | [diff] [blame] | 25 | import android.widget.ImageView; |
| 26 | |
| 27 | import com.android.systemui.Interpolators; |
Selim Cinek | 653c139 | 2018-05-15 13:02:42 -0700 | [diff] [blame] | 28 | import com.android.systemui.R; |
Lucas Dupin | 1a8b33d | 2018-11-12 18:18:15 -0800 | [diff] [blame] | 29 | import com.android.systemui.statusbar.notification.stack.StackStateAnimator; |
Adrian Roos | 7bcf6d3 | 2017-04-04 16:44:25 -0700 | [diff] [blame] | 30 | |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 31 | import java.util.function.Consumer; |
| 32 | |
Adrian Roos | 7bcf6d3 | 2017-04-04 16:44:25 -0700 | [diff] [blame] | 33 | public class NotificationDozeHelper { |
Selim Cinek | 653c139 | 2018-05-15 13:02:42 -0700 | [diff] [blame] | 34 | private static final int DOZE_ANIMATOR_TAG = R.id.doze_intensity_tag; |
Adrian Roos | 7bcf6d3 | 2017-04-04 16:44:25 -0700 | [diff] [blame] | 35 | private final ColorMatrix mGrayscaleColorMatrix = new ColorMatrix(); |
| 36 | |
| 37 | public void fadeGrayscale(final ImageView target, final boolean dark, long delay) { |
| 38 | startIntensityAnimation(new ValueAnimator.AnimatorUpdateListener() { |
| 39 | @Override |
| 40 | public void onAnimationUpdate(ValueAnimator animation) { |
Adrian Roos | f3eacd3 | 2017-04-14 16:35:58 -0700 | [diff] [blame] | 41 | updateGrayscale(target, (float) animation.getAnimatedValue()); |
Adrian Roos | 7bcf6d3 | 2017-04-04 16:44:25 -0700 | [diff] [blame] | 42 | } |
| 43 | }, dark, delay, new AnimatorListenerAdapter() { |
| 44 | @Override |
| 45 | public void onAnimationEnd(Animator animation) { |
| 46 | if (!dark) { |
| 47 | target.setColorFilter(null); |
| 48 | } |
| 49 | } |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | public void updateGrayscale(ImageView target, boolean dark) { |
Adrian Roos | f3eacd3 | 2017-04-14 16:35:58 -0700 | [diff] [blame] | 54 | updateGrayscale(target, dark ? 1 : 0); |
| 55 | } |
| 56 | |
| 57 | public void updateGrayscale(ImageView target, float darkAmount) { |
| 58 | if (darkAmount > 0) { |
| 59 | updateGrayscaleMatrix(darkAmount); |
Adrian Roos | 7bcf6d3 | 2017-04-04 16:44:25 -0700 | [diff] [blame] | 60 | target.setColorFilter(new ColorMatrixColorFilter(mGrayscaleColorMatrix)); |
| 61 | } else { |
| 62 | target.setColorFilter(null); |
| 63 | } |
| 64 | } |
| 65 | |
Lucas Dupin | 1a8b33d | 2018-11-12 18:18:15 -0800 | [diff] [blame] | 66 | // TODO: this should be using StatusBarStateController#getDozeAmount |
Adrian Roos | 7bcf6d3 | 2017-04-04 16:44:25 -0700 | [diff] [blame] | 67 | public void startIntensityAnimation(ValueAnimator.AnimatorUpdateListener updateListener, |
| 68 | boolean dark, long delay, Animator.AnimatorListener listener) { |
| 69 | float startIntensity = dark ? 0f : 1f; |
| 70 | float endIntensity = dark ? 1f : 0f; |
| 71 | ValueAnimator animator = ValueAnimator.ofFloat(startIntensity, endIntensity); |
| 72 | animator.addUpdateListener(updateListener); |
Lucas Dupin | 1a8b33d | 2018-11-12 18:18:15 -0800 | [diff] [blame] | 73 | animator.setDuration(StackStateAnimator.ANIMATION_DURATION_WAKEUP); |
Adrian Roos | 7bcf6d3 | 2017-04-04 16:44:25 -0700 | [diff] [blame] | 74 | animator.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN); |
| 75 | animator.setStartDelay(delay); |
| 76 | if (listener != null) { |
| 77 | animator.addListener(listener); |
| 78 | } |
| 79 | animator.start(); |
| 80 | } |
| 81 | |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 82 | public void setIntensityDark(Consumer<Float> listener, boolean dark, |
Selim Cinek | 653c139 | 2018-05-15 13:02:42 -0700 | [diff] [blame] | 83 | boolean animate, long delay, View view) { |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 84 | if (animate) { |
| 85 | startIntensityAnimation(a -> listener.accept((Float) a.getAnimatedValue()), dark, delay, |
Selim Cinek | 653c139 | 2018-05-15 13:02:42 -0700 | [diff] [blame] | 86 | new AnimatorListenerAdapter() { |
| 87 | |
| 88 | @Override |
| 89 | public void onAnimationEnd(Animator animation) { |
| 90 | view.setTag(DOZE_ANIMATOR_TAG, null); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void onAnimationStart(Animator animation) { |
| 95 | view.setTag(DOZE_ANIMATOR_TAG, animation); |
| 96 | } |
| 97 | } /* listener */); |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 98 | } else { |
Selim Cinek | 653c139 | 2018-05-15 13:02:42 -0700 | [diff] [blame] | 99 | Animator animator = (Animator) view.getTag(DOZE_ANIMATOR_TAG); |
| 100 | if (animator != null) { |
| 101 | animator.cancel(); |
| 102 | } |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 103 | listener.accept(dark ? 1f : 0f); |
| 104 | } |
| 105 | } |
| 106 | |
Adrian Roos | 7bcf6d3 | 2017-04-04 16:44:25 -0700 | [diff] [blame] | 107 | public void updateGrayscaleMatrix(float intensity) { |
| 108 | mGrayscaleColorMatrix.setSaturation(1 - intensity); |
| 109 | } |
| 110 | |
| 111 | public ColorMatrix getGrayscaleColorMatrix() { |
| 112 | return mGrayscaleColorMatrix; |
| 113 | } |
| 114 | } |