package com.humbughq.android;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

class AsyncPoller extends HumbugAsyncPushTask {

    private Message[] receivedMessages;
    private boolean continuePolling;
    private boolean updatePointer;

    public AsyncPoller(HumbugActivity humbugActivity, boolean continuePolling,
            boolean updatePointer) {
        super(humbugActivity);
        this.continuePolling = continuePolling;
        this.updatePointer = updatePointer;
    }

    public final void execute() {
        this.execute("api/v1/get_messages");
    }

    public final void execute(int anchor, String direction, int number) {
        this.setProperty("start", anchor + "");
        this.setProperty("which", direction + "");
        this.setProperty("number", number + "");
        this.execute("api/v1/get_old_messages");
    }

    @Override
    protected String doInBackground(String... api_path) {
        String result = super.doInBackground(api_path);

        if (result != null) {
            try {
                JSONObject response = new JSONObject(result);
                JSONArray objects = response.getJSONArray("messages");

                receivedMessages = new Message[objects.length()];

                for (int i = 0; i < objects.length(); i++) {
                    Log.i("json-iter", "" + i);
                    Message message = new Message(context,
                            objects.getJSONObject(i));
                    /*
                     * Add to the local message array and the global ArrayList
                     * of all messages.
                     * 
                     * We do this because we want to process JSON here, but do
                     * manipulation of the UI on the UI thread.
                     */
                    receivedMessages[i] = message;
                    this.context.messageIndex.append(message.getID(), message);

                }
            } catch (JSONException e) {
                Log.e("json", "parsing error");
                e.printStackTrace();
            }
        } else {
            Log.i("poll", "got nothing from the server");
        }
        return null; // since onPostExecute doesn't use the result
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if (receivedMessages != null) {
            try {
                this.context.adapter.addAll(receivedMessages);
            } catch (NoSuchMethodError e) {
                for (Message message : receivedMessages) {
                    this.context.adapter.add(message);
                }
            }
        }
        if (updatePointer) {
            (new AsyncPointerUpdate(this.context)).execute();
        }
        if (this.continuePolling) {
            this.context.current_poll = new AsyncPoller(this.context, true,
                    false);
            this.context.current_poll.execute();
        }

        callback.onTaskComplete(result);
    }
}

@@ -616,32 +616,78 @@
 get_messages%22);%0A
+        Log.v(%22poll%22, %22longpolling started%22);%0A
     %7D%0A%0A    publi
@@ -944,24 +944,74 @@
 messages%22);%0A
+        Log.v(%22poll%22, %22get_old_messages called%22);%0A
     %7D%0A%0A    @
@@ -2210,24 +2210,160 @@
 ackTrace();%0A
+            %7D catch (NullPointerException e) %7B%0A                Log.e(%22poll%22, %22No data returned?%22);%0A                e.printStackTrace();%0A
             
@@ -2628,48 +2628,156 @@
 t);%0A
-%0A        if (receivedMessages != null) %7B
+        Log.v(%22poll%22, %22Longpolling finished.%22);%0A%0A        if (receivedMessages != null) %7B%0A            Log.v(%22poll%22, %22Processing messages received.%22);
 %0A   
@@ -3057,37 +3057,164 @@
    %7D
-%0A        if (updatePointer) %7B
+ else %7B%0A            Log.v(%22poll%22, %22No messages returned.%22);%0A        %7D%0A        if (updatePointer) %7B%0A            Log.v(%22poll%22, %22Starting AsyncPointerUpdate%22);
 %0A   
@@ -3314,24 +3314,77 @@
 ePolling) %7B%0A
+            Log.v(%22poll%22, %22Starting new longpoll.%22);%0A
             

Add a bunch of logging statements.