package com.omnipasteapp.droidbugfreak;

import android.app.Application;

import com.omnipasteapp.droidbugfreak.components.ErrorReportHandler;
import com.omnipasteapp.droidbugfreak.components.ErrorReportQueue;
import com.omnipasteapp.droidbugfreak.components.ErrorReportQueueListener;

public class AgileReporter implements ReportingService {

  static ReportingService instance;

  public static void init() {
    Initializer.initialize();
  }

  public static ReportingService getInstance() {
    return instance;
  }

  AgileReporter() {
  }

  @Override
  public void beginReport(Throwable exc) {
    ErrorReport errorReport = createReport(exc);
    queue(errorReport);
  }

  private ErrorReport createReport(Throwable exc) {
    return ErrorReport.fromException(exc);
  }

  private void queue(ErrorReport errorReport) {
    ErrorReportQueue errorReportQueue = GlobalConfig.getServiceLocator().getService(ErrorReportQueue.class);

    errorReportQueue.enqueue(errorReport);
  }

  public static void dispose() {
    GlobalConfig.getServiceLocator().getService(ErrorReportQueueListener.class).dispose();
    GlobalConfig.getServiceLocator().getService(ErrorReportHandler.class).dispose();

    instance = null;
  }

  public static void hook(Application app) {
    init();

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
      @Override
      public void uncaughtException(Thread thread, Throwable ex) {
        instance.beginReport(ex);
      }
    });
  }
}

@@ -1424,16 +1424,22 @@
  thread,
+ final
  Throwab
@@ -1459,34 +1459,158 @@
     
-instance.beginReport(ex);%0A
+new Thread(new Runnable() %7B%0A          @Override%0A          public void run() %7B%0A            instance.beginReport(ex);%0A%09    AgileReporter.dispose();%0A    
     
@@ -1621,14 +1621,41 @@
     
+   
+ 
 %7D)
+.start();%0A      %7D%0A    %7D)
 ;%0A  
-%7D%0A
 %7D%0A
+%7D

send a request on a different thread