X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;ds=inline;f=src%2Fro%2Fieval%2Ffonbot%2FUtils.java;h=38c34644c8962bfca1f72f8de4644b307314d8aa;hb=4d0abc184849c968c33ffda5abd98156272541e8;hp=d5e0f5bcf8de0d506748fedce0dbdaf143bf4e6d;hpb=8dfb76c9431dbf8401412cb92c7512e7dc3081a2;p=fonbot.git
diff --git a/src/ro/ieval/fonbot/Utils.java b/src/ro/ieval/fonbot/Utils.java
index d5e0f5b..38c3464 100644
--- a/src/ro/ieval/fonbot/Utils.java
+++ b/src/ro/ieval/fonbot/Utils.java
@@ -24,6 +24,8 @@ import static ro.ieval.fonbot.R.string.the_second_argument_to_wipe_must_be;
import static ro.ieval.fonbot.R.string.unknown_command;
import static ro.ieval.fonbot.R.string.wipetype_should_be_one_of;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.util.Arrays;
import java.util.Locale;
@@ -84,7 +86,7 @@ public final class Utils {
NEXT, PREV, BATT, CALLLOG, SMSLOG,
LS, RM, CONTACTS, DISABLE, ENABLE,
POLL, HANGUP, ANSWER, LAUNCH, DATA,
- GPS, GLOCATION, REBOOT, SHUTDOWN, NOTIFY
+ GPS, GLOCATION, REBOOT, NOTIFY
}
/**
@@ -314,8 +316,8 @@ public final class Utils {
public static void sendMessage(final Context context, final Address address, final String message){
switch(address.protocol){
case HTTP:
- new SendHttpMessageAsyncTask(toNonNull(Arrays.asList(
- new Header("X-Action", "SEND "+address.data))), context).execute(message);
+ new HttpCallExecutableRunnable("/send", toNonNull(Arrays.asList(
+ new Header("X-Destination", toNonNull(address.data)))), context, null, true, message).execute();
break;
case SMS:
@@ -333,19 +335,6 @@ public final class Utils {
}
}
- /**
- * Parses a HTTP response message of the form (part1) part2
, returning part2
.
- *
- * @param message the raw HTTP response message
- * @return the extracted part
- */
- public static String parseHttpMessage(final String message){
- final int indexOfParen=message.indexOf(')');
- if(indexOfParen==-1)
- return message;
- return toNonNull(message.substring(indexOfParen+2));
- }
-
/**
* Splits a string into words.
*
@@ -406,6 +395,40 @@ public final class Utils {
context.startService(intent);
}
+ /**
+ * Gets the server URL according to the user preferences.
+ *
+ * @param context Context instance
+ * @param path URL path
+ * @return the server URL
+ * @throws MalformedURLException if the user preferences create an invalid URL
+ */
+ public static URL getServerURL(final Context context, final String path) throws MalformedURLException{
+ final String hostname=PreferenceManager.getDefaultSharedPreferences(context).getString("hostname", "fonbot.ieval.ro");
+ final int port=Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(context).getString("port", "443"));
+ final URL url=new URL("https", hostname, port, path);
+ return url;
+ }
+
+ /**
+ * Poll the server for pending commands. This function must not be called from BroadcastReceivers
+ *
+ * @param context Context instance
+ */
+ public static void pollServer(final Context context){
+ 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
*
@@ -416,7 +439,7 @@ public final class Utils {
*/
private static void processCommand(final Context context, final Command cmd,final String[] args,final Address replyTo){
if(Heavy.isCommandDisabled(context, cmd)){
- sendMessage(context, replyTo, command_disabled);
+ sendMessage(context, replyTo, command_disabled, cmd.toString());
return;
}
@@ -980,13 +1003,10 @@ public final class Utils {
Heavy.reboot(context, replyTo, args.length==0?null:args[0]);
break;
- case SHUTDOWN:
- //TODO: implement command
- break;
-
case NOTIFY:
if(args.length!=1 && args.length!=3){
Heavy.help(context, replyTo, toNonNull(Command.NOTIFY));
+ return;
}
final int id;