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.statusbar.notification;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ValueAnimator;
22import android.graphics.Color;
23import android.graphics.ColorMatrixColorFilter;
24import android.graphics.Paint;
25import android.graphics.drawable.ColorDrawable;
26import android.graphics.drawable.Drawable;
27import android.service.notification.StatusBarNotification;
28import android.support.v4.graphics.ColorUtils;
29import android.view.View;
30
31import com.android.systemui.R;
32import com.android.systemui.ViewInvertHelper;
33import com.android.systemui.statusbar.ExpandableNotificationRow;
34import com.android.systemui.statusbar.phone.NotificationPanelView;
35
36/**
37 * Wraps a notification containing a custom view.
38 */
39public class NotificationCustomViewWrapper extends NotificationViewWrapper {
40
41    private final ViewInvertHelper mInvertHelper;
42    private final Paint mGreyPaint = new Paint();
43    private int mBackgroundColor = 0;
44    private static final int CUSTOM_BACKGROUND_TAG = R.id.custom_background_color;
45    private boolean mShouldInvertDark;
46    private boolean mShowingLegacyBackground;
47
48    protected NotificationCustomViewWrapper(View view, ExpandableNotificationRow row) {
49        super(view, row);
50        mInvertHelper = new ViewInvertHelper(view, NotificationPanelView.DOZE_ANIMATION_DURATION);
51    }
52
53    @Override
54    public void setDark(boolean dark, boolean fade, long delay) {
55        if (dark == mDark && mDarkInitialized) {
56            return;
57        }
58        super.setDark(dark, fade, delay);
59        if (!mShowingLegacyBackground && mShouldInvertDark) {
60            if (fade) {
61                mInvertHelper.fade(dark, delay);
62            } else {
63                mInvertHelper.update(dark);
64            }
65        } else {
66            mView.setLayerType(dark ? View.LAYER_TYPE_HARDWARE : View.LAYER_TYPE_NONE, null);
67            if (fade) {
68                fadeGrayscale(dark, delay);
69            } else {
70                updateGrayscale(dark);
71            }
72        }
73    }
74
75    protected void fadeGrayscale(final boolean dark, long delay) {
76        startIntensityAnimation(new ValueAnimator.AnimatorUpdateListener() {
77            @Override
78            public void onAnimationUpdate(ValueAnimator animation) {
79                updateGrayscaleMatrix((float) animation.getAnimatedValue());
80                mGreyPaint.setColorFilter(new ColorMatrixColorFilter(mGrayscaleColorMatrix));
81                mView.setLayerPaint(mGreyPaint);
82            }
83        }, dark, delay, new AnimatorListenerAdapter() {
84            @Override
85            public void onAnimationEnd(Animator animation) {
86                if (!dark) {
87                    mView.setLayerType(View.LAYER_TYPE_NONE, null);
88                }
89            }
90        });
91    }
92
93    protected void updateGrayscale(boolean dark) {
94        if (dark) {
95            updateGrayscaleMatrix(1f);
96            mGreyPaint.setColorFilter(
97                    new ColorMatrixColorFilter(mGrayscaleColorMatrix));
98            mView.setLayerPaint(mGreyPaint);
99        }
100    }
101
102    @Override
103    public void setVisible(boolean visible) {
104        super.setVisible(visible);
105        mView.setAlpha(visible ? 1.0f : 0.0f);
106    }
107
108    @Override
109    public void notifyContentUpdated(StatusBarNotification notification) {
110        super.notifyContentUpdated(notification);
111        Drawable background = mView.getBackground();
112        mBackgroundColor = 0;
113        if (background instanceof ColorDrawable) {
114            mBackgroundColor = ((ColorDrawable) background).getColor();
115            mView.setBackground(null);
116            mView.setTag(CUSTOM_BACKGROUND_TAG, mBackgroundColor);
117        } else if (mView.getTag(CUSTOM_BACKGROUND_TAG) != null) {
118            mBackgroundColor = (int) mView.getTag(CUSTOM_BACKGROUND_TAG);
119        }
120        mShouldInvertDark = mBackgroundColor == 0 || isColorLight(mBackgroundColor);
121    }
122
123    private boolean isColorLight(int backgroundColor) {
124        return Color.alpha(backgroundColor) == 0
125                || ColorUtils.calculateLuminance(backgroundColor) > 0.5;
126    }
127
128    @Override
129    public int getCustomBackgroundColor() {
130        // Parent notifications should always use the normal background color
131        return mRow.isSummaryWithChildren() ? 0 : mBackgroundColor;
132    }
133
134    @Override
135    public void setShowingLegacyBackground(boolean showing) {
136        super.setShowingLegacyBackground(showing);
137        mShowingLegacyBackground = showing;
138    }
139}
140