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;

/**
 * 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.
 *
 * @author ctsims
 */
public class StateFragment extends Fragment {
    private CommCareActivity boundActivity;
    private CommCareActivity lastActivity;

    private CommCareTask currentTask;

    private WakeLock wakelock;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRetainInstance(true);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (activity instanceof CommCareActivity) {
            this.boundActivity = (CommCareActivity)activity;
            this.boundActivity.stateHolder = this;

            if (isCurrentTaskRunning()) {
                this.currentTask.connect(boundActivity);
            }
        }
    }

    private boolean isCurrentTaskRunning() {
        return this.currentTask != null &&
                this.currentTask.getStatus() == AsyncTask.Status.RUNNING;
    }

    @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();
            releaseWakeLock();
        }
    }

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

    public synchronized void acquireWakeLock(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 releaseWakeLock() {
        if (wakelock != null) {
            wakelock.release();
            wakelock = null;
        }
    }

    public CommCareActivity getPreviousState() {
        return lastActivity;
    }

    public void connectTask(CommCareTask task) {
        this.currentTask = task;
    }
}

@@ -2167,132 +2167,26 @@
     
-if (wakelock != null) %7B%0D%0A            if (wakelock.isHeld()) %7B%0D%0A                wakelock.release();%0D%0A            %7D%0D%0A        %7D
+releaseWakeLock();
 %0D%0A%0D%0A
@@ -2452,32 +2452,53 @@
 wakelock != null
+ && wakelock.isHeld()
 ) %7B%0D%0A           
@@ -2515,32 +2515,39 @@
 release();%0D%0A    
+    %7D%0D%0A
         wakelock
@@ -2556,27 +2556,16 @@
  null;%0D%0A
-        %7D%0D%0A
     %7D%0D%0A%0D

Remove dup wakelock release code