]>
Commit | Line | Data |
---|---|---|
1 | package ro.ieval.fonbot; | |
2 | ||
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; | |
26 | ||
27 | import java.util.Arrays; | |
28 | import java.util.Locale; | |
29 | ||
30 | import org.eclipse.jdt.annotation.NonNull; | |
31 | import org.eclipse.jdt.annotation.Nullable; | |
32 | ||
33 | import ro.ieval.fonbot.Address.Protocol; | |
34 | import android.content.Context; | |
35 | import android.content.Intent; | |
36 | import android.content.SharedPreferences; | |
37 | import android.database.Cursor; | |
38 | import android.location.LocationManager; | |
39 | import android.media.AudioManager; | |
40 | import android.net.Uri; | |
41 | import android.preference.PreferenceManager; | |
42 | import android.provider.ContactsContract.PhoneLookup; | |
43 | import android.telephony.SmsManager; | |
44 | import android.util.Log; | |
45 | import android.widget.Toast; | |
46 | ||
47 | /* | |
48 | * Copyright © 2013 Marius Gavrilescu | |
49 | * | |
50 | * This file is part of FonBot. | |
51 | * | |
52 | * FonBot is free software: you can redistribute it and/or modify | |
53 | * it under the terms of the GNU General Public License as published by | |
54 | * the Free Software Foundation, either version 3 of the License, or | |
55 | * (at your option) any later version. | |
56 | * | |
57 | * FonBot is distributed in the hope that it will be useful, | |
58 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
59 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
60 | * GNU General Public License for more details. | |
61 | * | |
62 | * You should have received a copy of the GNU General Public License | |
63 | * along with FonBot. If not, see <http://www.gnu.org/licenses/>. | |
64 | */ | |
65 | ||
66 | /** | |
67 | * Utility functions and enums used in various places. | |
68 | * | |
69 | * @author Marius Gavrilescu <marius@ieval.ro> | |
70 | */ | |
71 | public final class Utils { | |
72 | /** | |
73 | * Enum of all FonBot commands. | |
74 | * | |
75 | * @author Marius Gavrilescu <marius@ieval.ro> | |
76 | */ | |
77 | @SuppressWarnings("javadoc") | |
78 | public static enum Command{ | |
79 | TOAST, ECHO, SMS, FLASH, WIFI, | |
80 | BLUETOOTH, DIAL, RING, SPEAK, VIBRATE, | |
81 | DIALOG, LOCATION, NOLOCATION, RINGER, NCFILE, | |
82 | PHOTO, SETNOTIFICATION, DELNOTIFICATION, SETPASSWORD, HELP, | |
83 | WIPE, LOCK, VIEW, PLAY, PAUSE, | |
84 | NEXT, PREV, BATT, CALLLOG, SMSLOG, | |
85 | LS, RM, CONTACTS, DISABLE, ENABLE, | |
86 | POLL, HANGUP, ANSWER, LAUNCH, DATA, | |
87 | GPS, GLOCATION, REBOOT, SHUTDOWN, NOTIFY | |
88 | } | |
89 | ||
90 | /** | |
91 | * 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. | |
92 | * | |
93 | * @author Marius Gavrilescu <marius@ieval.ro> | |
94 | * | |
95 | */ | |
96 | public static enum MessageType{ | |
97 | /** SMS notifications */ | |
98 | SMS, | |
99 | /** Phone state (idle, offhook or ringing) notifications */ | |
100 | PHONE_STATE, | |
101 | /** Lockscreen failed password notifications */ | |
102 | WATCH_LOGIN, | |
103 | /** Device admin enable/disable notifications */ | |
104 | ADMIN, | |
105 | /** Coarse battery status (low battery, ok battery) notifications */ | |
106 | BATTERY, | |
107 | /** Fine battery status notifications */ | |
108 | BATTERY_CHANGED, | |
109 | /** Headset plug/unplug notifications */ | |
110 | HEADSET | |
111 | } | |
112 | ||
113 | /** | |
114 | * 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). | |
115 | * | |
116 | * @author Marius Gavrilescu <marius@ieval.ro> | |
117 | */ | |
118 | public static enum WipeType{ | |
119 | /** Factory reset the phone, without touching the SD card */ | |
120 | DATA, | |
121 | /** Factory reset the phone and wipe the SD card too */ | |
122 | FULL | |
123 | } | |
124 | ||
125 | /** | |
126 | * Enum of ringer modes. | |
127 | * | |
128 | * @author Marius Gavrilescu <marius@ieval.ro> | |
129 | */ | |
130 | public static enum RingerMode{ | |
131 | /** Sound is on */ | |
132 | NORMAL, | |
133 | /** Sound is off, vibrate is on */ | |
134 | VIBRATE, | |
135 | /** Sound is off, vibrate is off */ | |
136 | SILENT | |
137 | } | |
138 | ||
139 | /** | |
140 | * Enum of location providers | |
141 | * | |
142 | * @author Marius Gavrilescu <marius@ieval.ro> | |
143 | */ | |
144 | public static enum LocationProvider{ | |
145 | /** The network location provider */ | |
146 | NETWORK, | |
147 | /** The GPS location provider */ | |
148 | GPS | |
149 | } | |
150 | ||
151 | /** | |
152 | * Enum of toast lengths. | |
153 | * | |
154 | * @author Marius Gavrilescu <marius@ieval.ro> | |
155 | */ | |
156 | private static enum ToastLength{ | |
157 | /** Long toast */ | |
158 | LONG, | |
159 | /** Short toast */ | |
160 | SHORT | |
161 | } | |
162 | ||
163 | /** | |
164 | * Enum of the values on and off. Used for boolean arguments. | |
165 | * | |
166 | * @author Marius Gavrilescu <marius@ieval.ro> | |
167 | */ | |
168 | @SuppressWarnings("javadoc") | |
169 | private static enum OnOff{ | |
170 | ON, OFF | |
171 | } | |
172 | ||
173 | /** | |
174 | * Enum of ongoing event types | |
175 | * | |
176 | * @author Marius Gavrilescu <marius@ieval.ro> | |
177 | */ | |
178 | public static enum OngoingEvent{ | |
179 | /** Location tracking is active. Registered by {@link Command#LOCATION}, unregistered by {@link Command#NOLOCATION} */ | |
180 | LOCATION(location_tracking_is_active), | |
181 | /** The phone is ringing. Registered/unregistered by {@link Command#RING} */ | |
182 | RING(ringing), | |
183 | /** The polling alarm is on. Registered/unregistered by {@link Command#POLL} */ | |
184 | POLL(the_polling_service_is_running); | |
185 | ||
186 | /** String resource: the event description */ | |
187 | public final int resource; | |
188 | ||
189 | /** | |
190 | * Constructs an OngoingEvent from its event description. | |
191 | * | |
192 | * @param resource the event description | |
193 | */ | |
194 | private OngoingEvent(final int resource) { | |
195 | this.resource=resource; | |
196 | } | |
197 | } | |
198 | ||
199 | /** Confirmation string for the {@link Command#WIPE WIPE} command. */ | |
200 | public static final String WIPE_CONFIRM_STRING="I am aware this cannot be undone"; | |
201 | ||
202 | /** | |
203 | * Converts a Nullable object into a NonNull one. | |
204 | * | |
205 | * @param object the Nullable object to convert | |
206 | * @return a NonNull object equivalent to the Nullable parameter | |
207 | * @throws AssertionError if the given object is null | |
208 | */ | |
209 | public static <T> T toNonNull(@Nullable T object) throws AssertionError{ | |
210 | if(object==null){ | |
211 | Log.wtf(Utils.class.getName(), "toNonNull called with null"); | |
212 | throw new AssertionError("Log.wtf did not terminate the process"); | |
213 | } | |
214 | return object; | |
215 | } | |
216 | ||
217 | /** | |
218 | * Join an array of Objects with elements separated by a separator into a single string. | |
219 | * | |
220 | * @param separator the separator | |
221 | * @param offset the offset into the array | |
222 | * @param args the array of Objects to join | |
223 | * @return the joined string | |
224 | */ | |
225 | public static String join(final String separator,final int offset,final Object[] args){ | |
226 | final StringBuilder sb=new StringBuilder(240); | |
227 | sb.append(args[offset]); | |
228 | for (int i = offset+1; i < args.length; i++) { | |
229 | sb.append(separator); | |
230 | sb.append(args[i]); | |
231 | } | |
232 | return toNonNull(sb.toString()); | |
233 | } | |
234 | ||
235 | /** | |
236 | * Join an array of Objects with elements separated by a separator into a single string. | |
237 | * | |
238 | * @param separator the separator | |
239 | * @param args the array of objects to join | |
240 | * @return the joined string | |
241 | */ | |
242 | public static String join(final String separator, final Object[] args){ | |
243 | return join(separator,0,args); | |
244 | } | |
245 | ||
246 | /** | |
247 | * Send a notification to the user. | |
248 | * | |
249 | * @param context Context instance | |
250 | * @param type notification type | |
251 | * @param resource String resource for the message text | |
252 | */ | |
253 | public static void sendMessage(final Context context, final MessageType type, final int resource){ | |
254 | sendMessage(context, type, toNonNull(context.getString(resource))); | |
255 | } | |
256 | ||
257 | /** | |
258 | * Send a notification to the user. | |
259 | * | |
260 | * @param context Context instance | |
261 | * @param type notification type | |
262 | * @param resource String resource for the message text | |
263 | * @param args format parameters for the resource | |
264 | */ | |
265 | public static void sendMessage(final Context context, final MessageType type, final int resource, final Object... args){ | |
266 | sendMessage(context, type, toNonNull(context.getString(resource, args))); | |
267 | } | |
268 | ||
269 | /** | |
270 | * Send a message to a certain Address. | |
271 | * | |
272 | * @param context Context instance | |
273 | * @param address destination Address | |
274 | * @param resource String resource for the message text | |
275 | */ | |
276 | public static void sendMessage(final Context context, final Address address, final int resource){ | |
277 | sendMessage(context, address, toNonNull(context.getString(resource))); | |
278 | } | |
279 | ||
280 | /** | |
281 | * Send a message to a certain Address. | |
282 | * | |
283 | * @param context Context instance | |
284 | * @param address destination Address | |
285 | * @param resource String resource for the message text | |
286 | * @param args format parameters for the resource | |
287 | */ | |
288 | public static void sendMessage(final Context context, final Address address, final int resource, final Object... args){ | |
289 | sendMessage(context, address, toNonNull(context.getString(resource, args))); | |
290 | } | |
291 | ||
292 | /** | |
293 | * Send a notification to the user. | |
294 | * | |
295 | * @param context Context instance | |
296 | * @param type notification type | |
297 | * @param msg the notification text | |
298 | */ | |
299 | public static void sendMessage(final Context context, final MessageType type,final String msg){ | |
300 | final SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(context); | |
301 | final String address=sp.getString(type.toString(),null); | |
302 | if(address==null) | |
303 | return; | |
304 | sendMessage(context, new Address(address), msg); | |
305 | } | |
306 | ||
307 | /** | |
308 | * Send a message to a certain Address. | |
309 | * | |
310 | * @param context Context instance | |
311 | * @param address destination Address | |
312 | * @param message the message text | |
313 | */ | |
314 | public static void sendMessage(final Context context, final Address address, final String message){ | |
315 | switch(address.protocol){ | |
316 | case HTTP: | |
317 | new SendHttpMessageAsyncTask(toNonNull(Arrays.asList( | |
318 | new Header("X-Action", "SEND "+address.data))), context).execute(message); | |
319 | break; | |
320 | ||
321 | case SMS: | |
322 | SmsManager.getDefault().sendTextMessage(address.data, null, message, null, null); | |
323 | break; | |
324 | ||
325 | case LOCAL: | |
326 | final Intent intent = new Intent(FonBotLocalActivity.RESPONSE_RECEIVED_ACTION); | |
327 | intent.putExtra(FonBotLocalActivity.EXTRA_RESPONSE, message); | |
328 | context.sendBroadcast(intent); | |
329 | break; | |
330 | ||
331 | case NULL: | |
332 | break;//blackhole | |
333 | } | |
334 | } | |
335 | ||
336 | /** | |
337 | * Parses a HTTP response message of the form <code>(part1) part2</code>, returning <code>part2</code>. | |
338 | * | |
339 | * @param message the raw HTTP response message | |
340 | * @return the extracted part | |
341 | */ | |
342 | public static String parseHttpMessage(final String message){ | |
343 | final int indexOfParen=message.indexOf(')'); | |
344 | if(indexOfParen==-1) | |
345 | return message; | |
346 | return toNonNull(message.substring(indexOfParen+2)); | |
347 | } | |
348 | ||
349 | /** | |
350 | * Splits a string into words. | |
351 | * | |
352 | * @param string the string | |
353 | * @return an array of words | |
354 | */ | |
355 | public static String[] shellwords(final String string){ | |
356 | return toNonNull(string.split("[ ]+(?=([^\"]*\"[^\"]*\")*[^\"]*$)"));//TODO: Make this handle backslash escapes | |
357 | } | |
358 | ||
359 | /** | |
360 | * Returns the name associated with a phone number. | |
361 | * | |
362 | * @param context Context instance | |
363 | * @param number phone number to search for | |
364 | * @return the name associated with this number, or null if the number was not found in the contacts. | |
365 | */ | |
366 | public static @Nullable String callerId(final Context context, final String number){ | |
367 | final Cursor cursor=context.getContentResolver().query( | |
368 | Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)), | |
369 | new String[]{PhoneLookup.DISPLAY_NAME}, | |
370 | null, | |
371 | null, | |
372 | null); | |
373 | ||
374 | if(cursor.moveToFirst()){ | |
375 | final String name=cursor.getString(0); | |
376 | cursor.close(); | |
377 | return name; | |
378 | } | |
379 | cursor.close(); | |
380 | return null; | |
381 | } | |
382 | ||
383 | /** | |
384 | * Registers an ongoing event. | |
385 | * | |
386 | * @param context Context instance | |
387 | * @param event event to register | |
388 | */ | |
389 | public static void registerOngoing(final Context context, final OngoingEvent event){ | |
390 | final Intent intent=new Intent(context, FonBotMainService.class); | |
391 | intent.setAction(FonBotMainService.ACTION_PUT_ONGOING); | |
392 | intent.putExtra(FonBotMainService.EXTRA_ONGOING_ID, event.ordinal()); | |
393 | context.startService(intent); | |
394 | } | |
395 | ||
396 | /** | |
397 | * Unregisters an ongoing event | |
398 | * | |
399 | * @param context Context instance | |
400 | * @param event event to unregister | |
401 | */ | |
402 | public static void unregisterOngoing(final Context context, final OngoingEvent event){ | |
403 | final Intent intent=new Intent(context, FonBotMainService.class); | |
404 | intent.setAction(FonBotMainService.ACTION_DELETE_ONGOING); | |
405 | intent.putExtra(FonBotMainService.EXTRA_ONGOING_ID, event.ordinal()); | |
406 | context.startService(intent); | |
407 | } | |
408 | ||
409 | /** | |
410 | * Executes a given command | |
411 | * | |
412 | * @param context Context instance | |
413 | * @param cmd Command to execute | |
414 | * @param args arguments for the command | |
415 | * @param replyTo Address to send replies to | |
416 | */ | |
417 | private static void processCommand(final Context context, final Command cmd,final String[] args,final Address replyTo){ | |
418 | if(Heavy.isCommandDisabled(context, cmd)){ | |
419 | sendMessage(context, replyTo, command_disabled); | |
420 | return; | |
421 | } | |
422 | ||
423 | switch(cmd){ | |
424 | case TOAST: | |
425 | if(args.length < 1 || args.length > 2){ | |
426 | Heavy.help(context, replyTo, toNonNull(Command.TOAST)); | |
427 | break; | |
428 | } | |
429 | ||
430 | if(args.length==1) | |
431 | Heavy.toast(context, replyTo, toNonNull(args[0])); | |
432 | else { | |
433 | try { | |
434 | switch(ToastLength.valueOf(args[1].toUpperCase(Locale.ENGLISH))){ | |
435 | case LONG: | |
436 | Heavy.toast(context, replyTo, toNonNull(args[0]), Toast.LENGTH_LONG); | |
437 | break; | |
438 | case SHORT: | |
439 | Heavy.toast(context, replyTo, toNonNull(args[0]), Toast.LENGTH_SHORT); | |
440 | } | |
441 | } catch(IllegalArgumentException e){ | |
442 | sendMessage(context, replyTo, invalid_length_allowed_values_are, join(", ",toNonNull(ToastLength.values()))); | |
443 | } | |
444 | } | |
445 | break; | |
446 | ||
447 | case ECHO: | |
448 | if(args.length==0){ | |
449 | Heavy.help(context, replyTo, toNonNull(Command.ECHO)); | |
450 | break; | |
451 | } | |
452 | sendMessage(context, replyTo, join(" ",args)); | |
453 | break; | |
454 | ||
455 | case SMS: | |
456 | if(args.length < 2){ | |
457 | Heavy.help(context, replyTo, toNonNull(Command.SMS)); | |
458 | break; | |
459 | } | |
460 | Heavy.sms(context, replyTo, toNonNull(args[0]), join(" ", 1, args)); | |
461 | break; | |
462 | ||
463 | case FLASH: | |
464 | if(args.length != 1){ | |
465 | Heavy.help(context, replyTo, toNonNull(Command.FLASH)); | |
466 | break; | |
467 | } | |
468 | ||
469 | try { | |
470 | Heavy.flash(context, replyTo, OnOff.valueOf(args[0].toUpperCase(Locale.ENGLISH)) == OnOff.ON); | |
471 | } catch(IllegalArgumentException e) { | |
472 | sendMessage(context, replyTo, could_not_parse_argument_allowed_values_are, join(", ", toNonNull(OnOff.values()))); | |
473 | } | |
474 | break; | |
475 | ||
476 | case WIFI: | |
477 | if(args.length>1){ | |
478 | Heavy.help(context, replyTo, toNonNull(Command.WIFI)); | |
479 | break; | |
480 | } | |
481 | if(args.length==0) | |
482 | Heavy.wifi(context, replyTo); | |
483 | else { | |
484 | try { | |
485 | Heavy.wifi(context, replyTo, OnOff.valueOf(args[0].toUpperCase(Locale.ENGLISH)) == OnOff.ON); | |
486 | } catch(IllegalArgumentException e) { | |
487 | sendMessage(context, replyTo, could_not_parse_argument_allowed_values_are, join(", ", toNonNull(OnOff.values()))); | |
488 | } | |
489 | } | |
490 | break; | |
491 | ||
492 | case BLUETOOTH: | |
493 | if(args.length>1){ | |
494 | Heavy.help(context, replyTo, toNonNull(Command.BLUETOOTH)); | |
495 | break; | |
496 | } | |
497 | if(args.length==0) | |
498 | Heavy.bluetooth(context, replyTo); | |
499 | else { | |
500 | try { | |
501 | Heavy.bluetooth(context, replyTo, OnOff.valueOf(args[0].toUpperCase(Locale.ENGLISH)) == OnOff.ON); | |
502 | } catch(IllegalArgumentException e) { | |
503 | sendMessage(context, replyTo, could_not_parse_argument_allowed_values_are, join(", ", toNonNull(OnOff.values()))); | |
504 | } | |
505 | } | |
506 | break; | |
507 | ||
508 | case DIAL: | |
509 | if(args.length!=1){ | |
510 | Heavy.help(context, replyTo, toNonNull(Command.DIAL)); | |
511 | break; | |
512 | } | |
513 | Heavy.dial(context, replyTo, toNonNull(args[0])); | |
514 | break; | |
515 | ||
516 | case RING: | |
517 | if(args.length>1){ | |
518 | Heavy.help(context, replyTo, toNonNull(Command.RING)); | |
519 | break; | |
520 | } | |
521 | if(args.length==0) | |
522 | Heavy.ring(context, replyTo); | |
523 | else { | |
524 | try { | |
525 | Heavy.ring(context, replyTo, OnOff.valueOf(args[0].toUpperCase(Locale.ENGLISH)) == OnOff.ON); | |
526 | } catch(IllegalArgumentException e){ | |
527 | sendMessage(context, replyTo, could_not_parse_argument_allowed_values_are, join(", ", toNonNull(OnOff.values()))); | |
528 | } | |
529 | } | |
530 | break; | |
531 | ||
532 | case SPEAK: | |
533 | if(args.length==0){ | |
534 | Heavy.help(context, replyTo, toNonNull(Command.SPEAK)); | |
535 | break; | |
536 | } | |
537 | Heavy.speak(context, replyTo, join(" ",args)); | |
538 | break; | |
539 | ||
540 | case VIBRATE: | |
541 | if(args.length!=1){ | |
542 | Heavy.help(context, replyTo, toNonNull(Command.VIBRATE)); | |
543 | break; | |
544 | } | |
545 | final long ms; | |
546 | try{ | |
547 | ms=Long.parseLong(args[0]); | |
548 | } catch(NumberFormatException e){ | |
549 | sendMessage(context, replyTo, could_not_parse_ms); | |
550 | break; | |
551 | } | |
552 | Heavy.vibrate(context, replyTo, ms); | |
553 | break; | |
554 | ||
555 | case DIALOG://TODO: Should add an edittext | |
556 | if(args.length<1){ | |
557 | Heavy.help(context, replyTo, toNonNull(Command.DIALOG)); | |
558 | break; | |
559 | } | |
560 | final String[] buttons=new String[args.length-1]; | |
561 | System.arraycopy(args,1,buttons,0,buttons.length); | |
562 | Heavy.dialog(context, replyTo, toNonNull(args[0]), buttons); | |
563 | break; | |
564 | ||
565 | case LOCATION: | |
566 | if(args.length>3||args.length<1){ | |
567 | Heavy.help(context, replyTo, toNonNull(Command.LOCATION)); | |
568 | break; | |
569 | } | |
570 | ||
571 | final @NonNull String provider; | |
572 | try{ | |
573 | switch(LocationProvider.valueOf(args[0].toUpperCase(Locale.ENGLISH))){ | |
574 | case GPS: | |
575 | provider=toNonNull(LocationManager.GPS_PROVIDER); | |
576 | break; | |
577 | case NETWORK: | |
578 | default: | |
579 | provider=toNonNull(LocationManager.NETWORK_PROVIDER); | |
580 | break; | |
581 | } | |
582 | } catch(IllegalArgumentException e){ | |
583 | sendMessage(context, replyTo, cannot_parse_provider_allowed_values_are, join(", ",toNonNull(LocationProvider.values()))); | |
584 | break; | |
585 | } | |
586 | ||
587 | final long minTime; | |
588 | final float minDistance; | |
589 | ||
590 | if(args.length>1) | |
591 | try{ | |
592 | minTime=Long.parseLong(args[1]); | |
593 | } catch (NumberFormatException e){ | |
594 | sendMessage(context, replyTo, cannot_parse_min_time); | |
595 | break; | |
596 | } | |
597 | else | |
598 | minTime=500; | |
599 | ||
600 | if(args.length>2) | |
601 | try{ | |
602 | minDistance=Float.parseFloat(args[2]); | |
603 | } catch (NumberFormatException e){ | |
604 | sendMessage(context, replyTo, cannot_parse_min_distance); | |
605 | break; | |
606 | } | |
607 | else | |
608 | minDistance=0; | |
609 | Heavy.location(context, replyTo, provider, minTime, minDistance); | |
610 | break; | |
611 | ||
612 | case NOLOCATION: | |
613 | Heavy.nolocation(context, replyTo); | |
614 | break; | |
615 | ||
616 | case RINGER: | |
617 | if(args.length>1){ | |
618 | Heavy.help(context, replyTo, toNonNull(Command.RINGER)); | |
619 | break; | |
620 | } | |
621 | if(args.length==0) | |
622 | Heavy.ringer(context, replyTo); | |
623 | else{ | |
624 | try{ | |
625 | final RingerMode rm=RingerMode.valueOf(args[0].toUpperCase(Locale.ENGLISH)); | |
626 | switch(rm){ | |
627 | case NORMAL: | |
628 | Heavy.ringer(context, replyTo, AudioManager.RINGER_MODE_NORMAL); | |
629 | break; | |
630 | case VIBRATE: | |
631 | Heavy.ringer(context, replyTo, AudioManager.RINGER_MODE_VIBRATE); | |
632 | break; | |
633 | case SILENT: | |
634 | Heavy.ringer(context, replyTo, AudioManager.RINGER_MODE_SILENT); | |
635 | break; | |
636 | } | |
637 | } catch (IllegalArgumentException e){ | |
638 | Utils.sendMessage(context, replyTo, invalid_ringer_mode_valid_values_are, join(", ",toNonNull(RingerMode.values()))); | |
639 | } | |
640 | } | |
641 | break; | |
642 | ||
643 | case NCFILE: | |
644 | if(args.length!=3){ | |
645 | Heavy.help(context, replyTo, toNonNull(Command.NCFILE)); | |
646 | break; | |
647 | } | |
648 | ||
649 | final int ncfilePort; | |
650 | try{ | |
651 | ncfilePort=Integer.parseInt(args[2]); | |
652 | } catch (NumberFormatException e){ | |
653 | sendMessage(context, replyTo, cannot_parse_port); | |
654 | break; | |
655 | } | |
656 | Heavy.ncfile(context, replyTo, toNonNull(args[0]), toNonNull(args[1]), ncfilePort); | |
657 | break; | |
658 | ||
659 | case PHOTO: | |
660 | if(args.length!=2){ | |
661 | Heavy.help(context, replyTo, toNonNull(Command.PHOTO)); | |
662 | break; | |
663 | } | |
664 | final int photoPort; | |
665 | try{ | |
666 | photoPort=Integer.parseInt(args[1]); | |
667 | } catch (NumberFormatException e){ | |
668 | sendMessage(context, replyTo, cannot_parse_port); | |
669 | break; | |
670 | } | |
671 | Heavy.photo(context, replyTo, toNonNull(args[0]), photoPort); | |
672 | break; | |
673 | ||
674 | case SETNOTIFICATION: | |
675 | if(args.length!=1){ | |
676 | Heavy.help(context, replyTo, toNonNull(Command.SETNOTIFICATION)); | |
677 | break; | |
678 | } | |
679 | ||
680 | try{ | |
681 | PreferenceManager.getDefaultSharedPreferences(context).edit() | |
682 | .putString(MessageType.valueOf(args[0].toUpperCase(Locale.ENGLISH)).toString(), replyTo.toString()) | |
683 | .commit(); | |
684 | sendMessage(context, replyTo, notification_enabled); | |
685 | } catch (IllegalArgumentException e){ | |
686 | sendMessage(context, replyTo, messagetype_should_be_one_of, join(", ",toNonNull(MessageType.values()))); | |
687 | break; | |
688 | } | |
689 | ||
690 | break; | |
691 | ||
692 | case DELNOTIFICATION: | |
693 | if(args.length!=1){ | |
694 | Heavy.help(context, replyTo, toNonNull(Command.DELNOTIFICATION)); | |
695 | break; | |
696 | } | |
697 | ||
698 | try{ | |
699 | PreferenceManager.getDefaultSharedPreferences(context).edit() | |
700 | .remove(MessageType.valueOf(args[0].toUpperCase(Locale.ENGLISH)).toString()) | |
701 | .commit(); | |
702 | sendMessage(context, replyTo, notification_disabled); | |
703 | } catch (IllegalArgumentException e){ | |
704 | sendMessage(context, replyTo, messagetype_should_be_one_of, join(", ",toNonNull(MessageType.values()))); | |
705 | break; | |
706 | } | |
707 | ||
708 | ||
709 | break; | |
710 | case SETPASSWORD: | |
711 | if(args.length > 1){ | |
712 | Heavy.help(context, replyTo, toNonNull(Command.SETPASSWORD)); | |
713 | break; | |
714 | } | |
715 | ||
716 | try{ | |
717 | if(args.length==0) | |
718 | Heavy.setPassword(context, replyTo); | |
719 | else | |
720 | Heavy.setPassword(context, replyTo, toNonNull(args[0])); | |
721 | } catch (SecurityException e){ | |
722 | sendMessage(context, replyTo, security_exception+e.getMessage()); | |
723 | } | |
724 | break; | |
725 | ||
726 | case WIPE: | |
727 | if(args.length!=2){ | |
728 | Heavy.help(context, replyTo, toNonNull(Command.WIPE)); | |
729 | break; | |
730 | } | |
731 | ||
732 | if(!args[1].equalsIgnoreCase(WIPE_CONFIRM_STRING)){ | |
733 | sendMessage(context, replyTo, the_second_argument_to_wipe_must_be, WIPE_CONFIRM_STRING); | |
734 | break; | |
735 | } | |
736 | ||
737 | try{ | |
738 | Heavy.wipe(context, toNonNull(WipeType.valueOf(args[0].toUpperCase(Locale.ENGLISH)))); | |
739 | } catch (IllegalArgumentException e){ | |
740 | sendMessage(context, replyTo, wipetype_should_be_one_of, join (", ",toNonNull(WipeType.values()))); | |
741 | } catch (SecurityException e){ | |
742 | sendMessage(context, replyTo, security_exception, e.getMessage()); | |
743 | } | |
744 | break; | |
745 | ||
746 | case LOCK: | |
747 | try{ | |
748 | Heavy.lock(context, replyTo); | |
749 | } catch (SecurityException e){ | |
750 | sendMessage(context, replyTo, security_exception, e.getMessage()); | |
751 | } | |
752 | break; | |
753 | ||
754 | case VIEW: | |
755 | if(args.length!=1){ | |
756 | Heavy.help(context, replyTo, toNonNull(Command.VIEW)); | |
757 | break; | |
758 | } | |
759 | ||
760 | Heavy.view(context, replyTo, toNonNull(Uri.parse(args[0]))); | |
761 | break; | |
762 | ||
763 | case PLAY: | |
764 | Heavy.musicPlayerCommand(context, replyTo, "play"); | |
765 | break; | |
766 | ||
767 | case PAUSE: | |
768 | Heavy.musicPlayerCommand(context, replyTo, "pause"); | |
769 | break; | |
770 | ||
771 | case NEXT: | |
772 | Heavy.musicPlayerCommand(context, replyTo, "next"); | |
773 | break; | |
774 | ||
775 | case PREV: | |
776 | Heavy.musicPlayerCommand(context, replyTo, "previous"); | |
777 | break; | |
778 | ||
779 | case BATT: | |
780 | Heavy.batt(context, replyTo); | |
781 | break; | |
782 | ||
783 | case CALLLOG: | |
784 | if (args.length > 1) { | |
785 | Heavy.help(context, replyTo, toNonNull(Command.CALLLOG)); | |
786 | break; | |
787 | } | |
788 | ||
789 | if (args.length == 0) | |
790 | Heavy.calllog(context, replyTo, 5); | |
791 | else { | |
792 | try { | |
793 | Heavy.calllog(context, replyTo, Integer.parseInt(args[0])); | |
794 | } catch (IllegalArgumentException e){ | |
795 | sendMessage(context, replyTo, cannot_parse_count); | |
796 | } | |
797 | } | |
798 | break; | |
799 | ||
800 | case SMSLOG: | |
801 | if (args.length > 1) { | |
802 | Heavy.help(context, replyTo, toNonNull(Command.SMSLOG)); | |
803 | break; | |
804 | } | |
805 | ||
806 | if (args.length == 0) | |
807 | Heavy.smslog(context, replyTo, 5); | |
808 | else { | |
809 | try { | |
810 | Heavy.smslog(context, replyTo, Integer.parseInt(args[0])); | |
811 | } catch (IllegalArgumentException e) { | |
812 | sendMessage(context, replyTo, cannot_parse_count); | |
813 | } | |
814 | } | |
815 | break; | |
816 | ||
817 | case HELP: | |
818 | if(args.length != 1){ | |
819 | Heavy.help(context, replyTo, toNonNull(Command.HELP)); | |
820 | break; | |
821 | } | |
822 | ||
823 | try { | |
824 | Heavy.help(context, replyTo, toNonNull(Command.valueOf(args[0].toUpperCase(Locale.ENGLISH)))); | |
825 | } catch (IllegalArgumentException e) { | |
826 | sendMessage(context, replyTo, no_such_command_command_list, join(", ", toNonNull(Command.values()))); | |
827 | } | |
828 | break; | |
829 | ||
830 | case LS: | |
831 | if(args.length != 1){ | |
832 | Heavy.help(context, replyTo, toNonNull(Command.LS)); | |
833 | break; | |
834 | } | |
835 | ||
836 | Heavy.ls(context, replyTo, toNonNull(args[0])); | |
837 | break; | |
838 | ||
839 | case RM: | |
840 | if(args.length != 1){ | |
841 | Heavy.help(context, replyTo, toNonNull(Command.RM)); | |
842 | break; | |
843 | } | |
844 | ||
845 | Heavy.rm(context, replyTo, toNonNull(args[0])); | |
846 | break; | |
847 | ||
848 | case CONTACTS: | |
849 | if(args.length != 1){ | |
850 | Heavy.help(context, replyTo, toNonNull(Command.CONTACTS)); | |
851 | break; | |
852 | } | |
853 | ||
854 | Heavy.contacts(context, replyTo, toNonNull(args[0])); | |
855 | break; | |
856 | ||
857 | case DISABLE: | |
858 | if(replyTo.protocol != Protocol.LOCAL || args.length != 1){ | |
859 | Heavy.help(context, replyTo, toNonNull(Command.DISABLE)); | |
860 | break; | |
861 | } | |
862 | ||
863 | try{ | |
864 | Heavy.disable(context, replyTo, toNonNull(Command.valueOf(args[0].toUpperCase(Locale.ENGLISH)))); | |
865 | } catch (IllegalArgumentException e){ | |
866 | sendMessage(context, replyTo, no_such_command_command_list, join(", ", toNonNull(Command.values()))); | |
867 | } | |
868 | break; | |
869 | ||
870 | case ENABLE: | |
871 | if(replyTo.protocol != Protocol.LOCAL || args.length != 1){ | |
872 | Heavy.help(context, replyTo, toNonNull(Command.ENABLE)); | |
873 | break; | |
874 | } | |
875 | ||
876 | try{ | |
877 | Heavy.enable(context, replyTo, toNonNull(Command.valueOf(args[0].toUpperCase(Locale.ENGLISH)))); | |
878 | } catch (IllegalArgumentException e){ | |
879 | sendMessage(context, replyTo, no_such_command_command_list, join(", ", toNonNull(Command.values()))); | |
880 | } | |
881 | break; | |
882 | ||
883 | case POLL: | |
884 | if(args.length>1){ | |
885 | Heavy.help(context, replyTo, toNonNull(Command.POLL)); | |
886 | break; | |
887 | } | |
888 | ||
889 | if(args.length==0){ | |
890 | Heavy.poll(context, replyTo); | |
891 | break; | |
892 | } | |
893 | ||
894 | final long interval; | |
895 | try{ | |
896 | interval=Long.parseLong(args[0]); | |
897 | } catch(NumberFormatException e){ | |
898 | sendMessage(context, replyTo, cannot_parse_interval); | |
899 | break; | |
900 | } | |
901 | ||
902 | Heavy.poll(context, replyTo, interval); | |
903 | break; | |
904 | ||
905 | case HANGUP: | |
906 | Heavy.hangup(context, replyTo); | |
907 | break; | |
908 | ||
909 | case ANSWER: | |
910 | Heavy.answer(context, replyTo); | |
911 | break; | |
912 | ||
913 | case LAUNCH: | |
914 | if(args.length!=1){ | |
915 | Heavy.help(context, replyTo, toNonNull(Command.LAUNCH)); | |
916 | break; | |
917 | } | |
918 | Heavy.launch(context, replyTo, toNonNull(args[0])); | |
919 | break; | |
920 | ||
921 | case DATA: | |
922 | if(args.length>1){ | |
923 | Heavy.help(context, replyTo, toNonNull(Command.DATA)); | |
924 | break; | |
925 | } | |
926 | ||
927 | if(args.length==0){ | |
928 | Heavy.data(context, replyTo); | |
929 | break; | |
930 | } | |
931 | try { | |
932 | Heavy.data(context, replyTo, OnOff.valueOf(args[0].toUpperCase(Locale.ENGLISH)) == OnOff.ON); | |
933 | } catch(IllegalArgumentException e) { | |
934 | sendMessage(context, replyTo, could_not_parse_argument_allowed_values_are, join(", ", toNonNull(OnOff.values()))); | |
935 | } | |
936 | break; | |
937 | ||
938 | case GPS: | |
939 | if(args.length>1){ | |
940 | Heavy.help(context, replyTo, toNonNull(Command.GPS)); | |
941 | break; | |
942 | } | |
943 | ||
944 | if(args.length==0){ | |
945 | Heavy.gps(context, replyTo); | |
946 | break; | |
947 | } | |
948 | ||
949 | try { | |
950 | Heavy.gps(context, replyTo, OnOff.valueOf(args[0].toUpperCase(Locale.ENGLISH)) == OnOff.ON); | |
951 | } catch(IllegalArgumentException e) { | |
952 | sendMessage(context, replyTo, could_not_parse_argument_allowed_values_are, join(", ", toNonNull(OnOff.values()))); | |
953 | } | |
954 | break; | |
955 | ||
956 | case GLOCATION: | |
957 | if(args.length>1){ | |
958 | Heavy.help(context, replyTo, toNonNull(Command.GLOCATION)); | |
959 | break; | |
960 | } | |
961 | ||
962 | if(args.length==0){ | |
963 | Heavy.glocation(context, replyTo); | |
964 | break; | |
965 | } | |
966 | ||
967 | try { | |
968 | Heavy.glocation(context, replyTo, OnOff.valueOf(args[0].toUpperCase(Locale.ENGLISH)) == OnOff.ON); | |
969 | } catch(IllegalArgumentException e) { | |
970 | sendMessage(context, replyTo, could_not_parse_argument_allowed_values_are, join(", ", toNonNull(OnOff.values()))); | |
971 | } | |
972 | break; | |
973 | ||
974 | case REBOOT: | |
975 | if(args.length>1){ | |
976 | Heavy.help(context, replyTo, toNonNull(Command.REBOOT)); | |
977 | break; | |
978 | } | |
979 | ||
980 | Heavy.reboot(context, replyTo, args.length==0?null:args[0]); | |
981 | break; | |
982 | ||
983 | case SHUTDOWN: | |
984 | //TODO: implement command | |
985 | break; | |
986 | ||
987 | case NOTIFY: | |
988 | if(args.length!=1 && args.length!=3){ | |
989 | Heavy.help(context, replyTo, toNonNull(Command.NOTIFY)); | |
990 | } | |
991 | ||
992 | final int id; | |
993 | try{ | |
994 | id=Integer.parseInt(args[0]); | |
995 | } catch (NumberFormatException e){ | |
996 | sendMessage(context, replyTo, R.string.could_not_parse_id); | |
997 | break; | |
998 | } | |
999 | ||
1000 | if(args.length==1) | |
1001 | Heavy.notify(context, replyTo, id); | |
1002 | else | |
1003 | Heavy.notify(context, replyTo, id, toNonNull(args[1]), toNonNull(args[2])); | |
1004 | break; | |
1005 | } | |
1006 | ||
1007 | } | |
1008 | ||
1009 | /** | |
1010 | * Executes a given command | |
1011 | * | |
1012 | * @param context Context instance | |
1013 | * @param cmd Command to execute | |
1014 | * @param args arguments for the command | |
1015 | * @param replyTo Address to send replies to | |
1016 | */ | |
1017 | public static void processCommand(final Context context, final String cmd,final String[] args,final Address replyTo){ | |
1018 | final @NonNull Command command; | |
1019 | try{ | |
1020 | command=toNonNull(Command.valueOf(cmd.toUpperCase(Locale.ENGLISH))); | |
1021 | } catch (IllegalArgumentException e){ | |
1022 | sendMessage(context, replyTo, unknown_command, cmd.toUpperCase(Locale.ENGLISH), e.getMessage()); | |
1023 | return; | |
1024 | } | |
1025 | ||
1026 | try{ | |
1027 | processCommand(context, command,args,replyTo); | |
1028 | } catch(Exception e){ | |
1029 | sendMessage(context, replyTo, error_while_processing_command, e.getClass().getName(), e.getMessage()); | |
1030 | Log.w(Utils.class.getName(), "Error while processing command", e); | |
1031 | } | |
1032 | } | |
1033 | } |