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