diff options
| author | 2018-03-19 18:20:38 -0700 | |
|---|---|---|
| committer | 2018-03-23 12:26:51 -0700 | |
| commit | d2cf3c39fe25d06eac69740466a64c7623d68751 (patch) | |
| tree | 26246d5c4550c3403d8409dd8d519f837c175a4e | |
| parent | 8085d6f247316817c44dc1bb58142be3dfebf08b (diff) | |
Move the setActiveDevice logic from Settings to SettingsLib (1/2)
Currently the Settings app handles the logic of setting active device;
however, this should be handled by SettingsLib so the logic can be shared.
Bug: 75984255
Test: robolectric test
Change-Id: Ia44e16c550d5b57c985e29e46e17044a895b495a
Merged-In: Ia44e16c550d5b57c985e29e46e17044a895b495a
(cherry picked from commit 6a416327e5b52d6bb78202bd05a86ab84cf2304e)
2 files changed, 43 insertions, 0 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java index f6ec6a86d13d..ec25d2ddba62 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java @@ -416,6 +416,29 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> } } + /** + * Set this device as active device + * @return true if at least one profile on this device is set to active, false otherwise + */ + public boolean setActive() { + boolean result = false; + A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile(); + if (a2dpProfile != null && isConnectedProfile(a2dpProfile)) { + if (a2dpProfile.setActiveDevice(getDevice())) { + Log.i(TAG, "OnPreferenceClickListener: A2DP active device=" + this); + result = true; + } + } + HeadsetProfile headsetProfile = mProfileManager.getHeadsetProfile(); + if ((headsetProfile != null) && isConnectedProfile(headsetProfile)) { + if (headsetProfile.setActiveDevice(getDevice())) { + Log.i(TAG, "OnPreferenceClickListener: Headset active device=" + this); + result = true; + } + } + return result; + } + void refreshName() { fetchName(); dispatchAttributesChanged(); diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java index 2c91d5ab6755..efca4f7c4520 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java @@ -299,4 +299,24 @@ public class CachedBluetoothDeviceTest { // Verify new alias is returned on getName assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ALIAS_NEW); } + + @Test + public void testSetActive() { + when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile); + when(mProfileManager.getHeadsetProfile()).thenReturn(mHfpProfile); + when(mA2dpProfile.setActiveDevice(any(BluetoothDevice.class))).thenReturn(true); + when(mHfpProfile.setActiveDevice(any(BluetoothDevice.class))).thenReturn(true); + + assertThat(mCachedDevice.setActive()).isFalse(); + + mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); + assertThat(mCachedDevice.setActive()).isTrue(); + + mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); + mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); + assertThat(mCachedDevice.setActive()).isTrue(); + + mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.setActive()).isFalse(); + } } |