package com.github.pedrovgs.androidwifiadb.adb;

import com.github.pedrovgs.androidwifiadb.Device;
import java.util.LinkedList;
import java.util.List;

public class ADBParser {

  private static final String MODEL_INDICATOR = "model:";
  private static final String DEVICE_INDICATOR = "device:";

  public List<Device> parseGetDevicesOutput(String adbDevicesOutput) {
    List<Device> devices = new LinkedList<Device>();
    String[] splittedOutput = adbDevicesOutput.split("\\n");
    for (String line : splittedOutput) {
      String[] deviceLine = line.split("\\t");
      String id = deviceLine[0].substring(0, deviceLine[0].indexOf(" "));
      if (id.contains(".") || id.contains("List")) {
        continue;
      }
      int start = line.indexOf(MODEL_INDICATOR) + MODEL_INDICATOR.length();
      int end = line.indexOf(DEVICE_INDICATOR) - 1;
      if (end < 0) {
        end = line.length();
      }
      String name = line.substring(start, end);
      Device device = new Device(name, id);
      devices.add(device);
    }
    return devices;
  }

  public String parseGetDeviceIp(String ipInfoOutput) {
    if (ipInfoOutput.isEmpty()) {
      return "";
    }

    String[] splittedOutput = ipInfoOutput.split("\\n");
    int end = splittedOutput[1].indexOf("/");
    int start = splittedOutput[1].indexOf("t");
    return splittedOutput[1].substring(start + 2, end);
  }
}

@@ -288,16 +288,190 @@
 evice:%22;
+%0A  private static final String IP_SEPARATOR = %22.%22;%0A  private static final String END_DEVICE_IP_INDICATOR = %22/%22;%0A  private static final String START_DEVICE_IP_INDICATOR = %22t%22;
 %0A%0A  publ
@@ -645,24 +645,90 @@
 lit(%22%5C%5Cn%22);%0A
+    if (splittedOutput.length == 1) %7B%0A      return devices;%0A    %7D%0A
     for (Str
@@ -903,34 +903,20 @@
 ins(
-%22.%22) %7C%7C id.contains(%22List%22
+IP_SEPARATOR
 )) %7B
@@ -1489,19 +1489,39 @@
 indexOf(
-%22/%22
+END_DEVICE_IP_INDICATOR
 );%0A    i
@@ -1557,19 +1557,41 @@
 indexOf(
-%22t%22
+START_DEVICE_IP_INDICATOR
 );%0A    r

Fix bug related to the getDevices parsing when there are no devices connected