1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.R
.string
.cannot_parse_count
;
4 import static ro
.ieval
.fonbot
.R
.string
.cannot_parse_interval
;
5 import static ro
.ieval
.fonbot
.R
.string
.cannot_parse_min_distance
;
6 import static ro
.ieval
.fonbot
.R
.string
.cannot_parse_min_time
;
7 import static ro
.ieval
.fonbot
.R
.string
.cannot_parse_port
;
8 import static ro
.ieval
.fonbot
.R
.string
.cannot_parse_provider_allowed_values_are
;
9 import static ro
.ieval
.fonbot
.R
.string
.command_disabled
;
10 import static ro
.ieval
.fonbot
.R
.string
.could_not_parse_argument_allowed_values_are
;
11 import static ro
.ieval
.fonbot
.R
.string
.could_not_parse_ms
;
12 import static ro
.ieval
.fonbot
.R
.string
.error_while_processing_command
;
13 import static ro
.ieval
.fonbot
.R
.string
.invalid_length_allowed_values_are
;
14 import static ro
.ieval
.fonbot
.R
.string
.invalid_ringer_mode_valid_values_are
;
15 import static ro
.ieval
.fonbot
.R
.string
.location_tracking_is_active
;
16 import static ro
.ieval
.fonbot
.R
.string
.messagetype_should_be_one_of
;
17 import static ro
.ieval
.fonbot
.R
.string
.no_such_command_command_list
;
18 import static ro
.ieval
.fonbot
.R
.string
.notification_disabled
;
19 import static ro
.ieval
.fonbot
.R
.string
.notification_enabled
;
20 import static ro
.ieval
.fonbot
.R
.string
.ringing
;
21 import static ro
.ieval
.fonbot
.R
.string
.security_exception
;
22 import static ro
.ieval
.fonbot
.R
.string
.the_polling_service_is_running
;
23 import static ro
.ieval
.fonbot
.R
.string
.the_second_argument_to_wipe_must_be
;
24 import static ro
.ieval
.fonbot
.R
.string
.unknown_command
;
25 import static ro
.ieval
.fonbot
.R
.string
.wipetype_should_be_one_of
;
27 import java
.net
.MalformedURLException
;
29 import java
.util
.Arrays
;
30 import java
.util
.Locale
;
32 import org
.eclipse
.jdt
.annotation
.NonNull
;
33 import org
.eclipse
.jdt
.annotation
.Nullable
;
35 import ro
.ieval
.fonbot
.Address
.Protocol
;
36 import android
.content
.Context
;
37 import android
.content
.Intent
;
38 import android
.content
.SharedPreferences
;
39 import android
.database
.Cursor
;
40 import android
.location
.LocationManager
;
41 import android
.media
.AudioManager
;
42 import android
.net
.Uri
;
43 import android
.preference
.PreferenceManager
;
44 import android
.provider
.ContactsContract
.PhoneLookup
;
45 import android
.telephony
.SmsManager
;
46 import android
.util
.Log
;
47 import android
.widget
.Toast
;
50 * Copyright © 2013 Marius Gavrilescu
52 * This file is part of FonBot.
54 * FonBot is free software: you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
56 * the Free Software Foundation, either version 3 of the License, or
57 * (at your option) any later version.
59 * FonBot is distributed in the hope that it will be useful,
60 * but WITHOUT ANY WARRANTY; without even the implied warranty of
61 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
62 * GNU General Public License for more details.
64 * You should have received a copy of the GNU General Public License
65 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
69 * Utility functions and enums used in various places.
71 * @author Marius Gavrilescu <marius@ieval.ro>
73 public final class Utils
{
75 * Enum of all FonBot commands.
77 * @author Marius Gavrilescu <marius@ieval.ro>
79 @SuppressWarnings("javadoc")
80 public static enum Command
{
81 TOAST
, ECHO
, SMS
, FLASH
, WIFI
,
82 BLUETOOTH
, DIAL
, RING
, SPEAK
, VIBRATE
,
83 DIALOG
, LOCATION
, NOLOCATION
, RINGER
, NCFILE
,
84 PHOTO
, SETNOTIFICATION
, DELNOTIFICATION
, SETPASSWORD
, HELP
,
85 WIPE
, LOCK
, VIEW
, PLAY
, PAUSE
,
86 NEXT
, PREV
, BATT
, CALLLOG
, SMSLOG
,
87 LS
, RM
, CONTACTS
, DISABLE
, ENABLE
,
88 POLL
, HANGUP
, ANSWER
, LAUNCH
, DATA
,
89 GPS
, GLOCATION
, REBOOT
, NOTIFY
, SCREENCAP
,
94 * 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.
96 * @author Marius Gavrilescu <marius@ieval.ro>
99 public static enum MessageType
{
100 /** SMS notifications */
102 /** Phone state (idle, offhook or ringing) notifications */
104 /** Lockscreen failed password notifications */
106 /** Device admin enable/disable notifications */
108 /** Coarse battery status (low battery, ok battery) notifications */
110 /** Fine battery status notifications */
112 /** Headset plug/unplug notifications */
117 * 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).
119 * @author Marius Gavrilescu <marius@ieval.ro>
121 public static enum WipeType
{
122 /** Factory reset the phone, without touching the SD card */
124 /** Factory reset the phone and wipe the SD card too */
129 * Enum of ringer modes.
131 * @author Marius Gavrilescu <marius@ieval.ro>
133 public static enum RingerMode
{
136 /** Sound is off, vibrate is on */
138 /** Sound is off, vibrate is off */
143 * Enum of location providers
145 * @author Marius Gavrilescu <marius@ieval.ro>
147 public static enum LocationProvider
{
148 /** The network location provider */
150 /** The GPS location provider */
155 * Enum of toast lengths.
157 * @author Marius Gavrilescu <marius@ieval.ro>
159 private static enum ToastLength
{
167 * Enum of the values on and off. Used for boolean arguments.
169 * @author Marius Gavrilescu <marius@ieval.ro>
171 @SuppressWarnings("javadoc")
172 private static enum OnOff
{
177 * Enum of ongoing event types
179 * @author Marius Gavrilescu <marius@ieval.ro>
181 public static enum OngoingEvent
{
182 /** Location tracking is active. Registered by {@link Command#LOCATION}, unregistered by {@link Command#NOLOCATION} */
183 LOCATION(location_tracking_is_active
),
184 /** The phone is ringing. Registered/unregistered by {@link Command#RING} */
186 /** The polling alarm is on. Registered/unregistered by {@link Command#POLL} */
187 POLL(the_polling_service_is_running
);
189 /** String resource: the event description */
190 public final int resource
;
193 * Constructs an OngoingEvent from its event description.
195 * @param resource the event description
197 private OngoingEvent(final int resource
) {
198 this.resource
=resource
;
202 /** Confirmation string for the {@link Command#WIPE WIPE} command. */
203 public static final String WIPE_CONFIRM_STRING
="I am aware this cannot be undone";
206 * Converts a Nullable object into a NonNull one.
208 * @param object the Nullable object to convert
209 * @return a NonNull object equivalent to the Nullable parameter
210 * @throws AssertionError if the given object is null
212 public static <T
> T
toNonNull(@Nullable T object
) throws AssertionError
{
214 Log
.wtf(Utils
.class.getName(), "toNonNull called with null");
215 throw new AssertionError("Log.wtf did not terminate the process");
221 * Join an array of Objects with elements separated by a separator into a single string.
223 * @param separator the separator
224 * @param offset the offset into the array
225 * @param args the array of Objects to join
226 * @return the joined string
228 public static String
join(final String separator
,final int offset
,final Object
[] args
){
229 final StringBuilder sb
=new StringBuilder(240);
230 sb
.append(args
[offset
]);
231 for (int i
= offset
+1; i
< args
.length
; i
++) {
232 sb
.append(separator
);
235 return toNonNull(sb
.toString());
239 * Join an array of Objects with elements separated by a separator into a single string.
241 * @param separator the separator
242 * @param args the array of objects to join
243 * @return the joined string
245 public static String
join(final String separator
, final Object
[] args
){
246 return join(separator
,0,args
);
250 * Send a notification to the user.
252 * @param context Context instance
253 * @param type notification type
254 * @param resource String resource for the message text
256 public static void sendMessage(final Context context
, final MessageType type
, final int resource
){
257 sendMessage(context
, type
, toNonNull(context
.getString(resource
)));
261 * Send a notification to the user.
263 * @param context Context instance
264 * @param type notification type
265 * @param resource String resource for the message text
266 * @param args format parameters for the resource
268 public static void sendMessage(final Context context
, final MessageType type
, final int resource
, final Object
... args
){
269 sendMessage(context
, type
, toNonNull(context
.getString(resource
, args
)));
273 * Send a message to a certain Address.
275 * @param context Context instance
276 * @param address destination Address
277 * @param resource String resource for the message text
279 public static void sendMessage(final Context context
, final Address address
, final int resource
){
280 sendMessage(context
, address
, toNonNull(context
.getString(resource
)));
284 * Send a message to a certain Address.
286 * @param context Context instance
287 * @param address destination Address
288 * @param resource String resource for the message text
289 * @param args format parameters for the resource
291 public static void sendMessage(final Context context
, final Address address
, final int resource
, final Object
... args
){
292 sendMessage(context
, address
, toNonNull(context
.getString(resource
, args
)));
296 * Send a notification to the user.
298 * @param context Context instance
299 * @param type notification type
300 * @param msg the notification text
302 public static void sendMessage(final Context context
, final MessageType type
,final String msg
){
303 final SharedPreferences sp
=PreferenceManager
.getDefaultSharedPreferences(context
);
304 final String address
=sp
.getString(type
.toString(),null);
307 sendMessage(context
, new Address(address
), msg
);
311 * Send a message to a certain Address.
313 * @param context Context instance
314 * @param address destination Address
315 * @param message the message text
317 public static void sendMessage(final Context context
, final Address address
, final String message
){
318 switch(address
.protocol
){
320 new HttpCallExecutableRunnable("/send", toNonNull(Arrays
.asList(
321 new Header("X-Destination", toNonNull(address
.data
)))), context
, null, true, message
).execute();
325 SmsManager
.getDefault().sendTextMessage(address
.data
, null, message
, null, null);
329 final Intent intent
= new Intent(FonBotLocalActivity
.RESPONSE_RECEIVED_ACTION
);
330 intent
.putExtra(FonBotLocalActivity
.EXTRA_RESPONSE
, message
);
331 context
.sendBroadcast(intent
);
340 * Splits a string into words.
342 * @param string the string
343 * @return an array of words
345 public static String
[] shellwords(final String string
){
346 return toNonNull(string
.split("[ ]+(?=([^\"]*\"[^\"]*\")*[^\"]*$)"));//TODO: Make this handle backslash escapes
350 * Returns the name associated with a phone number.
352 * @param context Context instance
353 * @param number phone number to search for
354 * @return the name associated with this number, or null if the number was not found in the contacts.
356 public static @Nullable String
callerId(final Context context
, final String number
){
357 final Cursor cursor
=context
.getContentResolver().query(
358 Uri
.withAppendedPath(PhoneLookup
.CONTENT_FILTER_URI
, Uri
.encode(number
)),
359 new String
[]{PhoneLookup
.DISPLAY_NAME
},
364 if(cursor
.moveToFirst()){
365 final String name
=cursor
.getString(0);
374 * Registers an ongoing event.
376 * @param context Context instance
377 * @param event event to register
379 public static void registerOngoing(final Context context
, final OngoingEvent event
){
380 final Intent intent
=new Intent(context
, FonBotMainService
.class);
381 intent
.setAction(FonBotMainService
.ACTION_PUT_ONGOING
);
382 intent
.putExtra(FonBotMainService
.EXTRA_ONGOING_ID
, event
.ordinal());
383 context
.startService(intent
);
387 * Unregisters an ongoing event
389 * @param context Context instance
390 * @param event event to unregister
392 public static void unregisterOngoing(final Context context
, final OngoingEvent event
){
393 final Intent intent
=new Intent(context
, FonBotMainService
.class);
394 intent
.setAction(FonBotMainService
.ACTION_DELETE_ONGOING
);
395 intent
.putExtra(FonBotMainService
.EXTRA_ONGOING_ID
, event
.ordinal());
396 context
.startService(intent
);
400 * Gets the server URL according to the user preferences.
402 * @param context Context instance
403 * @param path URL path
404 * @return the server URL
405 * @throws MalformedURLException if the user preferences create an invalid URL
407 public static URL
getServerURL(final Context context
, final String path
) throws MalformedURLException
{
408 final String hostname
=PreferenceManager
.getDefaultSharedPreferences(context
).getString("hostname", "fonbot.ieval.ro");
409 final int port
=Integer
.parseInt(PreferenceManager
.getDefaultSharedPreferences(context
).getString("port", "443"));
410 final URL url
=new URL("https", hostname
, port
, path
);
415 * Poll the server for pending commands. This function must not be called from BroadcastReceivers
417 * @param context Context instance
419 public static void pollServer(final Context context
){
420 new HttpCallExecutableRunnable("/get", null, context
, new PollResultCallback(context
), false).execute();
424 * Poll the server for pending commands from {@link FonBotMainService}. This function should be used from BroadcastReceviers instead of {@link #pollServer}
426 * @param context Context instance
428 public static void safePollServer(final Context context
){
429 final Intent intent
=new Intent(context
, FonBotMainService
.class);
430 intent
.setAction(FonBotMainService
.ACTION_TRIGGER_POLL
);
431 context
.startService(intent
);
434 * Executes a given command
436 * @param context Context instance
437 * @param cmd Command to execute
438 * @param args arguments for the command
439 * @param replyTo Address to send replies to
441 private static void processCommand(final Context context
, final Command cmd
,final String
[] args
,final Address replyTo
){
442 if(Heavy
.isCommandDisabled(context
, cmd
)){
443 sendMessage(context
, replyTo
, command_disabled
, cmd
.toString());
449 if(args
.length
< 1 || args
.length
> 2){
450 Heavy
.help(context
, replyTo
, toNonNull(Command
.TOAST
));
455 Heavy
.toast(context
, replyTo
, toNonNull(args
[0]));
458 switch(ToastLength
.valueOf(args
[1].toUpperCase(Locale
.ENGLISH
))){
460 Heavy
.toast(context
, replyTo
, toNonNull(args
[0]), Toast
.LENGTH_LONG
);
463 Heavy
.toast(context
, replyTo
, toNonNull(args
[0]), Toast
.LENGTH_SHORT
);
465 } catch(IllegalArgumentException e
){
466 sendMessage(context
, replyTo
, invalid_length_allowed_values_are
, join(", ",toNonNull(ToastLength
.values())));
473 Heavy
.help(context
, replyTo
, toNonNull(Command
.ECHO
));
476 sendMessage(context
, replyTo
, join(" ",args
));
481 Heavy
.help(context
, replyTo
, toNonNull(Command
.SMS
));
484 Heavy
.sms(context
, replyTo
, toNonNull(args
[0]), join(" ", 1, args
));
488 if(args
.length
!= 1){
489 Heavy
.help(context
, replyTo
, toNonNull(Command
.FLASH
));
494 Heavy
.flash(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
495 } catch(IllegalArgumentException e
) {
496 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
502 Heavy
.help(context
, replyTo
, toNonNull(Command
.WIFI
));
506 Heavy
.wifi(context
, replyTo
);
509 Heavy
.wifi(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
510 } catch(IllegalArgumentException e
) {
511 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
518 Heavy
.help(context
, replyTo
, toNonNull(Command
.BLUETOOTH
));
522 Heavy
.bluetooth(context
, replyTo
);
525 Heavy
.bluetooth(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
526 } catch(IllegalArgumentException e
) {
527 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
534 Heavy
.help(context
, replyTo
, toNonNull(Command
.DIAL
));
537 Heavy
.dial(context
, replyTo
, toNonNull(args
[0]));
542 Heavy
.help(context
, replyTo
, toNonNull(Command
.RING
));
546 Heavy
.ring(context
, replyTo
);
549 Heavy
.ring(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
550 } catch(IllegalArgumentException e
){
551 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
558 Heavy
.help(context
, replyTo
, toNonNull(Command
.SPEAK
));
561 Heavy
.speak(context
, replyTo
, join(" ",args
));
566 Heavy
.help(context
, replyTo
, toNonNull(Command
.VIBRATE
));
571 ms
=Long
.parseLong(args
[0]);
572 } catch(NumberFormatException e
){
573 sendMessage(context
, replyTo
, could_not_parse_ms
);
576 Heavy
.vibrate(context
, replyTo
, ms
);
579 case DIALOG
://TODO: Should add an edittext
581 Heavy
.help(context
, replyTo
, toNonNull(Command
.DIALOG
));
584 final String
[] buttons
=new String
[args
.length
-1];
585 System
.arraycopy(args
,1,buttons
,0,buttons
.length
);
586 Heavy
.dialog(context
, replyTo
, toNonNull(args
[0]), buttons
);
590 if(args
.length
>3||args
.length
<1){
591 Heavy
.help(context
, replyTo
, toNonNull(Command
.LOCATION
));
595 final @NonNull String provider
;
597 switch(LocationProvider
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))){
599 provider
=toNonNull(LocationManager
.GPS_PROVIDER
);
603 provider
=toNonNull(LocationManager
.NETWORK_PROVIDER
);
606 } catch(IllegalArgumentException e
){
607 sendMessage(context
, replyTo
, cannot_parse_provider_allowed_values_are
, join(", ",toNonNull(LocationProvider
.values())));
612 final float minDistance
;
616 minTime
=Long
.parseLong(args
[1]);
617 } catch (NumberFormatException e
){
618 sendMessage(context
, replyTo
, cannot_parse_min_time
);
626 minDistance
=Float
.parseFloat(args
[2]);
627 } catch (NumberFormatException e
){
628 sendMessage(context
, replyTo
, cannot_parse_min_distance
);
633 Heavy
.location(context
, replyTo
, provider
, minTime
, minDistance
);
637 Heavy
.nolocation(context
, replyTo
);
642 Heavy
.help(context
, replyTo
, toNonNull(Command
.RINGER
));
646 Heavy
.ringer(context
, replyTo
);
649 final RingerMode rm
=RingerMode
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
));
652 Heavy
.ringer(context
, replyTo
, AudioManager
.RINGER_MODE_NORMAL
);
655 Heavy
.ringer(context
, replyTo
, AudioManager
.RINGER_MODE_VIBRATE
);
658 Heavy
.ringer(context
, replyTo
, AudioManager
.RINGER_MODE_SILENT
);
661 } catch (IllegalArgumentException e
){
662 Utils
.sendMessage(context
, replyTo
, invalid_ringer_mode_valid_values_are
, join(", ",toNonNull(RingerMode
.values())));
669 Heavy
.help(context
, replyTo
, toNonNull(Command
.NCFILE
));
673 final int ncfilePort
;
675 ncfilePort
=Integer
.parseInt(args
[2]);
676 } catch (NumberFormatException e
){
677 sendMessage(context
, replyTo
, cannot_parse_port
);
680 Heavy
.ncfile(context
, replyTo
, toNonNull(args
[0]), toNonNull(args
[1]), ncfilePort
);
685 Heavy
.help(context
, replyTo
, toNonNull(Command
.PHOTO
));
690 photoPort
=Integer
.parseInt(args
[1]);
691 } catch (NumberFormatException e
){
692 sendMessage(context
, replyTo
, cannot_parse_port
);
695 Heavy
.photo(context
, replyTo
, toNonNull(args
[0]), photoPort
);
698 case SETNOTIFICATION
:
700 Heavy
.help(context
, replyTo
, toNonNull(Command
.SETNOTIFICATION
));
705 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
706 .putString(MessageType
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)).toString(), replyTo
.toString())
708 sendMessage(context
, replyTo
, notification_enabled
);
709 } catch (IllegalArgumentException e
){
710 sendMessage(context
, replyTo
, messagetype_should_be_one_of
, join(", ",toNonNull(MessageType
.values())));
716 case DELNOTIFICATION
:
718 Heavy
.help(context
, replyTo
, toNonNull(Command
.DELNOTIFICATION
));
723 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
724 .remove(MessageType
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)).toString())
726 sendMessage(context
, replyTo
, notification_disabled
);
727 } catch (IllegalArgumentException e
){
728 sendMessage(context
, replyTo
, messagetype_should_be_one_of
, join(", ",toNonNull(MessageType
.values())));
736 Heavy
.help(context
, replyTo
, toNonNull(Command
.SETPASSWORD
));
742 Heavy
.setPassword(context
, replyTo
);
744 Heavy
.setPassword(context
, replyTo
, toNonNull(args
[0]));
745 } catch (SecurityException e
){
746 sendMessage(context
, replyTo
, security_exception
+e
.getMessage());
752 Heavy
.help(context
, replyTo
, toNonNull(Command
.WIPE
));
756 if(!args
[1].equalsIgnoreCase(WIPE_CONFIRM_STRING
)){
757 sendMessage(context
, replyTo
, the_second_argument_to_wipe_must_be
, WIPE_CONFIRM_STRING
);
762 Heavy
.wipe(context
, toNonNull(WipeType
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
763 } catch (IllegalArgumentException e
){
764 sendMessage(context
, replyTo
, wipetype_should_be_one_of
, join (", ",toNonNull(WipeType
.values())));
765 } catch (SecurityException e
){
766 sendMessage(context
, replyTo
, security_exception
, e
.getMessage());
772 Heavy
.lock(context
, replyTo
);
773 } catch (SecurityException e
){
774 sendMessage(context
, replyTo
, security_exception
, e
.getMessage());
780 Heavy
.help(context
, replyTo
, toNonNull(Command
.VIEW
));
784 Heavy
.view(context
, replyTo
, toNonNull(Uri
.parse(args
[0])));
788 Heavy
.musicPlayerCommand(context
, replyTo
, "play");
792 Heavy
.musicPlayerCommand(context
, replyTo
, "pause");
796 Heavy
.musicPlayerCommand(context
, replyTo
, "next");
800 Heavy
.musicPlayerCommand(context
, replyTo
, "previous");
804 Heavy
.batt(context
, replyTo
);
808 if (args
.length
> 1) {
809 Heavy
.help(context
, replyTo
, toNonNull(Command
.CALLLOG
));
813 if (args
.length
== 0)
814 Heavy
.calllog(context
, replyTo
, 5);
817 Heavy
.calllog(context
, replyTo
, Integer
.parseInt(args
[0]));
818 } catch (IllegalArgumentException e
){
819 sendMessage(context
, replyTo
, cannot_parse_count
);
825 if (args
.length
> 1) {
826 Heavy
.help(context
, replyTo
, toNonNull(Command
.SMSLOG
));
830 if (args
.length
== 0)
831 Heavy
.smslog(context
, replyTo
, 5);
834 Heavy
.smslog(context
, replyTo
, Integer
.parseInt(args
[0]));
835 } catch (IllegalArgumentException e
) {
836 sendMessage(context
, replyTo
, cannot_parse_count
);
842 if(args
.length
!= 1){
843 Heavy
.help(context
, replyTo
, toNonNull(Command
.HELP
));
848 Heavy
.help(context
, replyTo
, toNonNull(Command
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
849 } catch (IllegalArgumentException e
) {
850 sendMessage(context
, replyTo
, no_such_command_command_list
, join(", ", toNonNull(Command
.values())));
855 if(args
.length
!= 1){
856 Heavy
.help(context
, replyTo
, toNonNull(Command
.LS
));
860 Heavy
.ls(context
, replyTo
, toNonNull(args
[0]));
864 if(args
.length
!= 1){
865 Heavy
.help(context
, replyTo
, toNonNull(Command
.RM
));
869 Heavy
.rm(context
, replyTo
, toNonNull(args
[0]));
873 if(args
.length
!= 1){
874 Heavy
.help(context
, replyTo
, toNonNull(Command
.CONTACTS
));
878 Heavy
.contacts(context
, replyTo
, toNonNull(args
[0]));
882 if(replyTo
.protocol
!= Protocol
.LOCAL
|| args
.length
!= 1){
883 Heavy
.help(context
, replyTo
, toNonNull(Command
.DISABLE
));
888 Heavy
.disable(context
, replyTo
, toNonNull(Command
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
889 } catch (IllegalArgumentException e
){
890 sendMessage(context
, replyTo
, no_such_command_command_list
, join(", ", toNonNull(Command
.values())));
895 if(replyTo
.protocol
!= Protocol
.LOCAL
|| args
.length
!= 1){
896 Heavy
.help(context
, replyTo
, toNonNull(Command
.ENABLE
));
901 Heavy
.enable(context
, replyTo
, toNonNull(Command
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
902 } catch (IllegalArgumentException e
){
903 sendMessage(context
, replyTo
, no_such_command_command_list
, join(", ", toNonNull(Command
.values())));
909 Heavy
.help(context
, replyTo
, toNonNull(Command
.POLL
));
914 Heavy
.poll(context
, replyTo
);
920 interval
=Long
.parseLong(args
[0]);
921 } catch(NumberFormatException e
){
922 sendMessage(context
, replyTo
, cannot_parse_interval
);
926 Heavy
.poll(context
, replyTo
, interval
);
930 Heavy
.hangup(context
, replyTo
);
934 Heavy
.answer(context
, replyTo
);
939 Heavy
.help(context
, replyTo
, toNonNull(Command
.LAUNCH
));
942 Heavy
.launch(context
, replyTo
, toNonNull(args
[0]));
947 Heavy
.help(context
, replyTo
, toNonNull(Command
.DATA
));
952 Heavy
.data(context
, replyTo
);
956 Heavy
.data(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
957 } catch(IllegalArgumentException e
) {
958 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
964 Heavy
.help(context
, replyTo
, toNonNull(Command
.GPS
));
969 Heavy
.gps(context
, replyTo
);
974 Heavy
.gps(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
975 } catch(IllegalArgumentException e
) {
976 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
982 Heavy
.help(context
, replyTo
, toNonNull(Command
.GLOCATION
));
987 Heavy
.glocation(context
, replyTo
);
992 Heavy
.glocation(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
993 } catch(IllegalArgumentException e
) {
994 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
1000 Heavy
.help(context
, replyTo
, toNonNull(Command
.REBOOT
));
1004 Heavy
.reboot(context
, replyTo
, args
.length
==0?
null:args
[0]);
1008 if(args
.length
!=1 && args
.length
!=3){
1009 Heavy
.help(context
, replyTo
, toNonNull(Command
.NOTIFY
));
1015 id
=Integer
.parseInt(args
[0]);
1016 } catch (NumberFormatException e
){
1017 sendMessage(context
, replyTo
, R
.string
.could_not_parse_id
);
1022 Heavy
.notify(context
, replyTo
, id
);
1024 Heavy
.notify(context
, replyTo
, id
, toNonNull(args
[1]), toNonNull(args
[2]));
1028 if(args
.length
!= 1){
1029 Heavy
.help(context
, replyTo
, toNonNull(Command
.SCREENCAP
));
1033 Heavy
.screencap(context
, replyTo
, args
[0]);
1037 Heavy
.torch(context
, replyTo
);
1044 * Executes a given command
1046 * @param context Context instance
1047 * @param cmd Command to execute
1048 * @param args arguments for the command
1049 * @param replyTo Address to send replies to
1051 public static void processCommand(final Context context
, final String cmd
,final String
[] args
,final Address replyTo
){
1052 final @NonNull Command command
;
1054 command
=toNonNull(Command
.valueOf(cmd
.toUpperCase(Locale
.ENGLISH
)));
1055 } catch (IllegalArgumentException e
){
1056 sendMessage(context
, replyTo
, unknown_command
, cmd
.toUpperCase(Locale
.ENGLISH
), e
.getMessage());
1061 processCommand(context
, command
,args
,replyTo
);
1062 } catch(Exception e
){
1063 sendMessage(context
, replyTo
, error_while_processing_command
, e
.getClass().getName(), e
.getMessage());
1064 Log
.w(Utils
.class.getName(), "Error while processing command", e
);