Remove the login broadcast
authorMarius Gavrilescu <marius@ieval.ro>
Tue, 12 Mar 2013 16:39:53 +0000 (18:39 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Tue, 12 Mar 2013 16:39:53 +0000 (18:39 +0200)
The login status is now updated directly in the ResultCallback, so
the login broadcast is no longer needed.

src/ro/ieval/fonbot/FonBotMainActivity.java

index 2ec56e2442d1e960378f9869a1b339f9c3b8a12e..342606a506b8186d75c4badeec4068eaa65fb251 100644 (file)
@@ -16,6 +16,7 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.os.Bundle;
+import android.os.Handler;
 import android.os.IBinder;
 import android.support.v4.content.LocalBroadcastManager;
 import android.view.LayoutInflater;
@@ -141,52 +142,41 @@ public final class FonBotMainActivity extends ListActivity {
        }
 
        /**
-        * BroadcastReceiver that updates {@link #resultTextView}
-        *
-        * @author Marius Gavrilescu <marius@ieval.ro>
-        */
-       private final class LoginReceiver extends BroadcastReceiver {
-               @Override
-               public void onReceive(@Nullable final Context context, @Nullable final Intent intent) {
-                       if(intent==null)
-                               return;
-                       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
-                               resultTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.cross, 0, 0);
-               }
-       }
-
-       /**
-        * Implementation of <code>ResultCallback</code> that broadcasts a {@link #LOGIN_BROADCAST}
+        * Implementation of <code>ResultCallback</code> that updates the {@link #resultTextView}
         *
         * @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";
-
+       public final class UpdateResultCallback implements ResultCallback{
                @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);
+                       if(responseCode>=200 && responseCode<300)
+                               updateResultTextView(responseMessage, false);
+                       else
+                               updateResultTextView(responseMessage, true);
                }
 
                @Override
                public void onError(final String error) {
-                       final Intent intent=new Intent(LOGIN_BROADCAST);
-                       intent.putExtra(EXTRA_RESPONSE_MESSAGE, error);
-                       sendBroadcast(intent);
+                       updateResultTextView(error, true);
+               }
+
+               /**
+                * Update the {@link #resultTextView}.
+                *
+                * @param message new text for the TextView
+                * @param error true if the text is an error message, false otherwise
+                */
+               private void updateResultTextView(final String message, final boolean error){
+                       handler.post(new Runnable() {
+                               @Override
+                               public void run() {
+                                       resultTextView.setText(message);
+                                       if(error)
+                                               resultTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.cross, 0, 0);
+                                       else
+                                               resultTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.tick, 0, 0);
+                               }
+                       });
                }
        }
 
@@ -199,21 +189,9 @@ public final class FonBotMainActivity extends ListActivity {
         */
        private static final IntentFilter ONGOING_UPDATE_FILTER=new IntentFilter(FonBotMainService.ACTION_ONGOING_UPDATE);
        /**
-        * The one instance of {@link LoginReceiver}
+        * The one instance of {@link UpdateResultCallback}
         */
-       private final BroadcastReceiver loginReceiver=new LoginReceiver();
-       /**
-        * 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();
+       private final UpdateResultCallback updateResultCallback=new UpdateResultCallback();
        /**
         * The one instance of {@link ServiceConnection}
         */
@@ -223,13 +201,15 @@ public final class FonBotMainActivity extends ListActivity {
         * TextView that tells the user whether logging in failed or succeded.
         */
        private TextView resultTextView;
+       /** Handler instance */
+       private Handler handler;
 
        @Override
        protected void onCreate(@Nullable final Bundle icicle) {
                super.onCreate(icicle);
 
                setContentView(R.layout.main);
-
+               handler=new Handler();
                resultTextView=(TextView) findViewById(R.id.resultTextView);
        }
 
@@ -238,14 +218,13 @@ public final class FonBotMainActivity extends ListActivity {
                super.onStart();
                resultTextView.setText(logging_in);
                new HttpCallExecutableRunnable("/ok", null,
-                               toNonNull(getApplicationContext()), toNonNull(broadcastResultCallback)).execute();
+                               toNonNull(getApplicationContext()), toNonNull(updateResultCallback)).execute();
                connection.refreshAdapter();
        }
 
        @Override
        protected void onResume() {
                super.onResume();
-               registerReceiver(loginReceiver, LOGIN_FILTER);
                LocalBroadcastManager.getInstance(this).registerReceiver(ongoingUpdateReceiver, ONGOING_UPDATE_FILTER);
                bindService(new Intent(this, FonBotMainService.class), connection, 0);
        }
@@ -253,7 +232,6 @@ public final class FonBotMainActivity extends ListActivity {
        @Override
        protected void onPause() {
                super.onPause();
-               unregisterReceiver(loginReceiver);
                LocalBroadcastManager.getInstance(this).unregisterReceiver(ongoingUpdateReceiver);
                unbindService(connection);
        }
This page took 0.0151 seconds and 4 git commands to generate.