/*
 *  Copyright (c) Facebook, Inc.
 *
 *  This source code is licensed under the MIT license found in the LICENSE
 *  file in the root directory of this source tree.
 *
 */
package com.facebook.flipper.plugins.crashreporter;

import android.app.Activity;
import android.support.annotation.Nullable;
import com.facebook.flipper.core.FlipperConnection;
import com.facebook.flipper.core.FlipperObject;
import com.facebook.flipper.core.FlipperPlugin;
import java.io.PrintWriter;
import java.io.StringWriter;

public class CrashReporterPlugin implements FlipperPlugin {
  public static final String ID = "CrashReporter";

  @Nullable private Activity mActivity;

  @Nullable private FlipperConnection mConnection;

  @Nullable private Thread.UncaughtExceptionHandler prevHandler;

  /*
   * Activity to be used to display incoming messages
   */
  public void setActivity(Activity activity) {
    mActivity = activity;
  }

  @Override
  public void onConnect(FlipperConnection connection) {
    mConnection = connection;
    prevHandler = Thread.getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler(
        new Thread.UncaughtExceptionHandler() {
          @Override
          public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
            if (mConnection != null) {
              FlipperConnection connection = mConnection;
              StringWriter sw = new StringWriter();
              PrintWriter pw = new PrintWriter(sw);
              paramThrowable.printStackTrace(pw);
              connection.send(
                  "crash-report",
                  new FlipperObject.Builder()
                      .put("callstack", sw.toString())
                      .put("cause", paramThrowable.toString())
                      .put("reason", paramThrowable.toString())
                      .build());
            }
            if (prevHandler != null) {
              prevHandler.uncaughtException(paramThread, paramThrowable);
            }
          }
        });
  }

  @Override
  public void onDisconnect() {
    mConnection = null;
    Thread.setDefaultUncaughtExceptionHandler(prevHandler);
  }

  @Override
  public boolean runInBackground() {
    return true;
  }

  @Override
  public String getId() {
    return ID;
  }
}

@@ -293,16 +293,63 @@
 llable;%0A
+import com.facebook.flipper.core.FlipperArray;%0A
 import c
@@ -491,65 +491,8 @@
 gin;
-%0Aimport java.io.PrintWriter;%0Aimport java.io.StringWriter;
 %0A%0Apu
@@ -1382,145 +1382,280 @@
     
-StringWriter sw = new StringWriter();%0A              PrintWriter pw = new PrintWriter(sw);%0A              paramThrowable.printStackTrace(pw
+FlipperArray.Builder builder = new FlipperArray.Builder();%0A              for (StackTraceElement stackTraceElement : paramThrowable.getStackTrace()) %7B%0A                builder.put(stackTraceElement.toString());%0A              %7D%0A              FlipperArray arr = builder.build(
 );%0A 
@@ -1808,21 +1808,11 @@
 k%22, 
-sw.toString()
+arr
 )%0A  
@@ -1841,12 +1841,11 @@
 ut(%22
-caus
+nam
 e%22, 
@@ -1919,32 +1919,34 @@
 amThrowable.
-toString
+getMessage
 ())%0A        

Change crash reporter plugin to send callstack array