package com.fine47.http;

import android.util.Log;
import com.loopj.android.http.BinaryHttpResponseHandler;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.Header;

class BinaryResponseHandler extends BinaryHttpResponseHandler {

  private final static HashMap<String, ArrayList<BinaryResponse>>
    HANDLERS = new HashMap(100);

  public final String url;

  public BinaryResponseHandler(String url, BinaryResponse response) {
    super();

    // Binary downloads will always use the pool thread to fire callbacks.
    setUsePoolThread(true);

    // Remember the URL.
    this.url = url;

    // Get defined handlers for this URL.
    ArrayList<BinaryResponse> handlers = getHandlers();

    // If there are no handlers defined, create a new list.
    if(null == handlers) {
      handlers = new ArrayList();
      HANDLERS.put(this.url, handlers);
      if(ActivityHttpClient.isDebugging()) {
        Log.i(
          ActivityHttpClient.LOG_TAG,
          "Created new handlers array for URL: " + url);
      }
    }

    // Add the response handler to the list.
    handlers.add(response);
  }

  @Override
  public void onCancel() {
  }

  @Override
  public void onFinish() {
    ArrayList<BinaryResponse> handlers = getHandlers();
    if(null != handlers) {
      handlers.clear();
    }
    HANDLERS.remove(url);
    if(ActivityHttpClient.isDebugging()) {
      Log.i(
        ActivityHttpClient.LOG_TAG,
        "Binary request finished for URL: " + url);
    }
  }

  @Override
  public void onSuccess(
    int statusCode,
    Header[] headers,
    byte[] response
  ) {
    ArrayList<BinaryResponse> handlers = getHandlers();
    if(null != handlers) {
      for(BinaryResponse handler : handlers) {
        handler.onSuccess(response);
      }
    }
    if(ActivityHttpClient.isDebugging()) {
      Log.i(
        ActivityHttpClient.LOG_TAG,
        "Binary request successful for URL: " + url);
    }
  }

  @Override
  public void onFailure(
    int statusCode,
    Header[] headers,
    byte[] response,
    Throwable error
  ) {
    ArrayList<BinaryResponse> handlers = getHandlers();
    if(null != handlers) {
      for(BinaryResponse handler : handlers) {
        handler.onFailure(error, response);
      }
      handlers.clear();
    }
    HANDLERS.remove(url);
    if(ActivityHttpClient.isDebugging()) {
      Log.i(
        ActivityHttpClient.LOG_TAG,
        "Binary request failure for URL: " + url);
    }
  }

  private ArrayList<BinaryResponse> getHandlers() {
    return HANDLERS.get(url);
  }
}

@@ -1161,16 +1161,167 @@
 cel() %7B%0A
+    if(ActivityHttpClient.isDebugging()) %7B%0A      Log.w(%0A        ActivityHttpClient.LOG_TAG,%0A        %22Binary request cancelled for URL: %22 + url);%0A    %7D%0A
   %7D%0A%0A  @
@@ -2502,33 +2502,33 @@
 ()) %7B%0A      Log.
-i
+e
 (%0A        Activi
@@ -2577,19 +2577,18 @@
 est fail
-ur
 e
+d
  for URL
@@ -2588,32 +2588,78 @@
 for URL: %22 + url
+ + %22 (code=%22 + statusCode + %22)%22,%0A        error
 );%0A    %7D%0A  %7D%0A%0A  

Add debugging messages.