Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0d2ab6ab authored by Raphael Kim's avatar Raphael Kim
Browse files

[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
parent 2e0beb06
Loading
Loading
Loading
Loading
+23 −23
Original line number Diff line number Diff line
@@ -285,30 +285,31 @@ class AssociationRequestsProcessor {
                selfManaged, /* notifyOnDeviceNearby */ false, /* revoked */ false,
                timestamp, Long.MAX_VALUE, /* systemDataSyncFlags */ 0);

        if (deviceProfile != null) {
        // 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, deviceProfile);

                addAssociationToStore(association);
                sendCallbackAndFinish(association, callback, resultReceiver);
            } else {
                    Slog.e(TAG, "Failed to add u" + userId + "\\" + packageName
                            + " to the list of " + deviceProfile + " holders.");

                Slog.e(TAG, "Failed to add u" + association.getUserId()
                        + "\\" + association.getPackageName()
                        + " to the list of " + association.getDeviceProfile() + " holders.");
                sendCallbackAndFinish(null, callback, resultReceiver);
            }
        });
        } else {
            addAssociationToStore(association, null);

            sendCallbackAndFinish(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 enableSystemDataSync(int associationId, int flags) {
@@ -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,
+16 −1
Original line number Diff line number Diff line
@@ -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);