Loading packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java +16 −0 Original line number Original line Diff line number Diff line Loading @@ -1079,4 +1079,20 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> return getBondState() == BluetoothDevice.BOND_BONDING ? return getBondState() == BluetoothDevice.BOND_BONDING ? mContext.getString(R.string.bluetooth_pairing) : null; 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; } } } packages/SettingsLib/src/com/android/settingslib/bluetooth/HeadsetProfile.java +5 −0 Original line number Original line Diff line number Diff line Loading @@ -168,6 +168,11 @@ public class HeadsetProfile implements LocalBluetoothProfile { return mService.isAudioOn(); 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) { public boolean isPreferred(BluetoothDevice device) { if (mService == null) return false; if (mService == null) return false; return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF; return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF; Loading packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java +36 −0 Original line number Original line Diff line number Diff line Loading @@ -334,4 +334,40 @@ public class CachedBluetoothDeviceTest { mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); assertThat(mCachedDevice.setActive()).isFalse(); 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(); } } } packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HeadsetProfileTest.java +19 −1 Original line number Original line Diff line number Diff line Loading @@ -8,6 +8,7 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import static org.mockito.Mockito.when; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothHeadset; import android.bluetooth.BluetoothHeadset; import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothProfile; import android.content.Context; import android.content.Context; Loading @@ -31,7 +32,10 @@ public class HeadsetProfileTest { private LocalBluetoothProfileManager mProfileManager; private LocalBluetoothProfileManager mProfileManager; @Mock @Mock private BluetoothHeadset mService; private BluetoothHeadset mService; @Mock private CachedBluetoothDevice mCachedBluetoothDevice; @Mock private BluetoothDevice mBluetoothDevice; private BluetoothProfile.ServiceListener mServiceListener; private BluetoothProfile.ServiceListener mServiceListener; private HeadsetProfile mProfile; private HeadsetProfile mProfile; Loading @@ -44,6 +48,7 @@ public class HeadsetProfileTest { mServiceListener = (BluetoothProfile.ServiceListener) invocation.getArguments()[1]; mServiceListener = (BluetoothProfile.ServiceListener) invocation.getArguments()[1]; return null; return null; }).when(mAdapter).getProfileProxy(any(Context.class), any(), eq(BluetoothProfile.HEADSET)); }).when(mAdapter).getProfileProxy(any(Context.class), any(), eq(BluetoothProfile.HEADSET)); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); mProfile = new HeadsetProfile(context, mAdapter, mDeviceManager, mProfileManager); mProfile = new HeadsetProfile(context, mAdapter, mDeviceManager, mProfileManager); mServiceListener.onServiceConnected(BluetoothProfile.HEADSET, mService); mServiceListener.onServiceConnected(BluetoothProfile.HEADSET, mService); Loading @@ -57,4 +62,17 @@ public class HeadsetProfileTest { when(mService.isAudioOn()).thenReturn(false); when(mService.isAudioOn()).thenReturn(false); assertThat(mProfile.isAudioOn()).isFalse(); 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); } } } Loading
packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java +16 −0 Original line number Original line Diff line number Diff line Loading @@ -1079,4 +1079,20 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> return getBondState() == BluetoothDevice.BOND_BONDING ? return getBondState() == BluetoothDevice.BOND_BONDING ? mContext.getString(R.string.bluetooth_pairing) : null; 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; } } }
packages/SettingsLib/src/com/android/settingslib/bluetooth/HeadsetProfile.java +5 −0 Original line number Original line Diff line number Diff line Loading @@ -168,6 +168,11 @@ public class HeadsetProfile implements LocalBluetoothProfile { return mService.isAudioOn(); 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) { public boolean isPreferred(BluetoothDevice device) { if (mService == null) return false; if (mService == null) return false; return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF; return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF; Loading
packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java +36 −0 Original line number Original line Diff line number Diff line Loading @@ -334,4 +334,40 @@ public class CachedBluetoothDeviceTest { mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); assertThat(mCachedDevice.setActive()).isFalse(); 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(); } } }
packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HeadsetProfileTest.java +19 −1 Original line number Original line Diff line number Diff line Loading @@ -8,6 +8,7 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import static org.mockito.Mockito.when; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothHeadset; import android.bluetooth.BluetoothHeadset; import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothProfile; import android.content.Context; import android.content.Context; Loading @@ -31,7 +32,10 @@ public class HeadsetProfileTest { private LocalBluetoothProfileManager mProfileManager; private LocalBluetoothProfileManager mProfileManager; @Mock @Mock private BluetoothHeadset mService; private BluetoothHeadset mService; @Mock private CachedBluetoothDevice mCachedBluetoothDevice; @Mock private BluetoothDevice mBluetoothDevice; private BluetoothProfile.ServiceListener mServiceListener; private BluetoothProfile.ServiceListener mServiceListener; private HeadsetProfile mProfile; private HeadsetProfile mProfile; Loading @@ -44,6 +48,7 @@ public class HeadsetProfileTest { mServiceListener = (BluetoothProfile.ServiceListener) invocation.getArguments()[1]; mServiceListener = (BluetoothProfile.ServiceListener) invocation.getArguments()[1]; return null; return null; }).when(mAdapter).getProfileProxy(any(Context.class), any(), eq(BluetoothProfile.HEADSET)); }).when(mAdapter).getProfileProxy(any(Context.class), any(), eq(BluetoothProfile.HEADSET)); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); mProfile = new HeadsetProfile(context, mAdapter, mDeviceManager, mProfileManager); mProfile = new HeadsetProfile(context, mAdapter, mDeviceManager, mProfileManager); mServiceListener.onServiceConnected(BluetoothProfile.HEADSET, mService); mServiceListener.onServiceConnected(BluetoothProfile.HEADSET, mService); Loading @@ -57,4 +62,17 @@ public class HeadsetProfileTest { when(mService.isAudioOn()).thenReturn(false); when(mService.isAudioOn()).thenReturn(false); assertThat(mProfile.isAudioOn()).isFalse(); 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); } } }