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