ce275fab8c325e224f91b0361252fbec93002df4
[fonbot.git] / src / ro / ieval / fonbot / Utils.java
1 package ro.ieval.fonbot;
2
3 import static ro.ieval.fonbot.R.string.*;
4
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.util.Arrays;
8 import java.util.Locale;
9
10 import org.eclipse.jdt.annotation.NonNull;
11 import org.eclipse.jdt.annotation.Nullable;
12
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;
28
29 /*
30 * Copyright © 2013 Marius Gavrilescu
31 *
32 * This file is part of FonBot.
33 *
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.
38 *
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.
43 *
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/>.
46 */
47
48 /**
49 * Utility functions and enums used in various places.
50 *
51 * @author Marius Gavrilescu <marius@ieval.ro>
52 */
53 public final class Utils {
54 /**
55 * Enum of all FonBot commands.
56 *
57 * @author Marius Gavrilescu <marius@ieval.ro>
58 */
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,
70 TORCH, GETFILE
71 }
72
73 /**
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.
75 *
76 * @author Marius Gavrilescu <marius@ieval.ro>
77 *
78 */
79 public static enum MessageType{
80 /** SMS notifications */
81 SMS,
82 /** Phone state (idle, offhook or ringing) notifications */
83 PHONE_STATE,
84 /** Lockscreen failed password notifications */
85 WATCH_LOGIN,
86 /** Device admin enable/disable notifications */
87 ADMIN,
88 /** Coarse battery status (low battery, ok battery) notifications */
89 BATTERY,
90 /** Fine battery status notifications */
91 BATTERY_CHANGED,
92 /** Headset plug/unplug notifications */
93 HEADSET,
94 /** Phone booted notifications */
95 BOOT,
96 }
97
98 /**
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).
100 *
101 * @author Marius Gavrilescu <marius@ieval.ro>
102 */
103 public static enum WipeType{
104 /** Factory reset the phone, without touching the SD card */
105 DATA,
106 /** Factory reset the phone and wipe the SD card too */
107 FULL
108 }
109
110 /**
111 * Enum of ringer modes.
112 *
113 * @author Marius Gavrilescu <marius@ieval.ro>
114 */
115 public static enum RingerMode{
116 /** Sound is on */
117 NORMAL,
118 /** Sound is off, vibrate is on */
119 VIBRATE,
120 /** Sound is off, vibrate is off */
121 SILENT
122 }
123
124 /**
125 * Enum of location providers
126 *
127 * @author Marius Gavrilescu <marius@ieval.ro>
128 */
129 public static enum LocationProvider{
130 /** The network location provider */
131 NETWORK,
132 /** The GPS location provider */
133 GPS
134 }
135
136 /**
137 * Enum of toast lengths.
138 *
139 * @author Marius Gavrilescu <marius@ieval.ro>
140 */
141 private static enum ToastLength{
142 /** Long toast */
143 LONG,
144 /** Short toast */
145 SHORT
146 }
147
148 /**
149 * Enum of the values on and off. Used for boolean arguments.
150 *
151 * @author Marius Gavrilescu <marius@ieval.ro>
152 */
153 @SuppressWarnings("javadoc")
154 private static enum OnOff{
155 ON, OFF
156 }
157
158 /**
159 * Enum of ongoing event types
160 *
161 * @author Marius Gavrilescu <marius@ieval.ro>
162 */
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} */
167 RING(ringing);
168
169 /** String resource: the event description */
170 public final int resource;
171
172 /**
173 * Constructs an OngoingEvent from its event description.
174 *
175 * @param resource the event description
176 */
177 private OngoingEvent(final int resource) {
178 this.resource=resource;
179 }
180 }
181
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";
184
185 /**
186 * Converts a Nullable object into a NonNull one.
187 *
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
191 */
192 public static <T> T toNonNull(@Nullable T object) throws AssertionError{
193 if(object==null)
194 throw new NullPointerException();
195 return object;
196 }
197
198 /**
199 * Join an array of Objects with elements separated by a separator into a single string.
200 *
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
205 */
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);
211 sb.append(args[i]);
212 }
213 return toNonNull(sb.toString());
214 }
215
216 /**
217 * Join an array of Objects with elements separated by a separator into a single string.
218 *
219 * @param separator the separator
220 * @param args the array of objects to join
221 * @return the joined string
222 */
223 public static String join(final String separator, final Object[] args){
224 return join(separator,0,args);
225 }
226
227 /**
228 * Send a notification to the user.
229 *
230 * @param context Context instance
231 * @param type notification type
232 * @param resource String resource for the message text
233 */
234 public static void sendMessage(final Context context, final MessageType type, final int resource){
235 sendMessage(context, type, toNonNull(context.getString(resource)));
236 }
237
238 /**
239 * Send a notification to the user.
240 *
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
245 */
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)));
248 }
249
250 /**
251 * Send a message to a certain Address.
252 *
253 * @param context Context instance
254 * @param address destination Address
255 * @param resource String resource for the message text
256 */
257 public static void sendMessage(final Context context, final Address address, final int resource){
258 sendMessage(context, address, toNonNull(context.getString(resource)));
259 }
260
261 /**
262 * Send a message to a certain Address.
263 *
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
268 */
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)));
271 }
272
273 /**
274 * Send a notification to the user.
275 *
276 * @param context Context instance
277 * @param type notification type
278 * @param msg the notification text
279 */
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);
283 if(address==null)
284 return;
285 sendMessage(context, new Address(address), msg);
286 }
287
288 /**
289 * Send a message to a certain Address.
290 *
291 * @param context Context instance
292 * @param address destination Address
293 * @param message the message text
294 */
295 public static void sendMessage(final Context context, final Address address, final String message){
296 switch(address.protocol){
297 case HTTP:
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();
299 break;
300
301 case SMS:
302 new Handler(Looper.getMainLooper()).post(new Runnable(){
303 @Override
304 public void run(){
305 SmsManager.getDefault().sendTextMessage(address.data, null, message, null, null);
306 }
307 });
308 break;
309
310 case LOCAL:
311 final Intent intent = new Intent(FonBotLocalActivity.RESPONSE_RECEIVED_ACTION);
312 intent.putExtra(FonBotLocalActivity.EXTRA_RESPONSE, message);
313 context.sendBroadcast(intent);
314 break;
315
316 case NULL:
317 break;//blackhole
318 }
319 }
320
321 /**
322 * Splits a string into words.
323 *
324 * @param string the string
325 * @return an array of words
326 */
327 public static String[] shellwords(final String string){
328 return toNonNull(string.split("[ ]+(?=([^\"]*\"[^\"]*\")*[^\"]*$)"));//TODO: Make this handle backslash escapes
329 }
330
331 /**
332 * Returns the name associated with a phone number.
333 *
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.
337 */
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},
342 null,
343 null,
344 null);
345
346 if(cursor.moveToFirst()){
347 final String name=cursor.getString(0);
348 cursor.close();
349 return name;
350 }
351 cursor.close();
352 return null;
353 }
354
355 /**
356 * Registers an ongoing event.
357 *
358 * @param context Context instance
359 * @param event event to register
360 */
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);
366 }
367
368 /**
369 * Unregisters an ongoing event
370 *
371 * @param context Context instance
372 * @param event event to unregister
373 */
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);
379 }
380
381 /**
382 * Gets the server URL according to the user preferences.
383 *
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
388 */
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);
394 return url;
395 }
396
397 /**
398 * Executes a given command
399 *
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
404 */
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());
408 return;
409 }
410
411 switch(cmd){
412 case TOAST:
413 if(args.length < 1 || args.length > 2){
414 Heavy.help(context, replyTo, toNonNull(Command.TOAST));
415 break;
416 }
417
418 if(args.length==1)
419 Heavy.toast(context, replyTo, toNonNull(args[0]));
420 else {
421 try {
422 switch(ToastLength.valueOf(args[1].toUpperCase(Locale.ENGLISH))){
423 case LONG:
424 Heavy.toast(context, replyTo, toNonNull(args[0]), Toast.LENGTH_LONG);
425 break;
426 case SHORT:
427 Heavy.toast(context, replyTo, toNonNull(args[0]), Toast.LENGTH_SHORT);
428 }
429 } catch(IllegalArgumentException e){
430 sendMessage(context, replyTo, invalid_length_allowed_values_are, join(", ",toNonNull(ToastLength.values())));
431 }
432 }
433 break;
434
435 case ECHO:
436 if(args.length==0){
437 Heavy.help(context, replyTo, toNonNull(Command.ECHO));
438 break;
439 }
440 sendMessage(context, replyTo, join(" ",args));
441 break;
442
443 case SMS:
444 if(args.length < 2){
445 Heavy.help(context, replyTo, toNonNull(Command.SMS));
446 break;
447 }
448 Heavy.sms(context, replyTo, toNonNull(args[0]), join(" ", 1, args));
449 break;
450
451 case FLASH:
452 if(args.length != 1){
453 Heavy.help(context, replyTo, toNonNull(Command.FLASH));
454 break;
455 }
456
457 try {
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())));
461 }
462 break;
463
464 case WIFI:
465 if(args.length>1){
466 Heavy.help(context, replyTo, toNonNull(Command.WIFI));
467 break;
468 }
469 if(args.length==0)
470 Heavy.wifi(context, replyTo);
471 else {
472 try {
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())));
476 }
477 }
478 break;
479
480 case BLUETOOTH:
481 if(args.length>1){
482 Heavy.help(context, replyTo, toNonNull(Command.BLUETOOTH));
483 break;
484 }
485 if(args.length==0)
486 Heavy.bluetooth(context, replyTo);
487 else {
488 try {
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())));
492 }
493 }
494 break;
495
496 case DIAL:
497 if(args.length!=1){
498 Heavy.help(context, replyTo, toNonNull(Command.DIAL));
499 break;
500 }
501 Heavy.dial(context, replyTo, toNonNull(args[0]));
502 break;
503
504 case RING:
505 if(args.length>1){
506 Heavy.help(context, replyTo, toNonNull(Command.RING));
507 break;
508 }
509 if(args.length==0)
510 Heavy.ring(context, replyTo);
511 else {
512 try {
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())));
516 }
517 }
518 break;
519
520 case SPEAK:
521 if(args.length==0){
522 Heavy.help(context, replyTo, toNonNull(Command.SPEAK));
523 break;
524 }
525 Heavy.speak(context, replyTo, join(" ",args));
526 break;
527
528 case VIBRATE:
529 if(args.length!=1){
530 Heavy.help(context, replyTo, toNonNull(Command.VIBRATE));
531 break;
532 }
533 final long ms;
534 try{
535 ms=Long.parseLong(args[0]);
536 } catch(NumberFormatException e){
537 sendMessage(context, replyTo, could_not_parse_ms);
538 break;
539 }
540 Heavy.vibrate(context, replyTo, ms);
541 break;
542
543 case DIALOG://TODO: Should add an edittext
544 if(args.length<1){
545 Heavy.help(context, replyTo, toNonNull(Command.DIALOG));
546 break;
547 }
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);
551 break;
552
553 case LOCATION:
554 if(args.length>3||args.length<1){
555 Heavy.help(context, replyTo, toNonNull(Command.LOCATION));
556 break;
557 }
558
559 final @NonNull String provider;
560 try{
561 switch(LocationProvider.valueOf(args[0].toUpperCase(Locale.ENGLISH))){
562 case GPS:
563 provider=toNonNull(LocationManager.GPS_PROVIDER);
564 break;
565 case NETWORK:
566 default:
567 provider=toNonNull(LocationManager.NETWORK_PROVIDER);
568 break;
569 }
570 } catch(IllegalArgumentException e){
571 sendMessage(context, replyTo, cannot_parse_provider_allowed_values_are, join(", ",toNonNull(LocationProvider.values())));
572 break;
573 }
574
575 final long minTime;
576 final float minDistance;
577
578 if(args.length>1)
579 try{
580 minTime=Long.parseLong(args[1]);
581 } catch (NumberFormatException e){
582 sendMessage(context, replyTo, cannot_parse_min_time);
583 break;
584 }
585 else
586 minTime=500;
587
588 if(args.length>2)
589 try{
590 minDistance=Float.parseFloat(args[2]);
591 } catch (NumberFormatException e){
592 sendMessage(context, replyTo, cannot_parse_min_distance);
593 break;
594 }
595 else
596 minDistance=0;
597 Heavy.location(context, replyTo, provider, minTime, minDistance);
598 break;
599
600 case NOLOCATION:
601 Heavy.nolocation(context, replyTo);
602 break;
603
604 case RINGER:
605 if(args.length>1){
606 Heavy.help(context, replyTo, toNonNull(Command.RINGER));
607 break;
608 }
609 if(args.length==0)
610 Heavy.ringer(context, replyTo);
611 else{
612 try{
613 final RingerMode rm=RingerMode.valueOf(args[0].toUpperCase(Locale.ENGLISH));
614 switch(rm){
615 case NORMAL:
616 Heavy.ringer(context, replyTo, AudioManager.RINGER_MODE_NORMAL);
617 break;
618 case VIBRATE:
619 Heavy.ringer(context, replyTo, AudioManager.RINGER_MODE_VIBRATE);
620 break;
621 case SILENT:
622 Heavy.ringer(context, replyTo, AudioManager.RINGER_MODE_SILENT);
623 break;
624 }
625 } catch (IllegalArgumentException e){
626 Utils.sendMessage(context, replyTo, invalid_ringer_mode_valid_values_are, join(", ",toNonNull(RingerMode.values())));
627 }
628 }
629 break;
630
631 case NCFILE:
632 if(args.length!=3){
633 Heavy.help(context, replyTo, toNonNull(Command.NCFILE));
634 break;
635 }
636
637 final int ncfilePort;
638 try{
639 ncfilePort=Integer.parseInt(args[2]);
640 } catch (NumberFormatException e){
641 sendMessage(context, replyTo, cannot_parse_port);
642 break;
643 }
644 Heavy.ncfile(context, replyTo, toNonNull(args[0]), toNonNull(args[1]), ncfilePort);
645 break;
646
647 case PHOTO:
648 if(args.length!=3){
649 Heavy.help(context, replyTo, toNonNull(Command.PHOTO));
650 break;
651 }
652
653 final int cameraNumber;
654 try{
655 cameraNumber=Integer.parseInt(args[0]);
656 } catch (NumberFormatException e){
657 sendMessage(context, replyTo, cannot_parse_camera_number);
658 break;
659 }
660
661 final int photoPort;
662 try{
663 photoPort=Integer.parseInt(args[2]);
664 } catch (NumberFormatException e){
665 sendMessage(context, replyTo, cannot_parse_port);
666 break;
667 }
668 Heavy.photo(context, replyTo, cameraNumber, toNonNull(args[1]), photoPort);
669 break;
670
671 case SETNOTIFICATION:
672 if(args.length!=1){
673 Heavy.help(context, replyTo, toNonNull(Command.SETNOTIFICATION));
674 break;
675 }
676
677 try{
678 PreferenceManager.getDefaultSharedPreferences(context).edit()
679 .putString(MessageType.valueOf(args[0].toUpperCase(Locale.ENGLISH)).toString(), replyTo.toString())
680 .commit();
681 sendMessage(context, replyTo, notification_enabled);
682 } catch (IllegalArgumentException e){
683 sendMessage(context, replyTo, messagetype_should_be_one_of, join(", ",toNonNull(MessageType.values())));
684 break;
685 }
686
687 break;
688
689 case DELNOTIFICATION:
690 if(args.length!=1){
691 Heavy.help(context, replyTo, toNonNull(Command.DELNOTIFICATION));
692 break;
693 }
694
695 try{
696 PreferenceManager.getDefaultSharedPreferences(context).edit()
697 .remove(MessageType.valueOf(args[0].toUpperCase(Locale.ENGLISH)).toString())
698 .commit();
699 sendMessage(context, replyTo, notification_disabled);
700 } catch (IllegalArgumentException e){
701 sendMessage(context, replyTo, messagetype_should_be_one_of, join(", ",toNonNull(MessageType.values())));
702 break;
703 }
704
705
706 break;
707 case SETPASSWORD:
708 if(args.length > 1){
709 Heavy.help(context, replyTo, toNonNull(Command.SETPASSWORD));
710 break;
711 }
712
713 try{
714 if(args.length==0)
715 Heavy.setPassword(context, replyTo);
716 else
717 Heavy.setPassword(context, replyTo, toNonNull(args[0]));
718 } catch (SecurityException e){
719 sendMessage(context, replyTo, security_exception+e.getMessage());
720 }
721 break;
722
723 case WIPE:
724 if(args.length!=2){
725 Heavy.help(context, replyTo, toNonNull(Command.WIPE));
726 break;
727 }
728
729 if(!args[1].equalsIgnoreCase(WIPE_CONFIRM_STRING)){
730 sendMessage(context, replyTo, the_second_argument_to_wipe_must_be, WIPE_CONFIRM_STRING);
731 break;
732 }
733
734 try{
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());
740 }
741 break;
742
743 case LOCK:
744 try{
745 Heavy.lock(context, replyTo);
746 } catch (SecurityException e){
747 sendMessage(context, replyTo, security_exception, e.getMessage());
748 }
749 break;
750
751 case VIEW:
752 if(args.length!=1){
753 Heavy.help(context, replyTo, toNonNull(Command.VIEW));
754 break;
755 }
756
757 Heavy.view(context, replyTo, toNonNull(Uri.parse(args[0])));
758 break;
759
760 case PLAY:
761 Heavy.musicPlayerCommand(context, replyTo, "play");
762 break;
763
764 case PAUSE:
765 Heavy.musicPlayerCommand(context, replyTo, "pause");
766 break;
767
768 case NEXT:
769 Heavy.musicPlayerCommand(context, replyTo, "next");
770 break;
771
772 case PREV:
773 Heavy.musicPlayerCommand(context, replyTo, "previous");
774 break;
775
776 case BATT:
777 Heavy.batt(context, replyTo);
778 break;
779
780 case CALLLOG:
781 if (args.length > 1) {
782 Heavy.help(context, replyTo, toNonNull(Command.CALLLOG));
783 break;
784 }
785
786 if (args.length == 0)
787 Heavy.calllog(context, replyTo, 5);
788 else {
789 try {
790 Heavy.calllog(context, replyTo, Integer.parseInt(args[0]));
791 } catch (IllegalArgumentException e){
792 sendMessage(context, replyTo, cannot_parse_count);
793 }
794 }
795 break;
796
797 case SMSLOG:
798 if (args.length > 1) {
799 Heavy.help(context, replyTo, toNonNull(Command.SMSLOG));
800 break;
801 }
802
803 if (args.length == 0)
804 Heavy.smslog(context, replyTo, 5);
805 else {
806 try {
807 Heavy.smslog(context, replyTo, Integer.parseInt(args[0]));
808 } catch (IllegalArgumentException e) {
809 sendMessage(context, replyTo, cannot_parse_count);
810 }
811 }
812 break;
813
814 case HELP:
815 if(args.length != 1){
816 Heavy.help(context, replyTo, toNonNull(Command.HELP));
817 break;
818 }
819
820 try {
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())));
824 }
825 break;
826
827 case LS:
828 if(args.length != 1){
829 Heavy.help(context, replyTo, toNonNull(Command.LS));
830 break;
831 }
832
833 Heavy.ls(context, replyTo, toNonNull(args[0]));
834 break;
835
836 case RM:
837 if(args.length != 1){
838 Heavy.help(context, replyTo, toNonNull(Command.RM));
839 break;
840 }
841
842 Heavy.rm(context, replyTo, toNonNull(args[0]));
843 break;
844
845 case CONTACTS:
846 if(args.length != 1){
847 Heavy.help(context, replyTo, toNonNull(Command.CONTACTS));
848 break;
849 }
850
851 Heavy.contacts(context, replyTo, toNonNull(args[0]));
852 break;
853
854 case DISABLE:
855 if(replyTo.protocol != Protocol.LOCAL || args.length != 1){
856 Heavy.help(context, replyTo, toNonNull(Command.DISABLE));
857 break;
858 }
859
860 try{
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())));
864 }
865 break;
866
867 case ENABLE:
868 if(replyTo.protocol != Protocol.LOCAL || args.length != 1){
869 Heavy.help(context, replyTo, toNonNull(Command.ENABLE));
870 break;
871 }
872
873 try{
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())));
877 }
878 break;
879
880 case POLL:
881 Heavy.poll(context, replyTo);
882 break;
883
884 case HANGUP:
885 Heavy.hangup(context, replyTo);
886 break;
887
888 case ANSWER:
889 Heavy.answer(context, replyTo);
890 break;
891
892 case LAUNCH:
893 if(args.length!=1){
894 Heavy.help(context, replyTo, toNonNull(Command.LAUNCH));
895 break;
896 }
897 Heavy.launch(context, replyTo, toNonNull(args[0]));
898 break;
899
900 case DATA:
901 if(args.length>1){
902 Heavy.help(context, replyTo, toNonNull(Command.DATA));
903 break;
904 }
905
906 if(args.length==0){
907 Heavy.data(context, replyTo);
908 break;
909 }
910 try {
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())));
914 }
915 break;
916
917 case GPS:
918 if(args.length>1){
919 Heavy.help(context, replyTo, toNonNull(Command.GPS));
920 break;
921 }
922
923 if(args.length==0){
924 Heavy.gps(context, replyTo);
925 break;
926 }
927
928 try {
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())));
932 }
933 break;
934
935 case GLOCATION:
936 if(args.length>1){
937 Heavy.help(context, replyTo, toNonNull(Command.GLOCATION));
938 break;
939 }
940
941 if(args.length==0){
942 Heavy.glocation(context, replyTo);
943 break;
944 }
945
946 try {
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())));
950 }
951 break;
952
953 case REBOOT:
954 if(args.length>1){
955 Heavy.help(context, replyTo, toNonNull(Command.REBOOT));
956 break;
957 }
958
959 Heavy.reboot(context, replyTo, args.length==0?null:args[0]);
960 break;
961
962 case NOTIFY:
963 if(args.length!=1 && args.length!=3){
964 Heavy.help(context, replyTo, toNonNull(Command.NOTIFY));
965 return;
966 }
967
968 final int id;
969 try{
970 id=Integer.parseInt(args[0]);
971 } catch (NumberFormatException e){
972 sendMessage(context, replyTo, R.string.could_not_parse_id);
973 break;
974 }
975
976 if(args.length==1)
977 Heavy.notify(context, replyTo, id);
978 else
979 Heavy.notify(context, replyTo, id, toNonNull(args[1]), toNonNull(args[2]));
980 break;
981
982 case SCREENCAP:
983 if(args.length != 1){
984 Heavy.help(context, replyTo, toNonNull(Command.SCREENCAP));
985 return;
986 }
987
988 Heavy.screencap(context, replyTo, args[0]);
989 break;
990
991 case TORCH:
992 Heavy.torch(context, replyTo);
993 break;
994
995 case GETFILE:
996 if(args.length != 3){
997 Heavy.help(context, replyTo, toNonNull(Command.GETFILE));
998 return;
999 }
1000
1001 final int getfilePort;
1002 try{
1003 getfilePort=Integer.parseInt(args[2]);
1004 } catch (NumberFormatException e){
1005 sendMessage(context, replyTo, cannot_parse_port);
1006 break;
1007 }
1008 Heavy.getfile(context, replyTo, toNonNull(args[0]), toNonNull(args[1]), getfilePort);
1009 break;
1010 }
1011
1012 }
1013
1014 /**
1015 * Executes a given command
1016 *
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
1021 */
1022 public static void processCommand(final Context context, final String cmd,final String[] args,final Address replyTo){
1023 final @NonNull Command command;
1024 try{
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());
1028 return;
1029 }
1030
1031 try{
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);
1036 }
1037 }
1038 }
This page took 0.050545 seconds and 3 git commands to generate.