diff options
2 files changed, 307 insertions, 2 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java index ce4aef31b395..bb8fbe2da9c9 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java @@ -1076,6 +1076,116 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> } /** + * @return resource for android auto string that describes the connection state of this device. + */ + public String getCarConnectionSummary() { + boolean profileConnected = false; // at least one profile is connected + boolean a2dpNotConnected = false; // A2DP is preferred but not connected + boolean hfpNotConnected = false; // HFP is preferred but not connected + boolean hearingAidNotConnected = false; // Hearing Aid is preferred but not connected + + for (LocalBluetoothProfile profile : getProfiles()) { + int connectionStatus = getProfileConnectionState(profile); + + switch (connectionStatus) { + case BluetoothProfile.STATE_CONNECTING: + case BluetoothProfile.STATE_DISCONNECTING: + return mContext.getString(Utils.getConnectionStateSummary(connectionStatus)); + + case BluetoothProfile.STATE_CONNECTED: + profileConnected = true; + break; + + case BluetoothProfile.STATE_DISCONNECTED: + if (profile.isProfileReady()) { + if ((profile instanceof A2dpProfile) || + (profile instanceof A2dpSinkProfile)){ + a2dpNotConnected = true; + } else if ((profile instanceof HeadsetProfile) || + (profile instanceof HfpClientProfile)) { + hfpNotConnected = true; + } else if (profile instanceof HearingAidProfile) { + hearingAidNotConnected = true; + } + } + break; + } + } + + String batteryLevelPercentageString = null; + // Android framework should only set mBatteryLevel to valid range [0-100] or + // BluetoothDevice.BATTERY_LEVEL_UNKNOWN, any other value should be a framework bug. + // Thus assume here that if value is not BluetoothDevice.BATTERY_LEVEL_UNKNOWN, it must + // be valid + final int batteryLevel = getBatteryLevel(); + if (batteryLevel != BluetoothDevice.BATTERY_LEVEL_UNKNOWN) { + // TODO: name com.android.settingslib.bluetooth.Utils something different + batteryLevelPercentageString = + com.android.settingslib.Utils.formatPercentage(batteryLevel); + } + + // Prepare the string for the Active Device summary + String[] activeDeviceStringsArray = mContext.getResources().getStringArray( + R.array.bluetooth_audio_active_device_summaries); + String activeDeviceString = activeDeviceStringsArray[0]; // Default value: not active + if (mIsActiveDeviceA2dp && mIsActiveDeviceHeadset) { + activeDeviceString = activeDeviceStringsArray[1]; // Active for Media and Phone + } else { + if (mIsActiveDeviceA2dp) { + activeDeviceString = activeDeviceStringsArray[2]; // Active for Media only + } + if (mIsActiveDeviceHeadset) { + activeDeviceString = activeDeviceStringsArray[3]; // Active for Phone only + } + } + if (!hearingAidNotConnected && mIsActiveDeviceHearingAid) { + activeDeviceString = activeDeviceStringsArray[1]; + return mContext.getString(R.string.bluetooth_connected, activeDeviceString); + } + + if (profileConnected) { + if (a2dpNotConnected && hfpNotConnected) { + if (batteryLevelPercentageString != null) { + return mContext.getString( + R.string.bluetooth_connected_no_headset_no_a2dp_battery_level, + batteryLevelPercentageString, activeDeviceString); + } else { + return mContext.getString(R.string.bluetooth_connected_no_headset_no_a2dp, + activeDeviceString); + } + + } else if (a2dpNotConnected) { + if (batteryLevelPercentageString != null) { + return mContext.getString(R.string.bluetooth_connected_no_a2dp_battery_level, + batteryLevelPercentageString, activeDeviceString); + } else { + return mContext.getString(R.string.bluetooth_connected_no_a2dp, + activeDeviceString); + } + + } else if (hfpNotConnected) { + if (batteryLevelPercentageString != null) { + return mContext.getString(R.string.bluetooth_connected_no_headset_battery_level, + batteryLevelPercentageString, activeDeviceString); + } else { + return mContext.getString(R.string.bluetooth_connected_no_headset, + activeDeviceString); + } + } else { + if (batteryLevelPercentageString != null) { + return mContext.getString(R.string.bluetooth_connected_battery_level, + batteryLevelPercentageString, activeDeviceString); + } else { + return mContext.getString(R.string.bluetooth_connected, activeDeviceString); + } + } + } + + return getBondState() == BluetoothDevice.BOND_BONDING ? + mContext.getString(R.string.bluetooth_pairing) : null; + } + + /** * @return {@code true} if {@code cachedBluetoothDevice} is a2dp device */ public boolean isA2dpDevice() { 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 5853dcad083a..927a94f6b017 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 @@ -16,6 +16,7 @@ package com.android.settingslib.bluetooth; import static com.google.common.truth.Truth.assertThat; + import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doAnswer; @@ -159,7 +160,7 @@ public class CachedBluetoothDeviceTest { // Test with battery level mBatteryLevel = 10; - assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( + assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( "Active, 10% battery"); // Set A2DP profile to be disconnected and test connection state summary @@ -193,7 +194,7 @@ public class CachedBluetoothDeviceTest { // Test with battery level mBatteryLevel = 10; - assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( + assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( "Active, 10% battery"); // Set HFP profile to be disconnected and test connection state summary @@ -278,6 +279,200 @@ public class CachedBluetoothDeviceTest { } @Test + public void getCarConnectionSummary_singleProfileConnectDisconnect() { + // Test without battery level + // Set PAN profile to be connected and test connection state summary + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected"); + + // Set PAN profile to be disconnected and test connection state summary + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); + + // Test with battery level + mBatteryLevel = 10; + // Set PAN profile to be connected and test connection state summary + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, battery 10%"); + + // Set PAN profile to be disconnected and test connection state summary + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); + + // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level + mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; + + // Set PAN profile to be connected and test connection state summary + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected"); + + // Set PAN profile to be disconnected and test connection state summary + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); + } + + @Test + public void getCarConnectionSummary_multipleProfileConnectDisconnect() { + mBatteryLevel = 10; + + // Set HFP, A2DP and PAN profile to be connected and test connection state summary + mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); + mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_CONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, battery 10%"); + + // Disconnect HFP only and test connection state summary + mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( + "Connected (no phone), battery 10%"); + + // Disconnect A2DP only and test connection state summary + mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); + mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( + "Connected (no media), battery 10%"); + + // Disconnect both HFP and A2DP and test connection state summary + mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( + "Connected (no phone or media), battery 10%"); + + // Disconnect all profiles and test connection state summary + mCachedDevice.onProfileStateChanged(mPanProfile, BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); + } + + @Test + public void getCarConnectionSummary_singleProfileActiveDeviceA2dp() { + // Test without battery level + // Set A2DP profile to be connected and test connection state summary + mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected"); + + // Set device as Active for A2DP and test connection state summary + mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, active (media)"); + + // Test with battery level + mBatteryLevel = 10; + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( + "Connected, battery 10%, active (media)"); + + // Set A2DP profile to be disconnected and test connection state summary + mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); + + // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level + mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; + // Set A2DP profile to be connected, Active and test connection state summary + mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); + mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, active (media)"); + + // Set A2DP profile to be disconnected and test connection state summary + mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); + } + + @Test + public void getCarConnectionSummary_singleProfileActiveDeviceHfp() { + // Test without battery level + // Set HFP profile to be connected and test connection state summary + mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected"); + + // Set device as Active for HFP and test connection state summary + mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, active (phone)"); + + // Test with battery level + mBatteryLevel = 10; + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( + "Connected, battery 10%, active (phone)"); + + // Set HFP profile to be disconnected and test connection state summary + mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); + + // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level + mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; + // Set HFP profile to be connected, Active and test connection state summary + mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); + mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, active (phone)"); + + // Set HFP profile to be disconnected and test connection state summary + mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); + } + + @Test + public void getCarConnectionSummary_singleProfileActiveDeviceHearingAid() { + // Test without battery level + // Set Hearing Aid profile to be connected and test connection state summary + mCachedDevice.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected"); + + // Set device as Active for Hearing Aid and test connection state summary + mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, active"); + + // Set Hearing Aid profile to be disconnected and test connection state summary + mCachedDevice.onActiveDeviceChanged(false, BluetoothProfile.HEARING_AID); + mCachedDevice.onProfileStateChanged(mHearingAidProfile, + BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); + } + + @Test + public void getCarConnectionSummary_multipleProfilesActiveDevice() { + // Test without battery level + // Set A2DP and HFP profiles to be connected and test connection state summary + mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); + mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected"); + + // Set device as Active for A2DP and HFP and test connection state summary + mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); + mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, active"); + + // Test with battery level + mBatteryLevel = 10; + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( + "Connected, battery 10%, active"); + + // Disconnect A2DP only and test connection state summary + mCachedDevice.onActiveDeviceChanged(false, BluetoothProfile.A2DP); + mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( + "Connected (no media), battery 10%, active (phone)"); + + // Disconnect HFP only and test connection state summary + mCachedDevice.onActiveDeviceChanged(false, BluetoothProfile.HEADSET); + mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); + mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); + mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo( + "Connected (no phone), battery 10%, active (media)"); + + // Test with BluetoothDevice.BATTERY_LEVEL_UNKNOWN battery level + mBatteryLevel = BluetoothDevice.BATTERY_LEVEL_UNKNOWN; + // Set A2DP and HFP profiles to be connected, Active and test connection state summary + mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED); + mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED); + mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP); + mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET); + assertThat(mCachedDevice.getCarConnectionSummary()).isEqualTo("Connected, active"); + + // Set A2DP and HFP profiles to be disconnected and test connection state summary + mCachedDevice.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_DISCONNECTED); + mCachedDevice.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.getCarConnectionSummary()).isNull(); + } + + + @Test public void testDeviceName_testAliasNameAvailable() { when(mDevice.getAliasName()).thenReturn(DEVICE_ALIAS); when(mDevice.getName()).thenReturn(DEVICE_NAME); |