/**
 * Copyright 2009 Joe LaPenna
 */

package com.joelapenna.foursquared.util;

import com.googlecode.dumpcatcher.logging.Dumpcatcher;
import com.googlecode.dumpcatcher.logging.DumpcatcherUncaughtExceptionHandler;
import com.joelapenna.foursquared.FoursquaredSettings;
import com.joelapenna.foursquared.R;

import org.apache.http.HttpResponse;

import android.content.res.Resources;
import android.util.Log;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Joe LaPenna (joe@joelapenna.com)
 */
public class DumpcatcherHelper {
    private static final String TAG = "DumpcatcherHelper";
    private static final boolean DEBUG = FoursquaredSettings.DEBUG;

    private static final ExecutorService mExecutor = Executors.newFixedThreadPool(2);

    private static Dumpcatcher sDumpcatcher;
    private static String sClient;

    public DumpcatcherHelper(String client, Resources resources) {
        sClient = client;
        setupDumpcatcher(resources);
    }

    public static void setupDumpcatcher(Resources resources) {
        if (FoursquaredSettings.DUMPCATCHER_TEST) {
            if (FoursquaredSettings.DEBUG) Log.d(TAG, "Loading Dumpcatcher TEST");
            sDumpcatcher = new Dumpcatcher( //
                    resources.getString(R.string.test_dumpcatcher_product_key), //
                    resources.getString(R.string.test_dumpcatcher_secret), //
                    resources.getString(R.string.test_dumpcatcher_url), sClient, 5);
        } else {
            if (FoursquaredSettings.DEBUG) Log.d(TAG, "Loading Dumpcatcher Live");
            sDumpcatcher = new Dumpcatcher( //
                    resources.getString(R.string.dumpcatcher_product_key), //
                    resources.getString(R.string.dumpcatcher_secret), //
                    resources.getString(R.string.dumpcatcher_url), sClient, 5);
        }

        UncaughtExceptionHandler handler = new DefaultUnhandledExceptionHandler(sDumpcatcher);
        // This can hang the app starving android of its ability to properly kill threads... maybe.
        Thread.setDefaultUncaughtExceptionHandler(handler);
        Thread.currentThread().setUncaughtExceptionHandler(handler);
    }

    public static void sendCrash(final String shortMessage, final String longMessage,
            final String level, final String tag) {
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    HttpResponse response = sDumpcatcher.sendCrash(shortMessage, longMessage,
                            level, "usage");
                    response.getEntity().consumeContent();
                } catch (Exception e) {
                    if (DEBUG) Log.d(TAG, "Unable to sendCrash");
                }
            }
        });
    }

    public static void sendUsage(final String usage) {
        sendCrash(usage, null, null, "usage");
    }

    private static final class DefaultUnhandledExceptionHandler extends
            DumpcatcherUncaughtExceptionHandler {

        private static final UncaughtExceptionHandler mOriginalExceptionHandler = Thread
                .getDefaultUncaughtExceptionHandler();

        DefaultUnhandledExceptionHandler(Dumpcatcher dumpcatcher) {
            super(dumpcatcher);
        }

        public void uncaughtException(Thread t, Throwable e) {
            super.uncaughtException(t, e);
            mOriginalExceptionHandler.uncaughtException(t, e);
        }
    }

}

@@ -204,24 +204,87 @@
 ionHandler;%0A
+import com.googlecode.dumpcatcher.logging.StackFormattingUtil;%0A
 import com.j
@@ -599,16 +599,48 @@
 ecutors;
+%0Aimport java.util.logging.Level;
 %0A%0A/**%0A *
@@ -2966,32 +2966,328 @@
      %7D);%0A    %7D%0A%0A
+    public static void sendException(Throwable e) %7B%0A        sendCrash(//%0A                StackFormattingUtil.getStackMessageString(e), //%0A                StackFormattingUtil.getStackTraceString(e), //%0A                String.valueOf(Level.INFO.intValue()), //%0A                %22exception%22);%0A    %7D%0A%0A
     public stati

Add method for sending exceptions to dumpcatcher.