]> iEval git - fonbot.git/blobdiff - src/ro/ieval/fonbot/FonBotMainActivity.java
Clear status image when checking connectivity
[fonbot.git] / src / ro / ieval / fonbot / FonBotMainActivity.java
index 92a81af7ec4c70d57b9a89d53894758f669894cc..b879be0c203c1a2c90548d43f66e4cec730ce609 100644 (file)
@@ -1,25 +1,23 @@
 package ro.ieval.fonbot;
 
-import static ro.ieval.fonbot.R.string.*;
-
+import static ro.ieval.fonbot.R.string.logging_in;
 import static ro.ieval.fonbot.Utils.toNonNull;
 
-import java.util.Arrays;
+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;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.content.SharedPreferences;
 import android.os.Bundle;
+import android.os.Handler;
 import android.os.IBinder;
-import android.preference.PreferenceManager;
 import android.support.v4.content.LocalBroadcastManager;
 import android.view.LayoutInflater;
 import android.view.Menu;
@@ -144,21 +142,41 @@ public final class FonBotMainActivity extends ListActivity {
        }
 
        /**
-        * BroadcastReceiver that updates {@link #resultTextView}
+        * Implementation of <code>ResultCallback</code> that updates the {@link #resultTextView}
         *
-        * @author Marius Gavrilescu <marius@ieval.ro>
+        * @author Marius Gavrilescu
         */
-       private final class LoginReceiver extends BroadcastReceiver {
+       public final class UpdateResultCallback implements ResultCallback{
                @Override
-               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);
-                       if(responseCode>=200&&responseCode<300)
-                               resultTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.tick, 0, 0);
+               public void onResult(final int responseCode, final String responseMessage, final InputStream inputStream) {
+                       if(responseCode>=200 && responseCode<300)
+                               updateResultTextView(responseMessage, false);
                        else
-                               resultTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.cross, 0, 0);
+                               updateResultTextView(responseMessage, true);
+               }
+
+               @Override
+               public void onError(final String error) {
+                       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);
+                               }
+                       });
                }
        }
 
@@ -171,17 +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 SendHttpMessageAsyncTask} 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);
+       private final UpdateResultCallback updateResultCallback=new UpdateResultCallback();
        /**
         * The one instance of {@link ServiceConnection}
         */
@@ -191,34 +201,31 @@ 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);
        }
 
        @Override
        protected void onStart() {
                super.onStart();
-               final SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(this);
                resultTextView.setText(logging_in);
-               new SendHttpMessageAsyncTask(toNonNull(Arrays.asList(
-                               new Header("X-Action","LOGIN")
-                               )),LOGIN_BROADCAST,this).execute(
-                                               sp.getString("username", null),
-                                               sp.getString("password", null)
-                                               );
+               resultTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
+               new HttpCallExecutableRunnable("/ok", null,
+                               toNonNull(getApplicationContext()), toNonNull(updateResultCallback), false).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);
        }
@@ -226,7 +233,6 @@ public final class FonBotMainActivity extends ListActivity {
        @Override
        protected void onPause() {
                super.onPause();
-               unregisterReceiver(loginReceiver);
                LocalBroadcastManager.getInstance(this).unregisterReceiver(ongoingUpdateReceiver);
                unbindService(connection);
        }
@@ -255,4 +261,4 @@ public final class FonBotMainActivity extends ListActivity {
                        return super.onOptionsItemSelected(item);
                }
        }
-}
\ No newline at end of file
+}
This page took 0.028682 seconds and 4 git commands to generate.