Home | History | Annotate | Download | only in notification
      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;
     24 import android.widget.ImageView;
     25 
     26 import com.android.systemui.Interpolators;
     27 import com.android.systemui.statusbar.phone.NotificationPanelView;
     28 
     29 import java.util.function.Consumer;
     30 
     31 public class NotificationDozeHelper {
     32     private final ColorMatrix mGrayscaleColorMatrix = new ColorMatrix();
     33 
     34     public void fadeGrayscale(final ImageView target, final boolean dark, long delay) {
     35         startIntensityAnimation(new ValueAnimator.AnimatorUpdateListener() {
     36             @Override
     37             public void onAnimationUpdate(ValueAnimator animation) {
     38                 updateGrayscale(target, (float) animation.getAnimatedValue());
     39             }
     40         }, dark, delay, new AnimatorListenerAdapter() {
     41             @Override
     42             public void onAnimationEnd(Animator animation) {
     43                 if (!dark) {
     44                     target.setColorFilter(null);
     45                 }
     46             }
     47         });
     48     }
     49 
     50     public void updateGrayscale(ImageView target, boolean dark) {
     51         updateGrayscale(target, dark ? 1 : 0);
     52     }
     53 
     54     public void updateGrayscale(ImageView target, float darkAmount) {
     55         if (darkAmount > 0) {
     56             updateGrayscaleMatrix(darkAmount);
     57             target.setColorFilter(new ColorMatrixColorFilter(mGrayscaleColorMatrix));
     58         } else {
     59             target.setColorFilter(null);
     60         }
     61     }
     62 
     63     public void startIntensityAnimation(ValueAnimator.AnimatorUpdateListener updateListener,
     64             boolean dark, long delay, Animator.AnimatorListener listener) {
     65         float startIntensity = dark ? 0f : 1f;
     66         float endIntensity = dark ? 1f : 0f;
     67         ValueAnimator animator = ValueAnimator.ofFloat(startIntensity, endIntensity);
     68         animator.addUpdateListener(updateListener);
     69         animator.setDuration(NotificationPanelView.DOZE_ANIMATION_DURATION);
     70         animator.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);
     71         animator.setStartDelay(delay);
     72         if (listener != null) {
     73             animator.addListener(listener);
     74         }
     75         animator.start();
     76     }
     77 
     78     public void setIntensityDark(Consumer<Float> listener, boolean dark,
     79             boolean animate, long delay) {
     80         if (animate) {
     81             startIntensityAnimation(a -> listener.accept((Float) a.getAnimatedValue()), dark, delay,
     82                     null /* listener */);
     83         } else {
     84             listener.accept(dark ? 1f : 0f);
     85         }
     86     }
     87 
     88     public void updateGrayscaleMatrix(float intensity) {
     89         mGrayscaleColorMatrix.setSaturation(1 - intensity);
     90     }
     91 
     92     public ColorMatrix getGrayscaleColorMatrix() {
     93         return mGrayscaleColorMatrix;
     94     }
     95 }
     96