Loading packages/SettingsLib/src/com/android/settingslib/bluetooth/CsipDeviceManager.java +19 −11 Original line number Diff line number Diff line Loading @@ -49,7 +49,7 @@ public class CsipDeviceManager { List<CachedBluetoothDevice> cachedDevices) { mBtManager = localBtManager; mCachedDevices = cachedDevices; }; } void initCsipDeviceIfNeeded(CachedBluetoothDevice newDevice) { // Current it only supports the base uuid for CSIP and group this set in UI. Loading Loading @@ -218,11 +218,7 @@ public class CsipDeviceManager { * return {@code false}. */ public boolean isExistedGroupId(int groupId) { if (getCachedDevice(groupId) != null) { return true; } return false; return getCachedDevice(groupId) != null; } @VisibleForTesting Loading Loading @@ -271,7 +267,7 @@ public class CsipDeviceManager { .anyMatch(profile -> profile instanceof A2dpProfile || profile instanceof HeadsetProfile)) .findFirst().orElse(null); if (dualModeDevice != null && dualModeDevice.isConnected()) { if (isDeviceConnected(dualModeDevice)) { log("getPreferredMainDevice: The connected DUAL mode device"); return dualModeDevice; } Loading @@ -290,13 +286,15 @@ public class CsipDeviceManager { leAudioLeadDevice != null ? deviceManager.findDevice(leAudioLeadDevice) : null; if (leAudioLeadCachedDevice == null) { log("getPreferredMainDevice: The LeadDevice is not in the all of devices list"); } else if (leAudioLeadCachedDevice.isConnected()) { } else if (isDeviceConnected(leAudioLeadCachedDevice)) { log("getPreferredMainDevice: The connected LeadDevice from LE profile"); return leAudioLeadCachedDevice; } CachedBluetoothDevice oneOfConnectedDevices = groupDevicesList.stream() .filter(cachedDevice -> cachedDevice.isConnected()) .findFirst().orElse(null); CachedBluetoothDevice oneOfConnectedDevices = groupDevicesList.stream() .filter(cachedDevice -> isDeviceConnected(cachedDevice)) .findFirst() .orElse(null); if (oneOfConnectedDevices != null) { log("getPreferredMainDevice: One of the connected devices."); return oneOfConnectedDevices; Loading Loading @@ -390,4 +388,14 @@ public class CsipDeviceManager { Log.d(TAG, msg); } } private boolean isDeviceConnected(CachedBluetoothDevice cachedDevice) { if (cachedDevice == null) { return false; } final BluetoothDevice device = cachedDevice.getDevice(); return cachedDevice.isConnected() && device.getBondState() == BluetoothDevice.BOND_BONDED && device.isConnected(); } } packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CsipDeviceManagerTest.java +18 −8 Original line number Diff line number Diff line Loading @@ -29,9 +29,6 @@ import android.bluetooth.BluetoothProfile; import android.content.Context; import android.os.Parcel; import java.util.ArrayList; import java.util.List; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; Loading @@ -40,6 +37,9 @@ import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import java.util.ArrayList; import java.util.List; @RunWith(RobolectricTestRunner.class) public class CsipDeviceManagerTest { private final static String DEVICE_NAME_1 = "TestName_1"; Loading Loading @@ -84,7 +84,6 @@ public class CsipDeviceManagerTest { private Context mContext; private List<CachedBluetoothDevice> mCachedDevices = new ArrayList<CachedBluetoothDevice>(); private BluetoothClass createBtClass(int deviceClass) { Parcel p = Parcel.obtain(); p.writeInt(deviceClass); Loading Loading @@ -112,6 +111,12 @@ public class CsipDeviceManagerTest { when(mDevice1.getBluetoothClass()).thenReturn(DEVICE_CLASS_1); when(mDevice2.getBluetoothClass()).thenReturn(DEVICE_CLASS_2); when(mDevice3.getBluetoothClass()).thenReturn(DEVICE_CLASS_2); when(mDevice1.isConnected()).thenReturn(true); when(mDevice2.isConnected()).thenReturn(true); when(mDevice3.isConnected()).thenReturn(true); when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); when(mDevice2.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); when(mDevice3.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager); when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalProfileManager); when(mLocalProfileManager.getLeAudioProfile()).thenReturn(mLeAudioProfile); Loading Loading @@ -153,7 +158,6 @@ public class CsipDeviceManagerTest { profiles.add(mA2dpProfile); when(mCachedDevice3.getConnectableProfiles()).thenReturn(profiles); when(mCachedDevice3.isConnected()).thenReturn(true); } @Test Loading Loading @@ -203,6 +207,7 @@ public class CsipDeviceManagerTest { @Test public void getPreferredMainDevice_noConnectedDualModeDevice_returnLeadDevice() { when(mCachedDevice1.isConnected()).thenReturn(false); when(mDevice1.isConnected()).thenReturn(false); when(mLeAudioProfile.getConnectedGroupLeadDevice(anyInt())).thenReturn(mDevice2); CachedBluetoothDevice expectedDevice = mCachedDevice2; Loading @@ -216,6 +221,8 @@ public class CsipDeviceManagerTest { public void getPreferredMainDevice_noConnectedDualModeDeviceNoLeadDevice_returnConnectedOne() { when(mCachedDevice1.isConnected()).thenReturn(false); when(mCachedDevice2.isConnected()).thenReturn(true); when(mDevice1.isConnected()).thenReturn(false); when(mDevice2.isConnected()).thenReturn(true); CachedBluetoothDevice expectedDevice = mCachedDevice2; assertThat( Loading @@ -228,6 +235,8 @@ public class CsipDeviceManagerTest { public void getPreferredMainDevice_noConnectedDevice_returnDualModeDevice() { when(mCachedDevice1.isConnected()).thenReturn(false); when(mCachedDevice2.isConnected()).thenReturn(false); when(mDevice1.isConnected()).thenReturn(false); when(mDevice2.isConnected()).thenReturn(false); CachedBluetoothDevice expectedDevice = mCachedDevice1; assertThat( Loading @@ -240,6 +249,8 @@ public class CsipDeviceManagerTest { public void getPreferredMainDevice_noConnectedDeviceNoDualMode_returnFirstOneDevice() { when(mCachedDevice1.isConnected()).thenReturn(false); when(mCachedDevice2.isConnected()).thenReturn(false); when(mDevice1.isConnected()).thenReturn(false); when(mDevice2.isConnected()).thenReturn(false); List<LocalBluetoothProfile> profiles = new ArrayList<LocalBluetoothProfile>(); profiles.add(mLeAudioProfile); when(mCachedDevice1.getConnectableProfiles()).thenReturn(profiles); Loading Loading @@ -267,8 +278,8 @@ public class CsipDeviceManagerTest { } @Test public void addMemberDevicesIntoMainDevice_preferredDeviceIsMainAndNoOtherInList_noChangeList() { public void addMemberDevicesIntoMainDevice_preferredDeviceIsMainAndNoOtherInList_noChangeList() { // Condition: The preferredDevice is main and no other main device in top list // Expected Result: return false and the list is no changed CachedBluetoothDevice preferredDevice = mCachedDevice1; Loading @@ -295,7 +306,6 @@ public class CsipDeviceManagerTest { mCachedDevices.add(mCachedDevice2); mCachedDevices.add(mCachedDevice3); assertThat(mCsipDeviceManager.addMemberDevicesIntoMainDevice(GROUP1, preferredDevice)) .isTrue(); assertThat(mCachedDevices.contains(preferredDevice)).isTrue(); Loading Loading
packages/SettingsLib/src/com/android/settingslib/bluetooth/CsipDeviceManager.java +19 −11 Original line number Diff line number Diff line Loading @@ -49,7 +49,7 @@ public class CsipDeviceManager { List<CachedBluetoothDevice> cachedDevices) { mBtManager = localBtManager; mCachedDevices = cachedDevices; }; } void initCsipDeviceIfNeeded(CachedBluetoothDevice newDevice) { // Current it only supports the base uuid for CSIP and group this set in UI. Loading Loading @@ -218,11 +218,7 @@ public class CsipDeviceManager { * return {@code false}. */ public boolean isExistedGroupId(int groupId) { if (getCachedDevice(groupId) != null) { return true; } return false; return getCachedDevice(groupId) != null; } @VisibleForTesting Loading Loading @@ -271,7 +267,7 @@ public class CsipDeviceManager { .anyMatch(profile -> profile instanceof A2dpProfile || profile instanceof HeadsetProfile)) .findFirst().orElse(null); if (dualModeDevice != null && dualModeDevice.isConnected()) { if (isDeviceConnected(dualModeDevice)) { log("getPreferredMainDevice: The connected DUAL mode device"); return dualModeDevice; } Loading @@ -290,13 +286,15 @@ public class CsipDeviceManager { leAudioLeadDevice != null ? deviceManager.findDevice(leAudioLeadDevice) : null; if (leAudioLeadCachedDevice == null) { log("getPreferredMainDevice: The LeadDevice is not in the all of devices list"); } else if (leAudioLeadCachedDevice.isConnected()) { } else if (isDeviceConnected(leAudioLeadCachedDevice)) { log("getPreferredMainDevice: The connected LeadDevice from LE profile"); return leAudioLeadCachedDevice; } CachedBluetoothDevice oneOfConnectedDevices = groupDevicesList.stream() .filter(cachedDevice -> cachedDevice.isConnected()) .findFirst().orElse(null); CachedBluetoothDevice oneOfConnectedDevices = groupDevicesList.stream() .filter(cachedDevice -> isDeviceConnected(cachedDevice)) .findFirst() .orElse(null); if (oneOfConnectedDevices != null) { log("getPreferredMainDevice: One of the connected devices."); return oneOfConnectedDevices; Loading Loading @@ -390,4 +388,14 @@ public class CsipDeviceManager { Log.d(TAG, msg); } } private boolean isDeviceConnected(CachedBluetoothDevice cachedDevice) { if (cachedDevice == null) { return false; } final BluetoothDevice device = cachedDevice.getDevice(); return cachedDevice.isConnected() && device.getBondState() == BluetoothDevice.BOND_BONDED && device.isConnected(); } }
packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CsipDeviceManagerTest.java +18 −8 Original line number Diff line number Diff line Loading @@ -29,9 +29,6 @@ import android.bluetooth.BluetoothProfile; import android.content.Context; import android.os.Parcel; import java.util.ArrayList; import java.util.List; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; Loading @@ -40,6 +37,9 @@ import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import java.util.ArrayList; import java.util.List; @RunWith(RobolectricTestRunner.class) public class CsipDeviceManagerTest { private final static String DEVICE_NAME_1 = "TestName_1"; Loading Loading @@ -84,7 +84,6 @@ public class CsipDeviceManagerTest { private Context mContext; private List<CachedBluetoothDevice> mCachedDevices = new ArrayList<CachedBluetoothDevice>(); private BluetoothClass createBtClass(int deviceClass) { Parcel p = Parcel.obtain(); p.writeInt(deviceClass); Loading Loading @@ -112,6 +111,12 @@ public class CsipDeviceManagerTest { when(mDevice1.getBluetoothClass()).thenReturn(DEVICE_CLASS_1); when(mDevice2.getBluetoothClass()).thenReturn(DEVICE_CLASS_2); when(mDevice3.getBluetoothClass()).thenReturn(DEVICE_CLASS_2); when(mDevice1.isConnected()).thenReturn(true); when(mDevice2.isConnected()).thenReturn(true); when(mDevice3.isConnected()).thenReturn(true); when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); when(mDevice2.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); when(mDevice3.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager); when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalProfileManager); when(mLocalProfileManager.getLeAudioProfile()).thenReturn(mLeAudioProfile); Loading Loading @@ -153,7 +158,6 @@ public class CsipDeviceManagerTest { profiles.add(mA2dpProfile); when(mCachedDevice3.getConnectableProfiles()).thenReturn(profiles); when(mCachedDevice3.isConnected()).thenReturn(true); } @Test Loading Loading @@ -203,6 +207,7 @@ public class CsipDeviceManagerTest { @Test public void getPreferredMainDevice_noConnectedDualModeDevice_returnLeadDevice() { when(mCachedDevice1.isConnected()).thenReturn(false); when(mDevice1.isConnected()).thenReturn(false); when(mLeAudioProfile.getConnectedGroupLeadDevice(anyInt())).thenReturn(mDevice2); CachedBluetoothDevice expectedDevice = mCachedDevice2; Loading @@ -216,6 +221,8 @@ public class CsipDeviceManagerTest { public void getPreferredMainDevice_noConnectedDualModeDeviceNoLeadDevice_returnConnectedOne() { when(mCachedDevice1.isConnected()).thenReturn(false); when(mCachedDevice2.isConnected()).thenReturn(true); when(mDevice1.isConnected()).thenReturn(false); when(mDevice2.isConnected()).thenReturn(true); CachedBluetoothDevice expectedDevice = mCachedDevice2; assertThat( Loading @@ -228,6 +235,8 @@ public class CsipDeviceManagerTest { public void getPreferredMainDevice_noConnectedDevice_returnDualModeDevice() { when(mCachedDevice1.isConnected()).thenReturn(false); when(mCachedDevice2.isConnected()).thenReturn(false); when(mDevice1.isConnected()).thenReturn(false); when(mDevice2.isConnected()).thenReturn(false); CachedBluetoothDevice expectedDevice = mCachedDevice1; assertThat( Loading @@ -240,6 +249,8 @@ public class CsipDeviceManagerTest { public void getPreferredMainDevice_noConnectedDeviceNoDualMode_returnFirstOneDevice() { when(mCachedDevice1.isConnected()).thenReturn(false); when(mCachedDevice2.isConnected()).thenReturn(false); when(mDevice1.isConnected()).thenReturn(false); when(mDevice2.isConnected()).thenReturn(false); List<LocalBluetoothProfile> profiles = new ArrayList<LocalBluetoothProfile>(); profiles.add(mLeAudioProfile); when(mCachedDevice1.getConnectableProfiles()).thenReturn(profiles); Loading Loading @@ -267,8 +278,8 @@ public class CsipDeviceManagerTest { } @Test public void addMemberDevicesIntoMainDevice_preferredDeviceIsMainAndNoOtherInList_noChangeList() { public void addMemberDevicesIntoMainDevice_preferredDeviceIsMainAndNoOtherInList_noChangeList() { // Condition: The preferredDevice is main and no other main device in top list // Expected Result: return false and the list is no changed CachedBluetoothDevice preferredDevice = mCachedDevice1; Loading @@ -295,7 +306,6 @@ public class CsipDeviceManagerTest { mCachedDevices.add(mCachedDevice2); mCachedDevices.add(mCachedDevice3); assertThat(mCsipDeviceManager.addMemberDevicesIntoMainDevice(GROUP1, preferredDevice)) .isTrue(); assertThat(mCachedDevices.contains(preferredDevice)).isTrue(); Loading