Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 17 | package android.service.notification; |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 18 | |
Julia Reynolds | cbc45e7 | 2019-03-07 12:31:52 -0500 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Mathew Inwood | e380737 | 2018-08-10 09:51:03 +0100 | [diff] [blame] | 20 | import android.annotation.UnsupportedAppUsage; |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 21 | import android.app.Notification; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 22 | import android.app.NotificationManager; |
Dan Sandler | 68079d5 | 2015-07-22 10:45:30 -0400 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.pm.ApplicationInfo; |
| 25 | import android.content.pm.PackageManager; |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 26 | import android.metrics.LogMaker; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 27 | import android.os.Build; |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 28 | import android.os.Parcel; |
| 29 | import android.os.Parcelable; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 30 | import android.os.UserHandle; |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 31 | |
Will Brockman | 828427e | 2019-01-28 09:55:45 -0500 | [diff] [blame] | 32 | import com.android.internal.logging.nano.MetricsProto; |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 33 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 34 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 35 | /** |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 36 | * Class encapsulating a Notification. Sent by the NotificationManagerService to clients including |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 37 | * the status bar and any {@link android.service.notification.NotificationListenerService}s. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 38 | */ |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 39 | public class StatusBarNotification implements Parcelable { |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 40 | static final int MAX_LOG_TAG_LENGTH = 36; |
| 41 | |
Mathew Inwood | e380737 | 2018-08-10 09:51:03 +0100 | [diff] [blame] | 42 | @UnsupportedAppUsage |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 43 | private final String pkg; |
Mathew Inwood | e380737 | 2018-08-10 09:51:03 +0100 | [diff] [blame] | 44 | @UnsupportedAppUsage |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 45 | private final int id; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 46 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 47 | private final String tag; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 48 | private final String key; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 49 | private String groupKey; |
| 50 | private String overrideGroupKey; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 51 | |
Mathew Inwood | e380737 | 2018-08-10 09:51:03 +0100 | [diff] [blame] | 52 | @UnsupportedAppUsage |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 53 | private final int uid; |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 54 | private final String opPkg; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 55 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 56 | private final int initialPid; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 57 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 58 | private final Notification notification; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 59 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 60 | private final UserHandle user; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 61 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 62 | private final long postTime; |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 63 | |
Dan Sandler | 68079d5 | 2015-07-22 10:45:30 -0400 | [diff] [blame] | 64 | private Context mContext; // used for inflation & icon expansion |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 65 | |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 66 | // Contains the basic logging data of the notification. |
| 67 | private LogMaker mLogMaker; |
| 68 | |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 69 | /** @hide */ |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 70 | public StatusBarNotification(String pkg, String opPkg, int id, |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 71 | String tag, int uid, int initialPid, Notification notification, UserHandle user, |
| 72 | String overrideGroupKey, long postTime) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 73 | if (pkg == null) throw new NullPointerException(); |
| 74 | if (notification == null) throw new NullPointerException(); |
| 75 | |
| 76 | this.pkg = pkg; |
| 77 | this.opPkg = opPkg; |
| 78 | this.id = id; |
| 79 | this.tag = tag; |
| 80 | this.uid = uid; |
| 81 | this.initialPid = initialPid; |
| 82 | this.notification = notification; |
| 83 | this.user = user; |
| 84 | this.postTime = postTime; |
| 85 | this.overrideGroupKey = overrideGroupKey; |
| 86 | this.key = key(); |
| 87 | this.groupKey = groupKey(); |
| 88 | } |
| 89 | |
Julia Reynolds | a11d0b1 | 2017-02-16 15:01:36 -0500 | [diff] [blame] | 90 | /** |
| 91 | * @deprecated Non-system apps should not need to create StatusBarNotifications. |
| 92 | */ |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 93 | @Deprecated |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 94 | public StatusBarNotification(String pkg, String opPkg, int id, String tag, int uid, |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 95 | int initialPid, int score, Notification notification, UserHandle user, |
| 96 | long postTime) { |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 97 | if (pkg == null) throw new NullPointerException(); |
| 98 | if (notification == null) throw new NullPointerException(); |
| 99 | |
| 100 | this.pkg = pkg; |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 101 | this.opPkg = opPkg; |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 102 | this.id = id; |
| 103 | this.tag = tag; |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 104 | this.uid = uid; |
| 105 | this.initialPid = initialPid; |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 106 | this.notification = notification; |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 107 | this.user = user; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 108 | this.postTime = postTime; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 109 | this.key = key(); |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 110 | this.groupKey = groupKey(); |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | public StatusBarNotification(Parcel in) { |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 114 | this.pkg = in.readString(); |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 115 | this.opPkg = in.readString(); |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 116 | this.id = in.readInt(); |
| 117 | if (in.readInt() != 0) { |
| 118 | this.tag = in.readString(); |
| 119 | } else { |
| 120 | this.tag = null; |
| 121 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 122 | this.uid = in.readInt(); |
| 123 | this.initialPid = in.readInt(); |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 124 | this.notification = new Notification(in); |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 125 | this.user = UserHandle.readFromParcel(in); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 126 | this.postTime = in.readLong(); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 127 | if (in.readInt() != 0) { |
| 128 | this.overrideGroupKey = in.readString(); |
| 129 | } else { |
| 130 | this.overrideGroupKey = null; |
| 131 | } |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 132 | this.key = key(); |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 133 | this.groupKey = groupKey(); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | private String key() { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 137 | String sbnKey = user.getIdentifier() + "|" + pkg + "|" + id + "|" + tag + "|" + uid; |
| 138 | if (overrideGroupKey != null && getNotification().isGroupSummary()) { |
| 139 | sbnKey = sbnKey + "|" + overrideGroupKey; |
| 140 | } |
| 141 | return sbnKey; |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 144 | private String groupKey() { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 145 | if (overrideGroupKey != null) { |
| 146 | return user.getIdentifier() + "|" + pkg + "|" + "g:" + overrideGroupKey; |
| 147 | } |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 148 | final String group = getNotification().getGroup(); |
| 149 | final String sortKey = getNotification().getSortKey(); |
| 150 | if (group == null && sortKey == null) { |
| 151 | // a group of one |
| 152 | return key; |
| 153 | } |
| 154 | return user.getIdentifier() + "|" + pkg + "|" + |
| 155 | (group == null |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 156 | ? "c:" + notification.getChannelId() |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 157 | : "g:" + group); |
| 158 | } |
| 159 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 160 | /** |
| 161 | * Returns true if this notification is part of a group. |
| 162 | */ |
| 163 | public boolean isGroup() { |
Chris Wren | 8a1638f | 2016-05-02 16:19:14 -0400 | [diff] [blame] | 164 | if (overrideGroupKey != null || isAppGroup()) { |
| 165 | return true; |
| 166 | } |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns true if application asked that this notification be part of a group. |
| 172 | * @hide |
| 173 | */ |
| 174 | public boolean isAppGroup() { |
| 175 | if (getNotification().getGroup() != null || getNotification().getSortKey() != null) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 176 | return true; |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 181 | public void writeToParcel(Parcel out, int flags) { |
| 182 | out.writeString(this.pkg); |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 183 | out.writeString(this.opPkg); |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 184 | out.writeInt(this.id); |
| 185 | if (this.tag != null) { |
| 186 | out.writeInt(1); |
| 187 | out.writeString(this.tag); |
| 188 | } else { |
| 189 | out.writeInt(0); |
| 190 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 191 | out.writeInt(this.uid); |
| 192 | out.writeInt(this.initialPid); |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 193 | this.notification.writeToParcel(out, flags); |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 194 | user.writeToParcel(out, flags); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 195 | |
| 196 | out.writeLong(this.postTime); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 197 | if (this.overrideGroupKey != null) { |
| 198 | out.writeInt(1); |
| 199 | out.writeString(this.overrideGroupKey); |
| 200 | } else { |
| 201 | out.writeInt(0); |
| 202 | } |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | public int describeContents() { |
| 206 | return 0; |
| 207 | } |
| 208 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 209 | public static final @android.annotation.NonNull Parcelable.Creator<StatusBarNotification> CREATOR |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 210 | = new Parcelable.Creator<StatusBarNotification>() |
| 211 | { |
| 212 | public StatusBarNotification createFromParcel(Parcel parcel) |
| 213 | { |
| 214 | return new StatusBarNotification(parcel); |
| 215 | } |
| 216 | |
| 217 | public StatusBarNotification[] newArray(int size) |
| 218 | { |
| 219 | return new StatusBarNotification[size]; |
| 220 | } |
| 221 | }; |
| 222 | |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 223 | /** |
| 224 | * @hide |
| 225 | */ |
| 226 | public StatusBarNotification cloneLight() { |
| 227 | final Notification no = new Notification(); |
| 228 | this.notification.cloneInto(no, false); // light copy |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 229 | return new StatusBarNotification(this.pkg, this.opPkg, |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 230 | this.id, this.tag, this.uid, this.initialPid, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 231 | no, this.user, this.overrideGroupKey, this.postTime); |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 232 | } |
| 233 | |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 234 | @Override |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 235 | public StatusBarNotification clone() { |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 236 | return new StatusBarNotification(this.pkg, this.opPkg, |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 237 | this.id, this.tag, this.uid, this.initialPid, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 238 | this.notification.clone(), this.user, this.overrideGroupKey, this.postTime); |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 241 | @Override |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 242 | public String toString() { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 243 | return String.format( |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 244 | "StatusBarNotification(pkg=%s user=%s id=%d tag=%s key=%s: %s)", |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 245 | this.pkg, this.user, this.id, this.tag, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 246 | this.key, this.notification); |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 249 | /** Convenience method to check the notification's flags for |
| 250 | * {@link Notification#FLAG_ONGOING_EVENT}. |
| 251 | */ |
Joe Onorato | e345fff | 2010-05-23 15:18:27 -0400 | [diff] [blame] | 252 | public boolean isOngoing() { |
| 253 | return (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0; |
| 254 | } |
| 255 | |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 256 | /** Convenience method to check the notification's flags for |
| 257 | * either {@link Notification#FLAG_ONGOING_EVENT} or |
| 258 | * {@link Notification#FLAG_NO_CLEAR}. |
| 259 | */ |
Joe Onorato | 5dd1169 | 2010-09-27 15:34:04 -0700 | [diff] [blame] | 260 | public boolean isClearable() { |
| 261 | return ((notification.flags & Notification.FLAG_ONGOING_EVENT) == 0) |
| 262 | && ((notification.flags & Notification.FLAG_NO_CLEAR) == 0); |
| 263 | } |
Daniel Sandler | b9301c3 | 2012-08-14 15:08:24 -0400 | [diff] [blame] | 264 | |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 265 | /** |
Julia Reynolds | d5261e1 | 2016-12-19 14:14:25 -0500 | [diff] [blame] | 266 | * Returns a userid for whom this notification is intended. |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 267 | * |
| 268 | * @deprecated Use {@link #getUser()} instead. |
| 269 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 270 | @Deprecated |
Daniel Sandler | b9301c3 | 2012-08-14 15:08:24 -0400 | [diff] [blame] | 271 | public int getUserId() { |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 272 | return this.user.getIdentifier(); |
Daniel Sandler | b9301c3 | 2012-08-14 15:08:24 -0400 | [diff] [blame] | 273 | } |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 274 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 275 | /** The package that the notification belongs to. */ |
Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 276 | public String getPackageName() { |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 277 | return pkg; |
| 278 | } |
| 279 | |
Scott Main | 26510320 | 2013-05-22 19:00:09 -0700 | [diff] [blame] | 280 | /** The id supplied to {@link android.app.NotificationManager#notify(int,Notification)}. */ |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 281 | public int getId() { |
| 282 | return id; |
| 283 | } |
| 284 | |
Scott Main | 26510320 | 2013-05-22 19:00:09 -0700 | [diff] [blame] | 285 | /** The tag supplied to {@link android.app.NotificationManager#notify(int,Notification)}, |
| 286 | * or null if no tag was specified. */ |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 287 | public String getTag() { |
| 288 | return tag; |
| 289 | } |
| 290 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 291 | /** |
| 292 | * The notifying app's ({@link #getPackageName()}'s) uid. |
| 293 | */ |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 294 | public int getUid() { |
| 295 | return uid; |
| 296 | } |
| 297 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 298 | /** The package that posted the notification. |
| 299 | *<p> |
| 300 | * Might be different from {@link #getPackageName()} if the app owning the notification has |
| 301 | * a {@link NotificationManager#setNotificationDelegate(String) notification delegate}. |
| 302 | */ |
Julia Reynolds | cbc45e7 | 2019-03-07 12:31:52 -0500 | [diff] [blame] | 303 | public @NonNull String getOpPkg() { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 304 | return opPkg; |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /** @hide */ |
Mathew Inwood | e380737 | 2018-08-10 09:51:03 +0100 | [diff] [blame] | 308 | @UnsupportedAppUsage |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 309 | public int getInitialPid() { |
| 310 | return initialPid; |
| 311 | } |
| 312 | |
| 313 | /** The {@link android.app.Notification} supplied to |
Scott Main | 26510320 | 2013-05-22 19:00:09 -0700 | [diff] [blame] | 314 | * {@link android.app.NotificationManager#notify(int,Notification)}. */ |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 315 | public Notification getNotification() { |
| 316 | return notification; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * The {@link android.os.UserHandle} for whom this notification is intended. |
Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 321 | */ |
| 322 | public UserHandle getUser() { |
| 323 | return user; |
| 324 | } |
| 325 | |
| 326 | /** The time (in {@link System#currentTimeMillis} time) the notification was posted, |
| 327 | * which may be different than {@link android.app.Notification#when}. |
| 328 | */ |
| 329 | public long getPostTime() { |
| 330 | return postTime; |
| 331 | } |
| 332 | |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 333 | /** |
| 334 | * A unique instance key for this notification record. |
| 335 | */ |
| 336 | public String getKey() { |
| 337 | return key; |
| 338 | } |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 339 | |
| 340 | /** |
| 341 | * A key that indicates the group with which this message ranks. |
| 342 | */ |
| 343 | public String getGroupKey() { |
| 344 | return groupKey; |
| 345 | } |
Dan Sandler | 68079d5 | 2015-07-22 10:45:30 -0400 | [diff] [blame] | 346 | |
| 347 | /** |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 348 | * The ID passed to setGroup(), or the override, or null. |
| 349 | * @hide |
| 350 | */ |
| 351 | public String getGroup() { |
| 352 | if (overrideGroupKey != null) { |
| 353 | return overrideGroupKey; |
| 354 | } |
| 355 | return getNotification().getGroup(); |
| 356 | } |
| 357 | |
| 358 | /** |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 359 | * Sets the override group key. |
| 360 | */ |
| 361 | public void setOverrideGroupKey(String overrideGroupKey) { |
| 362 | this.overrideGroupKey = overrideGroupKey; |
| 363 | groupKey = groupKey(); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Returns the override group key. |
| 368 | */ |
| 369 | public String getOverrideGroupKey() { |
| 370 | return overrideGroupKey; |
| 371 | } |
| 372 | |
| 373 | /** |
Dan Sandler | 68079d5 | 2015-07-22 10:45:30 -0400 | [diff] [blame] | 374 | * @hide |
| 375 | */ |
Amin Shaikh | c3f0b06 | 2019-02-12 19:00:17 -0500 | [diff] [blame] | 376 | public void clearPackageContext() { |
| 377 | mContext = null; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * @hide |
| 382 | */ |
Mathew Inwood | e380737 | 2018-08-10 09:51:03 +0100 | [diff] [blame] | 383 | @UnsupportedAppUsage |
Dan Sandler | 68079d5 | 2015-07-22 10:45:30 -0400 | [diff] [blame] | 384 | public Context getPackageContext(Context context) { |
| 385 | if (mContext == null) { |
| 386 | try { |
| 387 | ApplicationInfo ai = context.getPackageManager() |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 388 | .getApplicationInfoAsUser(pkg, PackageManager.MATCH_UNINSTALLED_PACKAGES, |
| 389 | getUserId()); |
Dan Sandler | 68079d5 | 2015-07-22 10:45:30 -0400 | [diff] [blame] | 390 | mContext = context.createApplicationContext(ai, |
| 391 | Context.CONTEXT_RESTRICTED); |
| 392 | } catch (PackageManager.NameNotFoundException e) { |
| 393 | mContext = null; |
| 394 | } |
| 395 | } |
| 396 | if (mContext == null) { |
| 397 | mContext = context; |
| 398 | } |
| 399 | return mContext; |
| 400 | } |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 401 | |
| 402 | /** |
| 403 | * Returns a LogMaker that contains all basic information of the notification. |
| 404 | * @hide |
| 405 | */ |
| 406 | public LogMaker getLogMaker() { |
| 407 | if (mLogMaker == null) { |
| 408 | // Initialize fields that only change on update (so a new record). |
| 409 | mLogMaker = new LogMaker(MetricsEvent.VIEW_UNKNOWN) |
| 410 | .setPackageName(getPackageName()) |
| 411 | .addTaggedData(MetricsEvent.NOTIFICATION_ID, getId()) |
| 412 | .addTaggedData(MetricsEvent.NOTIFICATION_TAG, getTag()) |
| 413 | .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID, getChannelIdLogTag()); |
| 414 | } |
| 415 | // Reset fields that can change between updates, or are used by multiple logs. |
| 416 | return mLogMaker |
| 417 | .clearCategory() |
| 418 | .clearType() |
| 419 | .clearSubtype() |
| 420 | .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID, getGroupLogTag()) |
| 421 | .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_SUMMARY, |
Will Brockman | 828427e | 2019-01-28 09:55:45 -0500 | [diff] [blame] | 422 | getNotification().isGroupSummary() ? 1 : 0) |
| 423 | .addTaggedData(MetricsProto.MetricsEvent.FIELD_NOTIFICATION_CATEGORY, |
| 424 | getNotification().category); |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | private String getGroupLogTag() { |
| 428 | return shortenTag(getGroup()); |
| 429 | } |
| 430 | |
| 431 | private String getChannelIdLogTag() { |
| 432 | if (notification.getChannelId() == null) { |
| 433 | return null; |
| 434 | } |
| 435 | return shortenTag(notification.getChannelId()); |
| 436 | } |
| 437 | |
| 438 | // Make logTag with max size MAX_LOG_TAG_LENGTH. |
| 439 | // For shorter or equal tags, returns the tag. |
| 440 | // For longer tags, truncate the tag and append a hash of the full tag to |
| 441 | // fill the maximum size. |
| 442 | private String shortenTag(String logTag) { |
| 443 | if (logTag == null || logTag.length() <= MAX_LOG_TAG_LENGTH) { |
| 444 | return logTag; |
| 445 | } |
| 446 | String hash = Integer.toHexString(logTag.hashCode()); |
| 447 | return logTag.substring(0, MAX_LOG_TAG_LENGTH - hash.length() - 1) + "-" |
| 448 | + hash; |
| 449 | } |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 450 | } |