/*
 * Copyright (C) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.googlecode.android_scripting.activity;

import java.util.HashMap;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.googlecode.android_scripting.Sl4aApplication;
import com.googlecode.android_scripting.TaskQueue;
import com.googlecode.android_scripting.future.FutureActivityTask;
import com.googlecode.android_scripting.future.FutureResult;

/**
 * This {@link Activity} is launched by the {@link Sl4aService} in order to perform operations that
 * a {@link Service} is unable to do. For example: start another activity for result, show dialogs,
 * etc.
 * 
 * @author Damon Kohler (damonkohler@gmail.com)
 */
public class ScriptingLayerServiceHelper extends Activity {
  TaskQueue mTaskQueue;
  private Handler mHandler;
  private HashMap<Integer, FutureResult<?>> mResultMap;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mHandler = new Handler();
    mTaskQueue = ((Sl4aApplication) getApplication()).getTaskQueue();
    mResultMap = new HashMap<Integer, FutureResult<?>>();
    setPersistent(true);
  }

  public void taskDone(int taskId) {
    mResultMap.remove(taskId);
    if (mResultMap.isEmpty()) {
      finish();
    }
  }

  @Override
  protected void onStart() {
    super.onStart();
    FutureActivityTask<?> task = mTaskQueue.poll();
    mHandler.post(task.getRunnable(this));
    FutureResult<?> result = task.getFutureResult();
    mResultMap.put(task.getTaskId(), result);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    @SuppressWarnings("unchecked")
    FutureResult<Intent> result = (FutureResult<Intent>) mResultMap.get(requestCode);
    if (result != null) {
      result.set(data);
      taskDone(requestCode);
    }
  }
}

@@ -1375,17 +1375,26 @@
 ivity %7B%0A
- 
+%0A  private
  TaskQue
@@ -1491,16 +1491,45 @@
 sultMap;
+%0A  private boolean mFinished;
 %0A%0A  @Ove
@@ -1783,24 +1783,47 @@
 sult%3C?%3E%3E();%0A
+    mFinished = false;%0A
     setPersi
@@ -1916,16 +1916,29 @@
     if (
+mFinished && 
 mResultM
@@ -2038,24 +2038,45 @@
 .onStart();%0A
+    while (true) %7B%0A  
     FutureAc
@@ -2111,24 +2111,75 @@
 eue.poll();%0A
+      if (task == null) %7B%0A        break;%0A      %7D%0A  
     mHandler
@@ -2209,24 +2209,26 @@
 this));%0A    
+  
 FutureResult
@@ -2260,32 +2260,34 @@
 reResult();%0A    
+  
 mResultMap.put(t
@@ -2312,16 +2312,44 @@
 esult);%0A
+    %7D%0A    mFinished = true;%0A
   %7D%0A%0A  @

Make helper activity process all tasks in the queue.