Support the ANDROID_SERIAL property for targeting devices

Add a check in the ConnectedDeviceProvider for a specific
device serial to be targeted against based off the value
stored in the ANDROID_SERIAL environment variable.

The default behavior of running across all connected devices
will occur if this variable is not defined or is empty. An
exception will be thrown if the targeted DSN is not found.

Defect: 75407

Change-Id: Ib4dba8398cbb249420d13eb48da076ac67c61609
diff --git a/build-system/builder/src/main/java/com/android/builder/testing/ConnectedDeviceProvider.java b/build-system/builder/src/main/java/com/android/builder/testing/ConnectedDeviceProvider.java
index c23ad2a..4264f71 100644
--- a/build-system/builder/src/main/java/com/android/builder/testing/ConnectedDeviceProvider.java
+++ b/build-system/builder/src/main/java/com/android/builder/testing/ConnectedDeviceProvider.java
@@ -81,8 +81,17 @@
                 throw new RuntimeException("No connected devices!", null);
             }
 
+            final String androidSerial = System.getenv("ANDROID_SERIAL");
+            final Boolean isValidSerial = androidSerial != null && !androidSerial.isEmpty();
             for (IDevice iDevice : devices) {
-                localDevices.add(new ConnectedDevice(iDevice));
+                if (!isValidSerial || iDevice.getSerialNumber().equals(androidSerial)) {
+                    localDevices.add(new ConnectedDevice(iDevice));
+                }
+            }
+
+            if (isValidSerial && (localDevices.size() == 0)) {
+                throw new RuntimeException("Connected device with serial " + androidSerial
+                        + " not found!", null);
             }
         } catch (Exception e) {
             throw new DeviceException(e);