package ro.ieval.fonbot;
-import static ro.ieval.fonbot.R.string.cannot_parse_count;
-import static ro.ieval.fonbot.R.string.cannot_parse_interval;
-import static ro.ieval.fonbot.R.string.cannot_parse_min_distance;
-import static ro.ieval.fonbot.R.string.cannot_parse_min_time;
-import static ro.ieval.fonbot.R.string.cannot_parse_port;
-import static ro.ieval.fonbot.R.string.cannot_parse_provider_allowed_values_are;
-import static ro.ieval.fonbot.R.string.command_disabled;
-import static ro.ieval.fonbot.R.string.could_not_parse_argument_allowed_values_are;
-import static ro.ieval.fonbot.R.string.could_not_parse_ms;
-import static ro.ieval.fonbot.R.string.error_while_processing_command;
-import static ro.ieval.fonbot.R.string.invalid_length_allowed_values_are;
-import static ro.ieval.fonbot.R.string.invalid_ringer_mode_valid_values_are;
-import static ro.ieval.fonbot.R.string.location_tracking_is_active;
-import static ro.ieval.fonbot.R.string.messagetype_should_be_one_of;
-import static ro.ieval.fonbot.R.string.no_such_command_command_list;
-import static ro.ieval.fonbot.R.string.notification_disabled;
-import static ro.ieval.fonbot.R.string.notification_enabled;
-import static ro.ieval.fonbot.R.string.ringing;
-import static ro.ieval.fonbot.R.string.security_exception;
-import static ro.ieval.fonbot.R.string.the_polling_service_is_running;
-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 static ro.ieval.fonbot.R.string.*;
import java.net.MalformedURLException;
import java.net.URL;
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;
NEXT, PREV, BATT, CALLLOG, SMSLOG,
LS, RM, CONTACTS, DISABLE, ENABLE,
POLL, HANGUP, ANSWER, LAUNCH, DATA,
- GPS, GLOCATION, REBOOT, SHUTDOWN, NOTIFY
+ GPS, GLOCATION, REBOOT, NOTIFY, SCREENCAP,
+ TORCH, GETFILE
}
/**
/** Fine battery status notifications */
BATTERY_CHANGED,
/** Headset plug/unplug notifications */
- HEADSET
+ HEADSET,
+ /** Phone booted notifications */
+ BOOT,
}
/**
/** Location tracking is active. Registered by {@link Command#LOCATION}, unregistered by {@link Command#NOLOCATION} */
LOCATION(location_tracking_is_active),
/** The phone is ringing. Registered/unregistered by {@link Command#RING} */
- RING(ringing),
- /** The polling alarm is on. Registered/unregistered by {@link Command#POLL} */
- POLL(the_polling_service_is_running);
+ RING(ringing);
/** String resource: the event description */
public final int resource;
* @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;
}
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, 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(){
+ SmsManager.getDefault().sendTextMessage(address.data, null, message, null, null);
+ }
+ });
break;
case LOCAL:
}
}
- /**
- * Parses a HTTP response message of the form <code>(part1) part2</code>, returning <code>part2</code>.
- *
- * @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.
*
* @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", "ieval.ro");
- final int port=Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(context).getString("port", "7777"));
- final URL url=new URL("http", hostname, port, path);
+ 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(protocol, hostname, port, path);
return url;
}
*/
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;
}
break;
case PHOTO:
- if(args.length!=2){
+ if(args.length!=3){
Heavy.help(context, replyTo, toNonNull(Command.PHOTO));
break;
}
+
+ final int cameraNumber;
+ try{
+ cameraNumber=Integer.parseInt(args[0]);
+ } catch (NumberFormatException e){
+ sendMessage(context, replyTo, cannot_parse_camera_number);
+ break;
+ }
+
final int photoPort;
try{
- photoPort=Integer.parseInt(args[1]);
+ photoPort=Integer.parseInt(args[2]);
} catch (NumberFormatException e){
sendMessage(context, replyTo, cannot_parse_port);
break;
}
- Heavy.photo(context, replyTo, toNonNull(args[0]), photoPort);
+ Heavy.photo(context, replyTo, cameraNumber, toNonNull(args[1]), photoPort);
break;
case SETNOTIFICATION:
break;
case POLL:
- if(args.length>1){
- Heavy.help(context, replyTo, toNonNull(Command.POLL));
- break;
- }
-
- if(args.length==0){
- Heavy.poll(context, replyTo);
- break;
- }
-
- final long interval;
- try{
- interval=Long.parseLong(args[0]);
- } catch(NumberFormatException e){
- sendMessage(context, replyTo, cannot_parse_interval);
- break;
- }
-
- Heavy.poll(context, replyTo, interval);
+ Heavy.poll(context, replyTo);
break;
case HANGUP:
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;
else
Heavy.notify(context, replyTo, id, toNonNull(args[1]), toNonNull(args[2]));
break;
+
+ case SCREENCAP:
+ if(args.length != 1){
+ Heavy.help(context, replyTo, toNonNull(Command.SCREENCAP));
+ return;
+ }
+
+ Heavy.screencap(context, replyTo, args[0]);
+ break;
+
+ 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;
}
}