| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.notification; |
| 18 |
|
| 19 |
import static android.text.TextUtils.formatSimple; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.app.Notification; |
| 23 |
import android.app.NotificationManager; |
| 24 |
import android.app.Person; |
| 25 |
import android.compat.annotation.UnsupportedAppUsage; |
| 26 |
import android.content.Context; |
| 27 |
import android.content.pm.ApplicationInfo; |
| 28 |
import android.content.pm.PackageManager; |
| 29 |
import android.metrics.LogMaker; |
| 30 |
import android.os.Build; |
| 31 |
import android.os.Parcel; |
| 32 |
import android.os.Parcelable; |
| 33 |
import android.os.UserHandle; |
| 34 |
|
| 35 |
import com.android.internal.logging.InstanceId; |
| 36 |
import com.android.internal.logging.nano.MetricsProto; |
| 37 |
import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 38 |
|
| 39 |
import java.util.ArrayList; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class StatusBarNotification implements Parcelable { |
| 46 |
static final int MAX_LOG_TAG_LENGTH = 36; |
| 47 |
|
| 48 |
@UnsupportedAppUsage |
| 49 |
private final String pkg; |
| 50 |
@UnsupportedAppUsage |
| 51 |
private final int id; |
| 52 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 53 |
private final String tag; |
| 54 |
private final String key; |
| 55 |
private String groupKey; |
| 56 |
private String overrideGroupKey; |
| 57 |
|
| 58 |
@UnsupportedAppUsage |
| 59 |
private final int uid; |
| 60 |
private final String opPkg; |
| 61 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 62 |
private final int initialPid; |
| 63 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 64 |
private final Notification notification; |
| 65 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 66 |
private final UserHandle user; |
| 67 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 68 |
private final long postTime; |
| 69 |
|
| 70 |
private InstanceId mInstanceId; |
| 71 |
|
| 72 |
private Context mContext; |
| 73 |
|
| 74 |
|
| 75 |
public StatusBarNotification(String pkg, String opPkg, int id, |
| 76 |
String tag, int uid, int initialPid, Notification notification, UserHandle user, |
| 77 |
String overrideGroupKey, long postTime) { |
| 78 |
if (pkg == null) throw new NullPointerException(); |
| 79 |
if (notification == null) throw new NullPointerException(); |
| 80 |
|
| 81 |
this.pkg = pkg; |
| 82 |
this.opPkg = opPkg; |
| 83 |
this.id = id; |
| 84 |
this.tag = tag; |
| 85 |
this.uid = uid; |
| 86 |
this.initialPid = initialPid; |
| 87 |
this.notification = notification; |
| 88 |
this.user = user; |
| 89 |
this.postTime = postTime; |
| 90 |
this.overrideGroupKey = overrideGroupKey; |
| 91 |
this.key = key(); |
| 92 |
this.groupKey = groupKey(); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
@Deprecated |
| 99 |
public StatusBarNotification(String pkg, String opPkg, int id, String tag, int uid, |
| 100 |
int initialPid, int score, Notification notification, UserHandle user, |
| 101 |
long postTime) { |
| 102 |
if (pkg == null) throw new NullPointerException(); |
| 103 |
if (notification == null) throw new NullPointerException(); |
| 104 |
|
| 105 |
this.pkg = pkg; |
| 106 |
this.opPkg = opPkg; |
| 107 |
this.id = id; |
| 108 |
this.tag = tag; |
| 109 |
this.uid = uid; |
| 110 |
this.initialPid = initialPid; |
| 111 |
this.notification = notification; |
| 112 |
this.user = user; |
| 113 |
this.postTime = postTime; |
| 114 |
this.key = key(); |
| 115 |
this.groupKey = groupKey(); |
| 116 |
} |
| 117 |
|
| 118 |
public StatusBarNotification(Parcel in) { |
| 119 |
this.pkg = in.readString(); |
| 120 |
this.opPkg = in.readString(); |
| 121 |
this.id = in.readInt(); |
| 122 |
if (in.readInt() != 0) { |
| 123 |
this.tag = in.readString(); |
| 124 |
} else { |
| 125 |
this.tag = null; |
| 126 |
} |
| 127 |
this.uid = in.readInt(); |
| 128 |
this.initialPid = in.readInt(); |
| 129 |
this.notification = new Notification(in); |
| 130 |
this.user = UserHandle.readFromParcel(in); |
| 131 |
this.postTime = in.readLong(); |
| 132 |
if (in.readInt() != 0) { |
| 133 |
this.overrideGroupKey = in.readString(); |
| 134 |
} |
| 135 |
if (in.readInt() != 0) { |
| 136 |
this.mInstanceId = InstanceId.CREATOR.createFromParcel(in); |
| 137 |
} |
| 138 |
this.key = key(); |
| 139 |
this.groupKey = groupKey(); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public static int getUidFromKey(@NonNull String key) { |
| 146 |
String[] parts = key.split("\\|"); |
| 147 |
if (parts.length >= 5) { |
| 148 |
try { |
| 149 |
int uid = Integer.parseInt(parts[4]); |
| 150 |
return uid; |
| 151 |
} catch (NumberFormatException e) { |
| 152 |
return -1; |
| 153 |
} |
| 154 |
} |
| 155 |
return -1; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public static String getPkgFromKey(@NonNull String key) { |
| 162 |
String[] parts = key.split("\\|"); |
| 163 |
if (parts.length >= 2) { |
| 164 |
return parts[1]; |
| 165 |
} |
| 166 |
return null; |
| 167 |
} |
| 168 |
|
| 169 |
private String key() { |
| 170 |
String sbnKey = user.getIdentifier() + "|" + pkg + "|" + id + "|" + tag + "|" + uid; |
| 171 |
if (overrideGroupKey != null && getNotification().isGroupSummary()) { |
| 172 |
sbnKey = sbnKey + "|" + overrideGroupKey; |
| 173 |
} |
| 174 |
return sbnKey; |
| 175 |
} |
| 176 |
|
| 177 |
private String groupKey() { |
| 178 |
if (overrideGroupKey != null) { |
| 179 |
return user.getIdentifier() + "|" + pkg + "|" + "g:" + overrideGroupKey; |
| 180 |
} |
| 181 |
final String group = getNotification().getGroup(); |
| 182 |
final String sortKey = getNotification().getSortKey(); |
| 183 |
if (group == null && sortKey == null) { |
| 184 |
|
| 185 |
return key; |
| 186 |
} |
| 187 |
return user.getIdentifier() + "|" + pkg + "|" + |
| 188 |
(group == null |
| 189 |
? "c:" + notification.getChannelId() |
| 190 |
: "g:" + group); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public boolean isGroup() { |
| 197 |
if (overrideGroupKey != null || isAppGroup()) { |
| 198 |
return true; |
| 199 |
} |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public boolean isAppGroup() { |
| 207 |
if (getNotification().getGroup() != null || getNotification().getSortKey() != null) { |
| 208 |
return true; |
| 209 |
} |
| 210 |
return false; |
| 211 |
} |
| 212 |
|
| 213 |
public void writeToParcel(Parcel out, int flags) { |
| 214 |
out.writeString(this.pkg); |
| 215 |
out.writeString(this.opPkg); |
| 216 |
out.writeInt(this.id); |
| 217 |
if (this.tag != null) { |
| 218 |
out.writeInt(1); |
| 219 |
out.writeString(this.tag); |
| 220 |
} else { |
| 221 |
out.writeInt(0); |
| 222 |
} |
| 223 |
out.writeInt(this.uid); |
| 224 |
out.writeInt(this.initialPid); |
| 225 |
this.notification.writeToParcel(out, flags); |
| 226 |
user.writeToParcel(out, flags); |
| 227 |
out.writeLong(this.postTime); |
| 228 |
if (this.overrideGroupKey != null) { |
| 229 |
out.writeInt(1); |
| 230 |
out.writeString(this.overrideGroupKey); |
| 231 |
} else { |
| 232 |
out.writeInt(0); |
| 233 |
} |
| 234 |
if (this.mInstanceId != null) { |
| 235 |
out.writeInt(1); |
| 236 |
mInstanceId.writeToParcel(out, flags); |
| 237 |
} else { |
| 238 |
out.writeInt(0); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
public int describeContents() { |
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
public static final @android.annotation.NonNull |
| 247 |
Parcelable.Creator<StatusBarNotification> CREATOR = |
| 248 |
new Parcelable.Creator<StatusBarNotification>() { |
| 249 |
public StatusBarNotification createFromParcel(Parcel parcel) { |
| 250 |
return new StatusBarNotification(parcel); |
| 251 |
} |
| 252 |
|
| 253 |
public StatusBarNotification[] newArray(int size) { |
| 254 |
return new StatusBarNotification[size]; |
| 255 |
} |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public StatusBarNotification cloneLight() { |
| 262 |
final Notification no = new Notification(); |
| 263 |
this.notification.cloneInto(no, false); |
| 264 |
return cloneShallow(no); |
| 265 |
} |
| 266 |
|
| 267 |
@Override |
| 268 |
public StatusBarNotification clone() { |
| 269 |
return cloneShallow(this.notification.clone()); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
StatusBarNotification cloneShallow(Notification notification) { |
| 277 |
StatusBarNotification result = new StatusBarNotification(this.pkg, this.opPkg, |
| 278 |
this.id, this.tag, this.uid, this.initialPid, |
| 279 |
notification, this.user, this.overrideGroupKey, this.postTime); |
| 280 |
result.setInstanceId(this.mInstanceId); |
| 281 |
return result; |
| 282 |
} |
| 283 |
|
| 284 |
@Override |
| 285 |
public String toString() { |
| 286 |
return formatSimple( |
| 287 |
"StatusBarNotification(pkg=%s user=%s id=%d tag=%s key=%s: %s)", |
| 288 |
this.pkg, this.user, this.id, this.tag, |
| 289 |
this.key, this.notification); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
public boolean isOngoing() { |
| 297 |
return (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public boolean isClearable() { |
| 306 |
return ((notification.flags & Notification.FLAG_ONGOING_EVENT) == 0) |
| 307 |
&& ((notification.flags & Notification.FLAG_NO_CLEAR) == 0); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
@Deprecated |
| 316 |
public int getUserId() { |
| 317 |
return this.user.getIdentifier(); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
public int getNormalizedUserId() { |
| 325 |
int userId = getUserId(); |
| 326 |
if (userId == UserHandle.USER_ALL) { |
| 327 |
userId = UserHandle.USER_SYSTEM; |
| 328 |
} |
| 329 |
return userId; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
public String getPackageName() { |
| 334 |
return pkg; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
public int getId() { |
| 339 |
return id; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
public String getTag() { |
| 347 |
return tag; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public int getUid() { |
| 354 |
return uid; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
public @NonNull String getOpPkg() { |
| 363 |
return opPkg; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
@UnsupportedAppUsage |
| 368 |
public int getInitialPid() { |
| 369 |
return initialPid; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
public Notification getNotification() { |
| 377 |
return notification; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
public UserHandle getUser() { |
| 384 |
return user; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
public long getPostTime() { |
| 392 |
return postTime; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public String getKey() { |
| 399 |
return key; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
public String getGroupKey() { |
| 406 |
return groupKey; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
public String getGroup() { |
| 415 |
if (overrideGroupKey != null) { |
| 416 |
return overrideGroupKey; |
| 417 |
} |
| 418 |
return getNotification().getGroup(); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
public void setOverrideGroupKey(String overrideGroupKey) { |
| 425 |
this.overrideGroupKey = overrideGroupKey; |
| 426 |
groupKey = groupKey(); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
public String getOverrideGroupKey() { |
| 433 |
return overrideGroupKey; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
public void clearPackageContext() { |
| 440 |
mContext = null; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
public InstanceId getInstanceId() { |
| 447 |
return mInstanceId; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public void setInstanceId(InstanceId instanceId) { |
| 454 |
mInstanceId = instanceId; |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 461 |
public Context getPackageContext(Context context) { |
| 462 |
if (mContext == null) { |
| 463 |
try { |
| 464 |
ApplicationInfo ai = context.getPackageManager() |
| 465 |
.getApplicationInfoAsUser(pkg, PackageManager.MATCH_UNINSTALLED_PACKAGES, |
| 466 |
getNormalizedUserId()); |
| 467 |
mContext = context.createApplicationContext(ai, |
| 468 |
Context.CONTEXT_RESTRICTED); |
| 469 |
} catch (PackageManager.NameNotFoundException e) { |
| 470 |
mContext = null; |
| 471 |
} |
| 472 |
} |
| 473 |
if (mContext == null) { |
| 474 |
mContext = context; |
| 475 |
} |
| 476 |
return mContext; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
public LogMaker getLogMaker() { |
| 485 |
LogMaker logMaker = new LogMaker(MetricsEvent.VIEW_UNKNOWN).setPackageName(getPackageName()) |
| 486 |
.addTaggedData(MetricsEvent.NOTIFICATION_ID, getId()) |
| 487 |
.addTaggedData(MetricsEvent.NOTIFICATION_TAG, getTag()) |
| 488 |
.addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID, getChannelIdLogTag()) |
| 489 |
.addTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID, getGroupLogTag()) |
| 490 |
.addTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_SUMMARY, |
| 491 |
getNotification().isGroupSummary() ? 1 : 0) |
| 492 |
.addTaggedData(MetricsProto.MetricsEvent.FIELD_NOTIFICATION_CATEGORY, |
| 493 |
getNotification().category); |
| 494 |
if (getNotification().extras != null) { |
| 495 |
|
| 496 |
|
| 497 |
String template = getNotification().extras.getString(Notification.EXTRA_TEMPLATE); |
| 498 |
if (template != null && !template.isEmpty()) { |
| 499 |
logMaker.addTaggedData(MetricsEvent.FIELD_NOTIFICATION_STYLE, |
| 500 |
template.hashCode()); |
| 501 |
} |
| 502 |
ArrayList<Person> people = getNotification().extras.getParcelableArrayList( |
| 503 |
Notification.EXTRA_PEOPLE_LIST); |
| 504 |
if (people != null && !people.isEmpty()) { |
| 505 |
logMaker.addTaggedData(MetricsEvent.FIELD_NOTIFICATION_PEOPLE, people.size()); |
| 506 |
} |
| 507 |
} |
| 508 |
return logMaker; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
public String getShortcutId() { |
| 515 |
return getNotification().getShortcutId(); |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
public String getGroupLogTag() { |
| 525 |
return shortenTag(getGroup()); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
public String getChannelIdLogTag() { |
| 535 |
if (notification.getChannelId() == null) { |
| 536 |
return null; |
| 537 |
} |
| 538 |
return shortenTag(notification.getChannelId()); |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
private String shortenTag(String logTag) { |
| 546 |
if (logTag == null || logTag.length() <= MAX_LOG_TAG_LENGTH) { |
| 547 |
return logTag; |
| 548 |
} |
| 549 |
String hash = Integer.toHexString(logTag.hashCode()); |
| 550 |
return logTag.substring(0, MAX_LOG_TAG_LENGTH - hash.length() - 1) + "-" |
| 551 |
+ hash; |
| 552 |
} |
| 553 |
} |