Created
August 19, 2017 07:17
-
-
Save sjmach/c1d4034b70e60da5d1ba6ab7fa41284b to your computer and use it in GitHub Desktop.
A utility class for ADB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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