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
|
|
|
|
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;
|
2017-03-15 09:57:28 -07:00
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
2017-01-20 08:15:28 -08:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
import com.android.launcher3.Launcher;
|
2017-03-15 09:57:28 -07:00
|
|
|
import com.android.launcher3.LauncherAppState;
|
2017-01-27 08:45:49 -08:00
|
|
|
import com.android.launcher3.graphics.IconPalette;
|
2017-01-23 11:47:51 -08:00
|
|
|
import com.android.launcher3.popup.PopupContainerWithArrow;
|
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
|
|
|
|
|
* {@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
|
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
|
|
|
|
2017-03-15 09:57:28 -07:00
|
|
|
private int mBadgeIcon;
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2017-01-20 09:38:25 -08:00
|
|
|
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);
|
2017-03-15 09:44:40 -07:00
|
|
|
mBadgeIcon = notification.getBadgeIcon();
|
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.
|
2017-03-15 09:44:40 -07:00
|
|
|
Icon icon = mBadgeIcon == 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-01-27 08:45:49 -08:00
|
|
|
mIconDrawable = icon.loadDrawable(context);
|
|
|
|
|
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) {
|
|
|
|
|
mIconDrawable = new BitmapDrawable(context.getResources(), LauncherAppState
|
|
|
|
|
.getInstance(context).getIconCache()
|
|
|
|
|
.getDefaultIcon(statusBarNotification.getUser()));
|
2017-03-15 09:44:40 -07:00
|
|
|
mBadgeIcon = Notification.BADGE_ICON_NONE;
|
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;
|
2017-01-20 08:15:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View view) {
|
|
|
|
|
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);
|
2017-02-01 09:13:24 -08:00
|
|
|
launcher.getUserEventDispatcher().logNotificationLaunch(view, intent);
|
2017-01-20 08:15:28 -08:00
|
|
|
} catch (PendingIntent.CanceledException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
if (autoCancel) {
|
|
|
|
|
launcher.getPopupDataProvider().cancelNotification(notificationKey);
|
|
|
|
|
}
|
2017-01-23 11:47:51 -08:00
|
|
|
PopupContainerWithArrow.getOpen(launcher).close(true);
|
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-31 10:49:18 -08:00
|
|
|
|
|
|
|
|
public boolean isIconLarge() {
|
|
|
|
|
return mIsIconLarge;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean shouldShowIconInBadge() {
|
|
|
|
|
// If the icon we're using for this notification matches what the Notification
|
|
|
|
|
// specified should show in the badge, then return true.
|
2017-03-15 09:44:40 -07:00
|
|
|
return mIsIconLarge && mBadgeIcon == Notification.BADGE_ICON_LARGE
|
|
|
|
|
|| !mIsIconLarge && mBadgeIcon == Notification.BADGE_ICON_SMALL;
|
2017-01-31 10:49:18 -08:00
|
|
|
}
|
2017-01-20 08:15:28 -08:00
|
|
|
}
|