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.ActivityOptions;
20 import android.app.Notification;
21 import android.app.PendingIntent;
22 import android.content.Context;
23 import android.graphics.drawable.BitmapDrawable;
24 import android.graphics.drawable.Drawable;
25 import android.graphics.drawable.Icon;
26 import android.os.Bundle;
27 import android.service.notification.StatusBarNotification;
28 import android.view.View;
29 
30 import com.android.launcher3.AbstractFloatingView;
31 import com.android.launcher3.Launcher;
32 import com.android.launcher3.LauncherAppState;
33 import com.android.launcher3.dot.DotInfo;
34 import com.android.launcher3.graphics.IconPalette;
35 import com.android.launcher3.util.PackageUserKey;
36 
37 /**
38  * An object that contains relevant information from a {@link StatusBarNotification}. This should
39  * only be created when we need to show the notification contents on the UI; until then, a
40  * {@link DotInfo} with only the notification key should
41  * be passed around, and then this can be constructed using the StatusBarNotification from
42  * {@link NotificationListener#getNotificationsForKeys(java.util.List)}.
43  */
44 public class NotificationInfo implements View.OnClickListener {
45 
46     public final PackageUserKey packageUserKey;
47     public final String notificationKey;
48     public final CharSequence title;
49     public final CharSequence text;
50     public final PendingIntent intent;
51     public final boolean autoCancel;
52     public final boolean dismissable;
53 
54     private Drawable mIconDrawable;
55     private int mIconColor;
56     private boolean mIsIconLarge;
57 
58     /**
59      * Extracts the data that we need from the StatusBarNotification.
60      */
NotificationInfo(Context context, StatusBarNotification statusBarNotification)61     public NotificationInfo(Context context, StatusBarNotification statusBarNotification) {
62         packageUserKey = PackageUserKey.fromNotification(statusBarNotification);
63         notificationKey = statusBarNotification.getKey();
64         Notification notification = statusBarNotification.getNotification();
65         title = notification.extras.getCharSequence(Notification.EXTRA_TITLE);
66         text = notification.extras.getCharSequence(Notification.EXTRA_TEXT);
67 
68         int iconType = notification.getBadgeIconType();
69         // Load the icon. Since it is backed by ashmem, we won't copy the entire bitmap
70         // into our process as long as we don't touch it and it exists in systemui.
71         Icon icon = iconType == Notification.BADGE_ICON_SMALL ? null : notification.getLargeIcon();
72         if (icon == null) {
73             // Use the small icon.
74             icon = notification.getSmallIcon();
75             mIconDrawable = icon == null ? null : icon.loadDrawable(context);
76             mIconColor = statusBarNotification.getNotification().color;
77             mIsIconLarge = false;
78         } else {
79             // Use the large icon.
80             mIconDrawable = icon.loadDrawable(context);
81             mIsIconLarge = true;
82         }
83         if (mIconDrawable == null) {
84             mIconDrawable = new BitmapDrawable(context.getResources(), LauncherAppState
85                     .getInstance(context).getIconCache()
86                     .getDefaultIcon(statusBarNotification.getUser()).icon);
87         }
88         intent = notification.contentIntent;
89         autoCancel = (notification.flags & Notification.FLAG_AUTO_CANCEL) != 0;
90         dismissable = (notification.flags & Notification.FLAG_ONGOING_EVENT) == 0;
91     }
92 
93     @Override
onClick(View view)94     public void onClick(View view) {
95         if (intent == null) {
96             return;
97         }
98         final Launcher launcher = Launcher.getLauncher(view.getContext());
99         Bundle activityOptions = ActivityOptions.makeClipRevealAnimation(
100                 view, 0, 0, view.getWidth(), view.getHeight()).toBundle();
101         try {
102             intent.send(null, 0, null, null, null, null, activityOptions);
103             launcher.getUserEventDispatcher().logNotificationLaunch(view, intent);
104         } catch (PendingIntent.CanceledException e) {
105             e.printStackTrace();
106         }
107         if (autoCancel) {
108             launcher.getPopupDataProvider().cancelNotification(notificationKey);
109         }
110         AbstractFloatingView.closeOpenContainer(launcher, AbstractFloatingView
111                 .TYPE_ACTION_POPUP);
112     }
113 
getIconForBackground(Context context, int background)114     public Drawable getIconForBackground(Context context, int background) {
115         if (mIsIconLarge) {
116             // Only small icons should be tinted.
117             return mIconDrawable;
118         }
119         mIconColor = IconPalette.resolveContrastColor(context, mIconColor, background);
120         Drawable icon = mIconDrawable.mutate();
121         // DrawableContainer ignores the color filter if it's already set, so clear it first to
122         // get it set and invalidated properly.
123         icon.setTintList(null);
124         icon.setTint(mIconColor);
125         return icon;
126     }
127 }
128