Skip to content

Instantly share code, notes, and snippets.

@sjmach
Created August 19, 2017 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjmach/c1d4034b70e60da5d1ba6ab7fa41284b to your computer and use it in GitHub Desktop.
Save sjmach/c1d4034b70e60da5d1ba6ab7fa41284b to your computer and use it in GitHub Desktop.
A utility class for ADB
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;
import static com.automation.util.FileUtil.readPropertyFile;
/**
* Created by Sundeep Machado on 09/09/16.
*/
public class ADBUtil {
public static ProcessBuilder pb = null;
public static Process pc = null;
public static void printADBLog( String tag ){
Properties prop = readPropertyFile();
clearADBLogs();
pb = new ProcessBuilder("adb", "logcat", "-s", tag);
File output = new File(prop.getProperty("adbLogFile"));
if(output.delete()){
System.out.println("ADB File Deleted");
}
pb.redirectOutput(output);
try {
pc = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Clear the ADB logcat
*/
public static void clearADBLogs(){
pb = new ProcessBuilder("adb", "logcat", "-c");
try {
pc = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
pc.destroy();
}
/**
* Stop the ADB logging service
* @throws IOException
*/
public static void stopADBLogger() throws IOException {
pc.destroy();
}
/**
* This function waits till the ADb tag is present in log file
*/
public static void waitTillTagPresent(String f, String searchString){
boolean result = false;
Scanner in = null;
do {
try {
in = new Scanner(new FileReader(f));
while (in.hasNextLine() && !result) {
result = in.nextLine().indexOf(searchString) >= 0;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) { /* ignore */ }
}
}
while (result=false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment