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