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