Fix usage of the updated CDM APIs

Bug: 194301022
Test: make
Change-Id: I70633f43d37fdeec8329e0c58f7cffa402dba689
This commit is contained in:
Sergey Nikolaienkov
2021-10-27 15:41:25 +02:00
parent 74a797efd8
commit 7fea5bd54f
4 changed files with 37 additions and 17 deletions

View File

@@ -29,6 +29,7 @@ import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.provider.DeviceConfig;
import android.text.TextUtils;
import android.util.Log;
@@ -126,8 +127,8 @@ public class BluetoothDetailsCompanionAppsController extends BluetoothDetailsCon
try {
java.util.Objects.requireNonNull(ICompanionDeviceManager.Stub.asInterface(
ServiceManager.getService(
Context.COMPANION_DEVICE_SERVICE))).disassociate(
address, packageName);
Context.COMPANION_DEVICE_SERVICE))).legacyDisassociate(
address, packageName, UserHandle.myUserId());
} catch (RemoteException e) {
throw new RuntimeException(e);
}

View File

@@ -54,6 +54,7 @@ import android.util.Log;
import androidx.annotation.VisibleForTesting;
import com.android.internal.util.CollectionUtils;
import com.android.settingslib.R;
import com.android.settingslib.Utils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -159,7 +160,9 @@ public class NotificationBackend {
StringBuilder sb = new StringBuilder();
try {
List<String> associatedMacAddrs = cdm.getAssociations(pkg, userId);
List<String> associatedMacAddrs = CollectionUtils.mapNotNull(
cdm.getAssociations(pkg, userId),
a -> a.isSelfManaged() ? null : a.getDeviceMacAddress().toString());
if (associatedMacAddrs != null) {
for (String assocMac : associatedMacAddrs) {
final Collection<CachedBluetoothDevice> cachedDevices =

View File

@@ -24,9 +24,9 @@ import static org.mockito.Mockito.when;
import android.companion.AssociationInfo;
import android.companion.CompanionDeviceManager;
import android.companion.DeviceId;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.MacAddress;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
@@ -100,15 +100,14 @@ public class BluetoothDetailsCompanionAppsControllerTest extends
setupFakeLabelAndInfo(packageName, appName);
final int associationId = mAssociations.size() + 1;
final DeviceId deviceId =
new DeviceId(DeviceId.TYPE_MAC_ADDRESS, mCachedDevice.getAddress());
final AssociationInfo association = new AssociationInfo(
associationId,
/* userId */ 0,
packageName,
Arrays.asList(deviceId),
MacAddress.fromString(mCachedDevice.getAddress()),
/* displayName */ null,
/* deviceProfile */ "",
/* managedByCompanionApp */ false,
/* selfManaged */ false,
/* notifyOnDeviceNearby */ true,
/* timeApprovedMs */ System.currentTimeMillis());

View File

@@ -31,11 +31,13 @@ import android.app.INotificationManager;
import android.app.role.RoleManager;
import android.app.usage.UsageEvents;
import android.bluetooth.BluetoothAdapter;
import android.companion.AssociationInfo;
import android.companion.ICompanionDeviceManager;
import android.content.ComponentName;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.MacAddress;
import android.os.Parcel;
import android.provider.Settings;
@@ -234,8 +236,9 @@ public class NotificationBackendTest {
@Test
public void getDeviceList_associationsButNoDevice() throws Exception {
List<String> macs = ImmutableList.of("00:00:00:00:00:10", "00:00:00:00:00:20");
when(mCdm.getAssociations(mCn.getPackageName(), 0)).thenReturn(macs);
List<AssociationInfo> associations =
mockAssociations("00:00:00:00:00:10", "00:00:00:00:00:20");
when(mCdm.getAssociations(mCn.getPackageName(), 0)).thenReturn(associations);
when(mCbm.getCachedDevicesCopy()).thenReturn(new ArrayList<>());
@@ -245,12 +248,13 @@ public class NotificationBackendTest {
@Test
public void getDeviceList_singleDevice() throws Exception {
List<String> macs = ImmutableList.of("00:00:00:00:00:10", "00:00:00:00:00:20");
when(mCdm.getAssociations(mCn.getPackageName(), 0)).thenReturn(macs);
String[] macs = { "00:00:00:00:00:10", "00:00:00:00:00:20" };
List<AssociationInfo> associations = mockAssociations(macs);
when(mCdm.getAssociations(mCn.getPackageName(), 0)).thenReturn(associations);
Collection<CachedBluetoothDevice> cachedDevices = new ArrayList<>();
CachedBluetoothDevice cbd1 = mock(CachedBluetoothDevice.class);
when(cbd1.getAddress()).thenReturn(macs.get(0));
when(cbd1.getAddress()).thenReturn(macs[0]);
when(cbd1.getName()).thenReturn("Device 1");
cachedDevices.add(cbd1);
when(mCbm.getCachedDevicesCopy()).thenReturn(cachedDevices);
@@ -261,17 +265,18 @@ public class NotificationBackendTest {
@Test
public void getDeviceList_multipleDevices() throws Exception {
List<String> macs = ImmutableList.of("00:00:00:00:00:10", "00:00:00:00:00:20");
when(mCdm.getAssociations(mCn.getPackageName(), 0)).thenReturn(macs);
String[] macs = { "00:00:00:00:00:10", "00:00:00:00:00:20" };
List<AssociationInfo> associations = mockAssociations(macs);
when(mCdm.getAssociations(mCn.getPackageName(), 0)).thenReturn(associations);
Collection<CachedBluetoothDevice> cachedDevices = new ArrayList<>();
CachedBluetoothDevice cbd1 = mock(CachedBluetoothDevice.class);
when(cbd1.getAddress()).thenReturn(macs.get(0));
when(cbd1.getAddress()).thenReturn(macs[0]);
when(cbd1.getName()).thenReturn("Device 1");
cachedDevices.add(cbd1);
CachedBluetoothDevice cbd2 = mock(CachedBluetoothDevice.class);
when(cbd2.getAddress()).thenReturn(macs.get(1));
when(cbd2.getAddress()).thenReturn(macs[1]);
when(cbd2.getName()).thenReturn("Device 2");
cachedDevices.add(cbd2);
when(mCbm.getCachedDevicesCopy()).thenReturn(cachedDevices);
@@ -279,4 +284,16 @@ public class NotificationBackendTest {
assertThat(new NotificationBackend().getDeviceList(
mCdm, mBm, mCn.getPackageName(), 0).toString()).isEqualTo("Device 1, Device 2");
}
private ImmutableList<AssociationInfo> mockAssociations(String... macAddresses) {
final AssociationInfo[] associations = new AssociationInfo[macAddresses.length];
for (int index = 0; index < macAddresses.length; index++) {
final AssociationInfo association = mock(AssociationInfo.class);
when(association.isSelfManaged()).thenReturn(false);
when(association.getDeviceMacAddress())
.thenReturn(MacAddress.fromString(macAddresses[index]));
associations[index] = association;
}
return ImmutableList.copyOf(associations);
}
}