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.content.Context;
     20 import android.graphics.Color;
     21 import android.graphics.PorterDuff;
     22 import android.graphics.PorterDuffColorFilter;
     23 import android.graphics.drawable.Drawable;
     24 import android.widget.ImageView;
     25 
     26 import com.android.systemui.R;
     27 
     28 public class NotificationIconDozeHelper extends NotificationDozeHelper {
     29 
     30     private final int mImageDarkAlpha;
     31     private final int mImageDarkColor = 0xffffffff;
     32     private final PorterDuffColorFilter mImageColorFilter = new PorterDuffColorFilter(
     33             0, PorterDuff.Mode.SRC_ATOP);
     34 
     35     private int mColor = Color.BLACK;
     36 
     37     public NotificationIconDozeHelper(Context ctx) {
     38         mImageDarkAlpha = ctx.getResources().getInteger(R.integer.doze_small_icon_alpha);
     39     }
     40 
     41     public void setColor(int color) {
     42         mColor = color;
     43     }
     44 
     45     public void setImageDark(ImageView target, boolean dark, boolean fade, long delay,
     46             boolean useGrayscale) {
     47         if (fade) {
     48             if (!useGrayscale) {
     49                 fadeImageColorFilter(target, dark, delay);
     50                 fadeImageAlpha(target, dark, delay);
     51             } else {
     52                 fadeGrayscale(target, dark, delay);
     53             }
     54         } else {
     55             if (!useGrayscale) {
     56                 updateImageColorFilter(target, dark);
     57                 updateImageAlpha(target, dark);
     58             } else {
     59                 updateGrayscale(target, dark);
     60             }
     61         }
     62     }
     63 
     64     private void fadeImageColorFilter(final ImageView target, boolean dark, long delay) {
     65         startIntensityAnimation(animation -> {
     66             updateImageColorFilter(target, (Float) animation.getAnimatedValue());
     67         }, dark, delay, null /* listener */);
     68     }
     69 
     70     private void fadeImageAlpha(final ImageView target, boolean dark, long delay) {
     71         startIntensityAnimation(animation -> {
     72             float t = (float) animation.getAnimatedValue();
     73             target.setImageAlpha((int) (255 * (1f - t) + mImageDarkAlpha * t));
     74         }, dark, delay, null /* listener */);
     75     }
     76 
     77     private void updateImageColorFilter(ImageView target, boolean dark) {
     78         updateImageColorFilter(target, dark ? 1f : 0f);
     79     }
     80 
     81     private void updateImageColorFilter(ImageView target, float intensity) {
     82         int color = NotificationUtils.interpolateColors(mColor, mImageDarkColor, intensity);
     83         mImageColorFilter.setColor(color);
     84         Drawable imageDrawable = target.getDrawable();
     85 
     86         // Also, the notification might have been modified during the animation, so background
     87         // might be null here.
     88         if (imageDrawable != null) {
     89             Drawable d = imageDrawable.mutate();
     90             // DrawableContainer ignores the color filter if it's already set, so clear it first to
     91             // get it set and invalidated properly.
     92             d.setColorFilter(null);
     93             d.setColorFilter(mImageColorFilter);
     94         }
     95     }
     96 
     97     private void updateImageAlpha(ImageView target, boolean dark) {
     98         target.setImageAlpha(dark ? mImageDarkAlpha : 255);
     99     }
    100 
    101 }
    102