Don't call pollServer from a BroadcastRecevier
authorMarius Gavrilescu <marius@ieval.ro>
Mon, 25 Mar 2013 06:59:00 +0000 (08:59 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Mon, 25 Mar 2013 06:59:00 +0000 (08:59 +0200)
LocalBroadcastRecevier and DynamicBroadcastRecevier used to call pollServer directly. Thus, processCommand was called from a BroadcastRecevier context, which broke some commands (e.g. SPEAK). Now, they call a new function, safePollServer, which calls pollServer from FonBotMainService.

src/ro/ieval/fonbot/DynamicEventReceiver.java
src/ro/ieval/fonbot/FonBotMainService.java
src/ro/ieval/fonbot/LocalBroadcastReceiver.java
src/ro/ieval/fonbot/Utils.java

index 0912e8834f022709cbf082527bdef0609e801e52..cb1e80584e8a7c1f7c97454fab2cc2f98c59e93d 100644 (file)
@@ -47,7 +47,7 @@ public final class DynamicEventReceiver extends BroadcastReceiver {
 
                if(action.equals(Intent.ACTION_SCREEN_ON)){
                        if(PreferenceManager.getDefaultSharedPreferences(context).getBoolean("poll_on_screen_on", false))
-                               Utils.pollServer(context);
+                               Utils.safePollServer(context);
                } else if(action.equals(Intent.ACTION_BATTERY_CHANGED))
                        Heavy.describeBatteryLevel(context, null, toNonNull(MessageType.BATTERY_CHANGED));
                else if(action.equals(Intent.ACTION_HEADSET_PLUG)){
index a751d231c434173188b16dcd6a6037273eb83430..753425d18081daa9a54c67eb1f2c2ac8248f5612 100644 (file)
@@ -70,6 +70,8 @@ public final class FonBotMainService extends Service {
         * Broadcast action: remove an ongoing event
         */
        public static final String ACTION_DELETE_ONGOING="ro.ieval.fonbot.FonBotMainService.ACTION_DELETE_ONGOING";
+       /** Broadcast action: trigger a server poll */
+       public static final String ACTION_TRIGGER_POLL="ro.ieval.fonbot.FonBotMainService.ACTION_TRIGGER_POLL";
        /**
         * Extra: ongoing event id
         *
@@ -139,6 +141,9 @@ public final class FonBotMainService extends Service {
                        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE));
                }
 
+               if(intent!=null && intent.getAction()==ACTION_TRIGGER_POLL)
+                       Utils.pollServer(this);
+
                final boolean runForeground=PreferenceManager.getDefaultSharedPreferences(this).getBoolean("foreground", false);
                if(!runForeground)
                        stopForeground(true);
index 2aade7fac5755300213b04fb3693f7e99d4f6f1f..163310e5993fb9aae25622347989a55077ceca4a 100644 (file)
@@ -43,7 +43,7 @@ public final class LocalBroadcastReceiver extends BroadcastReceiver {
 
                final String action=intent.getAction();
                if(action.equals(LocalBroadcastReceiver.ACTION_POLL_ALARM))
-                       Utils.pollServer(context);
+                       Utils.safePollServer(context);
        }
 
 }
index 1262513a3a477a2358c54eb603e57647b6cec9d3..f06c43acc6813e0003796a472080e37d2bc7251b 100644 (file)
@@ -411,7 +411,7 @@ public final class Utils {
        }
 
        /**
-        * Poll the server for pending commands.
+        * Poll the server for pending commands. This function must not be called from BroadcastReceivers
         *
         * @param context Context instance
         */
@@ -419,6 +419,16 @@ public final class Utils {
                new HttpCallExecutableRunnable("/get", null, context, new PollResultCallback(context), false).execute();
        }
 
+       /**
+        * Poll the server for pending commands from {@link FonBotMainService}. This function should be used from BroadcastReceviers instead of {@link #pollServer}
+        *
+        * @param context Context instance
+        */
+       public static void safePollServer(final Context context){
+               final Intent intent=new Intent(context, FonBotMainService.class);
+               intent.setAction(FonBotMainService.ACTION_TRIGGER_POLL);
+               context.startService(intent);
+       }
        /**
         * Executes a given command
         *
This page took 0.012924 seconds and 4 git commands to generate.