package de.danoeh.antennapod;

import android.os.Build;
import android.util.Log;

import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import de.danoeh.antennapod.core.preferences.UserPreferences;

public class CrashReportWriter implements Thread.UncaughtExceptionHandler {

    private static final String TAG = "CrashReportWriter";

    private final Thread.UncaughtExceptionHandler defaultHandler;

    public CrashReportWriter() {
        defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
    }

    public static File getFile() {
        return new File(UserPreferences.getDataFolder(null), "crash-report.log");
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        File path = getFile();
        PrintWriter out = null;
        try {
            out = new PrintWriter(new FileWriter(path));
            out.println("[ Crash info ]");
            out.println("Time: " + new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault()).format(new Date()));
            out.println("AntennaPod version: " + BuildConfig.VERSION_NAME);
            out.println();
            out.println("[ StackTrace ]");
            ex.printStackTrace(out);
        } catch (IOException e) {
            Log.e(TAG, Log.getStackTraceString(e));
        } finally {
            IOUtils.closeQuietly(out);
        }
        defaultHandler.uncaughtException(thread, ex);
    }

    public static String getSystemInfo() {
        return "[ Environment ]" +
                "\nAndroid version: " + Build.VERSION.RELEASE +
                "\nOS version: " + System.getProperty("os.version") +
                "\nAntennaPod version: " + BuildConfig.VERSION_NAME +
                "\nModel: " + Build.MODEL +
                "\nDevice: " + Build.DEVICE +
                "\nProduct: " + Build.PRODUCT;
    }
}

@@ -1041,17 +1041,18 @@
 rintln(%22
-%5B
+##
  Crash i
@@ -1054,18 +1054,16 @@
 ash info
- %5D
 %22);%0A    
@@ -1307,17 +1307,18 @@
 rintln(%22
-%5B
+##
  StackTr
@@ -1320,18 +1320,48 @@
 ackTrace
- %5D
+%22);%0A            out.println(%22%60%60%60
 %22);%0A    
@@ -1389,24 +1389,56 @@
 Trace(out);%0A
+            out.println(%22%60%60%60%22);%0A
         %7D ca
@@ -1704,9 +1704,10 @@
 rn %22
-%5B
+##
  Env
@@ -1718,13 +1718,9 @@
 ment
- %5D%22 +
+%22
 %0A   
@@ -1727,24 +1727,26 @@
             
+ +
  %22%5CnAndroid 
@@ -1779,18 +1779,16 @@
 .RELEASE
- +
 %0A       
@@ -1791,24 +1791,26 @@
             
+ +
  %22%5CnOS versi
@@ -1849,19 +1849,18 @@
 ersion%22)
- +
 %0A
+ 
         
@@ -1862,24 +1862,25 @@
             
++
  %22%5CnAntennaP
@@ -1919,19 +1919,18 @@
 ION_NAME
- +
 %0A
+ 
         
@@ -1932,24 +1932,25 @@
             
++
  %22%5CnModel: %22
@@ -1963,18 +1963,16 @@
 ld.MODEL
- +
 %0A       
@@ -1975,24 +1975,26 @@
             
+ +
  %22%5CnDevice: 
@@ -2013,11 +2013,10 @@
 VICE
- +
 %0A
+ 
     
@@ -2026,16 +2026,17 @@
         
++
  %22%5CnProd

Write crash info in Markdown