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

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

public class Pulse implements ActivityLifecycleCallbacks {
    private static Pulse pulse;

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

                RaygunClient.SendPulseEvent("session_start");
            }
        }
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
        System.out.println("CREATED");
    }

    @Override
    public void onActivityStarted(Activity activity) {
        System.out.println("STARTED");
    }

    @Override
    public void onActivityResumed(Activity activity) {
        System.out.println("RESUMED");
    }

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

    @Override
    public void onActivityStopped(Activity activity) {
        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");
    }
}

@@ -258,18 +258,16 @@
 backs %7B%0A
-  
   privat
@@ -285,18 +285,136 @@
 lse 
+_
 pulse;%0A
-%0A
   
+private static Activity _mainActivity;%0A  private static Activity _currentActivity;%0A  private static long _startTime;%0A%0A
   pr
@@ -469,23 +469,38 @@
 ) %7B%0A    
-    if(
+if (_pulse == null && 
 mainActi
@@ -511,30 +511,24 @@
  != null) %7B%0A
-      
       Applic
@@ -582,22 +582,16 @@
 ();%0A
-      
       if
 (app
@@ -586,16 +586,17 @@
       if
+ 
 (applica
@@ -619,24 +619,55 @@
         
-  
+_mainActivity = mainActivity;%0A
       
+  _
 pulse = 
@@ -679,24 +679,16 @@
 ulse();%0A
-        
         
@@ -734,16 +734,17 @@
 llbacks(
+_
 pulse);%0A
@@ -744,24 +744,16 @@
 ulse);%0A%0A
-        
         
@@ -810,33 +810,402 @@
     
-    %7D%0A        %7D%0A    %7D%0A%0A  
+_currentActivity = _mainActivity;%0A        _startTime = System.nanoTime();%0A      %7D%0A    %7D%0A  %7D%0A%0A  protected static void Detach() %7B%0A    if(_pulse != null && _mainActivity != null && _mainActivity.getApplication() != null) %7B%0A      _mainActivity.getApplication().unregisterActivityLifecycleCallbacks(_pulse);%0A      _mainActivity = null;%0A      _currentActivity = null;%0A      _pulse = null;%0A    %7D%0A  %7D%0A%0A
   @O
@@ -1204,34 +1204,32 @@
  %7D%0A%0A  @Override%0A
-  
   public void on
@@ -1272,36 +1272,32 @@
 undle bundle) %7B%0A
-    
     System.out.p
@@ -1311,33 +1311,29 @@
 %22CREATED%22);%0A
-  
   %7D%0A%0A
-  
   @Override%0A
@@ -1324,34 +1324,32 @@
  %7D%0A%0A  @Override%0A
-  
   public void on
@@ -1377,36 +1377,32 @@
 ity activity) %7B%0A
-    
     System.out.p
@@ -1416,33 +1416,29 @@
 %22STARTED%22);%0A
-  
   %7D%0A%0A
-  
   @Override%0A
@@ -1429,34 +1429,32 @@
  %7D%0A%0A  @Override%0A
-  
   public void on
@@ -1482,36 +1482,32 @@
 ity activity) %7B%0A
-    
     System.out.p
@@ -1521,33 +1521,29 @@
 %22RESUMED%22);%0A
-  
   %7D%0A%0A
-  
   @Override%0A
@@ -1534,34 +1534,32 @@
  %7D%0A%0A  @Override%0A
-  
   public void on
@@ -1586,36 +1586,32 @@
 ity activity) %7B%0A
-    
     System.out.p
@@ -1624,33 +1624,29 @@
 (%22PAUSED%22);%0A
-  
   %7D%0A%0A
-  
   @Override%0A
@@ -1637,34 +1637,32 @@
  %7D%0A%0A  @Override%0A
-  
   public void on
@@ -1690,36 +1690,32 @@
 ity activity) %7B%0A
-    
     System.out.p
@@ -1729,33 +1729,29 @@
 %22STOPPED%22);%0A
-  
   %7D%0A%0A
-  
   @Override%0A
@@ -1742,34 +1742,32 @@
  %7D%0A%0A  @Override%0A
-  
   public void on
@@ -1824,28 +1824,24 @@
 e bundle) %7B%0A
-    
     System.o
@@ -1864,17 +1864,13 @@
 %22);%0A
-  
   %7D%0A%0A
-  
   @O
@@ -1877,18 +1877,16 @@
 verride%0A
-  
   public
@@ -1928,28 +1928,24 @@
 activity) %7B%0A
-    
     System.o
@@ -1969,16 +1969,14 @@
 OYED%22);%0A
-  
   %7D%0A%7D%0A

Attach and detach logic for Pulse