1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3.notification;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.service.notification.StatusBarNotification;
import android.view.View;
import com.android.launcher3.Launcher;
import com.android.launcher3.graphics.IconPalette;
import com.android.launcher3.popup.PopupContainerWithArrow;
import com.android.launcher3.util.PackageUserKey;
/**
* An object that contains relevant information from a {@link StatusBarNotification}. This should
* only be created when we need to show the notification contents on the UI; until then, a
* {@link com.android.launcher3.badge.BadgeInfo} with only the notification key should
* be passed around, and then this can be constructed using the StatusBarNotification from
* {@link NotificationListener#getNotificationsForKeys(String[])}.
*/
public class NotificationInfo implements View.OnClickListener {
public final PackageUserKey packageUserKey;
public final String notificationKey;
public final CharSequence title;
public final CharSequence text;
public final PendingIntent intent;
public final boolean autoCancel;
public final boolean dismissable;
private final Drawable mIconDrawable;
private boolean mShouldTintIcon;
private int mIconColor;
/**
* Extracts the data that we need from the StatusBarNotification.
*/
public NotificationInfo(Context context, StatusBarNotification statusBarNotification) {
packageUserKey = PackageUserKey.fromNotification(statusBarNotification);
notificationKey = statusBarNotification.getKey();
Notification notification = statusBarNotification.getNotification();
title = notification.extras.getCharSequence(Notification.EXTRA_TITLE);
text = notification.extras.getCharSequence(Notification.EXTRA_TEXT);
// Load the icon. Since it is backed by ashmem, we won't copy the entire bitmap
// into our process as long as we don't touch it and it exists in systemui.
Icon icon = notification.getLargeIcon();
if (icon == null) {
icon = notification.getSmallIcon();
mIconDrawable = icon.loadDrawable(context);
mIconColor = statusBarNotification.getNotification().color;
mShouldTintIcon = true;
} else {
mIconDrawable = icon.loadDrawable(context);
mShouldTintIcon = false;
}
intent = notification.contentIntent;
autoCancel = (notification.flags & Notification.FLAG_AUTO_CANCEL) != 0;
dismissable = (notification.flags & Notification.FLAG_ONGOING_EVENT) == 0;
}
@Override
public void onClick(View view) {
final Launcher launcher = Launcher.getLauncher(view.getContext());
try {
intent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
if (autoCancel) {
launcher.getPopupDataProvider().cancelNotification(notificationKey);
}
PopupContainerWithArrow.getOpen(launcher).close(true);
}
public Drawable getIconForBackground(Context context, int background) {
if (!mShouldTintIcon) {
return mIconDrawable;
}
mIconColor = IconPalette.resolveContrastColor(context, mIconColor, background);
Drawable icon = mIconDrawable.mutate();
// DrawableContainer ignores the color filter if it's already set, so clear it first to
// get it set and invalidated properly.
icon.setTintList(null);
icon.setTint(mIconColor);
mShouldTintIcon = false;
return icon;
}
}
|