OSDN Git Service

Ensure notification icons have enough contrast with background.
[android-x86/packages-apps-Launcher3.git] / src / com / android / launcher3 / notification / NotificationInfo.java
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.launcher3.notification;
18
19 import android.app.Notification;
20 import android.app.PendingIntent;
21 import android.content.Context;
22 import android.graphics.drawable.Drawable;
23 import android.graphics.drawable.Icon;
24 import android.service.notification.StatusBarNotification;
25 import android.view.View;
26
27 import com.android.launcher3.Launcher;
28 import com.android.launcher3.graphics.IconPalette;
29 import com.android.launcher3.popup.PopupContainerWithArrow;
30 import com.android.launcher3.util.PackageUserKey;
31
32 /**
33  * An object that contains relevant information from a {@link StatusBarNotification}. This should
34  * only be created when we need to show the notification contents on the UI; until then, a
35  * {@link com.android.launcher3.badge.BadgeInfo} with only the notification key should
36  * be passed around, and then this can be constructed using the StatusBarNotification from
37  * {@link NotificationListener#getNotificationsForKeys(String[])}.
38  */
39 public class NotificationInfo implements View.OnClickListener {
40
41     public final PackageUserKey packageUserKey;
42     public final String notificationKey;
43     public final CharSequence title;
44     public final CharSequence text;
45     public final PendingIntent intent;
46     public final boolean autoCancel;
47     public final boolean dismissable;
48
49     private final Drawable mIconDrawable;
50     private boolean mShouldTintIcon;
51     private int mIconColor;
52
53     /**
54      * Extracts the data that we need from the StatusBarNotification.
55      */
56     public NotificationInfo(Context context, StatusBarNotification statusBarNotification) {
57         packageUserKey = PackageUserKey.fromNotification(statusBarNotification);
58         notificationKey = statusBarNotification.getKey();
59         Notification notification = statusBarNotification.getNotification();
60         title = notification.extras.getCharSequence(Notification.EXTRA_TITLE);
61         text = notification.extras.getCharSequence(Notification.EXTRA_TEXT);
62         // Load the icon. Since it is backed by ashmem, we won't copy the entire bitmap
63         // into our process as long as we don't touch it and it exists in systemui.
64         Icon icon = notification.getLargeIcon();
65         if (icon == null) {
66             icon = notification.getSmallIcon();
67             mIconDrawable = icon.loadDrawable(context);
68             mIconColor = statusBarNotification.getNotification().color;
69             mShouldTintIcon = true;
70         } else {
71             mIconDrawable = icon.loadDrawable(context);
72             mShouldTintIcon = false;
73         }
74         intent = notification.contentIntent;
75         autoCancel = (notification.flags & Notification.FLAG_AUTO_CANCEL) != 0;
76         dismissable = (notification.flags & Notification.FLAG_ONGOING_EVENT) == 0;
77     }
78
79     @Override
80     public void onClick(View view) {
81         final Launcher launcher = Launcher.getLauncher(view.getContext());
82         try {
83             intent.send();
84         } catch (PendingIntent.CanceledException e) {
85             e.printStackTrace();
86         }
87         if (autoCancel) {
88             launcher.getPopupDataProvider().cancelNotification(notificationKey);
89         }
90         PopupContainerWithArrow.getOpen(launcher).close(true);
91     }
92
93     public Drawable getIconForBackground(Context context, int background) {
94         if (!mShouldTintIcon) {
95             return mIconDrawable;
96         }
97         mIconColor = IconPalette.resolveContrastColor(context, mIconColor, background);
98         Drawable icon = mIconDrawable.mutate();
99         // DrawableContainer ignores the color filter if it's already set, so clear it first to
100         // get it set and invalidated properly.
101         icon.setTintList(null);
102         icon.setTint(mIconColor);
103         mShouldTintIcon = false;
104         return icon;
105     }
106 }