package org.commcare.android.framework;

import org.commcare.android.tasks.templates.CommCareTask;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.support.v4.app.Fragment;
import android.util.Log;
/**
 * @author ctsims
 *
 */
public class StateFragment extends Fragment {
    
    CommCareActivity boundActivity;
    CommCareActivity lastActivity;
    
    CommCareTask currentTask;
    /**
     * Hold a reference to the parent Activity so we can report the
     * task's current progress and results. The Android framework 
     * will pass us a reference to the newly created Activity after 
     * each configuration change.
     */
      @Override
      public void onAttach(Activity activity) {
        super.onAttach(activity);
        if(activity instanceof CommCareActivity) {
            this.boundActivity = (CommCareActivity)activity;
            this.boundActivity.stateHolder = this;
            if(this.currentTask != null && this.currentTask.getStatus() == AsyncTask.Status.RUNNING) {
                this.currentTask.connect(boundActivity);
            }
        }
      }
      
      public CommCareActivity getPreviousState() { 
          return lastActivity;
      }
      
      public void connectTask(CommCareTask task) {
          this.currentTask = task;
      }
     
      /**
       * This method will only be called once when the retained
       * Fragment is first created.
       */
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     
        // Retain this fragment across configuration changes.
        setRetainInstance(true);
      }
     
      /**
       * Set the callback to null so we don't accidentally leak the 
       * Activity instance.
       */
      @Override
      public void onDetach() {
        super.onDetach();
        if(this.boundActivity != null) {
            lastActivity = boundActivity;
        }
        if(currentTask != null) {
            Log.i("CommCareUI", "Detaching activity from current task: " + this.currentTask);
            currentTask.disconnect();
            unlock();
        }
      }

    public void cancelTask() {
        if(currentTask != null) {
            currentTask.cancel(false);
        }
    }

    private WakeLock wakelock;

    public synchronized void wakelock(int lockLevel) {
        if(wakelock != null) {
            if(wakelock.isHeld()) {
                wakelock.release();
            }
        }
        PowerManager pm = (PowerManager) boundActivity.getSystemService(Context.POWER_SERVICE);
        wakelock = pm.newWakeLock(lockLevel, "CommCareLock");
        wakelock.acquire();
    }

    public synchronized void unlock() {
        if(wakelock != null) {
            wakelock.release();
            wakelock = null;
        }

    }
}

@@ -359,21 +359,251 @@
 l.Log;%0D%0A
+%0D%0A
 /**%0D%0A
+ * Hold a reference to the parent Activity so we can report the%0D%0A * task's current progress and results. The Android framework%0D%0A * will pass us a reference to the newly created Activity after%0D%0A * each configuration change.%0D%0A *%0D%0A
  * @auth
@@ -613,20 +613,16 @@
 ctsims%0D%0A
- *%0D%0A
  */%0D%0Apub
@@ -665,22 +665,16 @@
 ment %7B%0D%0A
-    %0D%0A
     Comm
@@ -738,20 +738,16 @@
 ivity;%0D%0A
-    
 %0D%0A    Co
@@ -775,266 +775,42 @@
 k;%0D%0A
-    /**%0D%0A     * Hold a reference to the parent Activity so we can report the%0D%0A     * task's current progress and results. The Android framework %0D%0A     * will pass us a reference to the newly created Activity after %0D%0A     * each configuration change.%0D%0A     */
+%0D%0A    private WakeLock wakelock;%0D%0A
 %0D%0A  
@@ -900,32 +900,34 @@
 ach(activity);%0D%0A
+%0D%0A
         if(activ
@@ -1277,344 +1277,8 @@
  %7D%0D%0A
-      %0D%0A      public CommCareActivity getPreviousState() %7B %0D%0A          return lastActivity;%0D%0A      %7D%0D%0A      %0D%0A      public void connectTask(CommCareTask task) %7B%0D%0A          this.currentTask = task;%0D%0A      %7D%0D%0A     %0D%0A      /**%0D%0A       * This method will only be called once when the retained%0D%0A       * Fragment is first created.%0D%0A       */
 %0D%0A  
@@ -1394,21 +1394,16 @@
 tate);%0D%0A
-     
 %0D%0A      
@@ -1506,134 +1506,8 @@
  %7D%0D%0A
-     %0D%0A      /**%0D%0A       * Set the callback to null so we don't accidentally leak the %0D%0A       * Activity instance.%0D%0A       */
 %0D%0A  
@@ -1576,24 +1576,26 @@
 nDetach();%0D%0A
+%0D%0A
         if(t
@@ -1670,32 +1670,34 @@
 ty;%0D%0A        %7D%0D%0A
+%0D%0A
         if(curre
@@ -2025,42 +2025,8 @@
 %0D%0A%0D%0A
-    private WakeLock wakelock;%0D%0A%0D%0A
     
@@ -2554,20 +2554,202 @@
      %7D%0D%0A
-%0D%0A
     %7D%0D%0A
+%0D%0A    public CommCareActivity getPreviousState() %7B%0D%0A        return lastActivity;%0D%0A    %7D%0D%0A%0D%0A    public void connectTask(CommCareTask task) %7B%0D%0A        this.currentTask = task;%0D%0A    %7D%0D%0A%0D%0A
 %7D%0D%0A

Reorder and remove obvious comments