Fix usage of the updated CDM APIs
Bug: 194301022 Test: make Change-Id: I70633f43d37fdeec8329e0c58f7cffa402dba689
This commit is contained in:
@@ -29,6 +29,7 @@ import android.graphics.drawable.Drawable;
|
|||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
import android.os.ServiceManager;
|
import android.os.ServiceManager;
|
||||||
|
import android.os.UserHandle;
|
||||||
import android.provider.DeviceConfig;
|
import android.provider.DeviceConfig;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@@ -126,8 +127,8 @@ public class BluetoothDetailsCompanionAppsController extends BluetoothDetailsCon
|
|||||||
try {
|
try {
|
||||||
java.util.Objects.requireNonNull(ICompanionDeviceManager.Stub.asInterface(
|
java.util.Objects.requireNonNull(ICompanionDeviceManager.Stub.asInterface(
|
||||||
ServiceManager.getService(
|
ServiceManager.getService(
|
||||||
Context.COMPANION_DEVICE_SERVICE))).disassociate(
|
Context.COMPANION_DEVICE_SERVICE))).legacyDisassociate(
|
||||||
address, packageName);
|
address, packageName, UserHandle.myUserId());
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@@ -54,6 +54,7 @@ import android.util.Log;
|
|||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
|
||||||
|
import com.android.internal.util.CollectionUtils;
|
||||||
import com.android.settingslib.R;
|
import com.android.settingslib.R;
|
||||||
import com.android.settingslib.Utils;
|
import com.android.settingslib.Utils;
|
||||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||||
@@ -159,7 +160,9 @@ public class NotificationBackend {
|
|||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
try {
|
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) {
|
if (associatedMacAddrs != null) {
|
||||||
for (String assocMac : associatedMacAddrs) {
|
for (String assocMac : associatedMacAddrs) {
|
||||||
final Collection<CachedBluetoothDevice> cachedDevices =
|
final Collection<CachedBluetoothDevice> cachedDevices =
|
||||||
|
@@ -24,9 +24,9 @@ import static org.mockito.Mockito.when;
|
|||||||
|
|
||||||
import android.companion.AssociationInfo;
|
import android.companion.AssociationInfo;
|
||||||
import android.companion.CompanionDeviceManager;
|
import android.companion.CompanionDeviceManager;
|
||||||
import android.companion.DeviceId;
|
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.net.MacAddress;
|
||||||
|
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
import androidx.preference.PreferenceCategory;
|
import androidx.preference.PreferenceCategory;
|
||||||
@@ -100,15 +100,14 @@ public class BluetoothDetailsCompanionAppsControllerTest extends
|
|||||||
setupFakeLabelAndInfo(packageName, appName);
|
setupFakeLabelAndInfo(packageName, appName);
|
||||||
|
|
||||||
final int associationId = mAssociations.size() + 1;
|
final int associationId = mAssociations.size() + 1;
|
||||||
final DeviceId deviceId =
|
|
||||||
new DeviceId(DeviceId.TYPE_MAC_ADDRESS, mCachedDevice.getAddress());
|
|
||||||
final AssociationInfo association = new AssociationInfo(
|
final AssociationInfo association = new AssociationInfo(
|
||||||
associationId,
|
associationId,
|
||||||
/* userId */ 0,
|
/* userId */ 0,
|
||||||
packageName,
|
packageName,
|
||||||
Arrays.asList(deviceId),
|
MacAddress.fromString(mCachedDevice.getAddress()),
|
||||||
|
/* displayName */ null,
|
||||||
/* deviceProfile */ "",
|
/* deviceProfile */ "",
|
||||||
/* managedByCompanionApp */ false,
|
/* selfManaged */ false,
|
||||||
/* notifyOnDeviceNearby */ true,
|
/* notifyOnDeviceNearby */ true,
|
||||||
/* timeApprovedMs */ System.currentTimeMillis());
|
/* timeApprovedMs */ System.currentTimeMillis());
|
||||||
|
|
||||||
|
@@ -31,11 +31,13 @@ import android.app.INotificationManager;
|
|||||||
import android.app.role.RoleManager;
|
import android.app.role.RoleManager;
|
||||||
import android.app.usage.UsageEvents;
|
import android.app.usage.UsageEvents;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
|
import android.companion.AssociationInfo;
|
||||||
import android.companion.ICompanionDeviceManager;
|
import android.companion.ICompanionDeviceManager;
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.net.MacAddress;
|
||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
|
||||||
@@ -234,8 +236,9 @@ public class NotificationBackendTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getDeviceList_associationsButNoDevice() throws Exception {
|
public void getDeviceList_associationsButNoDevice() throws Exception {
|
||||||
List<String> macs = ImmutableList.of("00:00:00:00:00:10", "00:00:00:00:00:20");
|
List<AssociationInfo> associations =
|
||||||
when(mCdm.getAssociations(mCn.getPackageName(), 0)).thenReturn(macs);
|
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<>());
|
when(mCbm.getCachedDevicesCopy()).thenReturn(new ArrayList<>());
|
||||||
|
|
||||||
@@ -245,12 +248,13 @@ public class NotificationBackendTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getDeviceList_singleDevice() throws Exception {
|
public void getDeviceList_singleDevice() throws Exception {
|
||||||
List<String> macs = ImmutableList.of("00:00:00:00:00:10", "00:00:00:00:00:20");
|
String[] macs = { "00:00:00:00:00:10", "00:00:00:00:00:20" };
|
||||||
when(mCdm.getAssociations(mCn.getPackageName(), 0)).thenReturn(macs);
|
List<AssociationInfo> associations = mockAssociations(macs);
|
||||||
|
when(mCdm.getAssociations(mCn.getPackageName(), 0)).thenReturn(associations);
|
||||||
|
|
||||||
Collection<CachedBluetoothDevice> cachedDevices = new ArrayList<>();
|
Collection<CachedBluetoothDevice> cachedDevices = new ArrayList<>();
|
||||||
CachedBluetoothDevice cbd1 = mock(CachedBluetoothDevice.class);
|
CachedBluetoothDevice cbd1 = mock(CachedBluetoothDevice.class);
|
||||||
when(cbd1.getAddress()).thenReturn(macs.get(0));
|
when(cbd1.getAddress()).thenReturn(macs[0]);
|
||||||
when(cbd1.getName()).thenReturn("Device 1");
|
when(cbd1.getName()).thenReturn("Device 1");
|
||||||
cachedDevices.add(cbd1);
|
cachedDevices.add(cbd1);
|
||||||
when(mCbm.getCachedDevicesCopy()).thenReturn(cachedDevices);
|
when(mCbm.getCachedDevicesCopy()).thenReturn(cachedDevices);
|
||||||
@@ -261,17 +265,18 @@ public class NotificationBackendTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getDeviceList_multipleDevices() throws Exception {
|
public void getDeviceList_multipleDevices() throws Exception {
|
||||||
List<String> macs = ImmutableList.of("00:00:00:00:00:10", "00:00:00:00:00:20");
|
String[] macs = { "00:00:00:00:00:10", "00:00:00:00:00:20" };
|
||||||
when(mCdm.getAssociations(mCn.getPackageName(), 0)).thenReturn(macs);
|
List<AssociationInfo> associations = mockAssociations(macs);
|
||||||
|
when(mCdm.getAssociations(mCn.getPackageName(), 0)).thenReturn(associations);
|
||||||
|
|
||||||
Collection<CachedBluetoothDevice> cachedDevices = new ArrayList<>();
|
Collection<CachedBluetoothDevice> cachedDevices = new ArrayList<>();
|
||||||
CachedBluetoothDevice cbd1 = mock(CachedBluetoothDevice.class);
|
CachedBluetoothDevice cbd1 = mock(CachedBluetoothDevice.class);
|
||||||
when(cbd1.getAddress()).thenReturn(macs.get(0));
|
when(cbd1.getAddress()).thenReturn(macs[0]);
|
||||||
when(cbd1.getName()).thenReturn("Device 1");
|
when(cbd1.getName()).thenReturn("Device 1");
|
||||||
cachedDevices.add(cbd1);
|
cachedDevices.add(cbd1);
|
||||||
|
|
||||||
CachedBluetoothDevice cbd2 = mock(CachedBluetoothDevice.class);
|
CachedBluetoothDevice cbd2 = mock(CachedBluetoothDevice.class);
|
||||||
when(cbd2.getAddress()).thenReturn(macs.get(1));
|
when(cbd2.getAddress()).thenReturn(macs[1]);
|
||||||
when(cbd2.getName()).thenReturn("Device 2");
|
when(cbd2.getName()).thenReturn("Device 2");
|
||||||
cachedDevices.add(cbd2);
|
cachedDevices.add(cbd2);
|
||||||
when(mCbm.getCachedDevicesCopy()).thenReturn(cachedDevices);
|
when(mCbm.getCachedDevicesCopy()).thenReturn(cachedDevices);
|
||||||
@@ -279,4 +284,16 @@ public class NotificationBackendTest {
|
|||||||
assertThat(new NotificationBackend().getDeviceList(
|
assertThat(new NotificationBackend().getDeviceList(
|
||||||
mCdm, mBm, mCn.getPackageName(), 0).toString()).isEqualTo("Device 1, Device 2");
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user