package main.java.com.mindscapehq.android.raygun4android;

import android.app.Activity;
import android.app.Application;
import android.app.Application.ActivityLifecycleCallbacks;
import android.os.Bundle;

import java.util.concurrent.TimeUnit;

public class Pulse implements ActivityLifecycleCallbacks {
  private static Pulse _pulse;
  private static Activity _mainActivity;
  private static Activity _currentActivity;
  private static long _startTime;

  protected static void Attach(Activity mainActivity) {
    if (_pulse == null && mainActivity != null) {
      Application application = mainActivity.getApplication();
      if (application != null) {
        _mainActivity = mainActivity;
        _pulse = new Pulse();
        application.registerActivityLifecycleCallbacks(_pulse);

        RaygunClient.SendPulseEvent("session_start");
        _currentActivity = _mainActivity;
        _startTime = System.nanoTime();
      }
    }
  }

  protected static void Detach() {
    if(_pulse != null && _mainActivity != null && _mainActivity.getApplication() != null) {
      _mainActivity.getApplication().unregisterActivityLifecycleCallbacks(_pulse);
      _mainActivity = null;
      _currentActivity = null;
      _pulse = null;
    }
  }

  protected static void SendRemainingActivity() {
    if(_pulse != null) {
      if(_currentActivity != null) {
        String activityName = getActivityName(_currentActivity);

        long diff = System.nanoTime() - _startTime;
        double duration = TimeUnit.NANOSECONDS.toMillis(diff);

        RaygunClient.SendPulsePageTimingEvent(activityName, duration);
      }
      RaygunClient.SendPulseEvent("session_end");
    }
  }

  @Override
  public void onActivityCreated(Activity activity, Bundle bundle) {
    if(_currentActivity == null) {
      RaygunClient.SendPulseEvent("session_start");
    }

    if(activity != _currentActivity) {
      _currentActivity = activity;
      _startTime = System.nanoTime();
    }
    System.out.println("CREATED");
  }

  @Override
  public void onActivityStarted(Activity activity) {
    if(_currentActivity == null) {
      RaygunClient.SendPulseEvent("session_start");
    }

    if(activity != _currentActivity) {
      _currentActivity = activity;
      _startTime = System.nanoTime();
    }
    System.out.println("STARTED");
  }

  @Override
  public void onActivityResumed(Activity activity) {
    if(_currentActivity == null) {
      RaygunClient.SendPulseEvent("session_start");
    }

    String activityName = getActivityName(activity);
    double duration = 0;
    if(activity == _currentActivity) {
      long diff = System.nanoTime() - _startTime;
      duration = TimeUnit.NANOSECONDS.toMillis(diff);
    }
    _currentActivity = activity;

    RaygunClient.SendPulsePageTimingEvent(activityName, duration);
    System.out.println("RESUMED");
  }

  @Override
  public void onActivityPaused(Activity activity) {
    //System.out.println("PAUSED");
  }

  @Override
  public void onActivityStopped(Activity activity) {
    if(activity == _currentActivity) {
      _currentActivity = null;
      RaygunClient.SendPulseEvent("session_end");
    }
    System.out.println("STOPPED");
  }

  @Override
  public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
    //System.out.println("SAVE");
  }

  @Override
  public void onActivityDestroyed(Activity activity) {
    //System.out.println("DESTROYED");
  }

  private static String getActivityName(Activity activity) {
    return activity.getClass().getSimpleName();
  }
}

@@ -1961,32 +1961,34 @@
 me();%0A    %7D%0A    
+//
 System.out.print
@@ -1998,24 +1998,24 @@
 %22CREATED%22);%0A
-
   %7D%0A%0A  @Over
@@ -2280,32 +2280,34 @@
 me();%0A    %7D%0A    
+//
 System.out.print
@@ -2813,24 +2813,26 @@
 ation);%0A    
+//
 System.out.p
@@ -3151,20 +3151,22 @@
 ;%0A    %7D%0A
-
     
+//
 System.o

Remove debugging