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 throw new NullPointerException();
199 * Join an array of Objects with elements separated by a separator into a single string.
201 * @param separator the separator
202 * @param offset the offset into the array
203 * @param args the array of Objects to join
204 * @return the joined string
206 public static String
join(final String separator
,final int offset
,final Object
[] args
){
207 final StringBuilder sb
=new StringBuilder(240);
208 sb
.append(args
[offset
]);
209 for (int i
= offset
+1; i
< args
.length
; i
++) {
210 sb
.append(separator
);
213 return toNonNull(sb
.toString());
217 * Join an array of Objects with elements separated by a separator into a single string.
219 * @param separator the separator
220 * @param args the array of objects to join
221 * @return the joined string
223 public static String
join(final String separator
, final Object
[] args
){
224 return join(separator
,0,args
);
228 * Send a notification to the user.
230 * @param context Context instance
231 * @param type notification type
232 * @param resource String resource for the message text
234 public static void sendMessage(final Context context
, final MessageType type
, final int resource
){
235 sendMessage(context
, type
, toNonNull(context
.getString(resource
)));
239 * Send a notification to the user.
241 * @param context Context instance
242 * @param type notification type
243 * @param resource String resource for the message text
244 * @param args format parameters for the resource
246 public static void sendMessage(final Context context
, final MessageType type
, final int resource
, final Object
... args
){
247 sendMessage(context
, type
, toNonNull(context
.getString(resource
, args
)));
251 * Send a message to a certain Address.
253 * @param context Context instance
254 * @param address destination Address
255 * @param resource String resource for the message text
257 public static void sendMessage(final Context context
, final Address address
, final int resource
){
258 sendMessage(context
, address
, toNonNull(context
.getString(resource
)));
262 * Send a message to a certain Address.
264 * @param context Context instance
265 * @param address destination Address
266 * @param resource String resource for the message text
267 * @param args format parameters for the resource
269 public static void sendMessage(final Context context
, final Address address
, final int resource
, final Object
... args
){
270 sendMessage(context
, address
, toNonNull(context
.getString(resource
, args
)));
274 * Send a notification to the user.
276 * @param context Context instance
277 * @param type notification type
278 * @param msg the notification text
280 public static void sendMessage(final Context context
, final MessageType type
,final String msg
){
281 final SharedPreferences sp
=PreferenceManager
.getDefaultSharedPreferences(context
);
282 final String address
=sp
.getString(type
.toString(),null);
285 sendMessage(context
, new Address(address
), msg
);
289 * Send a message to a certain Address.
291 * @param context Context instance
292 * @param address destination Address
293 * @param message the message text
295 public static void sendMessage(final Context context
, final Address address
, final String message
){
296 switch(address
.protocol
){
298 new HttpCallExecutableRunnable("/send", toNonNull(Arrays
.asList(new Header("X-Destination", toNonNull(address
.data
)))), context
, null, true, address
.requestId
== null ? message
: address
.requestId
+ " " + message
).execute();
302 new Handler(Looper
.getMainLooper()).post(new Runnable(){
305 SmsManager
.getDefault().sendTextMessage(address
.data
, null, message
, null, null);
311 final Intent intent
= new Intent(FonBotLocalActivity
.RESPONSE_RECEIVED_ACTION
);
312 intent
.putExtra(FonBotLocalActivity
.EXTRA_RESPONSE
, message
);
313 context
.sendBroadcast(intent
);
322 * Splits a string into words.
324 * @param string the string
325 * @return an array of words
327 public static String
[] shellwords(final String string
){
328 return toNonNull(string
.split("[ ]+(?=([^\"]*\"[^\"]*\")*[^\"]*$)"));//TODO: Make this handle backslash escapes
332 * Returns the name associated with a phone number.
334 * @param context Context instance
335 * @param number phone number to search for
336 * @return the name associated with this number, or null if the number was not found in the contacts.
338 public static @Nullable String
callerId(final Context context
, final String number
){
339 final Cursor cursor
=context
.getContentResolver().query(
340 Uri
.withAppendedPath(PhoneLookup
.CONTENT_FILTER_URI
, Uri
.encode(number
)),
341 new String
[]{PhoneLookup
.DISPLAY_NAME
},
346 if(cursor
.moveToFirst()){
347 final String name
=cursor
.getString(0);
356 * Registers an ongoing event.
358 * @param context Context instance
359 * @param event event to register
361 public static void registerOngoing(final Context context
, final OngoingEvent event
){
362 final Intent intent
=new Intent(context
, FonBotMainService
.class);
363 intent
.setAction(FonBotMainService
.ACTION_PUT_ONGOING
);
364 intent
.putExtra(FonBotMainService
.EXTRA_ONGOING_ID
, event
.ordinal());
365 context
.startService(intent
);
369 * Unregisters an ongoing event
371 * @param context Context instance
372 * @param event event to unregister
374 public static void unregisterOngoing(final Context context
, final OngoingEvent event
){
375 final Intent intent
=new Intent(context
, FonBotMainService
.class);
376 intent
.setAction(FonBotMainService
.ACTION_DELETE_ONGOING
);
377 intent
.putExtra(FonBotMainService
.EXTRA_ONGOING_ID
, event
.ordinal());
378 context
.startService(intent
);
382 * Gets the server URL according to the user preferences.
384 * @param context Context instance
385 * @param path URL path
386 * @return the server URL
387 * @throws MalformedURLException if the user preferences create an invalid URL
389 public static URL
getServerURL(final Context context
, final String path
) throws MalformedURLException
{
390 final String protocol
=PreferenceManager
.getDefaultSharedPreferences(context
).getString("protocol", "https");
391 final String hostname
=PreferenceManager
.getDefaultSharedPreferences(context
).getString("hostname", "fonbot.ieval.ro");
392 final int port
=Integer
.parseInt(PreferenceManager
.getDefaultSharedPreferences(context
).getString("port", "443"));
393 final URL url
=new URL(protocol
, hostname
, port
, path
);
398 * Executes a given command
400 * @param context Context instance
401 * @param cmd Command to execute
402 * @param args arguments for the command
403 * @param replyTo Address to send replies to
405 private static void processCommand(final Context context
, final Command cmd
,final String
[] args
,final Address replyTo
){
406 if(Heavy
.isCommandDisabled(context
, cmd
)){
407 sendMessage(context
, replyTo
, command_disabled
, cmd
.toString());
413 if(args
.length
< 1 || args
.length
> 2){
414 Heavy
.help(context
, replyTo
, toNonNull(Command
.TOAST
));
419 Heavy
.toast(context
, replyTo
, toNonNull(args
[0]));
422 switch(ToastLength
.valueOf(args
[1].toUpperCase(Locale
.ENGLISH
))){
424 Heavy
.toast(context
, replyTo
, toNonNull(args
[0]), Toast
.LENGTH_LONG
);
427 Heavy
.toast(context
, replyTo
, toNonNull(args
[0]), Toast
.LENGTH_SHORT
);
429 } catch(IllegalArgumentException e
){
430 sendMessage(context
, replyTo
, invalid_length_allowed_values_are
, join(", ",toNonNull(ToastLength
.values())));
437 Heavy
.help(context
, replyTo
, toNonNull(Command
.ECHO
));
440 sendMessage(context
, replyTo
, join(" ",args
));
445 Heavy
.help(context
, replyTo
, toNonNull(Command
.SMS
));
448 Heavy
.sms(context
, replyTo
, toNonNull(args
[0]), join(" ", 1, args
));
452 if(args
.length
!= 1){
453 Heavy
.help(context
, replyTo
, toNonNull(Command
.FLASH
));
458 Heavy
.flash(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
459 } catch(IllegalArgumentException e
) {
460 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
466 Heavy
.help(context
, replyTo
, toNonNull(Command
.WIFI
));
470 Heavy
.wifi(context
, replyTo
);
473 Heavy
.wifi(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
474 } catch(IllegalArgumentException e
) {
475 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
482 Heavy
.help(context
, replyTo
, toNonNull(Command
.BLUETOOTH
));
486 Heavy
.bluetooth(context
, replyTo
);
489 Heavy
.bluetooth(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
490 } catch(IllegalArgumentException e
) {
491 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
498 Heavy
.help(context
, replyTo
, toNonNull(Command
.DIAL
));
501 Heavy
.dial(context
, replyTo
, toNonNull(args
[0]));
506 Heavy
.help(context
, replyTo
, toNonNull(Command
.RING
));
510 Heavy
.ring(context
, replyTo
);
513 Heavy
.ring(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
514 } catch(IllegalArgumentException e
){
515 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
522 Heavy
.help(context
, replyTo
, toNonNull(Command
.SPEAK
));
525 Heavy
.speak(context
, replyTo
, join(" ",args
));
530 Heavy
.help(context
, replyTo
, toNonNull(Command
.VIBRATE
));
535 ms
=Long
.parseLong(args
[0]);
536 } catch(NumberFormatException e
){
537 sendMessage(context
, replyTo
, could_not_parse_ms
);
540 Heavy
.vibrate(context
, replyTo
, ms
);
543 case DIALOG
://TODO: Should add an edittext
545 Heavy
.help(context
, replyTo
, toNonNull(Command
.DIALOG
));
548 final String
[] buttons
=new String
[args
.length
-1];
549 System
.arraycopy(args
,1,buttons
,0,buttons
.length
);
550 Heavy
.dialog(context
, replyTo
, toNonNull(args
[0]), buttons
);
554 if(args
.length
>3||args
.length
<1){
555 Heavy
.help(context
, replyTo
, toNonNull(Command
.LOCATION
));
559 final @NonNull String provider
;
561 switch(LocationProvider
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))){
563 provider
=toNonNull(LocationManager
.GPS_PROVIDER
);
567 provider
=toNonNull(LocationManager
.NETWORK_PROVIDER
);
570 } catch(IllegalArgumentException e
){
571 sendMessage(context
, replyTo
, cannot_parse_provider_allowed_values_are
, join(", ",toNonNull(LocationProvider
.values())));
576 final float minDistance
;
580 minTime
=Long
.parseLong(args
[1]);
581 } catch (NumberFormatException e
){
582 sendMessage(context
, replyTo
, cannot_parse_min_time
);
590 minDistance
=Float
.parseFloat(args
[2]);
591 } catch (NumberFormatException e
){
592 sendMessage(context
, replyTo
, cannot_parse_min_distance
);
597 Heavy
.location(context
, replyTo
, provider
, minTime
, minDistance
);
601 Heavy
.nolocation(context
, replyTo
);
606 Heavy
.help(context
, replyTo
, toNonNull(Command
.RINGER
));
610 Heavy
.ringer(context
, replyTo
);
613 final RingerMode rm
=RingerMode
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
));
616 Heavy
.ringer(context
, replyTo
, AudioManager
.RINGER_MODE_NORMAL
);
619 Heavy
.ringer(context
, replyTo
, AudioManager
.RINGER_MODE_VIBRATE
);
622 Heavy
.ringer(context
, replyTo
, AudioManager
.RINGER_MODE_SILENT
);
625 } catch (IllegalArgumentException e
){
626 Utils
.sendMessage(context
, replyTo
, invalid_ringer_mode_valid_values_are
, join(", ",toNonNull(RingerMode
.values())));
633 Heavy
.help(context
, replyTo
, toNonNull(Command
.NCFILE
));
637 final int ncfilePort
;
639 ncfilePort
=Integer
.parseInt(args
[2]);
640 } catch (NumberFormatException e
){
641 sendMessage(context
, replyTo
, cannot_parse_port
);
644 Heavy
.ncfile(context
, replyTo
, toNonNull(args
[0]), toNonNull(args
[1]), ncfilePort
);
649 Heavy
.help(context
, replyTo
, toNonNull(Command
.PHOTO
));
653 final int cameraNumber
;
655 cameraNumber
=Integer
.parseInt(args
[0]);
656 } catch (NumberFormatException e
){
657 sendMessage(context
, replyTo
, cannot_parse_camera_number
);
663 photoPort
=Integer
.parseInt(args
[2]);
664 } catch (NumberFormatException e
){
665 sendMessage(context
, replyTo
, cannot_parse_port
);
668 Heavy
.photo(context
, replyTo
, cameraNumber
, toNonNull(args
[1]), photoPort
);
671 case SETNOTIFICATION
:
673 Heavy
.help(context
, replyTo
, toNonNull(Command
.SETNOTIFICATION
));
678 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
679 .putString(MessageType
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)).toString(), replyTo
.toString())
681 sendMessage(context
, replyTo
, notification_enabled
);
682 } catch (IllegalArgumentException e
){
683 sendMessage(context
, replyTo
, messagetype_should_be_one_of
, join(", ",toNonNull(MessageType
.values())));
689 case DELNOTIFICATION
:
691 Heavy
.help(context
, replyTo
, toNonNull(Command
.DELNOTIFICATION
));
696 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
697 .remove(MessageType
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)).toString())
699 sendMessage(context
, replyTo
, notification_disabled
);
700 } catch (IllegalArgumentException e
){
701 sendMessage(context
, replyTo
, messagetype_should_be_one_of
, join(", ",toNonNull(MessageType
.values())));
709 Heavy
.help(context
, replyTo
, toNonNull(Command
.SETPASSWORD
));
715 Heavy
.setPassword(context
, replyTo
);
717 Heavy
.setPassword(context
, replyTo
, toNonNull(args
[0]));
718 } catch (SecurityException e
){
719 sendMessage(context
, replyTo
, security_exception
+e
.getMessage());
725 Heavy
.help(context
, replyTo
, toNonNull(Command
.WIPE
));
729 if(!args
[1].equalsIgnoreCase(WIPE_CONFIRM_STRING
)){
730 sendMessage(context
, replyTo
, the_second_argument_to_wipe_must_be
, WIPE_CONFIRM_STRING
);
735 Heavy
.wipe(context
, toNonNull(WipeType
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
736 } catch (IllegalArgumentException e
){
737 sendMessage(context
, replyTo
, wipetype_should_be_one_of
, join (", ",toNonNull(WipeType
.values())));
738 } catch (SecurityException e
){
739 sendMessage(context
, replyTo
, security_exception
, e
.getMessage());
745 Heavy
.lock(context
, replyTo
);
746 } catch (SecurityException e
){
747 sendMessage(context
, replyTo
, security_exception
, e
.getMessage());
753 Heavy
.help(context
, replyTo
, toNonNull(Command
.VIEW
));
757 Heavy
.view(context
, replyTo
, toNonNull(Uri
.parse(args
[0])));
761 Heavy
.musicPlayerCommand(context
, replyTo
, "play");
765 Heavy
.musicPlayerCommand(context
, replyTo
, "pause");
769 Heavy
.musicPlayerCommand(context
, replyTo
, "next");
773 Heavy
.musicPlayerCommand(context
, replyTo
, "previous");
777 Heavy
.batt(context
, replyTo
);
781 if (args
.length
> 1) {
782 Heavy
.help(context
, replyTo
, toNonNull(Command
.CALLLOG
));
786 if (args
.length
== 0)
787 Heavy
.calllog(context
, replyTo
, 5);
790 Heavy
.calllog(context
, replyTo
, Integer
.parseInt(args
[0]));
791 } catch (IllegalArgumentException e
){
792 sendMessage(context
, replyTo
, cannot_parse_count
);
798 if (args
.length
> 1) {
799 Heavy
.help(context
, replyTo
, toNonNull(Command
.SMSLOG
));
803 if (args
.length
== 0)
804 Heavy
.smslog(context
, replyTo
, 5);
807 Heavy
.smslog(context
, replyTo
, Integer
.parseInt(args
[0]));
808 } catch (IllegalArgumentException e
) {
809 sendMessage(context
, replyTo
, cannot_parse_count
);
815 if(args
.length
!= 1){
816 Heavy
.help(context
, replyTo
, toNonNull(Command
.HELP
));
821 Heavy
.help(context
, replyTo
, toNonNull(Command
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
822 } catch (IllegalArgumentException e
) {
823 sendMessage(context
, replyTo
, no_such_command_command_list
, join(", ", toNonNull(Command
.values())));
828 if(args
.length
!= 1){
829 Heavy
.help(context
, replyTo
, toNonNull(Command
.LS
));
833 Heavy
.ls(context
, replyTo
, toNonNull(args
[0]));
837 if(args
.length
!= 1){
838 Heavy
.help(context
, replyTo
, toNonNull(Command
.RM
));
842 Heavy
.rm(context
, replyTo
, toNonNull(args
[0]));
846 if(args
.length
!= 1){
847 Heavy
.help(context
, replyTo
, toNonNull(Command
.CONTACTS
));
851 Heavy
.contacts(context
, replyTo
, toNonNull(args
[0]));
855 if(replyTo
.protocol
!= Protocol
.LOCAL
|| args
.length
!= 1){
856 Heavy
.help(context
, replyTo
, toNonNull(Command
.DISABLE
));
861 Heavy
.disable(context
, replyTo
, toNonNull(Command
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
862 } catch (IllegalArgumentException e
){
863 sendMessage(context
, replyTo
, no_such_command_command_list
, join(", ", toNonNull(Command
.values())));
868 if(replyTo
.protocol
!= Protocol
.LOCAL
|| args
.length
!= 1){
869 Heavy
.help(context
, replyTo
, toNonNull(Command
.ENABLE
));
874 Heavy
.enable(context
, replyTo
, toNonNull(Command
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
875 } catch (IllegalArgumentException e
){
876 sendMessage(context
, replyTo
, no_such_command_command_list
, join(", ", toNonNull(Command
.values())));
881 Heavy
.poll(context
, replyTo
);
885 Heavy
.hangup(context
, replyTo
);
889 Heavy
.answer(context
, replyTo
);
894 Heavy
.help(context
, replyTo
, toNonNull(Command
.LAUNCH
));
897 Heavy
.launch(context
, replyTo
, toNonNull(args
[0]));
902 Heavy
.help(context
, replyTo
, toNonNull(Command
.DATA
));
907 Heavy
.data(context
, replyTo
);
911 Heavy
.data(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
912 } catch(IllegalArgumentException e
) {
913 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
919 Heavy
.help(context
, replyTo
, toNonNull(Command
.GPS
));
924 Heavy
.gps(context
, replyTo
);
929 Heavy
.gps(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
930 } catch(IllegalArgumentException e
) {
931 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
937 Heavy
.help(context
, replyTo
, toNonNull(Command
.GLOCATION
));
942 Heavy
.glocation(context
, replyTo
);
947 Heavy
.glocation(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
948 } catch(IllegalArgumentException e
) {
949 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
955 Heavy
.help(context
, replyTo
, toNonNull(Command
.REBOOT
));
959 Heavy
.reboot(context
, replyTo
, args
.length
==0?
null:args
[0]);
963 if(args
.length
!=1 && args
.length
!=3){
964 Heavy
.help(context
, replyTo
, toNonNull(Command
.NOTIFY
));
970 id
=Integer
.parseInt(args
[0]);
971 } catch (NumberFormatException e
){
972 sendMessage(context
, replyTo
, R
.string
.could_not_parse_id
);
977 Heavy
.notify(context
, replyTo
, id
);
979 Heavy
.notify(context
, replyTo
, id
, toNonNull(args
[1]), toNonNull(args
[2]));
983 if(args
.length
!= 1){
984 Heavy
.help(context
, replyTo
, toNonNull(Command
.SCREENCAP
));
988 Heavy
.screencap(context
, replyTo
, args
[0]);
992 Heavy
.torch(context
, replyTo
);
996 if(args
.length
!= 3){
997 Heavy
.help(context
, replyTo
, toNonNull(Command
.GETFILE
));
1001 final int getfilePort
;
1003 getfilePort
=Integer
.parseInt(args
[2]);
1004 } catch (NumberFormatException e
){
1005 sendMessage(context
, replyTo
, cannot_parse_port
);
1008 Heavy
.getfile(context
, replyTo
, toNonNull(args
[0]), toNonNull(args
[1]), getfilePort
);
1015 * Executes a given command
1017 * @param context Context instance
1018 * @param cmd Command to execute
1019 * @param args arguments for the command
1020 * @param replyTo Address to send replies to
1022 public static void processCommand(final Context context
, final String cmd
,final String
[] args
,final Address replyTo
){
1023 final @NonNull Command command
;
1025 command
=toNonNull(Command
.valueOf(cmd
.toUpperCase(Locale
.ENGLISH
)));
1026 } catch (IllegalArgumentException e
){
1027 sendMessage(context
, replyTo
, unknown_command
, cmd
.toUpperCase(Locale
.ENGLISH
), e
.getMessage());
1032 processCommand(context
, command
,args
,replyTo
);
1033 } catch(Exception e
){
1034 sendMessage(context
, replyTo
, error_while_processing_command
, e
.getClass().getName(), e
.getMessage());
1035 Log
.w(Utils
.class.getName(), "Error while processing command", e
);