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

Commit abdb133c authored by Raphael Kim's avatar Raphael Kim Committed by Android (Google) Code Review
Browse files

Merge "[CDM] Add new API to remove bluetooth bond" into main

parents c870608e 6053fd7f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -9801,6 +9801,7 @@ package android.companion {
    method @NonNull public java.util.List<android.companion.AssociationInfo> getMyAssociations();
    method @Deprecated public boolean hasNotificationAccess(android.content.ComponentName);
    method @FlaggedApi("android.companion.perm_sync_user_consent") public boolean isPermissionTransferUserConsented(int);
    method @FlaggedApi("android.companion.unpair_associated_device") @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public boolean removeBond(int);
    method public void requestNotificationAccess(android.content.ComponentName);
    method @FlaggedApi("android.companion.association_tag") public void setAssociationTag(int, @NonNull String);
    method @RequiresPermission(android.Manifest.permission.REQUEST_OBSERVE_COMPANION_DEVICE_PRESENCE) public void startObservingDevicePresence(@NonNull String) throws android.companion.DeviceNotAssociatedException;
+26 −0
Original line number Diff line number Diff line
@@ -1149,6 +1149,32 @@ public final class CompanionDeviceManager {
        }
    }

    /**
     * Remove bonding between this device and an associated companion device.
     *
     * <p>This is an asynchronous call, it will return immediately. Register for {@link
     * BluetoothDevice#ACTION_BOND_STATE_CHANGED} intents to be notified when the bond removal
     * process completes, and its result.
     *
     * @param associationId an already-associated companion device to remove bond from
     * @return false on immediate error, true if bond removal process will begin
     */
    @FlaggedApi(Flags.FLAG_UNPAIR_ASSOCIATED_DEVICE)
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public boolean removeBond(int associationId) {
        if (mService == null) {
            Log.w(TAG, "CompanionDeviceManager service is not available.");
            return false;
        }

        try {
            return mService.removeBond(associationId, mContext.getOpPackageName(),
                    mContext.getUserId());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    // TODO(b/315163162) Add @Deprecated keyword after 24Q2 cut.
    /**
     * Register to receive callbacks whenever the associated device comes in and out of range.
+3 −0
Original line number Diff line number Diff line
@@ -141,4 +141,7 @@ interface ICompanionDeviceManager {
    byte[] getBackupPayload(int userId);

    void applyRestoredPayload(in byte[] payload, int userId);

    @EnforcePermission("BLUETOOTH_CONNECT")
    boolean removeBond(int associationId, in String packageName, int userId);
}
+7 −0
Original line number Diff line number Diff line
@@ -47,3 +47,10 @@ flag {
    description: "Enable ongoing perm sync"
    bug: "338469649"
}

flag {
    name: "unpair_associated_device"
    namespace: "companion"
    description: "Unpair with an associated bluetooth device"
    bug: "322237619"
}
 No newline at end of file
+29 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
package com.android.server.companion;

import static android.Manifest.permission.ASSOCIATE_COMPANION_DEVICES;
import static android.Manifest.permission.BLUETOOTH_CONNECT;
import static android.Manifest.permission.DELIVER_COMPANION_MESSAGES;
import static android.Manifest.permission.MANAGE_COMPANION_DEVICES;
import static android.Manifest.permission.REQUEST_COMPANION_SELF_MANAGED;
@@ -52,6 +53,9 @@ import android.app.AppOpsManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ecm.EnhancedConfirmationManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.companion.AssociationInfo;
import android.companion.AssociationRequest;
import android.companion.IAssociationRequestCallback;
@@ -540,6 +544,31 @@ public class CompanionDeviceManagerService extends SystemService {
            mDevicePresenceProcessor.stopObservingDevicePresence(request, packageName, userId);
        }

        @Override
        @EnforcePermission(BLUETOOTH_CONNECT)
        public boolean removeBond(int associationId, String packageName, int userId) {
            removeBond_enforcePermission();

            Slog.i(TAG, "removeBond() "
                    + "associationId=" + associationId + ", "
                    + "package=u" + userId + "/" + packageName);
            enforceCallerCanManageAssociationsForPackage(getContext(), userId, packageName,
                    "remove bonds");

            AssociationInfo association = mAssociationStore
                    .getAssociationWithCallerChecks(associationId);
            MacAddress address = association.getDeviceMacAddress();
            if (address == null) {
                throw new IllegalArgumentException(
                        "Association id=[" + associationId + "] doesn't have a device address.");
            }

            BluetoothAdapter btAdapter = getContext().getSystemService(BluetoothManager.class)
                    .getAdapter();
            BluetoothDevice btDevice = btAdapter.getRemoteDevice(address.toString().toUpperCase());
            return btDevice.removeBond();
        }

        @Override
        public PendingIntent buildPermissionTransferUserConsentIntent(String packageName,
                int userId, int associationId) {