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 */
114 /** Phone booted notifications */
119 * 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).
121 * @author Marius Gavrilescu <marius@ieval.ro>
123 public static enum WipeType
{
124 /** Factory reset the phone, without touching the SD card */
126 /** Factory reset the phone and wipe the SD card too */
131 * Enum of ringer modes.
133 * @author Marius Gavrilescu <marius@ieval.ro>
135 public static enum RingerMode
{
138 /** Sound is off, vibrate is on */
140 /** Sound is off, vibrate is off */
145 * Enum of location providers
147 * @author Marius Gavrilescu <marius@ieval.ro>
149 public static enum LocationProvider
{
150 /** The network location provider */
152 /** The GPS location provider */
157 * Enum of toast lengths.
159 * @author Marius Gavrilescu <marius@ieval.ro>
161 private static enum ToastLength
{
169 * Enum of the values on and off. Used for boolean arguments.
171 * @author Marius Gavrilescu <marius@ieval.ro>
173 @SuppressWarnings("javadoc")
174 private static enum OnOff
{
179 * Enum of ongoing event types
181 * @author Marius Gavrilescu <marius@ieval.ro>
183 public static enum OngoingEvent
{
184 /** Location tracking is active. Registered by {@link Command#LOCATION}, unregistered by {@link Command#NOLOCATION} */
185 LOCATION(location_tracking_is_active
),
186 /** The phone is ringing. Registered/unregistered by {@link Command#RING} */
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("http", hostname
, port
, path
);
415 * Executes a given command
417 * @param context Context instance
418 * @param cmd Command to execute
419 * @param args arguments for the command
420 * @param replyTo Address to send replies to
422 private static void processCommand(final Context context
, final Command cmd
,final String
[] args
,final Address replyTo
){
423 if(Heavy
.isCommandDisabled(context
, cmd
)){
424 sendMessage(context
, replyTo
, command_disabled
, cmd
.toString());
430 if(args
.length
< 1 || args
.length
> 2){
431 Heavy
.help(context
, replyTo
, toNonNull(Command
.TOAST
));
436 Heavy
.toast(context
, replyTo
, toNonNull(args
[0]));
439 switch(ToastLength
.valueOf(args
[1].toUpperCase(Locale
.ENGLISH
))){
441 Heavy
.toast(context
, replyTo
, toNonNull(args
[0]), Toast
.LENGTH_LONG
);
444 Heavy
.toast(context
, replyTo
, toNonNull(args
[0]), Toast
.LENGTH_SHORT
);
446 } catch(IllegalArgumentException e
){
447 sendMessage(context
, replyTo
, invalid_length_allowed_values_are
, join(", ",toNonNull(ToastLength
.values())));
454 Heavy
.help(context
, replyTo
, toNonNull(Command
.ECHO
));
457 sendMessage(context
, replyTo
, join(" ",args
));
462 Heavy
.help(context
, replyTo
, toNonNull(Command
.SMS
));
465 Heavy
.sms(context
, replyTo
, toNonNull(args
[0]), join(" ", 1, args
));
469 if(args
.length
!= 1){
470 Heavy
.help(context
, replyTo
, toNonNull(Command
.FLASH
));
475 Heavy
.flash(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())));
483 Heavy
.help(context
, replyTo
, toNonNull(Command
.WIFI
));
487 Heavy
.wifi(context
, replyTo
);
490 Heavy
.wifi(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
491 } catch(IllegalArgumentException e
) {
492 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
499 Heavy
.help(context
, replyTo
, toNonNull(Command
.BLUETOOTH
));
503 Heavy
.bluetooth(context
, replyTo
);
506 Heavy
.bluetooth(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
507 } catch(IllegalArgumentException e
) {
508 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
515 Heavy
.help(context
, replyTo
, toNonNull(Command
.DIAL
));
518 Heavy
.dial(context
, replyTo
, toNonNull(args
[0]));
523 Heavy
.help(context
, replyTo
, toNonNull(Command
.RING
));
527 Heavy
.ring(context
, replyTo
);
530 Heavy
.ring(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
531 } catch(IllegalArgumentException e
){
532 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
539 Heavy
.help(context
, replyTo
, toNonNull(Command
.SPEAK
));
542 Heavy
.speak(context
, replyTo
, join(" ",args
));
547 Heavy
.help(context
, replyTo
, toNonNull(Command
.VIBRATE
));
552 ms
=Long
.parseLong(args
[0]);
553 } catch(NumberFormatException e
){
554 sendMessage(context
, replyTo
, could_not_parse_ms
);
557 Heavy
.vibrate(context
, replyTo
, ms
);
560 case DIALOG
://TODO: Should add an edittext
562 Heavy
.help(context
, replyTo
, toNonNull(Command
.DIALOG
));
565 final String
[] buttons
=new String
[args
.length
-1];
566 System
.arraycopy(args
,1,buttons
,0,buttons
.length
);
567 Heavy
.dialog(context
, replyTo
, toNonNull(args
[0]), buttons
);
571 if(args
.length
>3||args
.length
<1){
572 Heavy
.help(context
, replyTo
, toNonNull(Command
.LOCATION
));
576 final @NonNull String provider
;
578 switch(LocationProvider
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))){
580 provider
=toNonNull(LocationManager
.GPS_PROVIDER
);
584 provider
=toNonNull(LocationManager
.NETWORK_PROVIDER
);
587 } catch(IllegalArgumentException e
){
588 sendMessage(context
, replyTo
, cannot_parse_provider_allowed_values_are
, join(", ",toNonNull(LocationProvider
.values())));
593 final float minDistance
;
597 minTime
=Long
.parseLong(args
[1]);
598 } catch (NumberFormatException e
){
599 sendMessage(context
, replyTo
, cannot_parse_min_time
);
607 minDistance
=Float
.parseFloat(args
[2]);
608 } catch (NumberFormatException e
){
609 sendMessage(context
, replyTo
, cannot_parse_min_distance
);
614 Heavy
.location(context
, replyTo
, provider
, minTime
, minDistance
);
618 Heavy
.nolocation(context
, replyTo
);
623 Heavy
.help(context
, replyTo
, toNonNull(Command
.RINGER
));
627 Heavy
.ringer(context
, replyTo
);
630 final RingerMode rm
=RingerMode
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
));
633 Heavy
.ringer(context
, replyTo
, AudioManager
.RINGER_MODE_NORMAL
);
636 Heavy
.ringer(context
, replyTo
, AudioManager
.RINGER_MODE_VIBRATE
);
639 Heavy
.ringer(context
, replyTo
, AudioManager
.RINGER_MODE_SILENT
);
642 } catch (IllegalArgumentException e
){
643 Utils
.sendMessage(context
, replyTo
, invalid_ringer_mode_valid_values_are
, join(", ",toNonNull(RingerMode
.values())));
650 Heavy
.help(context
, replyTo
, toNonNull(Command
.NCFILE
));
654 final int ncfilePort
;
656 ncfilePort
=Integer
.parseInt(args
[2]);
657 } catch (NumberFormatException e
){
658 sendMessage(context
, replyTo
, cannot_parse_port
);
661 Heavy
.ncfile(context
, replyTo
, toNonNull(args
[0]), toNonNull(args
[1]), ncfilePort
);
666 Heavy
.help(context
, replyTo
, toNonNull(Command
.PHOTO
));
671 photoPort
=Integer
.parseInt(args
[1]);
672 } catch (NumberFormatException e
){
673 sendMessage(context
, replyTo
, cannot_parse_port
);
676 Heavy
.photo(context
, replyTo
, toNonNull(args
[0]), photoPort
);
679 case SETNOTIFICATION
:
681 Heavy
.help(context
, replyTo
, toNonNull(Command
.SETNOTIFICATION
));
686 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
687 .putString(MessageType
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)).toString(), replyTo
.toString())
689 sendMessage(context
, replyTo
, notification_enabled
);
690 } catch (IllegalArgumentException e
){
691 sendMessage(context
, replyTo
, messagetype_should_be_one_of
, join(", ",toNonNull(MessageType
.values())));
697 case DELNOTIFICATION
:
699 Heavy
.help(context
, replyTo
, toNonNull(Command
.DELNOTIFICATION
));
704 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
705 .remove(MessageType
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)).toString())
707 sendMessage(context
, replyTo
, notification_disabled
);
708 } catch (IllegalArgumentException e
){
709 sendMessage(context
, replyTo
, messagetype_should_be_one_of
, join(", ",toNonNull(MessageType
.values())));
717 Heavy
.help(context
, replyTo
, toNonNull(Command
.SETPASSWORD
));
723 Heavy
.setPassword(context
, replyTo
);
725 Heavy
.setPassword(context
, replyTo
, toNonNull(args
[0]));
726 } catch (SecurityException e
){
727 sendMessage(context
, replyTo
, security_exception
+e
.getMessage());
733 Heavy
.help(context
, replyTo
, toNonNull(Command
.WIPE
));
737 if(!args
[1].equalsIgnoreCase(WIPE_CONFIRM_STRING
)){
738 sendMessage(context
, replyTo
, the_second_argument_to_wipe_must_be
, WIPE_CONFIRM_STRING
);
743 Heavy
.wipe(context
, toNonNull(WipeType
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
744 } catch (IllegalArgumentException e
){
745 sendMessage(context
, replyTo
, wipetype_should_be_one_of
, join (", ",toNonNull(WipeType
.values())));
746 } catch (SecurityException e
){
747 sendMessage(context
, replyTo
, security_exception
, e
.getMessage());
753 Heavy
.lock(context
, replyTo
);
754 } catch (SecurityException e
){
755 sendMessage(context
, replyTo
, security_exception
, e
.getMessage());
761 Heavy
.help(context
, replyTo
, toNonNull(Command
.VIEW
));
765 Heavy
.view(context
, replyTo
, toNonNull(Uri
.parse(args
[0])));
769 Heavy
.musicPlayerCommand(context
, replyTo
, "play");
773 Heavy
.musicPlayerCommand(context
, replyTo
, "pause");
777 Heavy
.musicPlayerCommand(context
, replyTo
, "next");
781 Heavy
.musicPlayerCommand(context
, replyTo
, "previous");
785 Heavy
.batt(context
, replyTo
);
789 if (args
.length
> 1) {
790 Heavy
.help(context
, replyTo
, toNonNull(Command
.CALLLOG
));
794 if (args
.length
== 0)
795 Heavy
.calllog(context
, replyTo
, 5);
798 Heavy
.calllog(context
, replyTo
, Integer
.parseInt(args
[0]));
799 } catch (IllegalArgumentException e
){
800 sendMessage(context
, replyTo
, cannot_parse_count
);
806 if (args
.length
> 1) {
807 Heavy
.help(context
, replyTo
, toNonNull(Command
.SMSLOG
));
811 if (args
.length
== 0)
812 Heavy
.smslog(context
, replyTo
, 5);
815 Heavy
.smslog(context
, replyTo
, Integer
.parseInt(args
[0]));
816 } catch (IllegalArgumentException e
) {
817 sendMessage(context
, replyTo
, cannot_parse_count
);
823 if(args
.length
!= 1){
824 Heavy
.help(context
, replyTo
, toNonNull(Command
.HELP
));
829 Heavy
.help(context
, replyTo
, toNonNull(Command
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
830 } catch (IllegalArgumentException e
) {
831 sendMessage(context
, replyTo
, no_such_command_command_list
, join(", ", toNonNull(Command
.values())));
836 if(args
.length
!= 1){
837 Heavy
.help(context
, replyTo
, toNonNull(Command
.LS
));
841 Heavy
.ls(context
, replyTo
, toNonNull(args
[0]));
845 if(args
.length
!= 1){
846 Heavy
.help(context
, replyTo
, toNonNull(Command
.RM
));
850 Heavy
.rm(context
, replyTo
, toNonNull(args
[0]));
854 if(args
.length
!= 1){
855 Heavy
.help(context
, replyTo
, toNonNull(Command
.CONTACTS
));
859 Heavy
.contacts(context
, replyTo
, toNonNull(args
[0]));
863 if(replyTo
.protocol
!= Protocol
.LOCAL
|| args
.length
!= 1){
864 Heavy
.help(context
, replyTo
, toNonNull(Command
.DISABLE
));
869 Heavy
.disable(context
, replyTo
, toNonNull(Command
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
870 } catch (IllegalArgumentException e
){
871 sendMessage(context
, replyTo
, no_such_command_command_list
, join(", ", toNonNull(Command
.values())));
876 if(replyTo
.protocol
!= Protocol
.LOCAL
|| args
.length
!= 1){
877 Heavy
.help(context
, replyTo
, toNonNull(Command
.ENABLE
));
882 Heavy
.enable(context
, replyTo
, toNonNull(Command
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
883 } catch (IllegalArgumentException e
){
884 sendMessage(context
, replyTo
, no_such_command_command_list
, join(", ", toNonNull(Command
.values())));
889 Heavy
.poll(context
, replyTo
);
893 Heavy
.hangup(context
, replyTo
);
897 Heavy
.answer(context
, replyTo
);
902 Heavy
.help(context
, replyTo
, toNonNull(Command
.LAUNCH
));
905 Heavy
.launch(context
, replyTo
, toNonNull(args
[0]));
910 Heavy
.help(context
, replyTo
, toNonNull(Command
.DATA
));
915 Heavy
.data(context
, replyTo
);
919 Heavy
.data(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
920 } catch(IllegalArgumentException e
) {
921 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
927 Heavy
.help(context
, replyTo
, toNonNull(Command
.GPS
));
932 Heavy
.gps(context
, replyTo
);
937 Heavy
.gps(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
938 } catch(IllegalArgumentException e
) {
939 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
945 Heavy
.help(context
, replyTo
, toNonNull(Command
.GLOCATION
));
950 Heavy
.glocation(context
, replyTo
);
955 Heavy
.glocation(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
956 } catch(IllegalArgumentException e
) {
957 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
963 Heavy
.help(context
, replyTo
, toNonNull(Command
.REBOOT
));
967 Heavy
.reboot(context
, replyTo
, args
.length
==0?
null:args
[0]);
971 if(args
.length
!=1 && args
.length
!=3){
972 Heavy
.help(context
, replyTo
, toNonNull(Command
.NOTIFY
));
978 id
=Integer
.parseInt(args
[0]);
979 } catch (NumberFormatException e
){
980 sendMessage(context
, replyTo
, R
.string
.could_not_parse_id
);
985 Heavy
.notify(context
, replyTo
, id
);
987 Heavy
.notify(context
, replyTo
, id
, toNonNull(args
[1]), toNonNull(args
[2]));
991 if(args
.length
!= 1){
992 Heavy
.help(context
, replyTo
, toNonNull(Command
.SCREENCAP
));
996 Heavy
.screencap(context
, replyTo
, args
[0]);
1000 Heavy
.torch(context
, replyTo
);
1007 * Executes a given command
1009 * @param context Context instance
1010 * @param cmd Command to execute
1011 * @param args arguments for the command
1012 * @param replyTo Address to send replies to
1014 public static void processCommand(final Context context
, final String cmd
,final String
[] args
,final Address replyTo
){
1015 final @NonNull Command command
;
1017 command
=toNonNull(Command
.valueOf(cmd
.toUpperCase(Locale
.ENGLISH
)));
1018 } catch (IllegalArgumentException e
){
1019 sendMessage(context
, replyTo
, unknown_command
, cmd
.toUpperCase(Locale
.ENGLISH
), e
.getMessage());
1024 processCommand(context
, command
,args
,replyTo
);
1025 } catch(Exception e
){
1026 sendMessage(context
, replyTo
, error_while_processing_command
, e
.getClass().getName(), e
.getMessage());
1027 Log
.w(Utils
.class.getName(), "Error while processing command", e
);