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