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
17package com.android.systemui;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ValueAnimator;
22import android.content.Context;
23import android.graphics.ColorMatrix;
24import android.graphics.ColorMatrixColorFilter;
25import android.graphics.Paint;
26import android.view.View;
27
28import java.util.ArrayList;
29
30/**
31 * Helper to invert the colors of views and fade between the states.
32 */
33public class ViewInvertHelper {
34
35    private final Paint mDarkPaint = new Paint();
36    private final ColorMatrix mMatrix = new ColorMatrix();
37    private final ColorMatrix mGrayscaleMatrix = new ColorMatrix();
38    private final long mFadeDuration;
39    private final ArrayList<View> mTargets = new ArrayList<>();
40
41    public ViewInvertHelper(View v, long fadeDuration) {
42        this(v.getContext(), fadeDuration);
43        addTarget(v);
44    }
45    public ViewInvertHelper(Context context, long fadeDuration) {
46        mFadeDuration = fadeDuration;
47    }
48
49    private static ArrayList<View> constructArray(View target) {
50        final ArrayList<View> views = new ArrayList<>();
51        views.add(target);
52        return views;
53    }
54
55    public void clearTargets() {
56        mTargets.clear();
57    }
58
59    public void addTarget(View target) {
60        mTargets.add(target);
61    }
62
63    public void fade(final boolean invert, long delay) {
64        float startIntensity = invert ? 0f : 1f;
65        float endIntensity = invert ? 1f : 0f;
66        ValueAnimator animator = ValueAnimator.ofFloat(startIntensity, endIntensity);
67        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
68            @Override
69            public void onAnimationUpdate(ValueAnimator animation) {
70                updateInvertPaint((Float) animation.getAnimatedValue());
71                for (int i = 0; i < mTargets.size(); i++) {
72                    mTargets.get(i).setLayerType(View.LAYER_TYPE_HARDWARE, mDarkPaint);
73                }
74            }
75        });
76        animator.addListener(new AnimatorListenerAdapter() {
77            @Override
78            public void onAnimationEnd(Animator animation) {
79                if (!invert) {
80                    for (int i = 0; i < mTargets.size(); i++) {
81                        mTargets.get(i).setLayerType(View.LAYER_TYPE_NONE, null);
82                    }
83                }
84            }
85        });
86        animator.setDuration(mFadeDuration);
87        animator.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);
88        animator.setStartDelay(delay);
89        animator.start();
90    }
91
92    public void update(boolean invert) {
93        if (invert) {
94            updateInvertPaint(1f);
95            for (int i = 0; i < mTargets.size(); i++) {
96                mTargets.get(i).setLayerType(View.LAYER_TYPE_HARDWARE, mDarkPaint);
97            }
98        } else {
99            for (int i = 0; i < mTargets.size(); i++) {
100                mTargets.get(i).setLayerType(View.LAYER_TYPE_NONE, null);
101            }
102        }
103    }
104
105    private void updateInvertPaint(float intensity) {
106        float components = 1 - 2 * intensity;
107        final float[] invert = {
108                components, 0f,         0f,         0f, 255f * intensity,
109                0f,         components, 0f,         0f, 255f * intensity,
110                0f,         0f,         components, 0f, 255f * intensity,
111                0f,         0f,         0f,         1f, 0f
112        };
113        mMatrix.set(invert);
114        mGrayscaleMatrix.setSaturation(1 - intensity);
115        mMatrix.preConcat(mGrayscaleMatrix);
116        mDarkPaint.setColorFilter(new ColorMatrixColorFilter(mMatrix));
117    }
118
119    public void setInverted(boolean invert, boolean fade, long delay) {
120        if (fade) {
121            fade(invert, delay);
122        } else {
123            update(invert);
124        }
125    }
126}