summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Raphael Kim <raphk@google.com> 2023-11-29 13:06:51 -0800
committer Raphael Kim <raphk@google.com> 2024-01-02 15:55:35 -0800
commit0d2ab6ab9702bede09a9b5acda16431d1255fbfa (patch)
treee830e9facf24d56cc3c6c3e3267c560d3ecac28c
parent2e0beb067dcc9588a700c73b8401c922e5bdb7f8 (diff)
[CDM] Resolve callback for adding role holder for an association even if device profile is null.
Action will succeed as NO-OP without granting any role. Bug: 303263276 Test: atest CtsCompanionDeviceManagerCoreTestCases Change-Id: I31496fb83677ce471b7355affd493c3313d24cff
-rw-r--r--services/companion/java/com/android/server/companion/AssociationRequestsProcessor.java46
-rw-r--r--services/companion/java/com/android/server/companion/RolesUtils.java17
2 files changed, 39 insertions, 24 deletions
diff --git a/services/companion/java/com/android/server/companion/AssociationRequestsProcessor.java b/services/companion/java/com/android/server/companion/AssociationRequestsProcessor.java
index 69647633eaff..10924a4c8fb7 100644
--- a/services/companion/java/com/android/server/companion/AssociationRequestsProcessor.java
+++ b/services/companion/java/com/android/server/companion/AssociationRequestsProcessor.java
@@ -285,32 +285,33 @@ class AssociationRequestsProcessor {
selfManaged, /* notifyOnDeviceNearby */ false, /* revoked */ false,
timestamp, Long.MAX_VALUE, /* systemDataSyncFlags */ 0);
- if (deviceProfile != null) {
- // If the "Device Profile" is specified, make the companion application a holder of the
- // corresponding role.
- addRoleHolderForAssociation(mService.getContext(), association, success -> {
- if (success) {
- addAssociationToStore(association, deviceProfile);
-
- sendCallbackAndFinish(association, callback, resultReceiver);
- } else {
- Slog.e(TAG, "Failed to add u" + userId + "\\" + packageName
- + " to the list of " + deviceProfile + " holders.");
-
- sendCallbackAndFinish(null, callback, resultReceiver);
- }
- });
- } else {
- addAssociationToStore(association, null);
-
- sendCallbackAndFinish(association, callback, resultReceiver);
- }
+ // Add role holder for association (if specified) and add new association to store.
+ maybeGrantRoleAndStoreAssociation(association, callback, resultReceiver);
// Don't need to update the mRevokedAssociationsPendingRoleHolderRemoval since
// maybeRemoveRoleHolderForAssociation in PackageInactivityListener will handle the case
// that there are other devices with the same profile, so the role holder won't be removed.
}
+ public void maybeGrantRoleAndStoreAssociation(@NonNull AssociationInfo association,
+ @Nullable IAssociationRequestCallback callback,
+ @Nullable ResultReceiver resultReceiver) {
+ // If the "Device Profile" is specified, make the companion application a holder of the
+ // corresponding role.
+ // If it is null, then the operation will succeed without granting any role.
+ addRoleHolderForAssociation(mService.getContext(), association, success -> {
+ if (success) {
+ addAssociationToStore(association);
+ sendCallbackAndFinish(association, callback, resultReceiver);
+ } else {
+ Slog.e(TAG, "Failed to add u" + association.getUserId()
+ + "\\" + association.getPackageName()
+ + " to the list of " + association.getDeviceProfile() + " holders.");
+ sendCallbackAndFinish(null, callback, resultReceiver);
+ }
+ });
+ }
+
public void enableSystemDataSync(int associationId, int flags) {
AssociationInfo association = mAssociationStore.getAssociationById(associationId);
AssociationInfo updated = (new AssociationInfo.Builder(association))
@@ -325,15 +326,14 @@ class AssociationRequestsProcessor {
mAssociationStore.updateAssociation(updated);
}
- private void addAssociationToStore(@NonNull AssociationInfo association,
- @Nullable String deviceProfile) {
+ private void addAssociationToStore(@NonNull AssociationInfo association) {
Slog.i(TAG, "New CDM association created=" + association);
mAssociationStore.addAssociation(association);
mService.updateSpecialAccessPermissionForAssociatedPackage(association);
- logCreateAssociation(deviceProfile);
+ logCreateAssociation(association.getDeviceProfile());
}
private void sendCallbackAndFinish(@Nullable AssociationInfo association,
diff --git a/services/companion/java/com/android/server/companion/RolesUtils.java b/services/companion/java/com/android/server/companion/RolesUtils.java
index 163f614fb65d..af9d2d783100 100644
--- a/services/companion/java/com/android/server/companion/RolesUtils.java
+++ b/services/companion/java/com/android/server/companion/RolesUtils.java
@@ -47,6 +47,17 @@ final class RolesUtils {
return roleHolders.contains(packageName);
}
+ /**
+ * Attempt to add the association's companion app as the role holder for the device profile
+ * specified in the association. If the association does not have any device profile specified,
+ * then the operation will always be successful as a no-op.
+ *
+ * @param context
+ * @param associationInfo the association for which the role should be granted to the app
+ * @param roleGrantResult the result callback for adding role holder. True if successful, and
+ * false if failed. If the association does not have any device profile
+ * specified, then the operation will always be successful as a no-op.
+ */
static void addRoleHolderForAssociation(
@NonNull Context context, @NonNull AssociationInfo associationInfo,
@NonNull Consumer<Boolean> roleGrantResult) {
@@ -55,7 +66,11 @@ final class RolesUtils {
}
final String deviceProfile = associationInfo.getDeviceProfile();
- if (deviceProfile == null) return;
+ if (deviceProfile == null) {
+ // If no device profile is specified, then no-op and resolve callback with success.
+ roleGrantResult.accept(true);
+ return;
+ }
final RoleManager roleManager = context.getSystemService(RoleManager.class);