OSDN Git Service

Audio: Check if BluetoothDevice.getName() returns null
authorJack He <siyuanh@google.com>
Wed, 3 Apr 2019 07:48:49 +0000 (00:48 -0700)
committerJack He <siyuanh@google.com>
Wed, 3 Apr 2019 20:39:26 +0000 (13:39 -0700)
* 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

services/core/java/com/android/server/audio/AudioDeviceInventory.java
services/core/java/com/android/server/audio/BtHelper.java

index 5f624ba..7750bfe 100644 (file)
@@ -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(
index 522a55d..2d9156b 100644 (file)
@@ -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(