package com.njlabs.showjava.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

import com.njlabs.showjava.ui.JavaExplorer;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Created by Niranjan on 27-05-2015.
 */
@SuppressWarnings({"FieldCanBeLocal", "StringBufferReplaceableByString"})
public class ExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler {

    private final Context myContext;
    private final String LINE_SEPARATOR = "\n";
    private final String sourceDir;
    private final String packageID;

    public ExceptionHandler(Context context, String sourceDir, String packageID) {
        this.myContext = context;
        this.sourceDir = sourceDir;
        this.packageID = packageID;
    }

    public void uncaughtException(Thread thread, Throwable exception) {

        StringWriter stackTrace = new StringWriter();
        exception.printStackTrace(new PrintWriter(stackTrace));
        StringBuilder errorReport = new StringBuilder();
        errorReport.append("************ CAUSE OF ERROR ************\n\n");
        errorReport.append(stackTrace.toString());

        errorReport.append("\n************ DEVICE INFORMATION ***********\n");
        errorReport.append("Brand: ");
        errorReport.append(Build.BRAND);
        errorReport.append(LINE_SEPARATOR);
        errorReport.append("Device: ");
        errorReport.append(Build.DEVICE);
        errorReport.append(LINE_SEPARATOR);
        errorReport.append("Model: ");
        errorReport.append(Build.MODEL);
        errorReport.append(LINE_SEPARATOR);
        errorReport.append("Id: ");
        errorReport.append(Build.ID);
        errorReport.append(LINE_SEPARATOR);
        errorReport.append("Product: ");
        errorReport.append(Build.PRODUCT);
        errorReport.append(LINE_SEPARATOR);
        errorReport.append("\n************ FIRMWARE ************\n");
        errorReport.append("SDK: ");
        errorReport.append(Build.VERSION.SDK_INT);
        errorReport.append(LINE_SEPARATOR);
        errorReport.append("Release: ");
        errorReport.append(Build.VERSION.RELEASE);
        errorReport.append(LINE_SEPARATOR);
        errorReport.append("Incremental: ");
        errorReport.append(Build.VERSION.INCREMENTAL);
        errorReport.append(LINE_SEPARATOR);

        Log.e("com.njlabs.showjava", errorReport.toString());

        Toast.makeText(myContext, "There was an error decompiling this app. Showing incomplete source.", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(myContext, JavaExplorer.class);
        intent.putExtra("java_source_dir", sourceDir);
        intent.putExtra("package_id", packageID);
        myContext.startActivity(intent);

        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(10);
    }

}

@@ -96,33 +96,8 @@
 nt;%0A
-import android.os.Build;%0A
 impo
@@ -147,16 +147,60 @@
 Toast;%0A%0A
+import com.crashlytics.android.Crashlytics;%0A
 import c
@@ -923,24 +923,70 @@
 ception) %7B%0A%0A
+        Crashlytics.logException(exception);%0A%0A
         Stri
@@ -1280,1190 +1280,8 @@
 );%0A%0A
-        errorReport.append(%22%5Cn************ DEVICE INFORMATION ***********%5Cn%22);%0A        errorReport.append(%22Brand: %22);%0A        errorReport.append(Build.BRAND);%0A        errorReport.append(LINE_SEPARATOR);%0A        errorReport.append(%22Device: %22);%0A        errorReport.append(Build.DEVICE);%0A        errorReport.append(LINE_SEPARATOR);%0A        errorReport.append(%22Model: %22);%0A        errorReport.append(Build.MODEL);%0A        errorReport.append(LINE_SEPARATOR);%0A        errorReport.append(%22Id: %22);%0A        errorReport.append(Build.ID);%0A        errorReport.append(LINE_SEPARATOR);%0A        errorReport.append(%22Product: %22);%0A        errorReport.append(Build.PRODUCT);%0A        errorReport.append(LINE_SEPARATOR);%0A        errorReport.append(%22%5Cn************ FIRMWARE ************%5Cn%22);%0A        errorReport.append(%22SDK: %22);%0A        errorReport.append(Build.VERSION.SDK_INT);%0A        errorReport.append(LINE_SEPARATOR);%0A        errorReport.append(%22Release: %22);%0A        errorReport.append(Build.VERSION.RELEASE);%0A        errorReport.append(LINE_SEPARATOR);%0A        errorReport.append(%22Incremental: %22);%0A        errorReport.append(Build.VERSION.INCREMENTAL);%0A        errorReport.append(LINE_SEPARATOR);%0A%0A
     

Simplify exception handler