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
, SHUTDOWN
, NOTIFY
93 * 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.
95 * @author Marius Gavrilescu <marius@ieval.ro>
98 public static enum MessageType
{
99 /** SMS notifications */
101 /** Phone state (idle, offhook or ringing) notifications */
103 /** Lockscreen failed password notifications */
105 /** Device admin enable/disable notifications */
107 /** Coarse battery status (low battery, ok battery) notifications */
109 /** Fine battery status notifications */
111 /** Headset plug/unplug notifications */
116 * 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).
118 * @author Marius Gavrilescu <marius@ieval.ro>
120 public static enum WipeType
{
121 /** Factory reset the phone, without touching the SD card */
123 /** Factory reset the phone and wipe the SD card too */
128 * Enum of ringer modes.
130 * @author Marius Gavrilescu <marius@ieval.ro>
132 public static enum RingerMode
{
135 /** Sound is off, vibrate is on */
137 /** Sound is off, vibrate is off */
142 * Enum of location providers
144 * @author Marius Gavrilescu <marius@ieval.ro>
146 public static enum LocationProvider
{
147 /** The network location provider */
149 /** The GPS location provider */
154 * Enum of toast lengths.
156 * @author Marius Gavrilescu <marius@ieval.ro>
158 private static enum ToastLength
{
166 * Enum of the values on and off. Used for boolean arguments.
168 * @author Marius Gavrilescu <marius@ieval.ro>
170 @SuppressWarnings("javadoc")
171 private static enum OnOff
{
176 * Enum of ongoing event types
178 * @author Marius Gavrilescu <marius@ieval.ro>
180 public static enum OngoingEvent
{
181 /** Location tracking is active. Registered by {@link Command#LOCATION}, unregistered by {@link Command#NOLOCATION} */
182 LOCATION(location_tracking_is_active
),
183 /** The phone is ringing. Registered/unregistered by {@link Command#RING} */
185 /** The polling alarm is on. Registered/unregistered by {@link Command#POLL} */
186 POLL(the_polling_service_is_running
);
188 /** String resource: the event description */
189 public final int resource
;
192 * Constructs an OngoingEvent from its event description.
194 * @param resource the event description
196 private OngoingEvent(final int resource
) {
197 this.resource
=resource
;
201 /** Confirmation string for the {@link Command#WIPE WIPE} command. */
202 public static final String WIPE_CONFIRM_STRING
="I am aware this cannot be undone";
205 * Converts a Nullable object into a NonNull one.
207 * @param object the Nullable object to convert
208 * @return a NonNull object equivalent to the Nullable parameter
209 * @throws AssertionError if the given object is null
211 public static <T
> T
toNonNull(@Nullable T object
) throws AssertionError
{
213 Log
.wtf(Utils
.class.getName(), "toNonNull called with null");
214 throw new AssertionError("Log.wtf did not terminate the process");
220 * Join an array of Objects with elements separated by a separator into a single string.
222 * @param separator the separator
223 * @param offset the offset into the array
224 * @param args the array of Objects to join
225 * @return the joined string
227 public static String
join(final String separator
,final int offset
,final Object
[] args
){
228 final StringBuilder sb
=new StringBuilder(240);
229 sb
.append(args
[offset
]);
230 for (int i
= offset
+1; i
< args
.length
; i
++) {
231 sb
.append(separator
);
234 return toNonNull(sb
.toString());
238 * Join an array of Objects with elements separated by a separator into a single string.
240 * @param separator the separator
241 * @param args the array of objects to join
242 * @return the joined string
244 public static String
join(final String separator
, final Object
[] args
){
245 return join(separator
,0,args
);
249 * Send a notification to the user.
251 * @param context Context instance
252 * @param type notification type
253 * @param resource String resource for the message text
255 public static void sendMessage(final Context context
, final MessageType type
, final int resource
){
256 sendMessage(context
, type
, toNonNull(context
.getString(resource
)));
260 * Send a notification to the user.
262 * @param context Context instance
263 * @param type notification type
264 * @param resource String resource for the message text
265 * @param args format parameters for the resource
267 public static void sendMessage(final Context context
, final MessageType type
, final int resource
, final Object
... args
){
268 sendMessage(context
, type
, toNonNull(context
.getString(resource
, args
)));
272 * Send a message to a certain Address.
274 * @param context Context instance
275 * @param address destination Address
276 * @param resource String resource for the message text
278 public static void sendMessage(final Context context
, final Address address
, final int resource
){
279 sendMessage(context
, address
, toNonNull(context
.getString(resource
)));
283 * Send a message to a certain Address.
285 * @param context Context instance
286 * @param address destination Address
287 * @param resource String resource for the message text
288 * @param args format parameters for the resource
290 public static void sendMessage(final Context context
, final Address address
, final int resource
, final Object
... args
){
291 sendMessage(context
, address
, toNonNull(context
.getString(resource
, args
)));
295 * Send a notification to the user.
297 * @param context Context instance
298 * @param type notification type
299 * @param msg the notification text
301 public static void sendMessage(final Context context
, final MessageType type
,final String msg
){
302 final SharedPreferences sp
=PreferenceManager
.getDefaultSharedPreferences(context
);
303 final String address
=sp
.getString(type
.toString(),null);
306 sendMessage(context
, new Address(address
), msg
);
310 * Send a message to a certain Address.
312 * @param context Context instance
313 * @param address destination Address
314 * @param message the message text
316 public static void sendMessage(final Context context
, final Address address
, final String message
){
317 switch(address
.protocol
){
319 new HttpCallExecutableRunnable("/send", toNonNull(Arrays
.asList(
320 new Header("X-Destination", toNonNull(address
.data
)))), context
, null, message
).execute();
324 SmsManager
.getDefault().sendTextMessage(address
.data
, null, message
, null, null);
328 final Intent intent
= new Intent(FonBotLocalActivity
.RESPONSE_RECEIVED_ACTION
);
329 intent
.putExtra(FonBotLocalActivity
.EXTRA_RESPONSE
, message
);
330 context
.sendBroadcast(intent
);
339 * Parses a HTTP response message of the form <code>(part1) part2</code>, returning <code>part2</code>.
341 * @param message the raw HTTP response message
342 * @return the extracted part
344 public static String
parseHttpMessage(final String message
){
345 final int indexOfParen
=message
.indexOf(')');
348 if(indexOfParen
== message
.length()-1)
349 return toNonNull(message
.substring(1, indexOfParen
));
350 return toNonNull(message
.substring(indexOfParen
+2));
354 * Splits a string into words.
356 * @param string the string
357 * @return an array of words
359 public static String
[] shellwords(final String string
){
360 return toNonNull(string
.split("[ ]+(?=([^\"]*\"[^\"]*\")*[^\"]*$)"));//TODO: Make this handle backslash escapes
364 * Returns the name associated with a phone number.
366 * @param context Context instance
367 * @param number phone number to search for
368 * @return the name associated with this number, or null if the number was not found in the contacts.
370 public static @Nullable String
callerId(final Context context
, final String number
){
371 final Cursor cursor
=context
.getContentResolver().query(
372 Uri
.withAppendedPath(PhoneLookup
.CONTENT_FILTER_URI
, Uri
.encode(number
)),
373 new String
[]{PhoneLookup
.DISPLAY_NAME
},
378 if(cursor
.moveToFirst()){
379 final String name
=cursor
.getString(0);
388 * Registers an ongoing event.
390 * @param context Context instance
391 * @param event event to register
393 public static void registerOngoing(final Context context
, final OngoingEvent event
){
394 final Intent intent
=new Intent(context
, FonBotMainService
.class);
395 intent
.setAction(FonBotMainService
.ACTION_PUT_ONGOING
);
396 intent
.putExtra(FonBotMainService
.EXTRA_ONGOING_ID
, event
.ordinal());
397 context
.startService(intent
);
401 * Unregisters an ongoing event
403 * @param context Context instance
404 * @param event event to unregister
406 public static void unregisterOngoing(final Context context
, final OngoingEvent event
){
407 final Intent intent
=new Intent(context
, FonBotMainService
.class);
408 intent
.setAction(FonBotMainService
.ACTION_DELETE_ONGOING
);
409 intent
.putExtra(FonBotMainService
.EXTRA_ONGOING_ID
, event
.ordinal());
410 context
.startService(intent
);
414 * Gets the server URL according to the user preferences.
416 * @param context Context instance
417 * @param path URL path
418 * @return the server URL
419 * @throws MalformedURLException if the user preferences create an invalid URL
421 public static URL
getServerURL(final Context context
, final String path
) throws MalformedURLException
{
422 final String hostname
=PreferenceManager
.getDefaultSharedPreferences(context
).getString("hostname", "ieval.ro");
423 final int port
=Integer
.parseInt(PreferenceManager
.getDefaultSharedPreferences(context
).getString("port", "7777"));
424 final URL url
=new URL("http", hostname
, port
, path
);
429 * Poll the server for pending commands.
431 * @param context Context instance
433 public static void pollServer(final Context context
){
434 new HttpCallExecutableRunnable("/get", null, context
, new PollResultCallback(context
)).execute();
438 * Executes a given command
440 * @param context Context instance
441 * @param cmd Command to execute
442 * @param args arguments for the command
443 * @param replyTo Address to send replies to
445 private static void processCommand(final Context context
, final Command cmd
,final String
[] args
,final Address replyTo
){
446 if(Heavy
.isCommandDisabled(context
, cmd
)){
447 sendMessage(context
, replyTo
, command_disabled
);
453 if(args
.length
< 1 || args
.length
> 2){
454 Heavy
.help(context
, replyTo
, toNonNull(Command
.TOAST
));
459 Heavy
.toast(context
, replyTo
, toNonNull(args
[0]));
462 switch(ToastLength
.valueOf(args
[1].toUpperCase(Locale
.ENGLISH
))){
464 Heavy
.toast(context
, replyTo
, toNonNull(args
[0]), Toast
.LENGTH_LONG
);
467 Heavy
.toast(context
, replyTo
, toNonNull(args
[0]), Toast
.LENGTH_SHORT
);
469 } catch(IllegalArgumentException e
){
470 sendMessage(context
, replyTo
, invalid_length_allowed_values_are
, join(", ",toNonNull(ToastLength
.values())));
477 Heavy
.help(context
, replyTo
, toNonNull(Command
.ECHO
));
480 sendMessage(context
, replyTo
, join(" ",args
));
485 Heavy
.help(context
, replyTo
, toNonNull(Command
.SMS
));
488 Heavy
.sms(context
, replyTo
, toNonNull(args
[0]), join(" ", 1, args
));
492 if(args
.length
!= 1){
493 Heavy
.help(context
, replyTo
, toNonNull(Command
.FLASH
));
498 Heavy
.flash(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
499 } catch(IllegalArgumentException e
) {
500 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
506 Heavy
.help(context
, replyTo
, toNonNull(Command
.WIFI
));
510 Heavy
.wifi(context
, replyTo
);
513 Heavy
.wifi(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
.BLUETOOTH
));
526 Heavy
.bluetooth(context
, replyTo
);
529 Heavy
.bluetooth(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
530 } catch(IllegalArgumentException e
) {
531 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
538 Heavy
.help(context
, replyTo
, toNonNull(Command
.DIAL
));
541 Heavy
.dial(context
, replyTo
, toNonNull(args
[0]));
546 Heavy
.help(context
, replyTo
, toNonNull(Command
.RING
));
550 Heavy
.ring(context
, replyTo
);
553 Heavy
.ring(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
554 } catch(IllegalArgumentException e
){
555 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
562 Heavy
.help(context
, replyTo
, toNonNull(Command
.SPEAK
));
565 Heavy
.speak(context
, replyTo
, join(" ",args
));
570 Heavy
.help(context
, replyTo
, toNonNull(Command
.VIBRATE
));
575 ms
=Long
.parseLong(args
[0]);
576 } catch(NumberFormatException e
){
577 sendMessage(context
, replyTo
, could_not_parse_ms
);
580 Heavy
.vibrate(context
, replyTo
, ms
);
583 case DIALOG
://TODO: Should add an edittext
585 Heavy
.help(context
, replyTo
, toNonNull(Command
.DIALOG
));
588 final String
[] buttons
=new String
[args
.length
-1];
589 System
.arraycopy(args
,1,buttons
,0,buttons
.length
);
590 Heavy
.dialog(context
, replyTo
, toNonNull(args
[0]), buttons
);
594 if(args
.length
>3||args
.length
<1){
595 Heavy
.help(context
, replyTo
, toNonNull(Command
.LOCATION
));
599 final @NonNull String provider
;
601 switch(LocationProvider
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))){
603 provider
=toNonNull(LocationManager
.GPS_PROVIDER
);
607 provider
=toNonNull(LocationManager
.NETWORK_PROVIDER
);
610 } catch(IllegalArgumentException e
){
611 sendMessage(context
, replyTo
, cannot_parse_provider_allowed_values_are
, join(", ",toNonNull(LocationProvider
.values())));
616 final float minDistance
;
620 minTime
=Long
.parseLong(args
[1]);
621 } catch (NumberFormatException e
){
622 sendMessage(context
, replyTo
, cannot_parse_min_time
);
630 minDistance
=Float
.parseFloat(args
[2]);
631 } catch (NumberFormatException e
){
632 sendMessage(context
, replyTo
, cannot_parse_min_distance
);
637 Heavy
.location(context
, replyTo
, provider
, minTime
, minDistance
);
641 Heavy
.nolocation(context
, replyTo
);
646 Heavy
.help(context
, replyTo
, toNonNull(Command
.RINGER
));
650 Heavy
.ringer(context
, replyTo
);
653 final RingerMode rm
=RingerMode
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
));
656 Heavy
.ringer(context
, replyTo
, AudioManager
.RINGER_MODE_NORMAL
);
659 Heavy
.ringer(context
, replyTo
, AudioManager
.RINGER_MODE_VIBRATE
);
662 Heavy
.ringer(context
, replyTo
, AudioManager
.RINGER_MODE_SILENT
);
665 } catch (IllegalArgumentException e
){
666 Utils
.sendMessage(context
, replyTo
, invalid_ringer_mode_valid_values_are
, join(", ",toNonNull(RingerMode
.values())));
673 Heavy
.help(context
, replyTo
, toNonNull(Command
.NCFILE
));
677 final int ncfilePort
;
679 ncfilePort
=Integer
.parseInt(args
[2]);
680 } catch (NumberFormatException e
){
681 sendMessage(context
, replyTo
, cannot_parse_port
);
684 Heavy
.ncfile(context
, replyTo
, toNonNull(args
[0]), toNonNull(args
[1]), ncfilePort
);
689 Heavy
.help(context
, replyTo
, toNonNull(Command
.PHOTO
));
694 photoPort
=Integer
.parseInt(args
[1]);
695 } catch (NumberFormatException e
){
696 sendMessage(context
, replyTo
, cannot_parse_port
);
699 Heavy
.photo(context
, replyTo
, toNonNull(args
[0]), photoPort
);
702 case SETNOTIFICATION
:
704 Heavy
.help(context
, replyTo
, toNonNull(Command
.SETNOTIFICATION
));
709 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
710 .putString(MessageType
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)).toString(), replyTo
.toString())
712 sendMessage(context
, replyTo
, notification_enabled
);
713 } catch (IllegalArgumentException e
){
714 sendMessage(context
, replyTo
, messagetype_should_be_one_of
, join(", ",toNonNull(MessageType
.values())));
720 case DELNOTIFICATION
:
722 Heavy
.help(context
, replyTo
, toNonNull(Command
.DELNOTIFICATION
));
727 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
728 .remove(MessageType
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)).toString())
730 sendMessage(context
, replyTo
, notification_disabled
);
731 } catch (IllegalArgumentException e
){
732 sendMessage(context
, replyTo
, messagetype_should_be_one_of
, join(", ",toNonNull(MessageType
.values())));
740 Heavy
.help(context
, replyTo
, toNonNull(Command
.SETPASSWORD
));
746 Heavy
.setPassword(context
, replyTo
);
748 Heavy
.setPassword(context
, replyTo
, toNonNull(args
[0]));
749 } catch (SecurityException e
){
750 sendMessage(context
, replyTo
, security_exception
+e
.getMessage());
756 Heavy
.help(context
, replyTo
, toNonNull(Command
.WIPE
));
760 if(!args
[1].equalsIgnoreCase(WIPE_CONFIRM_STRING
)){
761 sendMessage(context
, replyTo
, the_second_argument_to_wipe_must_be
, WIPE_CONFIRM_STRING
);
766 Heavy
.wipe(context
, toNonNull(WipeType
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
767 } catch (IllegalArgumentException e
){
768 sendMessage(context
, replyTo
, wipetype_should_be_one_of
, join (", ",toNonNull(WipeType
.values())));
769 } catch (SecurityException e
){
770 sendMessage(context
, replyTo
, security_exception
, e
.getMessage());
776 Heavy
.lock(context
, replyTo
);
777 } catch (SecurityException e
){
778 sendMessage(context
, replyTo
, security_exception
, e
.getMessage());
784 Heavy
.help(context
, replyTo
, toNonNull(Command
.VIEW
));
788 Heavy
.view(context
, replyTo
, toNonNull(Uri
.parse(args
[0])));
792 Heavy
.musicPlayerCommand(context
, replyTo
, "play");
796 Heavy
.musicPlayerCommand(context
, replyTo
, "pause");
800 Heavy
.musicPlayerCommand(context
, replyTo
, "next");
804 Heavy
.musicPlayerCommand(context
, replyTo
, "previous");
808 Heavy
.batt(context
, replyTo
);
812 if (args
.length
> 1) {
813 Heavy
.help(context
, replyTo
, toNonNull(Command
.CALLLOG
));
817 if (args
.length
== 0)
818 Heavy
.calllog(context
, replyTo
, 5);
821 Heavy
.calllog(context
, replyTo
, Integer
.parseInt(args
[0]));
822 } catch (IllegalArgumentException e
){
823 sendMessage(context
, replyTo
, cannot_parse_count
);
829 if (args
.length
> 1) {
830 Heavy
.help(context
, replyTo
, toNonNull(Command
.SMSLOG
));
834 if (args
.length
== 0)
835 Heavy
.smslog(context
, replyTo
, 5);
838 Heavy
.smslog(context
, replyTo
, Integer
.parseInt(args
[0]));
839 } catch (IllegalArgumentException e
) {
840 sendMessage(context
, replyTo
, cannot_parse_count
);
846 if(args
.length
!= 1){
847 Heavy
.help(context
, replyTo
, toNonNull(Command
.HELP
));
852 Heavy
.help(context
, replyTo
, toNonNull(Command
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
853 } catch (IllegalArgumentException e
) {
854 sendMessage(context
, replyTo
, no_such_command_command_list
, join(", ", toNonNull(Command
.values())));
859 if(args
.length
!= 1){
860 Heavy
.help(context
, replyTo
, toNonNull(Command
.LS
));
864 Heavy
.ls(context
, replyTo
, toNonNull(args
[0]));
868 if(args
.length
!= 1){
869 Heavy
.help(context
, replyTo
, toNonNull(Command
.RM
));
873 Heavy
.rm(context
, replyTo
, toNonNull(args
[0]));
877 if(args
.length
!= 1){
878 Heavy
.help(context
, replyTo
, toNonNull(Command
.CONTACTS
));
882 Heavy
.contacts(context
, replyTo
, toNonNull(args
[0]));
886 if(replyTo
.protocol
!= Protocol
.LOCAL
|| args
.length
!= 1){
887 Heavy
.help(context
, replyTo
, toNonNull(Command
.DISABLE
));
892 Heavy
.disable(context
, replyTo
, toNonNull(Command
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
893 } catch (IllegalArgumentException e
){
894 sendMessage(context
, replyTo
, no_such_command_command_list
, join(", ", toNonNull(Command
.values())));
899 if(replyTo
.protocol
!= Protocol
.LOCAL
|| args
.length
!= 1){
900 Heavy
.help(context
, replyTo
, toNonNull(Command
.ENABLE
));
905 Heavy
.enable(context
, replyTo
, toNonNull(Command
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
))));
906 } catch (IllegalArgumentException e
){
907 sendMessage(context
, replyTo
, no_such_command_command_list
, join(", ", toNonNull(Command
.values())));
913 Heavy
.help(context
, replyTo
, toNonNull(Command
.POLL
));
918 Heavy
.poll(context
, replyTo
);
924 interval
=Long
.parseLong(args
[0]);
925 } catch(NumberFormatException e
){
926 sendMessage(context
, replyTo
, cannot_parse_interval
);
930 Heavy
.poll(context
, replyTo
, interval
);
934 Heavy
.hangup(context
, replyTo
);
938 Heavy
.answer(context
, replyTo
);
943 Heavy
.help(context
, replyTo
, toNonNull(Command
.LAUNCH
));
946 Heavy
.launch(context
, replyTo
, toNonNull(args
[0]));
951 Heavy
.help(context
, replyTo
, toNonNull(Command
.DATA
));
956 Heavy
.data(context
, replyTo
);
960 Heavy
.data(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
961 } catch(IllegalArgumentException e
) {
962 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
968 Heavy
.help(context
, replyTo
, toNonNull(Command
.GPS
));
973 Heavy
.gps(context
, replyTo
);
978 Heavy
.gps(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
979 } catch(IllegalArgumentException e
) {
980 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
986 Heavy
.help(context
, replyTo
, toNonNull(Command
.GLOCATION
));
991 Heavy
.glocation(context
, replyTo
);
996 Heavy
.glocation(context
, replyTo
, OnOff
.valueOf(args
[0].toUpperCase(Locale
.ENGLISH
)) == OnOff
.ON
);
997 } catch(IllegalArgumentException e
) {
998 sendMessage(context
, replyTo
, could_not_parse_argument_allowed_values_are
, join(", ", toNonNull(OnOff
.values())));
1004 Heavy
.help(context
, replyTo
, toNonNull(Command
.REBOOT
));
1008 Heavy
.reboot(context
, replyTo
, args
.length
==0?
null:args
[0]);
1012 //TODO: implement command
1016 if(args
.length
!=1 && args
.length
!=3){
1017 Heavy
.help(context
, replyTo
, toNonNull(Command
.NOTIFY
));
1022 id
=Integer
.parseInt(args
[0]);
1023 } catch (NumberFormatException e
){
1024 sendMessage(context
, replyTo
, R
.string
.could_not_parse_id
);
1029 Heavy
.notify(context
, replyTo
, id
);
1031 Heavy
.notify(context
, replyTo
, id
, toNonNull(args
[1]), toNonNull(args
[2]));
1038 * Executes a given command
1040 * @param context Context instance
1041 * @param cmd Command to execute
1042 * @param args arguments for the command
1043 * @param replyTo Address to send replies to
1045 public static void processCommand(final Context context
, final String cmd
,final String
[] args
,final Address replyTo
){
1046 final @NonNull Command command
;
1048 command
=toNonNull(Command
.valueOf(cmd
.toUpperCase(Locale
.ENGLISH
)));
1049 } catch (IllegalArgumentException e
){
1050 sendMessage(context
, replyTo
, unknown_command
, cmd
.toUpperCase(Locale
.ENGLISH
), e
.getMessage());
1055 processCommand(context
, command
,args
,replyTo
);
1056 } catch(Exception e
){
1057 sendMessage(context
, replyTo
, error_while_processing_command
, e
.getClass().getName(), e
.getMessage());
1058 Log
.w(Utils
.class.getName(), "Error while processing command", e
);