From: Marius Gavrilescu Date: Mon, 25 Mar 2013 06:59:00 +0000 (+0200) Subject: Don't call pollServer from a BroadcastRecevier X-Git-Tag: 0.000_001~44 X-Git-Url: http://git.ieval.ro/?p=fonbot.git;a=commitdiff_plain;h=6b9507db27500572e4366b3f23eefeea079a9d8f Don't call pollServer from a BroadcastRecevier 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. --- diff --git a/src/ro/ieval/fonbot/DynamicEventReceiver.java b/src/ro/ieval/fonbot/DynamicEventReceiver.java index 0912e88..cb1e805 100644 --- a/src/ro/ieval/fonbot/DynamicEventReceiver.java +++ b/src/ro/ieval/fonbot/DynamicEventReceiver.java @@ -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)){ diff --git a/src/ro/ieval/fonbot/FonBotMainService.java b/src/ro/ieval/fonbot/FonBotMainService.java index a751d23..753425d 100644 --- a/src/ro/ieval/fonbot/FonBotMainService.java +++ b/src/ro/ieval/fonbot/FonBotMainService.java @@ -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); diff --git a/src/ro/ieval/fonbot/LocalBroadcastReceiver.java b/src/ro/ieval/fonbot/LocalBroadcastReceiver.java index 2aade7f..163310e 100644 --- a/src/ro/ieval/fonbot/LocalBroadcastReceiver.java +++ b/src/ro/ieval/fonbot/LocalBroadcastReceiver.java @@ -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); } } diff --git a/src/ro/ieval/fonbot/Utils.java b/src/ro/ieval/fonbot/Utils.java index 1262513..f06c43a 100644 --- a/src/ro/ieval/fonbot/Utils.java +++ b/src/ro/ieval/fonbot/Utils.java @@ -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 *