| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.RequiresPermission; |
| 22 |
import android.annotation.SystemApi; |
| 23 |
import android.annotation.TestApi; |
| 24 |
import android.app.ActivityThread; |
| 25 |
import android.app.AppGlobals; |
| 26 |
import android.os.Binder; |
| 27 |
import android.os.Build; |
| 28 |
import android.os.IBinder; |
| 29 |
import android.os.Parcel; |
| 30 |
import android.os.Parcelable; |
| 31 |
import android.os.Process; |
| 32 |
import android.os.UserHandle; |
| 33 |
import android.permission.PermissionManager; |
| 34 |
import android.util.ArraySet; |
| 35 |
|
| 36 |
import com.android.internal.annotations.Immutable; |
| 37 |
|
| 38 |
import java.util.Arrays; |
| 39 |
import java.util.Collections; |
| 40 |
import java.util.Objects; |
| 41 |
import java.util.Set; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
@Immutable |
| 90 |
public final class AttributionSource implements Parcelable { |
| 91 |
private static final String DESCRIPTOR = "android.content.AttributionSource"; |
| 92 |
|
| 93 |
private static final Binder sDefaultToken = new Binder(DESCRIPTOR); |
| 94 |
|
| 95 |
private final @NonNull AttributionSourceState mAttributionSourceState; |
| 96 |
|
| 97 |
private @Nullable AttributionSource mNextCached; |
| 98 |
private @Nullable Set<String> mRenouncedPermissionsCached; |
| 99 |
|
| 100 |
|
| 101 |
@TestApi |
| 102 |
public AttributionSource(int uid, @Nullable String packageName, |
| 103 |
@Nullable String attributionTag) { |
| 104 |
this(uid, packageName, attributionTag, sDefaultToken); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
@TestApi |
| 109 |
public AttributionSource(int uid, @Nullable String packageName, |
| 110 |
@Nullable String attributionTag, @NonNull IBinder token) { |
| 111 |
this(uid, packageName, attributionTag, token, null, |
| 112 |
null); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
public AttributionSource(int uid, @Nullable String packageName, |
| 117 |
@Nullable String attributionTag, @NonNull IBinder token, |
| 118 |
@Nullable AttributionSource next) { |
| 119 |
this(uid, packageName, attributionTag, token, null, next); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
@TestApi |
| 124 |
public AttributionSource(int uid, @Nullable String packageName, |
| 125 |
@Nullable String attributionTag, @Nullable Set<String> renouncedPermissions, |
| 126 |
@Nullable AttributionSource next) { |
| 127 |
this(uid, packageName, attributionTag, (renouncedPermissions != null) |
| 128 |
? renouncedPermissions.toArray(new String[0]) : null, next); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
public AttributionSource(@NonNull AttributionSource current, @Nullable AttributionSource next) { |
| 133 |
this(current.getUid(), current.getPackageName(), current.getAttributionTag(), |
| 134 |
current.getToken(), current.mAttributionSourceState.renouncedPermissions, next); |
| 135 |
} |
| 136 |
|
| 137 |
AttributionSource(int uid, @Nullable String packageName, @Nullable String attributionTag, |
| 138 |
@Nullable String[] renouncedPermissions, @Nullable AttributionSource next) { |
| 139 |
this(uid, packageName, attributionTag, sDefaultToken, renouncedPermissions, next); |
| 140 |
} |
| 141 |
|
| 142 |
AttributionSource(int uid, @Nullable String packageName, @Nullable String attributionTag, |
| 143 |
@NonNull IBinder token, @Nullable String[] renouncedPermissions, |
| 144 |
@Nullable AttributionSource next) { |
| 145 |
mAttributionSourceState = new AttributionSourceState(); |
| 146 |
mAttributionSourceState.uid = uid; |
| 147 |
mAttributionSourceState.token = token; |
| 148 |
mAttributionSourceState.packageName = packageName; |
| 149 |
mAttributionSourceState.attributionTag = attributionTag; |
| 150 |
mAttributionSourceState.renouncedPermissions = renouncedPermissions; |
| 151 |
mAttributionSourceState.next = (next != null) ? new AttributionSourceState[] |
| 152 |
{next.mAttributionSourceState} : new AttributionSourceState[0]; |
| 153 |
} |
| 154 |
|
| 155 |
AttributionSource(@NonNull Parcel in) { |
| 156 |
this(AttributionSourceState.CREATOR.createFromParcel(in)); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
enforceCallingUidAndPid(); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
public AttributionSource(@NonNull AttributionSourceState attributionSourceState) { |
| 165 |
mAttributionSourceState = attributionSourceState; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
public AttributionSource withNextAttributionSource(@Nullable AttributionSource next) { |
| 170 |
return new AttributionSource(getUid(), getPackageName(), getAttributionTag(), |
| 171 |
mAttributionSourceState.renouncedPermissions, next); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
public AttributionSource withPackageName(@Nullable String packageName) { |
| 176 |
return new AttributionSource(getUid(), packageName, getAttributionTag(), |
| 177 |
mAttributionSourceState.renouncedPermissions, getNext()); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
public AttributionSource withToken(@NonNull Binder token) { |
| 182 |
return new AttributionSource(getUid(), getPackageName(), getAttributionTag(), |
| 183 |
token, mAttributionSourceState.renouncedPermissions, getNext()); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
public @NonNull AttributionSourceState asState() { |
| 188 |
return mAttributionSourceState; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
public @NonNull ScopedParcelState asScopedParcelState() { |
| 193 |
return new ScopedParcelState(this); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public static @NonNull AttributionSource myAttributionSource() { |
| 214 |
|
| 215 |
final AttributionSource globalSource = ActivityThread.currentAttributionSource(); |
| 216 |
if (globalSource != null) { |
| 217 |
return globalSource; |
| 218 |
} |
| 219 |
|
| 220 |
int uid = Process.myUid(); |
| 221 |
if (uid == Process.ROOT_UID) { |
| 222 |
uid = Process.SYSTEM_UID; |
| 223 |
} |
| 224 |
try { |
| 225 |
return new AttributionSource.Builder(uid) |
| 226 |
.setPackageName(AppGlobals.getPackageManager().getPackagesForUid(uid)[0]) |
| 227 |
.build(); |
| 228 |
} catch (Exception ignored) { |
| 229 |
} |
| 230 |
|
| 231 |
throw new IllegalStateException("Failed to resolve AttributionSource"); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public static class ScopedParcelState implements AutoCloseable { |
| 245 |
private final Parcel mParcel; |
| 246 |
|
| 247 |
public @NonNull Parcel getParcel() { |
| 248 |
return mParcel; |
| 249 |
} |
| 250 |
|
| 251 |
public ScopedParcelState(AttributionSource attributionSource) { |
| 252 |
mParcel = Parcel.obtain(); |
| 253 |
attributionSource.writeToParcel(mParcel, 0); |
| 254 |
mParcel.setDataPosition(0); |
| 255 |
} |
| 256 |
|
| 257 |
public void close() { |
| 258 |
mParcel.recycle(); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
private void enforceCallingUidAndPid() { |
| 270 |
enforceCallingUid(); |
| 271 |
enforceCallingPid(); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public void enforceCallingUid() { |
| 283 |
if (!checkCallingUid()) { |
| 284 |
throw new SecurityException("Calling uid: " + Binder.getCallingUid() |
| 285 |
+ " doesn't match source uid: " + mAttributionSourceState.uid); |
| 286 |
} |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public boolean checkCallingUid() { |
| 299 |
final int callingUid = Binder.getCallingUid(); |
| 300 |
if (callingUid != Process.ROOT_UID |
| 301 |
&& UserHandle.getAppId(callingUid) != Process.SYSTEM_UID |
| 302 |
&& callingUid != mAttributionSourceState.uid) { |
| 303 |
return false; |
| 304 |
} |
| 305 |
|
| 306 |
return true; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
@TestApi |
| 316 |
public void enforceCallingPid() { |
| 317 |
if (!checkCallingPid()) { |
| 318 |
throw new SecurityException("Calling pid: " + Binder.getCallingPid() |
| 319 |
+ " doesn't match source pid: " + mAttributionSourceState.pid); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
private boolean checkCallingPid() { |
| 329 |
final int callingPid = Binder.getCallingPid(); |
| 330 |
if (mAttributionSourceState.pid != -1 && callingPid != mAttributionSourceState.pid) { |
| 331 |
return false; |
| 332 |
} |
| 333 |
return true; |
| 334 |
} |
| 335 |
|
| 336 |
@Override |
| 337 |
public String toString() { |
| 338 |
if (Build.IS_DEBUGGABLE) { |
| 339 |
return "AttributionSource { " + |
| 340 |
"uid = " + mAttributionSourceState.uid + ", " + |
| 341 |
"packageName = " + mAttributionSourceState.packageName + ", " + |
| 342 |
"attributionTag = " + mAttributionSourceState.attributionTag + ", " + |
| 343 |
"token = " + mAttributionSourceState.token + ", " + |
| 344 |
"next = " + (mAttributionSourceState.next != null |
| 345 |
&& mAttributionSourceState.next.length > 0 |
| 346 |
? mAttributionSourceState.next[0] : null) + |
| 347 |
" }"; |
| 348 |
} |
| 349 |
return super.toString(); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
public int getNextUid() { |
| 358 |
if (mAttributionSourceState.next != null |
| 359 |
&& mAttributionSourceState.next.length > 0) { |
| 360 |
return mAttributionSourceState.next[0].uid; |
| 361 |
} |
| 362 |
return Process.INVALID_UID; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
public @Nullable String getNextPackageName() { |
| 371 |
if (mAttributionSourceState.next != null |
| 372 |
&& mAttributionSourceState.next.length > 0) { |
| 373 |
return mAttributionSourceState.next[0].packageName; |
| 374 |
} |
| 375 |
return null; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
public @Nullable String getNextAttributionTag() { |
| 385 |
if (mAttributionSourceState.next != null |
| 386 |
&& mAttributionSourceState.next.length > 0) { |
| 387 |
return mAttributionSourceState.next[0].attributionTag; |
| 388 |
} |
| 389 |
return null; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public @Nullable IBinder getNextToken() { |
| 399 |
if (mAttributionSourceState.next != null |
| 400 |
&& mAttributionSourceState.next.length > 0) { |
| 401 |
return mAttributionSourceState.next[0].token; |
| 402 |
} |
| 403 |
return null; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
public boolean isTrusted(@NonNull Context context) { |
| 414 |
return mAttributionSourceState.token != null |
| 415 |
&& context.getSystemService(PermissionManager.class) |
| 416 |
.isRegisteredAttributionSource(this); |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
@SystemApi |
| 425 |
@RequiresPermission(android.Manifest.permission.RENOUNCE_PERMISSIONS) |
| 426 |
@NonNull |
| 427 |
public Set<String> getRenouncedPermissions() { |
| 428 |
if (mRenouncedPermissionsCached == null) { |
| 429 |
if (mAttributionSourceState.renouncedPermissions != null) { |
| 430 |
mRenouncedPermissionsCached = new ArraySet<>( |
| 431 |
mAttributionSourceState.renouncedPermissions); |
| 432 |
} else { |
| 433 |
mRenouncedPermissionsCached = Collections.emptySet(); |
| 434 |
} |
| 435 |
} |
| 436 |
return mRenouncedPermissionsCached; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public int getUid() { |
| 443 |
return mAttributionSourceState.uid; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
public @Nullable String getPackageName() { |
| 450 |
return mAttributionSourceState.packageName; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
public @Nullable String getAttributionTag() { |
| 457 |
return mAttributionSourceState.attributionTag; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public @NonNull IBinder getToken() { |
| 466 |
return mAttributionSourceState.token; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
public @Nullable AttributionSource getNext() { |
| 473 |
if (mNextCached == null && mAttributionSourceState.next != null |
| 474 |
&& mAttributionSourceState.next.length > 0) { |
| 475 |
mNextCached = new AttributionSource(mAttributionSourceState.next[0]); |
| 476 |
} |
| 477 |
return mNextCached; |
| 478 |
} |
| 479 |
|
| 480 |
@Override |
| 481 |
public boolean equals(@Nullable Object o) { |
| 482 |
if (this == o) return true; |
| 483 |
if (o == null || getClass() != o.getClass()) return false; |
| 484 |
AttributionSource that = (AttributionSource) o; |
| 485 |
return mAttributionSourceState.uid == that.mAttributionSourceState.uid |
| 486 |
&& Objects.equals(mAttributionSourceState.packageName, |
| 487 |
that.mAttributionSourceState.packageName) |
| 488 |
&& Objects.equals(mAttributionSourceState.attributionTag, |
| 489 |
that.mAttributionSourceState.attributionTag) |
| 490 |
&& Objects.equals(mAttributionSourceState.token, |
| 491 |
that.mAttributionSourceState.token) |
| 492 |
&& Arrays.equals(mAttributionSourceState.renouncedPermissions, |
| 493 |
that.mAttributionSourceState.renouncedPermissions) |
| 494 |
&& Objects.equals(getNext(), that.getNext()); |
| 495 |
} |
| 496 |
|
| 497 |
@Override |
| 498 |
public int hashCode() { |
| 499 |
return Objects.hash(mAttributionSourceState.uid, mAttributionSourceState.packageName, |
| 500 |
mAttributionSourceState.attributionTag, mAttributionSourceState.token, |
| 501 |
Arrays.hashCode(mAttributionSourceState.renouncedPermissions), getNext()); |
| 502 |
} |
| 503 |
|
| 504 |
@Override |
| 505 |
public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 506 |
mAttributionSourceState.writeToParcel(dest, flags); |
| 507 |
} |
| 508 |
|
| 509 |
@Override |
| 510 |
public int describeContents() { return 0; } |
| 511 |
|
| 512 |
public static final @NonNull Parcelable.Creator<AttributionSource> CREATOR |
| 513 |
= new Parcelable.Creator<AttributionSource>() { |
| 514 |
@Override |
| 515 |
public AttributionSource[] newArray(int size) { |
| 516 |
return new AttributionSource[size]; |
| 517 |
} |
| 518 |
|
| 519 |
@Override |
| 520 |
public AttributionSource createFromParcel(@NonNull Parcel in) { |
| 521 |
return new AttributionSource(in); |
| 522 |
} |
| 523 |
}; |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
public static final class Builder { |
| 529 |
private @NonNull final AttributionSourceState mAttributionSourceState = |
| 530 |
new AttributionSourceState(); |
| 531 |
|
| 532 |
private long mBuilderFieldsSet = 0L; |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
public Builder(int uid) { |
| 541 |
mAttributionSourceState.uid = uid; |
| 542 |
} |
| 543 |
|
| 544 |
public Builder(@NonNull AttributionSource current) { |
| 545 |
if (current == null) { |
| 546 |
throw new IllegalArgumentException("current AttributionSource can not be null"); |
| 547 |
} |
| 548 |
mAttributionSourceState.uid = current.getUid(); |
| 549 |
mAttributionSourceState.packageName = current.getPackageName(); |
| 550 |
mAttributionSourceState.attributionTag = current.getAttributionTag(); |
| 551 |
mAttributionSourceState.token = current.getToken(); |
| 552 |
mAttributionSourceState.renouncedPermissions = |
| 553 |
current.mAttributionSourceState.renouncedPermissions; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
public @NonNull Builder setPackageName(@Nullable String value) { |
| 560 |
checkNotUsed(); |
| 561 |
mBuilderFieldsSet |= 0x2; |
| 562 |
mAttributionSourceState.packageName = value; |
| 563 |
return this; |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
public @NonNull Builder setAttributionTag(@Nullable String value) { |
| 570 |
checkNotUsed(); |
| 571 |
mBuilderFieldsSet |= 0x4; |
| 572 |
mAttributionSourceState.attributionTag = value; |
| 573 |
return this; |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
@SystemApi |
| 601 |
@RequiresPermission(android.Manifest.permission.RENOUNCE_PERMISSIONS) |
| 602 |
public @NonNull Builder setRenouncedPermissions(@Nullable Set<String> value) { |
| 603 |
checkNotUsed(); |
| 604 |
mBuilderFieldsSet |= 0x8; |
| 605 |
mAttributionSourceState.renouncedPermissions = (value != null) |
| 606 |
? value.toArray(new String[0]) : null; |
| 607 |
return this; |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
public @NonNull Builder setNext(@Nullable AttributionSource value) { |
| 614 |
checkNotUsed(); |
| 615 |
mBuilderFieldsSet |= 0x10; |
| 616 |
mAttributionSourceState.next = (value != null) ? new AttributionSourceState[] |
| 617 |
{value.mAttributionSourceState} : mAttributionSourceState.next; |
| 618 |
return this; |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
public @NonNull AttributionSource build() { |
| 623 |
checkNotUsed(); |
| 624 |
mBuilderFieldsSet |= 0x40; |
| 625 |
|
| 626 |
if ((mBuilderFieldsSet & 0x2) == 0) { |
| 627 |
mAttributionSourceState.packageName = null; |
| 628 |
} |
| 629 |
if ((mBuilderFieldsSet & 0x4) == 0) { |
| 630 |
mAttributionSourceState.attributionTag = null; |
| 631 |
} |
| 632 |
if ((mBuilderFieldsSet & 0x8) == 0) { |
| 633 |
mAttributionSourceState.renouncedPermissions = null; |
| 634 |
} |
| 635 |
if ((mBuilderFieldsSet & 0x10) == 0) { |
| 636 |
mAttributionSourceState.next = null; |
| 637 |
} |
| 638 |
|
| 639 |
mAttributionSourceState.token = sDefaultToken; |
| 640 |
|
| 641 |
if (mAttributionSourceState.next == null) { |
| 642 |
|
| 643 |
mAttributionSourceState.next = new AttributionSourceState[0]; |
| 644 |
} |
| 645 |
return new AttributionSource(mAttributionSourceState); |
| 646 |
} |
| 647 |
|
| 648 |
private void checkNotUsed() { |
| 649 |
if ((mBuilderFieldsSet & 0x40) != 0) { |
| 650 |
throw new IllegalStateException( |
| 651 |
"This Builder should not be reused. Use a new Builder instance instead"); |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
} |