Split text messages in sendMessage
[fonbot.git] / src / ro / ieval / fonbot / Utils.java
index c425420e84013dfa545d55ff2e6633853304d044..be7923ba5694d7c1eb169c0eb649fc9cfad8f4cc 100644 (file)
@@ -4,6 +4,7 @@ import static ro.ieval.fonbot.R.string.*;
 
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Locale;
 
@@ -18,6 +19,8 @@ import android.database.Cursor;
 import android.location.LocationManager;
 import android.media.AudioManager;
 import android.net.Uri;
+import android.os.Handler;
+import android.os.Looper;
 import android.preference.PreferenceManager;
 import android.provider.ContactsContract.PhoneLookup;
 import android.telephony.SmsManager;
@@ -65,7 +68,7 @@ public final class Utils {
                LS, RM, CONTACTS, DISABLE, ENABLE,
                POLL, HANGUP, ANSWER, LAUNCH, DATA,
                GPS, GLOCATION, REBOOT, NOTIFY, SCREENCAP,
-               TORCH
+               TORCH, GETFILE
        }
 
        /**
@@ -188,10 +191,8 @@ public final class Utils {
         * @throws AssertionError if the given object is null
         */
        public static <T> T toNonNull(@Nullable T object) throws AssertionError{
-               if(object==null){
-                       Log.wtf(Utils.class.getName(), "toNonNull called with null");
-                       throw new AssertionError("Log.wtf did not terminate the process");
-               }
+               if(object==null)
+                       throw new NullPointerException();
                return object;
        }
 
@@ -295,12 +296,17 @@ public final class Utils {
        public static void sendMessage(final Context context, final Address address, final String message){
                switch(address.protocol){
                case HTTP:
-                       new HttpCallExecutableRunnable("/send", toNonNull(Arrays.asList(
-                                       new Header("X-Destination", toNonNull(address.data)))), context, null, true, message).execute();
+                       new HttpCallExecutableRunnable("/send", toNonNull(Arrays.asList(new Header("X-Destination", toNonNull(address.data)))), context, null, true, address.requestId == null ? message : address.requestId + " " + message).execute();
                        break;
 
                case SMS:
-                       SmsManager.getDefault().sendTextMessage(address.data, null, message, null, null);
+                       new Handler(Looper.getMainLooper()).post(new Runnable(){
+                                       @Override
+                                       public void run(){
+                                               final ArrayList<String> parts = SmsManager.getDefault().divideMessage(message);
+                                               SmsManager.getDefault().sendMultipartTextMessage(address.data, null, parts, null, null);
+                                       }
+                               });
                        break;
 
                case LOCAL:
@@ -383,9 +389,10 @@ public final class Utils {
         * @throws MalformedURLException if the user preferences create an invalid URL
         */
        public static URL getServerURL(final Context context, final String path) throws MalformedURLException{
+               final String protocol=PreferenceManager.getDefaultSharedPreferences(context).getString("protocol", "https");
                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("http", hostname, port, path);
+               final URL url=new URL(protocol, hostname, port, path);
                return url;
        }
 
@@ -986,6 +993,22 @@ public final class Utils {
                case TORCH:
                        Heavy.torch(context, replyTo);
                        break;
+
+               case GETFILE:
+                       if(args.length != 3){
+                               Heavy.help(context, replyTo, toNonNull(Command.GETFILE));
+                               return;
+                       }
+
+                       final int getfilePort;
+                       try{
+                               getfilePort=Integer.parseInt(args[2]);
+                       } catch (NumberFormatException e){
+                               sendMessage(context, replyTo, cannot_parse_port);
+                               break;
+                       }
+                       Heavy.getfile(context, replyTo, toNonNull(args[0]), toNonNull(args[1]), getfilePort);
+                       break;
                }
 
        }
This page took 0.011424 seconds and 4 git commands to generate.