package org.jsystemtest.mobile.core.device;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.jsystemtest.mobile.core.ConnectionException;

import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.InstallException;

public class USBDevice extends AbstractAndroidDevice {

	private final static Logger logger = Logger.getLogger(USBDevice.class);

	public USBDevice(AndroidDebugBridge adb, IDevice device) throws Exception {
		super(adb, device);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void connect() throws ConnectionException {
		// Done automatically via IDeviceChangeListener

	}

	@Override
	public void disconnect() {
		// Done automatically via IDeviceChangeListener

	}

	/**
	 * Install APK on device
	 * 
	 * @param apkLocation
	 * @throws InstallException
	 */
	public void installPackage(String apkLocation, boolean reinstall) throws InstallException {
		final String result = device.installPackage(apkLocation, reinstall);
		if (result != null) {
			throw new InstallException("Failed to install: " + result, null);
		}

	}

	/**
	 * Limor B
	 * 
	 * Execute test on device.
	 * 
	 * @param pakageName
	 * @param testClassName
	 * @param testName
	 * @throws Exception
	 */
	public void runTestOnDevice(String pakageName, String testClassName, String testName) throws Exception {

		if (null == adbLocation || !adbLocation.exists()) {
			throw new IOException("Can't find adb location");
		}
		String cmd = adbLocation.getAbsolutePath() + "\\adb -s "
				+ device.getSerialNumber() + " shell am instrument -e class "
				+ pakageName + "." + testClassName + "#" + testName + " "
				+ pakageName + "/android.test.InstrumentationTestRunner";
		logger.info("Try to run command:" + cmd);
		Runtime run = Runtime.getRuntime();
		Process pr = run.exec(cmd);
		try {
			pr.waitFor();
			Thread.sleep(TimeUnit.SECONDS.toMillis(2));
			BufferedReader stdInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
			String s;
			String allBuffer = " ";
			while ((s = stdInput.readLine()) != null) {
				allBuffer+=s;
			} 
			if(allBuffer.contains("Exception")){
				Exception e = new Exception(allBuffer);
				logger.error(e);
				throw e;
			}
			
			
		} catch (InterruptedException e) {
			// Don't care
		}
	}

}

@@ -105,37 +105,8 @@
 n;%0D%0A
-import java.io.InputStream;%0D%0A
 impo
@@ -638,51 +638,8 @@
 );%0D%0A
-%09%09// TODO Auto-generated constructor stub%0D%0A
 %09%7D%0D%0A
@@ -1375,24 +1375,36 @@
 %09 * @throws 
+IOException,
 Exception%0D%0A%09
@@ -2191,16 +2191,24 @@
 %09%09String
+Builder 
  allBuff
@@ -2216,12 +2216,29 @@
 r = 
-%22 %22;
+new StringBuilder(); 
 %0D%0A%09%09
@@ -2300,11 +2300,18 @@
 ffer
-+=s
+.append(s)
 ;%0D%0A%09
@@ -2336,16 +2336,15 @@
 fer.
-contains
+indexOf
 (%22Ex
@@ -2352,16 +2352,20 @@
 eption%22)
+!=-1
 )%7B%0D%0A%09%09%09%09
@@ -2401,16 +2401,27 @@
 llBuffer
+.toString()
 );%0D%0A%09%09%09%09
@@ -2463,21 +2463,16 @@
 %7D%0D%0A%09%09%09%0D%0A
-%09%09%09%0D%0A
 %09%09%7D catc

Fix : comment in git