From cb49964b533c05fa0106d6090644e6b3dd741273 Mon Sep 17 00:00:00 2001 From: Jack He Date: Wed, 3 Apr 2019 00:48:49 -0700 Subject: [PATCH] Audio: Check if BluetoothDevice.getName() returns null * BluetoothDevice.getName() could return null if Bluetooth process is not running. This patch uses empty string "" to replace the null value returned from BluetoothDevice.getName(). The logic is wrapped in BtHelper#getName(BluetoothDevice) method Fixes: 129806133 Test: atest BluetoothInstrumentationTests on eng build Change-Id: If44e675aafa779fa86451742aa917404987fc15a --- .../java/com/android/server/audio/AudioDeviceInventory.java | 9 ++++----- services/core/java/com/android/server/audio/BtHelper.java | 10 +++++++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/services/core/java/com/android/server/audio/AudioDeviceInventory.java b/services/core/java/com/android/server/audio/AudioDeviceInventory.java index 5f624ba9be9d..7750bfefae05 100644 --- a/services/core/java/com/android/server/audio/AudioDeviceInventory.java +++ b/services/core/java/com/android/server/audio/AudioDeviceInventory.java @@ -207,7 +207,7 @@ public final class AudioDeviceInventory { mDeviceBroker.setDeviceVolume( streamState, AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP); } - makeA2dpDeviceAvailable(address, btDevice.getName(), + makeA2dpDeviceAvailable(address, BtHelper.getName(btDevice), "onSetA2dpSinkConnectionState", a2dpCodec); } } @@ -257,7 +257,7 @@ public final class AudioDeviceInventory { if (isConnected && state != BluetoothProfile.STATE_CONNECTED) { makeHearingAidDeviceUnavailable(address); } else if (!isConnected && state == BluetoothProfile.STATE_CONNECTED) { - makeHearingAidDeviceAvailable(address, btDevice.getName(), + makeHearingAidDeviceAvailable(address, BtHelper.getName(btDevice), "onSetHearingAidConnectionState"); } } @@ -318,7 +318,7 @@ public final class AudioDeviceInventory { } } if (AudioSystem.handleDeviceConfigChange(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, address, - btDevice.getName(), a2dpCodec) != AudioSystem.AUDIO_STATUS_OK) { + BtHelper.getName(btDevice), a2dpCodec) != AudioSystem.AUDIO_STATUS_OK) { int musicDevice = mDeviceBroker.getDeviceForStream(AudioSystem.STREAM_MUSIC); // force A2DP device disconnection in case of error so that AudioService state is // consistent with audio policy manager state @@ -603,10 +603,9 @@ public final class AudioDeviceInventory { } // A2DP device exists, handle active device change final String existingDevicekey = mConnectedDevices.keyAt(i); - final String deviceName = device.getName(); mConnectedDevices.remove(existingDevicekey); mConnectedDevices.put(deviceKey, new DeviceInfo( - AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, deviceName, + AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, BtHelper.getName(device), address, a2dpCodec)); mDeviceBroker.postA2dpActiveDeviceChange( new BtHelper.BluetoothA2dpDeviceInfo( diff --git a/services/core/java/com/android/server/audio/BtHelper.java b/services/core/java/com/android/server/audio/BtHelper.java index 522a55d8ea1e..2d9156b8782e 100644 --- a/services/core/java/com/android/server/audio/BtHelper.java +++ b/services/core/java/com/android/server/audio/BtHelper.java @@ -152,6 +152,14 @@ public class BtHelper { } } + /*package*/ @NonNull static String getName(@NonNull BluetoothDevice device) { + final String deviceName = device.getName(); + if (deviceName == null) { + return ""; + } + return deviceName; + } + //---------------------------------------------------------------------- // Interface for AudioDeviceBroker @@ -515,7 +523,7 @@ public class BtHelper { if (!BluetoothAdapter.checkBluetoothAddress(address)) { address = ""; } - String btDeviceName = btDevice.getName(); + String btDeviceName = getName(btDevice); boolean result = false; if (isActive) { result |= mDeviceBroker.handleDeviceConnection( -- 2.11.0