]> iEval git - fonbot.git/commitdiff
Execute commands received via SMS on the main service
authorMarius Gavrilescu <marius@ieval.ro>
Mon, 25 Aug 2014 10:54:48 +0000 (13:54 +0300)
committerMarius Gavrilescu <marius@ieval.ro>
Mon, 25 Aug 2014 10:54:48 +0000 (13:54 +0300)
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.

src/ro/ieval/fonbot/FonBotMainService.java
src/ro/ieval/fonbot/SmsReceiver.java

index dd13affe715aef6f403d1fba71fc6c87e00bf8ac..8bc69ae7541ad8c2bea7857f1af8c43d8037246f 100644 (file)
@@ -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());
index b6fce8e6276cd503e97d27993f921c0ad3b27f85..63609bdd9d23c935c884dc78d7f7765a8b4dcb55 100644 (file)
@@ -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();
This page took 0.039193 seconds and 4 git commands to generate.