2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.statusbar.notification;
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;
24 import android.view.View;
25 import android.widget.ImageView;
27 import com.android.systemui.Interpolators;
28 import com.android.systemui.R;
29 import com.android.systemui.statusbar.notification.stack.StackStateAnimator;
31 import java.util.function.Consumer;
33 public class NotificationDozeHelper {
34 private static final int DOZE_ANIMATOR_TAG = R.id.doze_intensity_tag;
35 private final ColorMatrix mGrayscaleColorMatrix = new ColorMatrix();
37 public void fadeGrayscale(final ImageView target, final boolean dark, long delay) {
38 startIntensityAnimation(new ValueAnimator.AnimatorUpdateListener() {
40 public void onAnimationUpdate(ValueAnimator animation) {
41 updateGrayscale(target, (float) animation.getAnimatedValue());
43 }, dark, delay, new AnimatorListenerAdapter() {
45 public void onAnimationEnd(Animator animation) {
47 target.setColorFilter(null);
53 public void updateGrayscale(ImageView target, boolean dark) {
54 updateGrayscale(target, dark ? 1 : 0);
57 public void updateGrayscale(ImageView target, float darkAmount) {
59 updateGrayscaleMatrix(darkAmount);
60 target.setColorFilter(new ColorMatrixColorFilter(mGrayscaleColorMatrix));
62 target.setColorFilter(null);
66 // TODO: this should be using StatusBarStateController#getDozeAmount
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);
73 animator.setDuration(StackStateAnimator.ANIMATION_DURATION_WAKEUP);
74 animator.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);
75 animator.setStartDelay(delay);
76 if (listener != null) {
77 animator.addListener(listener);
82 public void setIntensityDark(Consumer<Float> listener, boolean dark,
83 boolean animate, long delay, View view) {
85 startIntensityAnimation(a -> listener.accept((Float) a.getAnimatedValue()), dark, delay,
86 new AnimatorListenerAdapter() {
89 public void onAnimationEnd(Animator animation) {
90 view.setTag(DOZE_ANIMATOR_TAG, null);
94 public void onAnimationStart(Animator animation) {
95 view.setTag(DOZE_ANIMATOR_TAG, animation);
99 Animator animator = (Animator) view.getTag(DOZE_ANIMATOR_TAG);
100 if (animator != null) {
103 listener.accept(dark ? 1f : 0f);
107 public void updateGrayscaleMatrix(float intensity) {
108 mGrayscaleColorMatrix.setSaturation(1 - intensity);
111 public ColorMatrix getGrayscaleColorMatrix() {
112 return mGrayscaleColorMatrix;