package xyz.ylimit.androcov;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;

/**
 * Created by liyc on 12/23/15.
 * useful utils
 */
public class Util {
    public static final Logger LOGGER = Logger.getLogger(Config.PROJECT_NAME);

    public static String getTimeString() {
        long timeMillis = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-hhmmss");
        Date date = new Date(timeMillis);
        return sdf.format(date);
    }

    public static void logException(Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        Util.LOGGER.warning(sw.toString());
    }

    public static float safeDivide(int obfuscated, int total) {
        if (total <= 0) return 1;
        return (float) obfuscated / total;
    }

    public static void signAPK(String apkPath) {
        Runtime r = Runtime.getRuntime();
        URL keystoreUrl = Instrumenter.class.getClass().getResource("/debug.keystore");
        String keystorePath = String.format("%s/debug.keystore", Config.tempDirPath);
        String signCmd = String.format(
                "jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -storepass android " +
                        "-keystore %s %s androiddebugkey", keystorePath, apkPath);
        try {
            FileUtils.copyURLToFile(keystoreUrl, new File(keystorePath));
            Process p = r.exec(signCmd);
            p.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

}

@@ -162,32 +162,125 @@
 o.StringWriter;%0A
+import java.io.InputStream;%0Aimport java.io.InputStreamReader;%0Aimport java.io.BufferedReader;%0A
 import java.net.
@@ -1743,138 +1743,1247 @@
 d);%0A
-            p.waitFor();%0A        %7D catch (IOException %7C InterruptedException e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A
+%0A            ReadStream s1 = new ReadStream(%22stdin%22, p.getInputStream());%0A            ReadStream s2 = new ReadStream(%22stderr%22, p.getErrorStream());%0A            s1.start();%0A            s2.start();%0A%0A            p.waitFor();%0A        %7D catch (IOException %7C InterruptedException e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%7D%0A%0A/**%0A * Created by yangzy on 11/14/16.%0A * to solve Runtime.exec hang problem%0A */%0Aclass ReadStream implements Runnable %7B%0A    String name;%0A    InputStream is;%0A    Thread thread;%0A    public ReadStream(String name, InputStream is) %7B%0A        this.name = name;%0A        this.is = is;%0A    %7D%0A    public void start () %7B%0A        thread = new Thread (this);%0A        thread.start ();%0A    %7D%0A    public void run () %7B%0A        try %7B%0A            InputStreamReader isr = new InputStreamReader (is);%0A            BufferedReader br = new BufferedReader (isr);%0A            while (true) %7B%0A                String s = br.readLine ();%0A                if (s == null) break;%0A                System.out.println (%22%5B%22 + name + %22%5D %22 + s);%0A            %7D%0A            is.close ();%0A        %7D catch (Exception ex) %7B%0A            System.out.println (%22Problem reading stream %22 + name + %22... :%22 + ex);%0A            ex.printStackTrace ();%0A        %7D%0A    %7D
 %0A%7D%0A

fix jarsigner hang problem