From: Marius Gavrilescu Date: Mon, 25 Aug 2014 10:54:48 +0000 (+0300) Subject: Execute commands received via SMS on the main service X-Git-Tag: 0.000_002~7 X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=e0c7f061c5a2a4d8798deed39a8347f6e3a0e54b;p=fonbot.git Execute commands received via SMS on the main service Until now, commands received via SMS were executed on the BroadcastReceiver, breaking some commands (such as speak). Thanks to Valerio Bozzolan for reporting this issue. --- diff --git a/src/ro/ieval/fonbot/FonBotMainService.java b/src/ro/ieval/fonbot/FonBotMainService.java index dd13aff..8bc69ae 100644 --- a/src/ro/ieval/fonbot/FonBotMainService.java +++ b/src/ro/ieval/fonbot/FonBotMainService.java @@ -10,6 +10,7 @@ import java.util.Set; import org.eclipse.jdt.annotation.Nullable; +import ro.ieval.fonbot.Address.Protocol; import ro.ieval.fonbot.Utils.OngoingEvent; import android.app.Notification; @@ -98,6 +99,21 @@ public final class FonBotMainService extends Service { */ public static final String ACTION_DELETE_ONGOING="ro.ieval.fonbot.FonBotMainService.ACTION_DELETE_ONGOING"; + /** + * Broadcast action: process a command received via SMS + */ + public static final String ACTION_PROCESS_COMMAND="ro.ieval.fonbot.FonBotMainService.ACTION_PROCESS_COMMAND"; + + /** + * Extra: command line + */ + public static final String EXTRA_COMMAND_LINE="ro.ieval.fonbot.FonBotMainService.EXTRA_COMMAND_LINE"; + + /** + * Extra: SMS originating address + */ + public static final String EXTRA_SMS_ORIGIN_ADDRESS="ro.ieval.fonbot.FonBotMainService.EXTRA_SMS_ORIGIN_ADDRESS"; + /** * Extra: ongoing event id * @@ -175,6 +191,16 @@ public final class FonBotMainService extends Service { LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE)); updateNotification=true; } + if(intent!=null && intent.getAction()==ACTION_PROCESS_COMMAND) { + final String cmdline = intent.getStringExtra(EXTRA_COMMAND_LINE); + final String origin = intent.getStringExtra(EXTRA_SMS_ORIGIN_ADDRESS); + final Address address= new Address(toNonNull(Protocol.SMS), origin);//NOPMD variable depends on originAddress + final String[] words=Utils.shellwords(toNonNull(cmdline)); + final String[] args=new String[words.length-1];//NOPMD variable size depends on words.length + System.arraycopy(words, 1, args, 0, args.length); + + Utils.processCommand(this, toNonNull(words[0]), args, toNonNull(address)); + } if(longPollThread == null || !longPollThread.isAlive()){ longPollThread = new Thread(new LongPollRunnable()); diff --git a/src/ro/ieval/fonbot/SmsReceiver.java b/src/ro/ieval/fonbot/SmsReceiver.java index b6fce8e..63609bd 100644 --- a/src/ro/ieval/fonbot/SmsReceiver.java +++ b/src/ro/ieval/fonbot/SmsReceiver.java @@ -6,7 +6,6 @@ import static ro.ieval.fonbot.Utils.toNonNull; import org.eclipse.jdt.annotation.Nullable; -import ro.ieval.fonbot.Address.Protocol; import ro.ieval.fonbot.Utils.MessageType; import android.content.BroadcastReceiver; import android.content.Context; @@ -69,13 +68,12 @@ public final class SmsReceiver extends BroadcastReceiver { if(lines.length==0 || !lines[0].equals(password)) continue; - final Address address=new Address(toNonNull(Protocol.SMS), originAddress);//NOPMD variable depends on originAddress for (int i = 1; i < lines.length; i++) { - final String[] words=Utils.shellwords(toNonNull(lines[i])); - final String[] args=new String[words.length-1];//NOPMD variable size depends on words.length - System.arraycopy(words, 1, args, 0, args.length); - - Utils.processCommand(context, toNonNull(words[0]), args, toNonNull(address)); + final Intent process_intent = new Intent(context, FonBotMainService.class); + process_intent.setAction(FonBotMainService.ACTION_PROCESS_COMMAND); + process_intent.putExtra(FonBotMainService.EXTRA_COMMAND_LINE, lines[i]); + process_intent.putExtra(FonBotMainService.EXTRA_SMS_ORIGIN_ADDRESS, originAddress); + context.startService(process_intent); } abortBroadcast();