1 /* 2 * Copyright (C) 2022 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 17 package android.app.adservices; 18 19 import static android.adservices.common.AdServicesPermissions.ACCESS_ADSERVICES_MANAGER; 20 21 import android.annotation.IntDef; 22 import android.annotation.NonNull; 23 import android.annotation.Nullable; 24 import android.annotation.RequiresPermission; 25 import android.app.adservices.consent.ConsentParcel; 26 import android.app.adservices.topics.TopicParcel; 27 import android.app.sdksandbox.SdkSandboxManager; 28 import android.content.Context; 29 import android.os.Build; 30 import android.os.IBinder; 31 import android.os.RemoteException; 32 33 import androidx.annotation.RequiresApi; 34 35 import com.android.internal.annotations.GuardedBy; 36 import com.android.internal.annotations.VisibleForTesting; 37 38 import java.lang.annotation.Retention; 39 import java.lang.annotation.RetentionPolicy; 40 import java.util.List; 41 import java.util.Objects; 42 43 /** 44 * AdServices Manager to handle the internal communication between PPAPI process and AdServices 45 * System Service. 46 * 47 * @hide 48 */ 49 // TODO(b/269798827): Enable for R. 50 @RequiresApi(Build.VERSION_CODES.S) 51 public final class AdServicesManager { 52 53 public static final String AD_SERVICES_SYSTEM_SERVICE = "adservices_manager"; 54 55 @GuardedBy("SINGLETON_LOCK") 56 private static AdServicesManager sSingleton; 57 58 private final IAdServicesManager mService; 59 private static final Object SINGLETON_LOCK = new Object(); 60 61 @IntDef(value = {MEASUREMENT_DELETION}) 62 @Retention(RetentionPolicy.SOURCE) 63 public @interface DeletionApiType {} 64 65 public static final int MEASUREMENT_DELETION = 0; 66 67 // TODO(b/267789077): Create bit for other APIs. 68 69 @VisibleForTesting AdServicesManager(@onNull IAdServicesManager iAdServicesManager)70 public AdServicesManager(@NonNull IAdServicesManager iAdServicesManager) { 71 Objects.requireNonNull(iAdServicesManager, "AdServicesManager is NULL!"); 72 mService = iAdServicesManager; 73 } 74 75 /** Get the singleton of AdServicesManager. Only used on T+ */ 76 @Nullable getInstance(@onNull Context context)77 public static AdServicesManager getInstance(@NonNull Context context) { 78 synchronized (SINGLETON_LOCK) { 79 if (sSingleton == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { 80 // TODO(b/262282035): Fix this work around in U+. 81 // Get the AdServicesManagerService's Binder from the SdkSandboxManager. 82 // This is a workaround for b/262282035. 83 IBinder iBinder = 84 context.getSystemService(SdkSandboxManager.class).getAdServicesManager(); 85 sSingleton = new AdServicesManager(IAdServicesManager.Stub.asInterface(iBinder)); 86 } 87 } 88 return sSingleton; 89 } 90 91 /** Return the User Consent */ 92 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) getConsent(@onsentParcel.ConsentApiType int consentApiType)93 public ConsentParcel getConsent(@ConsentParcel.ConsentApiType int consentApiType) { 94 try { 95 return mService.getConsent(consentApiType); 96 } catch (RemoteException e) { 97 throw e.rethrowFromSystemServer(); 98 } 99 } 100 101 /** Set the User Consent */ 102 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) setConsent(@onNull ConsentParcel consentParcel)103 public void setConsent(@NonNull ConsentParcel consentParcel) { 104 Objects.requireNonNull(consentParcel); 105 try { 106 mService.setConsent(consentParcel); 107 } catch (RemoteException e) { 108 throw e.rethrowFromSystemServer(); 109 } 110 } 111 112 /** 113 * Saves information to the storage that notification was displayed for the first time to the 114 * user. 115 */ 116 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) recordNotificationDisplayed()117 public void recordNotificationDisplayed() { 118 try { 119 mService.recordNotificationDisplayed(); 120 } catch (RemoteException e) { 121 throw e.rethrowFromSystemServer(); 122 } 123 } 124 125 /** 126 * Returns information whether Consent Notification was displayed or not. 127 * 128 * @return true if Consent Notification was displayed, otherwise false. 129 */ 130 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) wasNotificationDisplayed()131 public boolean wasNotificationDisplayed() { 132 try { 133 return mService.wasNotificationDisplayed(); 134 } catch (RemoteException e) { 135 throw e.rethrowFromSystemServer(); 136 } 137 } 138 139 /** 140 * Saves information to the storage that notification was displayed for the first time to the 141 * user. 142 */ 143 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) recordGaUxNotificationDisplayed()144 public void recordGaUxNotificationDisplayed() { 145 try { 146 mService.recordGaUxNotificationDisplayed(); 147 } catch (RemoteException e) { 148 throw e.rethrowFromSystemServer(); 149 } 150 } 151 152 /** 153 * Returns information whether user interacted with consent manually. 154 * 155 * @return 156 * <ul> 157 * <li>-1 when no manual interaction was recorded 158 * <li>0 when no data about interaction (similar to null) 159 * <li>1 when manual interaction was recorded 160 * </ul> 161 */ 162 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) getUserManualInteractionWithConsent()163 public int getUserManualInteractionWithConsent() { 164 try { 165 return mService.getUserManualInteractionWithConsent(); 166 } catch (RemoteException e) { 167 throw e.rethrowFromSystemServer(); 168 } 169 } 170 171 /** Saves information to the storage that user interacted with consent manually. */ 172 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) recordUserManualInteractionWithConsent(int interaction)173 public void recordUserManualInteractionWithConsent(int interaction) { 174 try { 175 mService.recordUserManualInteractionWithConsent(interaction); 176 } catch (RemoteException e) { 177 throw e.rethrowFromSystemServer(); 178 } 179 } 180 181 /** 182 * Returns information whether Consent GA UX Notification was displayed or not. 183 * 184 * @return true if Consent GA UX Notification was displayed, otherwise false. 185 */ 186 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) wasGaUxNotificationDisplayed()187 public boolean wasGaUxNotificationDisplayed() { 188 try { 189 return mService.wasGaUxNotificationDisplayed(); 190 } catch (RemoteException e) { 191 throw e.rethrowFromSystemServer(); 192 } 193 } 194 195 /** 196 * Record a blocked topic. 197 * 198 * @param blockedTopicParcels the blocked topic to record 199 */ 200 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) recordBlockedTopic(@onNull List<TopicParcel> blockedTopicParcels)201 public void recordBlockedTopic(@NonNull List<TopicParcel> blockedTopicParcels) { 202 try { 203 mService.recordBlockedTopic(blockedTopicParcels); 204 } catch (RemoteException e) { 205 throw e.rethrowFromSystemServer(); 206 } 207 } 208 209 /** 210 * Remove a blocked topic. 211 * 212 * @param blockedTopicParcel the blocked topic to remove 213 */ 214 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) removeBlockedTopic(@onNull TopicParcel blockedTopicParcel)215 public void removeBlockedTopic(@NonNull TopicParcel blockedTopicParcel) { 216 try { 217 mService.removeBlockedTopic(blockedTopicParcel); 218 } catch (RemoteException e) { 219 throw e.rethrowFromSystemServer(); 220 } 221 } 222 223 /** 224 * Get all blocked topics. 225 * 226 * @return a {@code List} of all blocked topics. 227 */ 228 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) retrieveAllBlockedTopics()229 public List<TopicParcel> retrieveAllBlockedTopics() { 230 try { 231 return mService.retrieveAllBlockedTopics(); 232 } catch (RemoteException e) { 233 throw e.rethrowFromSystemServer(); 234 } 235 } 236 237 /** Clear all Blocked Topics */ 238 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) clearAllBlockedTopics()239 public void clearAllBlockedTopics() { 240 try { 241 mService.clearAllBlockedTopics(); 242 } catch (RemoteException e) { 243 throw e.rethrowFromSystemServer(); 244 } 245 } 246 247 /** Returns the list of apps with consent. */ 248 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) getKnownAppsWithConsent(List<String> installedPackages)249 public List<String> getKnownAppsWithConsent(List<String> installedPackages) { 250 try { 251 return mService.getKnownAppsWithConsent(installedPackages); 252 } catch (RemoteException e) { 253 throw e.rethrowFromSystemServer(); 254 } 255 } 256 257 /** Returns the list of apps with revoked consent. */ 258 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) getAppsWithRevokedConsent(List<String> installedPackages)259 public List<String> getAppsWithRevokedConsent(List<String> installedPackages) { 260 try { 261 return mService.getAppsWithRevokedConsent(installedPackages); 262 } catch (RemoteException e) { 263 throw e.rethrowFromSystemServer(); 264 } 265 } 266 267 /** Set user consent for an app */ 268 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) setConsentForApp(String packageName, int packageUid, boolean isConsentRevoked)269 public void setConsentForApp(String packageName, int packageUid, boolean isConsentRevoked) { 270 try { 271 mService.setConsentForApp(packageName, packageUid, isConsentRevoked); 272 } catch (RemoteException e) { 273 throw e.rethrowFromSystemServer(); 274 } 275 } 276 277 /** Reset all apps and blocked apps. */ 278 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) clearKnownAppsWithConsent()279 public void clearKnownAppsWithConsent() { 280 try { 281 mService.clearKnownAppsWithConsent(); 282 } catch (RemoteException e) { 283 throw e.rethrowFromSystemServer(); 284 } 285 } 286 287 /** Reset all apps consent. */ 288 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) clearAllAppConsentData()289 public void clearAllAppConsentData() { 290 try { 291 mService.clearAllAppConsentData(); 292 } catch (RemoteException e) { 293 throw e.rethrowFromSystemServer(); 294 } 295 } 296 297 /** 298 * Get if user consent is revoked for a given app. 299 * 300 * @return {@code true} if the user consent was revoked. 301 */ 302 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) isConsentRevokedForApp(String packageName, int packageUid)303 public boolean isConsentRevokedForApp(String packageName, int packageUid) { 304 try { 305 return mService.isConsentRevokedForApp(packageName, packageUid); 306 } catch (RemoteException e) { 307 throw e.rethrowFromSystemServer(); 308 } 309 } 310 311 /** 312 * Set user consent if the app first time request access and/or return consent value for the 313 * app. 314 * 315 * @return {@code true} if user consent was given. 316 */ 317 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) setConsentForAppIfNew( String packageName, int packageUid, boolean isConsentRevoked)318 public boolean setConsentForAppIfNew( 319 String packageName, int packageUid, boolean isConsentRevoked) { 320 try { 321 return mService.setConsentForAppIfNew(packageName, packageUid, isConsentRevoked); 322 } catch (RemoteException e) { 323 throw e.rethrowFromSystemServer(); 324 } 325 } 326 327 /** Clear the app consent entry for uninstalled app. */ 328 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) clearConsentForUninstalledApp(String packageName, int packageUid)329 public void clearConsentForUninstalledApp(String packageName, int packageUid) { 330 try { 331 mService.clearConsentForUninstalledApp(packageName, packageUid); 332 } catch (RemoteException e) { 333 throw e.rethrowFromSystemServer(); 334 } 335 } 336 337 /** Saves information to the storage that a deletion of measurement data occurred. */ 338 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) recordAdServicesDeletionOccurred(@eletionApiType int deletionType)339 public void recordAdServicesDeletionOccurred(@DeletionApiType int deletionType) { 340 try { 341 mService.recordAdServicesDeletionOccurred(deletionType); 342 } catch (RemoteException e) { 343 throw e.rethrowFromSystemServer(); 344 } 345 } 346 347 /** Saves the PP API default consent of a user. */ 348 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) recordDefaultConsent(boolean defaultConsent)349 public void recordDefaultConsent(boolean defaultConsent) { 350 try { 351 mService.recordDefaultConsent(defaultConsent); 352 } catch (RemoteException e) { 353 throw e.rethrowFromSystemServer(); 354 } 355 } 356 357 /** Saves the topics default consent of a user. */ 358 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) recordTopicsDefaultConsent(boolean defaultConsent)359 public void recordTopicsDefaultConsent(boolean defaultConsent) { 360 try { 361 mService.recordTopicsDefaultConsent(defaultConsent); 362 } catch (RemoteException e) { 363 throw e.rethrowFromSystemServer(); 364 } 365 } 366 367 /** Saves the FLEDGE default consent of a user. */ 368 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) recordFledgeDefaultConsent(boolean defaultConsent)369 public void recordFledgeDefaultConsent(boolean defaultConsent) { 370 try { 371 mService.recordFledgeDefaultConsent(defaultConsent); 372 } catch (RemoteException e) { 373 throw e.rethrowFromSystemServer(); 374 } 375 } 376 377 /** Saves the measurement default consent of a user. */ 378 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) recordMeasurementDefaultConsent(boolean defaultConsent)379 public void recordMeasurementDefaultConsent(boolean defaultConsent) { 380 try { 381 mService.recordMeasurementDefaultConsent(defaultConsent); 382 } catch (RemoteException e) { 383 throw e.rethrowFromSystemServer(); 384 } 385 } 386 387 /** Saves the default AdId state of a user. */ 388 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) recordDefaultAdIdState(boolean defaultAdIdState)389 public void recordDefaultAdIdState(boolean defaultAdIdState) { 390 try { 391 mService.recordDefaultAdIdState(defaultAdIdState); 392 } catch (RemoteException e) { 393 throw e.rethrowFromSystemServer(); 394 } 395 } 396 397 /** 398 * Checks whether the AdServices module needs to handle data reconciliation after a rollback. 399 */ 400 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) needsToHandleRollbackReconciliation(@eletionApiType int deletionType)401 public boolean needsToHandleRollbackReconciliation(@DeletionApiType int deletionType) { 402 try { 403 return mService.needsToHandleRollbackReconciliation(deletionType); 404 } catch (RemoteException e) { 405 throw e.rethrowFromSystemServer(); 406 } 407 } 408 409 /** 410 * Returns the PP API default consent of a user. 411 * 412 * @return true if the PP API default consent is given, false otherwise. 413 */ 414 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) getDefaultConsent()415 public boolean getDefaultConsent() { 416 try { 417 return mService.getDefaultConsent(); 418 } catch (RemoteException e) { 419 throw e.rethrowFromSystemServer(); 420 } 421 } 422 423 /** 424 * Returns the topics default consent of a user. 425 * 426 * @return true if the topics default consent is given, false otherwise. 427 */ 428 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) getTopicsDefaultConsent()429 public boolean getTopicsDefaultConsent() { 430 try { 431 return mService.getTopicsDefaultConsent(); 432 } catch (RemoteException e) { 433 throw e.rethrowFromSystemServer(); 434 } 435 } 436 437 /** 438 * Returns the FLEDGE default consent of a user. 439 * 440 * @return true if the FLEDGE default consent is given, false otherwise. 441 */ 442 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) getFledgeDefaultConsent()443 public boolean getFledgeDefaultConsent() { 444 try { 445 return mService.getFledgeDefaultConsent(); 446 } catch (RemoteException e) { 447 throw e.rethrowFromSystemServer(); 448 } 449 } 450 451 /** 452 * Returns the measurement default consent of a user. 453 * 454 * @return true if the measurement default consent is given, false otherwise. 455 */ 456 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) getMeasurementDefaultConsent()457 public boolean getMeasurementDefaultConsent() { 458 try { 459 return mService.getMeasurementDefaultConsent(); 460 } catch (RemoteException e) { 461 throw e.rethrowFromSystemServer(); 462 } 463 } 464 465 /** 466 * Returns the default AdId state of a user. 467 * 468 * @return true if the default AdId State is enabled, false otherwise. 469 */ 470 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) getDefaultAdIdState()471 public boolean getDefaultAdIdState() { 472 try { 473 return mService.getDefaultAdIdState(); 474 } catch (RemoteException e) { 475 throw e.rethrowFromSystemServer(); 476 } 477 } 478 479 /** Returns the current privacy sandbox feature. */ 480 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) getCurrentPrivacySandboxFeature()481 public String getCurrentPrivacySandboxFeature() { 482 try { 483 return mService.getCurrentPrivacySandboxFeature(); 484 } catch (RemoteException e) { 485 throw e.rethrowFromSystemServer(); 486 } 487 } 488 489 /** Set the current privacy sandbox feature. */ 490 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) setCurrentPrivacySandboxFeature(String featureType)491 public void setCurrentPrivacySandboxFeature(String featureType) { 492 try { 493 mService.setCurrentPrivacySandboxFeature(featureType); 494 } catch (RemoteException e) { 495 throw e.rethrowFromSystemServer(); 496 } 497 } 498 499 /** Returns whether the isAdIdEnabled bit is true. */ 500 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) isAdIdEnabled()501 public boolean isAdIdEnabled() { 502 try { 503 return mService.isAdIdEnabled(); 504 } catch (RemoteException e) { 505 throw e.rethrowFromSystemServer(); 506 } 507 } 508 509 /** Saves the isAdIdEnabled bit. */ 510 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) setAdIdEnabled(boolean isAdIdEnabled)511 public void setAdIdEnabled(boolean isAdIdEnabled) { 512 try { 513 mService.setAdIdEnabled(isAdIdEnabled); 514 } catch (RemoteException e) { 515 throw e.rethrowFromSystemServer(); 516 } 517 } 518 519 /** Returns whether the isU18Account bit is true. */ 520 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) isU18Account()521 public boolean isU18Account() { 522 try { 523 return mService.isU18Account(); 524 } catch (RemoteException e) { 525 throw e.rethrowFromSystemServer(); 526 } 527 } 528 529 /** Saves the isU18Account bit. */ 530 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) setU18Account(boolean isU18Account)531 public void setU18Account(boolean isU18Account) { 532 try { 533 mService.setU18Account(isU18Account); 534 } catch (RemoteException e) { 535 throw e.rethrowFromSystemServer(); 536 } 537 } 538 539 /** Returns whether the isEntryPointEnabled bit is true. */ 540 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) isEntryPointEnabled()541 public boolean isEntryPointEnabled() { 542 try { 543 return mService.isEntryPointEnabled(); 544 } catch (RemoteException e) { 545 throw e.rethrowFromSystemServer(); 546 } 547 } 548 549 /** Saves the isEntryPointEnabled bit. */ 550 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) setEntryPointEnabled(boolean isEntryPointEnabled)551 public void setEntryPointEnabled(boolean isEntryPointEnabled) { 552 try { 553 mService.setEntryPointEnabled(isEntryPointEnabled); 554 } catch (RemoteException e) { 555 throw e.rethrowFromSystemServer(); 556 } 557 } 558 559 /** Returns whether the isAdultAccount bit is true. */ 560 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) isAdultAccount()561 public boolean isAdultAccount() { 562 try { 563 return mService.isAdultAccount(); 564 } catch (RemoteException e) { 565 throw e.rethrowFromSystemServer(); 566 } 567 } 568 569 /** Saves the isAdultAccount bit. */ 570 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) setAdultAccount(boolean isAdultAccount)571 public void setAdultAccount(boolean isAdultAccount) { 572 try { 573 mService.setAdultAccount(isAdultAccount); 574 } catch (RemoteException e) { 575 throw e.rethrowFromSystemServer(); 576 } 577 } 578 579 /** Returns whether the wasU18NotificationDisplayed bit is true. */ 580 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) wasU18NotificationDisplayed()581 public boolean wasU18NotificationDisplayed() { 582 try { 583 return mService.wasU18NotificationDisplayed(); 584 } catch (RemoteException e) { 585 throw e.rethrowFromSystemServer(); 586 } 587 } 588 589 /** Saves the wasU18NotificationDisplayed bit. */ 590 @RequiresPermission(ACCESS_ADSERVICES_MANAGER) setU18NotificationDisplayed(boolean wasU18NotificationDisplayed)591 public void setU18NotificationDisplayed(boolean wasU18NotificationDisplayed) { 592 try { 593 mService.setU18NotificationDisplayed(wasU18NotificationDisplayed); 594 } catch (RemoteException e) { 595 throw e.rethrowFromSystemServer(); 596 } 597 } 598 } 599