1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.R
.string
.*;
5 import java
.net
.MalformedURLException
;
7 import java
.util
.Arrays
;
8 import java
.util
.Locale
;
10 import org
.eclipse
.jdt
.annotation
.NonNull
;
11 import org
.eclipse
.jdt
.annotation
.Nullable
;
13 import ro
.ieval
.fonbot
.Address
.Protocol
;
14 import android
.content
.Context
;
15 import android
.content
.Intent
;
16 import android
.content
.SharedPreferences
;
17 import android
.database
.Cursor
;
18 import android
.location
.LocationManager
;
19 import android
.media
.AudioManager
;
20 import android
.net
.Uri
;
21 import android
.os
.Handler
;
22 import android
.os
.Looper
;
23 import android
.preference
.PreferenceManager
;
24 import android
.provider
.ContactsContract
.PhoneLookup
;
25 import android
.telephony
.SmsManager
;
26 import android
.util
.Log
;
27 import android
.widget
.Toast
;
30 * Copyright © 2013 Marius Gavrilescu
32 * This file is part of FonBot.
34 * FonBot is free software: you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation, either version 3 of the License, or
37 * (at your option) any later version.
39 * FonBot is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
44 * You should have received a copy of the GNU General Public License
45 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
49 * Utility functions and enums used in various places.
51 * @author Marius Gavrilescu <marius@ieval.ro>
53 public final class Utils
{
55 * Enum of all FonBot commands.
57 * @author Marius Gavrilescu <marius@ieval.ro>
59 @SuppressWarnings("javadoc")
60 public static enum Command
{
61 TOAST
, ECHO
, SMS
, FLASH
, WIFI
,
62 BLUETOOTH
, DIAL
, RING
, SPEAK
, VIBRATE
,
63 DIALOG
, LOCATION
, NOLOCATION
, RINGER
, NCFILE
,
64 PHOTO
, SETNOTIFICATION
, DELNOTIFICATION
, SETPASSWORD
, HELP
,
65 WIPE
, LOCK
, VIEW
, PLAY
, PAUSE
,
66 NEXT
, PREV
, BATT
, CALLLOG
, SMSLOG
,
67 LS
, RM
, CONTACTS
, DISABLE
, ENABLE
,
68 POLL
, HANGUP
, ANSWER
, LAUNCH
, DATA
,
69 GPS
, GLOCATION
, REBOOT
, NOTIFY
, SCREENCAP
,
74 * Enum of all message types. Each message type is a kind of notification which can be enabled/disabled by the user and directed to a certain address.
76 * @author Marius Gavrilescu <marius@ieval.ro>
79 public static enum MessageType
{
80 /** SMS notifications */
82 /** Phone state (idle, offhook or ringing) notifications */
84 /** Lockscreen failed password notifications */
86 /** Device admin enable/disable notifications */
88 /** Coarse battery status (low battery, ok battery) notifications */
90 /** Fine battery status notifications */
92 /** Headset plug/unplug notifications */
94 /** Phone booted notifications */
99 * Enum of wipe types. The two defined types are the data wipe (which does not include the SD card) and the full wipe (which includes the SD card).
101 * @author Marius Gavrilescu <marius@ieval.ro>
103 public static enum WipeType
{
104 /** Factory reset the phone, without touching the SD card */
106 /** Factory reset the phone and wipe the SD card too */
111 * Enum of ringer modes.
113 * @author Marius Gavrilescu <marius@ieval.ro>
115 public static enum RingerMode
{
118 /** Sound is off, vibrate is on */
120 /** Sound is off, vibrate is off */
125 * Enum of location providers
127 * @author Marius Gavrilescu <marius@ieval.ro>
129 public static enum LocationProvider
{
130 /** The network location provider */
132 /** The GPS location provider */
137 * Enum of toast lengths.
139 * @author Marius Gavrilescu <marius@ieval.ro>
141 private static enum ToastLength
{
149 * Enum of the values on and off. Used for boolean arguments.
151 * @author Marius Gavrilescu <marius@ieval.ro>
153 @SuppressWarnings("javadoc")
154 private static enum OnOff
{
159 * Enum of ongoing event types
161 * @author Marius Gavrilescu <marius@ieval.ro>
163 public static enum OngoingEvent
{
164 /** Location tracking is active. Registered by {@link Command#LOCATION}, unregistered by {@link Command#NOLOCATION} */
165 LOCATION(location_tracking_is_active
),
166 /** The phone is ringing. Registered/unregistered by {@link Command#RING} */
169 /** String resource: the event description */
170 public final int resource
;
173 * Constructs an OngoingEvent from its event description.
175 * @param resource the event description
177 private OngoingEvent(final int resource
) {
178 this.resource
=resource
;
182 /** Confirmation string for the {@link Command#WIPE WIPE} command. */
183 public static final String WIPE_CONFIRM_STRING
="I am aware this cannot be undone";
186 * Converts a Nullable object into a NonNull one.
188 * @param object the Nullable object to convert
189 * @return a NonNull object equivalent to the Nullable parameter
190 * @throws AssertionError if the given object is null
192 public static <T
> T
toNonNull(@Nullable T object
) throws AssertionError
{
194 Log
.wtf(Utils
.class.getName(), "toNonNull called with null");
195 throw new AssertionError("Log.wtf did not terminate the process");
201 * Join an array of Objects with elements separated by a separator into a single string.
203 * @param separator the separator
204 * @param offset the offset into the array
205 * @param args the array of Objects to join
206 * @return the joined string
208 public static String
join(final String separator
,final int offset
,final Object
[] args
){
209 final StringBuilder sb
=new StringBuilder(240);
210 sb
.append(args
[offset
]);
211 for (int i
= offset
+1; i
< args
.length
; i
++) {
212 sb
.append(separator
);
215 return toNonNull(sb
.toString());
219 * Join an array of Objects with elements separated by a separator into a single string.
221 * @param separator the separator
222 * @param args the array of objects to join
223 * @return the joined string
225 public static String
join(final String separator
, final Object
[] args
){
226 return join(separator
,0,args
);
230 * Send a notification to the user.
232 * @param context Context instance
233 * @param type notification type
234 * @param resource String resource for the message text
236 public static void sendMessage(final Context context
, final MessageType type
, final int resource
){
237 sendMessage(context
, type
, toNonNull(context
.getString(resource
)));
241 * Send a notification to the user.
243 * @param context Context instance
244 * @param type notification type
245 * @param resource String resource for the message text
246 * @param args format parameters for the resource
248 public static void sendMessage(final Context context
, final MessageType type
, final int resource
, final Object
... args
){
249 sendMessage(context
, type
, toNonNull(context
.getString(resource
, args
)));
253 * Send a message to a certain Address.
255 * @param context Context instance
256 * @param address destination Address
257 * @param resource String resource for the message text
259 public static void sendMessage(final Context context
, final Address address
, final int resource
){
260 sendMessage(context
, address
, toNonNull(context
.getString(resource
)));
264 * Send a message to a certain Address.
266 * @param context Context instance
267 * @param address destination Address
268 * @param resource String resource for the message text
269 * @param args format parameters for the resource
271 public static void sendMessage(final Context context
, final Address address
, final int resource
, final Object
... args
){
272 sendMessage(context
, address
, toNonNull(context
.getString(resource
, args
)));
276 * Send a notification to the user.
278 * @param context Context instance
279 * @param type notification type
280 * @param msg the notification text
282 public static void sendMessage(final Context context
, final MessageType type
,final String msg
){
283 final SharedPreferences sp
=PreferenceManager
.getDefaultSharedPreferences(context
);
284 final String address
=sp
.getString(type
.toString(),null);
287 sendMessage(context
, new Address(address
), msg
);
291 * Send a message to a certain Address.
293 * @param context Context instance
294 * @param address destination Address
295 * @param message the message text
297 public static void sendMessage(final Context context
, final Address address
, final String message
){
298 switch(address
.protocol
){
300 new HttpCallExecutableRunnable("/send", toNonNull(Arrays
.asList(new Header("X-Destination", toNonNull(address
.data
)))), context
, null, true, address
.requestId
== null ? message
: address
.requestId
+ " " + message
).execute();
304 new Handler(Looper
.getMainLooper()).post(new Runnable(){
307 SmsManager
.getDefault().sendTextMessage(address
.data
, null, message
, null, null);
313 final Intent intent
= new Intent(FonBotLocalActivity
.RESPONSE_RECEIVED_ACTION
);
314 intent
.putExtra(FonBotLocalActivity
.EXTRA_RESPONSE
, message
);
315 context
.sendBroadcast(intent
);
324 * Splits a string into words.
326 * @param string the string
327 * @return an array of words
329 public static String
[] shellwords(final String string
){
330 return toNonNull(string
.split("[ ]+(?=([^\"]*\"[^\"]*\")*[^\"]*$)"));//TODO: Make this handle backslash escapes
334 * Returns the name associated with a phone number.
336 * @param context Context instance
337 * @param number phone number to search for
338 * @return the name associated with this number, or null if the number was not found in the contacts.
340 public static @Nullable String
callerId(final Context context
, final String number
){
341 final Cursor cursor
=context
.getContentResolver().query(
342 Uri
.withAppendedPath(PhoneLookup
.CONTENT_FILTER_URI
, Uri
.encode(number
)),
343 new String
[]{PhoneLookup
.DISPLAY_NAME
},
348 if(cursor
.moveToFirst()){
349 final String name
=cursor
.getString(0);
358 * Registers an ongoing event.
360 * @param context Context instance
361 * @param event event to register
363 public static void registerOngoing(final Context context
, final OngoingEvent event
){
364 final Intent intent
=new Intent(context
, FonBotMainService
.class);
365 intent
.setAction(FonBotMainService
.ACTION_PUT_ONGOING
);
366 intent
.putExtra(FonBotMainService
.EXTRA_ONGOING_ID
, event
.ordinal());
367 context
.startService(intent
);
371 * Unregisters an ongoing event
373 * @param context Context instance
374 * @param event event to unregister
376 public static void unregisterOngoing(final Context context
, final OngoingEvent event
){
377 final Intent intent
=new Intent(context
, FonBotMainService
.class);
378 intent
.setAction(FonBotMainService
.ACTION_DELETE_ONGOING
);
379 intent
.putExtra(FonBotMainService
.EXTRA_ONGOING_ID
, event
.ordinal());
380 context
.startService(intent
);
384 * Gets the server URL according to the user preferences.
386 * @param context Context instance
387 * @param path URL path
388 * @return the server URL
389 * @throws MalformedURLException if the user preferences create an invalid URL
391 public static URL
getServerURL(final Context context
, final String path
) throws MalformedURLException
{
392 final String protocol
=PreferenceManager
.getDefaultSharedPreferences(context
).getString("protocol", "https");
393 final String hostname
=PreferenceManager
.getDefaultSharedPreferences(context
).getString("hostname", "fonbot.ieval.ro");
394 final int port
=Integer
.parseInt(PreferenceManager
.getDefaultSharedPreferences(context
).getString("port", "443"));
395 final URL url
=new URL(protocol
, hostname
, port
, path
);
400 * Executes a given command
402 * @param context Context instance
403 * @param cmd Command to execute
404 * @param args arguments for the command
405 * @param replyTo Address to send replies to
407 private static void processCommand(final Context context
, final Command cmd
,final String
[] args
,final Address replyTo
){
408 if(Heavy
.isCommandDisabled(context
, cmd
)){
409 sendMessage(context
, replyTo
, command_disabled
, cmd
.toString());
415 if(args
.length
< 1 || args
.length
> 2){
416 Heavy
.help(context
, replyTo
, toNonNull(Command
.TOAST
));
421 Heavy
.toast(context
, replyTo
, toNonNull(args
[0]));
424 switch(ToastLength
.valueOf(args
[1].toUpperCase(Locale
.ENGLISH
))){
426 Heavy
.toast(context
, replyTo
, toNonNull(args
[0]), Toast
.LENGTH_LONG
);
429 Heavy
.toast(context
, replyTo
, toNonNull(args
[0]), Toast
.LENGTH_SHORT
);
431 } catch(IllegalArgumentException e
){
432 sendMessage(context
, replyTo
, invalid_length_allowed_values_are
, join(", ",toNonNull(ToastLength
.values())));
439 Heavy
.help(context
, replyTo
, toNonNull(Command
.ECHO
));
442 sendMessage(context
, replyTo
, join(" ",args
));
447 Heavy
.help(context
, replyTo
, toNonNull(Command
.SMS
));
450 Heavy
.sms(context
, replyTo
, toNonNull(args
[0]), join(" ", 1, args
));
454 if(args
.length
!= 1){
455 Heavy
.help(context
, replyTo
, toNonNull(Command
.FLASH
));
460 Heavy
.flash(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
461 } catch(IllegalArgumentException e
) {
462 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
468 Heavy
.help(context
, replyTo
, toNonNull(Command
.WIFI
));
472 Heavy
.wifi(context
, replyTo
);
475 Heavy
.wifi(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
476 } catch(IllegalArgumentException e
) {
477 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
484 Heavy
.help(context
, replyTo
, toNonNull(Command
.BLUETOOTH
));
488 Heavy
.bluetooth(context
, replyTo
);
491 Heavy
.bluetooth(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
492 } catch(IllegalArgumentException e
) {
493 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
500 Heavy
.help(context
, replyTo
, toNonNull(Command
.DIAL
));
503 Heavy
.dial(context
, replyTo
, toNonNull(args
[0]));
508 Heavy
.help(context
, replyTo
, toNonNull(Command
.RING
));
512 Heavy
.ring(context
, replyTo
);
515 Heavy
.ring(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
516 } catch(IllegalArgumentException e
){
517 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
524 Heavy
.help(context
, replyTo
, toNonNull(Command
.SPEAK
));
527 Heavy
.speak(context
, replyTo
, join(" ",args
));
532 Heavy
.help(context
, replyTo
, toNonNull(Command
.VIBRATE
));
537 ms
=Long
.parseLong(args
[0]);
538 } catch(NumberFormatException e
){
539 sendMessage(context
, replyTo
, could_not_parse_ms
);
542 Heavy
.vibrate(context
, replyTo
, ms
);
545 case DIALOG
://TODO: Should add an edittext
547 Heavy
.help(context
, replyTo
, toNonNull(Command
.DIALOG
));
550 final String
[] buttons
=new String
[args
.length
-1];
551 System
.arraycopy(args
,1,buttons
,0,buttons
.length
);
552 Heavy
.dialog(context
, replyTo
, toNonNull(args
[0]), buttons
);
556 if(args
.length
>3||args
.length
<1){
557 Heavy
.help(context
, replyTo
, toNonNull(Command
.LOCATION
));
561 final @NonNull String provider
;
563 switch(LocationProvider
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))){
565 provider
=toNonNull(LocationManager
.GPS_PROVIDER
);
569 provider
=toNonNull(LocationManager
.NETWORK_PROVIDER
);
572 } catch(IllegalArgumentException e
){
573 sendMessage(context
, replyTo
, cannot_parse_provider_allowed_values_are
, join(", ",toNonNull(LocationProvider
.values())));
578 final float minDistance
;
582 minTime
=Long
.parseLong(args
[1]);
583 } catch (NumberFormatException e
){
584 sendMessage(context
, replyTo
, cannot_parse_min_time
);
592 minDistance
=Float
.parseFloat(args
[2]);
593 } catch (NumberFormatException e
){
594 sendMessage(context
, replyTo
, cannot_parse_min_distance
);
599 Heavy
.location(context
, replyTo
, provider
, minTime
, minDistance
);
603 Heavy
.nolocation(context
, replyTo
);
608 Heavy
.help(context
, replyTo
, toNonNull(Command
.RINGER
));
612 Heavy
.ringer(context
, replyTo
);
615 final RingerMode rm
=RingerMode
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
));
618 Heavy
.ringer(context
, replyTo
, AudioManager
.RINGER_MODE_NORMAL
);
621 Heavy
.ringer(context
, replyTo
, AudioManager
.RINGER_MODE_VIBRATE
);
624 Heavy
.ringer(context
, replyTo
, AudioManager
.RINGER_MODE_SILENT
);
627 } catch (IllegalArgumentException e
){
628 Utils
.sendMessage(context
, replyTo
, invalid_ringer_mode_valid_values_are
, join(", ",toNonNull(RingerMode
.values())));
635 Heavy
.help(context
, replyTo
, toNonNull(Command
.NCFILE
));
639 final int ncfilePort
;
641 ncfilePort
=Integer
.parseInt(args
[2]);
642 } catch (NumberFormatException e
){
643 sendMessage(context
, replyTo
, cannot_parse_port
);
646 Heavy
.ncfile(context
, replyTo
, toNonNull(args
[0]), toNonNull(args
[1]), ncfilePort
);
651 Heavy
.help(context
, replyTo
, toNonNull(Command
.PHOTO
));
655 final int cameraNumber
;
657 cameraNumber
=Integer
.parseInt(args
[0]);
658 } catch (NumberFormatException e
){
659 sendMessage(context
, replyTo
, cannot_parse_camera_number
);
665 photoPort
=Integer
.parseInt(args
[2]);
666 } catch (NumberFormatException e
){
667 sendMessage(context
, replyTo
, cannot_parse_port
);
670 Heavy
.photo(context
, replyTo
, cameraNumber
, toNonNull(args
[1]), photoPort
);
673 case SETNOTIFICATION
:
675 Heavy
.help(context
, replyTo
, toNonNull(Command
.SETNOTIFICATION
));
680 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
681 .putString(MessageType
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)).toString(), replyTo
.toString())
683 sendMessage(context
, replyTo
, notification_enabled
);
684 } catch (IllegalArgumentException e
){
685 sendMessage(context
, replyTo
, messagetype_should_be_one_of
, join(", ",toNonNull(MessageType
.values())));
691 case DELNOTIFICATION
:
693 Heavy
.help(context
, replyTo
, toNonNull(Command
.DELNOTIFICATION
));
698 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
699 .remove(MessageType
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)).toString())
701 sendMessage(context
, replyTo
, notification_disabled
);
702 } catch (IllegalArgumentException e
){
703 sendMessage(context
, replyTo
, messagetype_should_be_one_of
, join(", ",toNonNull(MessageType
.values())));
711 Heavy
.help(context
, replyTo
, toNonNull(Command
.SETPASSWORD
));
717 Heavy
.setPassword(context
, replyTo
);
719 Heavy
.setPassword(context
, replyTo
, toNonNull(args
[0]));
720 } catch (SecurityException e
){
721 sendMessage(context
, replyTo
, security_exception
+e
.getMessage());
727 Heavy
.help(context
, replyTo
, toNonNull(Command
.WIPE
));
731 if(!args
[1].equalsIgnoreCase(WIPE_CONFIRM_STRING
)){
732 sendMessage(context
, replyTo
, the_second_argument_to_wipe_must_be
, WIPE_CONFIRM_STRING
);
737 Heavy
.wipe(context
, toNonNull(WipeType
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
738 } catch (IllegalArgumentException e
){
739 sendMessage(context
, replyTo
, wipetype_should_be_one_of
, join (", ",toNonNull(WipeType
.values())));
740 } catch (SecurityException e
){
741 sendMessage(context
, replyTo
, security_exception
, e
.getMessage());
747 Heavy
.lock(context
, replyTo
);
748 } catch (SecurityException e
){
749 sendMessage(context
, replyTo
, security_exception
, e
.getMessage());
755 Heavy
.help(context
, replyTo
, toNonNull(Command
.VIEW
));
759 Heavy
.view(context
, replyTo
, toNonNull(Uri
.parse(args
[0])));
763 Heavy
.musicPlayerCommand(context
, replyTo
, "play");
767 Heavy
.musicPlayerCommand(context
, replyTo
, "pause");
771 Heavy
.musicPlayerCommand(context
, replyTo
, "next");
775 Heavy
.musicPlayerCommand(context
, replyTo
, "previous");
779 Heavy
.batt(context
, replyTo
);
783 if (args
.length
> 1) {
784 Heavy
.help(context
, replyTo
, toNonNull(Command
.CALLLOG
));
788 if (args
.length
== 0)
789 Heavy
.calllog(context
, replyTo
, 5);
792 Heavy
.calllog(context
, replyTo
, Integer
.parseInt(args
[0]));
793 } catch (IllegalArgumentException e
){
794 sendMessage(context
, replyTo
, cannot_parse_count
);
800 if (args
.length
> 1) {
801 Heavy
.help(context
, replyTo
, toNonNull(Command
.SMSLOG
));
805 if (args
.length
== 0)
806 Heavy
.smslog(context
, replyTo
, 5);
809 Heavy
.smslog(context
, replyTo
, Integer
.parseInt(args
[0]));
810 } catch (IllegalArgumentException e
) {
811 sendMessage(context
, replyTo
, cannot_parse_count
);
817 if(args
.length
!= 1){
818 Heavy
.help(context
, replyTo
, toNonNull(Command
.HELP
));
823 Heavy
.help(context
, replyTo
, toNonNull(Command
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
824 } catch (IllegalArgumentException e
) {
825 sendMessage(context
, replyTo
, no_such_command_command_list
, join(", ", toNonNull(Command
.values())));
830 if(args
.length
!= 1){
831 Heavy
.help(context
, replyTo
, toNonNull(Command
.LS
));
835 Heavy
.ls(context
, replyTo
, toNonNull(args
[0]));
839 if(args
.length
!= 1){
840 Heavy
.help(context
, replyTo
, toNonNull(Command
.RM
));
844 Heavy
.rm(context
, replyTo
, toNonNull(args
[0]));
848 if(args
.length
!= 1){
849 Heavy
.help(context
, replyTo
, toNonNull(Command
.CONTACTS
));
853 Heavy
.contacts(context
, replyTo
, toNonNull(args
[0]));
857 if(replyTo
.protocol
!= Protocol
.LOCAL
|| args
.length
!= 1){
858 Heavy
.help(context
, replyTo
, toNonNull(Command
.DISABLE
));
863 Heavy
.disable(context
, replyTo
, toNonNull(Command
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
864 } catch (IllegalArgumentException e
){
865 sendMessage(context
, replyTo
, no_such_command_command_list
, join(", ", toNonNull(Command
.values())));
870 if(replyTo
.protocol
!= Protocol
.LOCAL
|| args
.length
!= 1){
871 Heavy
.help(context
, replyTo
, toNonNull(Command
.ENABLE
));
876 Heavy
.enable(context
, replyTo
, toNonNull(Command
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
877 } catch (IllegalArgumentException e
){
878 sendMessage(context
, replyTo
, no_such_command_command_list
, join(", ", toNonNull(Command
.values())));
883 Heavy
.poll(context
, replyTo
);
887 Heavy
.hangup(context
, replyTo
);
891 Heavy
.answer(context
, replyTo
);
896 Heavy
.help(context
, replyTo
, toNonNull(Command
.LAUNCH
));
899 Heavy
.launch(context
, replyTo
, toNonNull(args
[0]));
904 Heavy
.help(context
, replyTo
, toNonNull(Command
.DATA
));
909 Heavy
.data(context
, replyTo
);
913 Heavy
.data(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
914 } catch(IllegalArgumentException e
) {
915 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
921 Heavy
.help(context
, replyTo
, toNonNull(Command
.GPS
));
926 Heavy
.gps(context
, replyTo
);
931 Heavy
.gps(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
932 } catch(IllegalArgumentException e
) {
933 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
939 Heavy
.help(context
, replyTo
, toNonNull(Command
.GLOCATION
));
944 Heavy
.glocation(context
, replyTo
);
949 Heavy
.glocation(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
950 } catch(IllegalArgumentException e
) {
951 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
957 Heavy
.help(context
, replyTo
, toNonNull(Command
.REBOOT
));
961 Heavy
.reboot(context
, replyTo
, args
.length
==0?
null:args
[0]);
965 if(args
.length
!=1 && args
.length
!=3){
966 Heavy
.help(context
, replyTo
, toNonNull(Command
.NOTIFY
));
972 id
=Integer
.parseInt(args
[0]);
973 } catch (NumberFormatException e
){
974 sendMessage(context
, replyTo
, R
.string
.could_not_parse_id
);
979 Heavy
.notify(context
, replyTo
, id
);
981 Heavy
.notify(context
, replyTo
, id
, toNonNull(args
[1]), toNonNull(args
[2]));
985 if(args
.length
!= 1){
986 Heavy
.help(context
, replyTo
, toNonNull(Command
.SCREENCAP
));
990 Heavy
.screencap(context
, replyTo
, args
[0]);
994 Heavy
.torch(context
, replyTo
);
998 if(args
.length
!= 3){
999 Heavy
.help(context
, replyTo
, toNonNull(Command
.GETFILE
));
1003 final int getfilePort
;
1005 getfilePort
=Integer
.parseInt(args
[2]);
1006 } catch (NumberFormatException e
){
1007 sendMessage(context
, replyTo
, cannot_parse_port
);
1010 Heavy
.getfile(context
, replyTo
, toNonNull(args
[0]), toNonNull(args
[1]), getfilePort
);
1017 * Executes a given command
1019 * @param context Context instance
1020 * @param cmd Command to execute
1021 * @param args arguments for the command
1022 * @param replyTo Address to send replies to
1024 public static void processCommand(final Context context
, final String cmd
,final String
[] args
,final Address replyTo
){
1025 final @NonNull Command command
;
1027 command
=toNonNull(Command
.valueOf(cmd
.toUpperCase(Locale
.ENGLISH
)));
1028 } catch (IllegalArgumentException e
){
1029 sendMessage(context
, replyTo
, unknown_command
, cmd
.toUpperCase(Locale
.ENGLISH
), e
.getMessage());
1034 processCommand(context
, command
,args
,replyTo
);
1035 } catch(Exception e
){
1036 sendMessage(context
, replyTo
, error_while_processing_command
, e
.getClass().getName(), e
.getMessage());
1037 Log
.w(Utils
.class.getName(), "Error while processing command", e
);