Merge SendHttpMessageAsyncTask and PollServerAsyncTask
[fonbot.git] / src / ro / ieval / fonbot / FonBotMainActivity.java
index 7ef45b849c485400cfc83276141a1fdb382ab67a..2ec56e2442d1e960378f9869a1b339f9c3b8a12e 100644 (file)
@@ -3,11 +3,11 @@ package ro.ieval.fonbot;
 import static ro.ieval.fonbot.R.string.logging_in;
 import static ro.ieval.fonbot.Utils.toNonNull;
 
-import java.util.Collections;
-
+import java.io.InputStream;
 import org.eclipse.jdt.annotation.Nullable;
 
 import ro.ieval.fonbot.FonBotMainService.Binder;
+import ro.ieval.fonbot.HttpCallExecutableRunnable.ResultCallback;
 import ro.ieval.fonbot.Utils.OngoingEvent;
 import android.app.ListActivity;
 import android.content.BroadcastReceiver;
@@ -150,8 +150,8 @@ public final class FonBotMainActivity extends ListActivity {
                public void onReceive(@Nullable final Context context, @Nullable final Intent intent) {
                        if(intent==null)
                                return;
-                       resultTextView.setText(intent.getStringExtra(SendHttpMessageAsyncTask.EXTRA_RESPONSE_MESSAGE));
-                       final int responseCode=intent.getIntExtra(SendHttpMessageAsyncTask.EXTRA_RESPONSE_CODE, 0);
+                       resultTextView.setText(intent.getStringExtra(BroadcastResultCallback.EXTRA_RESPONSE_MESSAGE));
+                       final int responseCode=intent.getIntExtra(BroadcastResultCallback.EXTRA_RESPONSE_CODE, 0);
                        if(responseCode>=200&&responseCode<300)
                                resultTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.tick, 0, 0);
                        else
@@ -159,6 +159,37 @@ public final class FonBotMainActivity extends ListActivity {
                }
        }
 
+       /**
+        * Implementation of <code>ResultCallback</code> that broadcasts a {@link #LOGIN_BROADCAST}
+        *
+        * @author Marius Gavrilescu
+        */
+       public final class BroadcastResultCallback implements ResultCallback{
+               /**
+                * Extra: the response message
+                */
+               public static final String EXTRA_RESPONSE_MESSAGE="response_message";
+               /**
+                * Extra: the response code
+                */
+               public static final String EXTRA_RESPONSE_CODE="response_code";
+
+               @Override
+               public void onResult(final int responseCode, final String responseMessage, final InputStream inputStream) {
+                       final Intent intent=new Intent(LOGIN_BROADCAST);
+                       intent.putExtra(EXTRA_RESPONSE_MESSAGE, responseMessage);
+                       intent.putExtra(EXTRA_RESPONSE_CODE, responseCode);
+                       sendBroadcast(intent);
+               }
+
+               @Override
+               public void onError(final String error) {
+                       final Intent intent=new Intent(LOGIN_BROADCAST);
+                       intent.putExtra(EXTRA_RESPONSE_MESSAGE, error);
+                       sendBroadcast(intent);
+               }
+       }
+
        /**
         * The one instance of {@link OngoingUpdateReceiver}
         */
@@ -172,13 +203,17 @@ public final class FonBotMainActivity extends ListActivity {
         */
        private final BroadcastReceiver loginReceiver=new LoginReceiver();
        /**
-        * The broadcast sent by {@link SendHttpMessageAsyncTask} when logging in. 
+        * The broadcast sent by {@link HttpCallExecutableRunnable} when logging in.
         */
        private static final String LOGIN_BROADCAST="ro.ieval.fonbot.LOGIN_RESULT";
        /**
         * IntentFilter for {@link #loginReceiver}
         */
        private static final IntentFilter LOGIN_FILTER=new IntentFilter(LOGIN_BROADCAST);
+       /**
+        * The one instance of {@link BroadcastResultCallback}
+        */
+       private final BroadcastResultCallback broadcastResultCallback=new BroadcastResultCallback();
        /**
         * The one instance of {@link ServiceConnection}
         */
@@ -202,8 +237,8 @@ public final class FonBotMainActivity extends ListActivity {
        protected void onStart() {
                super.onStart();
                resultTextView.setText(logging_in);
-               new SendHttpMessageAsyncTask("/ok", toNonNull(Collections.<Header>emptySet()),
-                               LOGIN_BROADCAST,this).execute();
+               new HttpCallExecutableRunnable("/ok", null,
+                               toNonNull(getApplicationContext()), toNonNull(broadcastResultCallback)).execute();
                connection.refreshAdapter();
        }
 
This page took 0.011723 seconds and 4 git commands to generate.