2017-01-20 08:15:28 -08:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-01-20 09:38:25 -08:00
|
|
|
package com.android.launcher3.notification;
|
2017-01-20 08:15:28 -08:00
|
|
|
|
2020-06-01 17:17:24 -07:00
|
|
|
import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_NOTIFICATION_LAUNCH_TAP;
|
|
|
|
|
|
2017-02-01 11:12:12 -08:00
|
|
|
import android.app.ActivityOptions;
|
2017-01-20 08:15:28 -08:00
|
|
|
import android.app.Notification;
|
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
|
import android.graphics.drawable.Icon;
|
2017-02-01 11:12:12 -08:00
|
|
|
import android.os.Bundle;
|
2017-01-20 08:15:28 -08:00
|
|
|
import android.service.notification.StatusBarNotification;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
|
2017-06-15 18:04:04 -07:00
|
|
|
import com.android.launcher3.AbstractFloatingView;
|
2017-01-20 08:15:28 -08:00
|
|
|
import com.android.launcher3.Launcher;
|
2017-03-15 09:57:28 -07:00
|
|
|
import com.android.launcher3.LauncherAppState;
|
2018-12-03 18:11:39 -08:00
|
|
|
import com.android.launcher3.dot.DotInfo;
|
2017-01-27 08:45:49 -08:00
|
|
|
import com.android.launcher3.graphics.IconPalette;
|
2020-06-01 17:17:24 -07:00
|
|
|
import com.android.launcher3.model.data.ItemInfo;
|
2017-01-20 08:15:28 -08:00
|
|
|
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
|
2018-12-03 18:11:39 -08:00
|
|
|
* {@link DotInfo} with only the notification key should
|
2017-01-20 08:15:28 -08:00
|
|
|
* be passed around, and then this can be constructed using the StatusBarNotification from
|
2017-03-24 11:31:12 -07:00
|
|
|
* {@link NotificationListener#getNotificationsForKeys(java.util.List)}.
|
2017-01-20 08:15:28 -08:00
|
|
|
*/
|
|
|
|
|
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;
|
2017-01-20 09:38:25 -08:00
|
|
|
public final boolean dismissable;
|
2017-01-20 08:15:28 -08:00
|
|
|
|
2020-06-01 17:17:24 -07:00
|
|
|
private final ItemInfo mItemInfo;
|
2017-03-15 09:57:28 -07:00
|
|
|
private Drawable mIconDrawable;
|
2017-01-27 08:45:49 -08:00
|
|
|
private int mIconColor;
|
2017-01-31 10:49:18 -08:00
|
|
|
private boolean mIsIconLarge;
|
2017-01-27 08:45:49 -08:00
|
|
|
|
2017-01-20 08:15:28 -08:00
|
|
|
/**
|
|
|
|
|
* Extracts the data that we need from the StatusBarNotification.
|
|
|
|
|
*/
|
2020-06-01 17:17:24 -07:00
|
|
|
public NotificationInfo(Context context, StatusBarNotification statusBarNotification,
|
|
|
|
|
ItemInfo itemInfo) {
|
2017-01-20 09:38:25 -08:00
|
|
|
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);
|
2017-04-03 10:18:42 -07:00
|
|
|
|
2018-12-04 15:54:27 -08:00
|
|
|
int iconType = notification.getBadgeIconType();
|
2017-01-26 09:24:41 -08:00
|
|
|
// 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.
|
2018-12-04 15:54:27 -08:00
|
|
|
Icon icon = iconType == Notification.BADGE_ICON_SMALL ? null : notification.getLargeIcon();
|
2017-01-20 08:15:28 -08:00
|
|
|
if (icon == null) {
|
2017-01-31 10:49:18 -08:00
|
|
|
// Use the small icon.
|
2017-01-20 09:38:25 -08:00
|
|
|
icon = notification.getSmallIcon();
|
2017-10-06 13:46:55 -07:00
|
|
|
mIconDrawable = icon == null ? null : icon.loadDrawable(context);
|
2017-01-27 08:45:49 -08:00
|
|
|
mIconColor = statusBarNotification.getNotification().color;
|
2017-01-31 10:49:18 -08:00
|
|
|
mIsIconLarge = false;
|
2017-01-20 08:15:28 -08:00
|
|
|
} else {
|
2017-01-31 10:49:18 -08:00
|
|
|
// Use the large icon.
|
2017-01-27 08:45:49 -08:00
|
|
|
mIconDrawable = icon.loadDrawable(context);
|
2017-01-31 10:49:18 -08:00
|
|
|
mIsIconLarge = true;
|
2017-01-20 08:15:28 -08:00
|
|
|
}
|
2017-03-15 09:57:28 -07:00
|
|
|
if (mIconDrawable == null) {
|
2021-03-28 10:31:43 -07:00
|
|
|
mIconDrawable = LauncherAppState.getInstance(context).getIconCache()
|
|
|
|
|
.getDefaultIcon(statusBarNotification.getUser()).newIcon(context);
|
2017-03-15 09:57:28 -07:00
|
|
|
}
|
2017-01-20 09:38:25 -08:00
|
|
|
intent = notification.contentIntent;
|
|
|
|
|
autoCancel = (notification.flags & Notification.FLAG_AUTO_CANCEL) != 0;
|
|
|
|
|
dismissable = (notification.flags & Notification.FLAG_ONGOING_EVENT) == 0;
|
2020-06-01 17:17:24 -07:00
|
|
|
this.mItemInfo = itemInfo;
|
2017-01-20 08:15:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View view) {
|
2017-09-15 17:38:48 -07:00
|
|
|
if (intent == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-01-20 08:15:28 -08:00
|
|
|
final Launcher launcher = Launcher.getLauncher(view.getContext());
|
2017-02-01 11:12:12 -08:00
|
|
|
Bundle activityOptions = ActivityOptions.makeClipRevealAnimation(
|
|
|
|
|
view, 0, 0, view.getWidth(), view.getHeight()).toBundle();
|
2017-01-20 08:15:28 -08:00
|
|
|
try {
|
2017-02-01 11:12:12 -08:00
|
|
|
intent.send(null, 0, null, null, null, null, activityOptions);
|
2020-06-16 18:58:13 -07:00
|
|
|
launcher.getStatsLogManager().logger().withItemInfo(mItemInfo)
|
|
|
|
|
.log(LAUNCHER_NOTIFICATION_LAUNCH_TAP);
|
2017-01-20 08:15:28 -08:00
|
|
|
} catch (PendingIntent.CanceledException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
if (autoCancel) {
|
|
|
|
|
launcher.getPopupDataProvider().cancelNotification(notificationKey);
|
|
|
|
|
}
|
2017-06-15 18:04:04 -07:00
|
|
|
AbstractFloatingView.closeOpenContainer(launcher, AbstractFloatingView
|
2017-10-09 14:56:21 -07:00
|
|
|
.TYPE_ACTION_POPUP);
|
2017-01-20 08:15:28 -08:00
|
|
|
}
|
2017-01-27 08:45:49 -08:00
|
|
|
|
|
|
|
|
public Drawable getIconForBackground(Context context, int background) {
|
2017-01-31 10:49:18 -08:00
|
|
|
if (mIsIconLarge) {
|
|
|
|
|
// Only small icons should be tinted.
|
2017-01-27 08:45:49 -08:00
|
|
|
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);
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
2017-01-20 08:15:28 -08:00
|
|
|
}
|