From: hughchen Date: Sat, 31 Mar 2018 09:32:53 +0000 (+0800) Subject: Add new function to get audio state X-Git-Tag: android-x86-9.0-r1~137^2~34^2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=23b947e2458b3968f3ae611fe5aa73198921fe6a;p=android-x86%2Fframeworks-base.git Add new function to get audio state * Add getAudioState() * Add isA2dpDevice() and isHfpDevice() in CachedBluetoothDevice Bug: 74134939 Test: make RunSettingsLibRoboTests -j40 Change-Id: I18f4c1fcad0325a57de379f5ffe5b1135a00d084 --- diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java index dc2eceace066..6cbc4b2685a3 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java @@ -1065,4 +1065,20 @@ public class CachedBluetoothDevice implements Comparable return getBondState() == BluetoothDevice.BOND_BONDING ? mContext.getString(R.string.bluetooth_pairing) : null; } + + /** + * @return {@code true} if {@code cachedBluetoothDevice} is a2dp device + */ + public boolean isA2dpDevice() { + return mProfileManager.getA2dpProfile().getConnectionStatus(mDevice) == + BluetoothProfile.STATE_CONNECTED; + } + + /** + * @return {@code true} if {@code cachedBluetoothDevice} is HFP device + */ + public boolean isHfpDevice() { + return mProfileManager.getHeadsetProfile().getConnectionStatus(mDevice) == + BluetoothProfile.STATE_CONNECTED; + } } diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HeadsetProfile.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HeadsetProfile.java index f9f80eb0d3c4..ad813368fbd0 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HeadsetProfile.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HeadsetProfile.java @@ -168,6 +168,11 @@ public class HeadsetProfile implements LocalBluetoothProfile { return mService.isAudioOn(); } + public int getAudioState(BluetoothDevice device) { + if (mService == null) return BluetoothHeadset.STATE_AUDIO_DISCONNECTED; + return mService.getAudioState(device); + } + public boolean isPreferred(BluetoothDevice device) { if (mService == null) return false; return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF; 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 49f58a42d13e..7863fc588778 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 @@ -334,4 +334,40 @@ public class CachedBluetoothDeviceTest { mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); assertThat(mCachedDevice.setActive()).isFalse(); } + + @Test + public void testIsA2dpDevice_isA2dpDevice() { + when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile); + when(mA2dpProfile.getConnectionStatus(mDevice)). + thenReturn(BluetoothProfile.STATE_CONNECTED); + + assertThat(mCachedDevice.isA2dpDevice()).isTrue(); + } + + @Test + public void testIsA2dpDevice_isNotA2dpDevice() { + when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile); + when(mA2dpProfile.getConnectionStatus(mDevice)). + thenReturn(BluetoothProfile.STATE_DISCONNECTING); + + assertThat(mCachedDevice.isA2dpDevice()).isFalse(); + } + + @Test + public void testIsHfpDevice_isHfpDevice() { + when(mProfileManager.getHeadsetProfile()).thenReturn(mHfpProfile); + when(mHfpProfile.getConnectionStatus(mDevice)). + thenReturn(BluetoothProfile.STATE_CONNECTED); + + assertThat(mCachedDevice.isHfpDevice()).isTrue(); + } + + @Test + public void testIsHfpDevice_isNotHfpDevice() { + when(mProfileManager.getHeadsetProfile()).thenReturn(mHfpProfile); + when(mHfpProfile.getConnectionStatus(mDevice)). + thenReturn(BluetoothProfile.STATE_DISCONNECTING); + + assertThat(mCachedDevice.isHfpDevice()).isFalse(); + } } diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HeadsetProfileTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HeadsetProfileTest.java index 117f4472825c..03b023b75f4a 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HeadsetProfileTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HeadsetProfileTest.java @@ -8,6 +8,7 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; +import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothHeadset; import android.bluetooth.BluetoothProfile; import android.content.Context; @@ -31,7 +32,10 @@ public class HeadsetProfileTest { private LocalBluetoothProfileManager mProfileManager; @Mock private BluetoothHeadset mService; - + @Mock + private CachedBluetoothDevice mCachedBluetoothDevice; + @Mock + private BluetoothDevice mBluetoothDevice; private BluetoothProfile.ServiceListener mServiceListener; private HeadsetProfile mProfile; @@ -44,6 +48,7 @@ public class HeadsetProfileTest { mServiceListener = (BluetoothProfile.ServiceListener) invocation.getArguments()[1]; return null; }).when(mAdapter).getProfileProxy(any(Context.class), any(), eq(BluetoothProfile.HEADSET)); + when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); mProfile = new HeadsetProfile(context, mAdapter, mDeviceManager, mProfileManager); mServiceListener.onServiceConnected(BluetoothProfile.HEADSET, mService); @@ -57,4 +62,17 @@ public class HeadsetProfileTest { when(mService.isAudioOn()).thenReturn(false); assertThat(mProfile.isAudioOn()).isFalse(); } + + @Test + public void testHeadsetProfile_shouldReturnAudioState() { + when(mService.getAudioState(mBluetoothDevice)). + thenReturn(BluetoothHeadset.STATE_AUDIO_DISCONNECTED); + assertThat(mProfile.getAudioState(mBluetoothDevice)). + isEqualTo(BluetoothHeadset.STATE_AUDIO_DISCONNECTED); + + when(mService.getAudioState(mBluetoothDevice)). + thenReturn(BluetoothHeadset.STATE_AUDIO_CONNECTED); + assertThat(mProfile.getAudioState(mBluetoothDevice)). + isEqualTo(BluetoothHeadset.STATE_AUDIO_CONNECTED); + } }