Merge "Avoid NPEs by checking for null."
diff --git a/car-usb-handler/src/android/car/usb/handler/BootUsbScanner.java b/car-usb-handler/src/android/car/usb/handler/BootUsbScanner.java
index 5255f16..18dabdc 100644
--- a/car-usb-handler/src/android/car/usb/handler/BootUsbScanner.java
+++ b/car-usb-handler/src/android/car/usb/handler/BootUsbScanner.java
@@ -20,11 +20,13 @@
             } else {
                 UsbDeviceConnection connection = UsbUtil.openConnection(manager, device);
                 try {
-                    if (AoapInterface.isSupported(connection)) {
+                    if (connection != null && AoapInterface.isSupported(connection)) {
                         handle(context, device);
                     }
                 } finally {
-                    connection.close();
+                    if (connection != null) {
+                        connection.close();
+                    }
                 }
             }
         }
diff --git a/car-usb-handler/src/android/car/usb/handler/UsbDeviceHandlerResolver.java b/car-usb-handler/src/android/car/usb/handler/UsbDeviceHandlerResolver.java
index c0eebbe..51c2838 100644
--- a/car-usb-handler/src/android/car/usb/handler/UsbDeviceHandlerResolver.java
+++ b/car-usb-handler/src/android/car/usb/handler/UsbDeviceHandlerResolver.java
@@ -15,6 +15,7 @@
  */
 package android.car.usb.handler;
 
+import android.annotation.Nullable;
 import android.car.IUsbAoapSupportCheckService;
 import android.content.ComponentName;
 import android.content.Context;
@@ -37,13 +38,16 @@
 import android.os.RemoteException;
 import android.util.Log;
 import android.util.Pair;
+
 import com.android.internal.util.XmlUtils;
+
+import org.xmlpull.v1.XmlPullParser;
+
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Queue;
-import org.xmlpull.v1.XmlPullParser;
 
 /** Resolves supported handlers for USB device. */
 public final class UsbDeviceHandlerResolver {
@@ -70,7 +74,7 @@
 
     private class DeviceContext {
         public final UsbDevice usbDevice;
-        public final UsbDeviceConnection connection;
+        @Nullable public final UsbDeviceConnection connection;
         public final UsbDeviceSettings settings;
         public final List<UsbDeviceSettings> activeDeviceSettings;
         public final Queue<Pair<ResolveInfo, DeviceFilter>> mActiveDeviceOptions =
@@ -484,7 +488,8 @@
         }
         DeviceContext deviceContext =
                 new DeviceContext(device, UsbDeviceSettings.constructSettings(device), settings);
-        if (AoapInterface.isSupported(deviceContext.connection)) {
+        if (deviceContext.connection != null
+                && AoapInterface.isSupported(deviceContext.connection)) {
             deviceContext.mActiveDeviceOptions.addAll(getDeviceMatches(device, intent, true));
             queryNextAoapHandler(deviceContext);
         } else {
@@ -516,6 +521,10 @@
 
     private void requestAoapSwitch(UsbDevice device, DeviceFilter filter) {
         UsbDeviceConnection connection = UsbUtil.openConnection(mUsbManager, device);
+        if (connection == null) {
+            Log.e(TAG, "Failed to connect to usb device.");
+            return;
+        }
         try {
             UsbUtil.sendAoapAccessoryStart(
                     connection,
diff --git a/car-usb-handler/src/android/car/usb/handler/UsbUtil.java b/car-usb-handler/src/android/car/usb/handler/UsbUtil.java
index 939fe3f..823c660 100644
--- a/car-usb-handler/src/android/car/usb/handler/UsbUtil.java
+++ b/car-usb-handler/src/android/car/usb/handler/UsbUtil.java
@@ -15,10 +15,12 @@
  */
 package android.car.usb.handler;
 
+import android.annotation.Nullable;
 import android.hardware.usb.UsbDevice;
 import android.hardware.usb.UsbDeviceConnection;
 import android.hardware.usb.UsbManager;
 import android.text.TextUtils;
+
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -30,7 +32,7 @@
 class UsbUtil {
     public static List<UsbDevice> findAllPossibleAndroidDevices(UsbManager usbManager) {
         HashMap<String, UsbDevice> devices = usbManager.getDeviceList();
-    ArrayList<UsbDevice> androidDevices = new ArrayList<>(devices.size());
+        ArrayList<UsbDevice> androidDevices = new ArrayList<>(devices.size());
         for (UsbDevice device : devices.values()) {
             UsbDeviceConnection connection = openConnection(usbManager, device);
             if (AoapInterface.isSupported(connection)) {
@@ -41,11 +43,12 @@
         return androidDevices;
     }
 
+    @Nullable
     public static UsbDeviceConnection openConnection(UsbManager manager, UsbDevice device) {
-    manager.grantPermission(device);
-    return manager.openDevice(device);
+        manager.grantPermission(device);
+        return manager.openDevice(device);
     }
-    
+
     public static void sendAoapAccessoryStart(UsbDeviceConnection connection, String manufacturer,
             String model, String description, String version, String uri, String serial)
                     throws IOException {