1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.R
.string
.*;
4 import static ro
.ieval
.fonbot
.Utils
.toNonNull
;
7 import java
.io
.FileInputStream
;
8 import java
.io
.FileNotFoundException
;
9 import java
.io
.IOException
;
10 import java
.lang
.reflect
.InvocationTargetException
;
11 import java
.lang
.reflect
.Method
;
12 import java
.net
.InetSocketAddress
;
13 import java
.net
.Socket
;
14 import java
.net
.UnknownHostException
;
15 import java
.nio
.channels
.FileChannel
;
16 import java
.nio
.channels
.SocketChannel
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Date
;
20 import org
.eclipse
.jdt
.annotation
.Nullable
;
22 import ro
.ieval
.fonbot
.Utils
.Command
;
23 import ro
.ieval
.fonbot
.Utils
.MessageType
;
24 import ro
.ieval
.fonbot
.Utils
.OngoingEvent
;
25 import ro
.ieval
.fonbot
.Utils
.RingerMode
;
26 import ro
.ieval
.fonbot
.Utils
.WipeType
;
27 import android
.annotation
.SuppressLint
;
28 import android
.app
.AlarmManager
;
29 import android
.app
.NotificationManager
;
30 import android
.app
.PendingIntent
;
31 import android
.app
.admin
.DevicePolicyManager
;
32 import android
.bluetooth
.BluetoothAdapter
;
33 import android
.content
.ActivityNotFoundException
;
34 import android
.content
.Context
;
35 import android
.content
.Intent
;
36 import android
.content
.IntentFilter
;
37 import android
.database
.Cursor
;
38 import android
.graphics
.ImageFormat
;
39 import android
.hardware
.Camera
;
40 import android
.hardware
.Camera
.PictureCallback
;
41 import android
.location
.Location
;
42 import android
.location
.LocationListener
;
43 import android
.location
.LocationManager
;
44 import android
.location
.LocationProvider
;
45 import android
.media
.AudioManager
;
46 import android
.media
.Ringtone
;
47 import android
.media
.RingtoneManager
;
48 import android
.net
.ConnectivityManager
;
49 import android
.net
.Uri
;
50 import android
.net
.wifi
.WifiManager
;
51 import android
.os
.AsyncTask
;
52 import android
.os
.BatteryManager
;
53 import android
.os
.Bundle
;
54 import android
.os
.Handler
;
55 import android
.os
.PowerManager
;
56 import android
.os
.Vibrator
;
57 import android
.preference
.PreferenceManager
;
58 import android
.provider
.BaseColumns
;
59 import android
.provider
.CallLog
.Calls
;
60 import android
.provider
.ContactsContract
.CommonDataKinds
;
61 import android
.provider
.ContactsContract
.CommonDataKinds
.BaseTypes
;
62 import android
.provider
.ContactsContract
.CommonDataKinds
.Phone
;
63 import android
.provider
.ContactsContract
.Contacts
;
64 import android
.provider
.ContactsContract
.Data
;
65 import android
.provider
.Settings
.Secure
;
66 import android
.speech
.tts
.TextToSpeech
;
67 import android
.speech
.tts
.TextToSpeech
.OnInitListener
;
68 import android
.support
.v4
.app
.NotificationCompat
;
69 import android
.telephony
.SmsManager
;
70 import android
.telephony
.TelephonyManager
;
71 import android
.util
.Log
;
72 import android
.view
.SurfaceView
;
73 import android
.widget
.Toast
;
75 import com
.android
.internal
.telephony
.ITelephony
;
78 * Copyright © 2013 Marius Gavrilescu
80 * This file is part of FonBot.
82 * FonBot is free software: you can redistribute it and/or modify
83 * it under the terms of the GNU General Public License as published by
84 * the Free Software Foundation, either version 3 of the License, or
85 * (at your option) any later version.
87 * FonBot is distributed in the hope that it will be useful,
88 * but WITHOUT ANY WARRANTY; without even the implied warranty of
89 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
90 * GNU General Public License for more details.
92 * You should have received a copy of the GNU General Public License
93 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
97 * Implementation of all FonBot commands. The methods of this class do not do argument checking.
99 * @author Marius Gavrilescu <marius@ieval.ro>
103 * LocationListener that sends notifications to the user.
105 * @author Marius Gavrilescu <marius@ieval.ro>
107 private static final class FonBotLocationListener
implements LocationListener
{
108 /** Context instance */
109 private final Context context
;
110 /** Destination address for notifications */
111 private final Address replyTo
;
114 * Construct a FonBotLocationListener.
116 * @param context Context instance
117 * @param replyTo the reply address
119 FonBotLocationListener(final Context context
, final Address replyTo
) {
120 this.context
=context
;
121 this.replyTo
=replyTo
;
125 public void onLocationChanged(@Nullable final Location loc
) {
128 final StringBuilder sb
=new StringBuilder(toNonNull(context
.getString(location
)));
130 sb
.append(toNonNull(context
.getString(latitude
)));
132 sb
.append(loc
.getLatitude());
134 sb
.append(toNonNull(context
.getString(longitude
)));
136 sb
.append(loc
.getLongitude());
138 if(loc
.hasAccuracy()){
140 sb
.append(toNonNull(context
.getString(accuracy
)));
142 sb
.append(loc
.getAccuracy());
145 if(loc
.hasAltitude()){
147 sb
.append(toNonNull(context
.getString(altitude
)));
149 sb
.append(loc
.getAltitude());
152 if(loc
.hasBearing()){
154 sb
.append(toNonNull(context
.getString(bearing
)));
156 sb
.append(loc
.getBearing());
161 sb
.append(toNonNull(context
.getString(speed
)));
163 sb
.append(loc
.getSpeed());
166 final Date locationDate
=new Date(loc
.getTime());
168 sb
.append(toNonNull(context
.getString(at
)));
170 sb
.append(locationDate
.toString());
173 sb
.append("http://openstreetmap.org/?zoom=15&mlat=");
174 sb
.append(loc
.getLatitude());
176 sb
.append(loc
.getLongitude());
177 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), toNonNull(sb
.toString()));
181 public void onProviderDisabled(@Nullable final String provider
) {
182 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), location_provider_disabled
, provider
);
186 public void onProviderEnabled(@Nullable final String provider
) {
187 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), location_provider_enabled
, provider
);
191 public void onStatusChanged(@Nullable final String provider
, final int status
, @Nullable final Bundle extras
) {
194 case LocationProvider
.AVAILABLE
:
195 state
=location_provider_available
;
197 case LocationProvider
.TEMPORARILY_UNAVAILABLE
:
198 state
=location_provider_temporary_unavailable
;
200 case LocationProvider
.OUT_OF_SERVICE
:
201 state
=location_provider_out_of_service
;
204 state
=location_provider_unknown_state
;
206 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), state
, provider
);
210 * Currently active FonBotLocationListener
212 private static FonBotLocationListener locationListener
= null;
215 * AsyncTask that sends a byte[] to a server
217 * @author Marius Gavrilescu <marius@ieval.ro>
220 private static class SendDataAsyncTask
extends AsyncTask
<Void
, Void
, Void
>{
222 * Context instance used by this class
224 private final Context context
;
228 private final String hostname
;
232 private final int port
;
236 private final byte[] data
;
238 * Address for sending back errors and success messages
240 private final Address replyTo
;
243 * Constructs a SendDataAsyncTasks from its parameters
245 * @param context the context
246 * @param replyTo the reply Address
247 * @param hostname the server hostname
248 * @param port the server port
249 * @param data the data to send
251 public SendDataAsyncTask(final Context context
,final Address replyTo
, final String hostname
, final int port
, final byte[] data
) {//NOPMD array is immutable
253 this.context
=context
;
254 this.hostname
=hostname
;
257 this.replyTo
=replyTo
;
261 protected @Nullable Void
doInBackground(@Nullable final Void
... params
) {
264 sock
= new Socket(hostname
, port
);
266 sock
.getOutputStream().write(data
);
267 } catch (IOException e
) {
268 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), error_writing_to_socket
, e
.getMessage());
274 } catch (IOException e
) {
275 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), cannot_close_socket
, e
.getMessage());
278 } catch (UnknownHostException e
) {
279 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), unknown_host
, hostname
);
281 } catch (IOException e
) {
282 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), cannot_connect_to_host_on_port
, hostname
, Integer
.valueOf(port
));
285 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), photo_sent
);
291 * PictureCallback that sends the picture to a server.
293 * @author Marius Gavrilescu <marius@ieval.ro>
295 private static final class FonBotPictureCallback
implements PictureCallback
{
296 /** Server hostname */
297 private final String hostname
;
299 private final int port
;
300 /** Context instance */
301 private final Context context
;
303 private final Address replyTo
;
306 * Construct a FonBotPictureCallback.
308 * @param context Context instance
309 * @param replyTo reply Address
310 * @param hostname server hostname
311 * @param port server port
313 FonBotPictureCallback(final Context context
, final Address replyTo
, final String hostname
, final int port
) {
314 this.hostname
=hostname
;
316 this.context
=context
;
317 this.replyTo
=replyTo
;
321 @SuppressWarnings("hiding")
322 public void onPictureTaken(final @Nullable byte[] data
, final @Nullable Camera camera
) {
323 if(camera
==null || data
==null)
325 camera
.stopPreview();
327 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), sending_photo
);
328 new SendDataAsyncTask(toNonNull(context
), toNonNull(replyTo
), toNonNull(hostname
), port
, data
).execute();
333 * Runnable that takes a screen capture and stores it in a file.
335 private static final class ScreencapRunnable
implements Runnable
{
336 private final Context context
;
337 private final Address replyTo
;
338 private final String filename
;
340 ScreencapRunnable(final Context context
, final Address replyTo
, final String filename
){
341 this.context
=context
;
342 this.replyTo
=replyTo
;
343 this.filename
=filename
;
350 exitCode
=Runtime
.getRuntime().exec(new String
[]{
353 "screencap -p \"" + filename
+ "\""
355 } catch (final Exception e
){
357 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), screencap_failed
);
361 if(exitCode
== 0 && new File(filename
).exists())
362 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), screencap_successful
);
364 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), screencap_failed
);
369 * Get help for a particular command
371 * @param context Context instance
372 * @param replyTo reply Address
373 * @param command command to get help for
375 public static void help(final Context context
, final Address replyTo
, final Command command
){//NOPMD method is a big switch statement. Nothing confusing.
378 Utils
.sendMessage(context
, replyTo
, answer_help
);
381 Utils
.sendMessage(context
, replyTo
, batt_help
);
384 Utils
.sendMessage(context
, replyTo
, bluetooth_help
);
387 Utils
.sendMessage(context
, replyTo
, calllog_help
);
390 Utils
.sendMessage(context
, replyTo
, contacts_help
);
393 Utils
.sendMessage(context
, replyTo
, data_help
);
395 case DELNOTIFICATION
:
396 Utils
.sendMessage(context
, replyTo
, delnotification_help
, Utils
.join(", ", toNonNull(MessageType
.values())));
399 Utils
.sendMessage(context
, replyTo
, dial_help
);
402 Utils
.sendMessage(context
, replyTo
, dialog_help
);
405 Utils
.sendMessage(context
, replyTo
, disable_help
, Utils
.join(", ", toNonNull(Command
.values())));
408 Utils
.sendMessage(context
, replyTo
, echo_help
);
411 Utils
.sendMessage(context
, replyTo
, enable_help
, Utils
.join(", ", toNonNull(Command
.values())));
414 Utils
.sendMessage(context
, replyTo
, flash_help
);
417 Utils
.sendMessage(context
, replyTo
, glocation_help
);
420 Utils
.sendMessage(context
, replyTo
, gps_help
);
423 Utils
.sendMessage(context
, replyTo
, hangup_help
);
426 Utils
.sendMessage(context
, replyTo
, help_help
, Utils
.join(", ",toNonNull(Command
.values())));
429 Utils
.sendMessage(context
, replyTo
, launch_help
);
432 Utils
.sendMessage(context
, replyTo
, location_help
, Utils
.join(", ",toNonNull(Utils
.LocationProvider
.values())));
435 Utils
.sendMessage(context
, replyTo
, lock_help
);
438 Utils
.sendMessage(context
, replyTo
, ls_help
);
441 Utils
.sendMessage(context
, replyTo
, ncfile_help
);
444 Utils
.sendMessage(context
, replyTo
, next_help
);
447 Utils
.sendMessage(context
, replyTo
, nolocation_help
);
450 Utils
.sendMessage(context
, replyTo
, pause_help
);
453 Utils
.sendMessage(context
, replyTo
, photo_help
);
456 Utils
.sendMessage(context
, replyTo
, play_help
);
459 Utils
.sendMessage(context
, replyTo
, poll_help
);
462 Utils
.sendMessage(context
, replyTo
, prev_help
);
465 Utils
.sendMessage(context
, replyTo
, ring_help
);
468 Utils
.sendMessage(context
, replyTo
, ringer_help
, Utils
.join(", ", toNonNull(RingerMode
.values())));
471 Utils
.sendMessage(context
, replyTo
, rm_help
);
473 case SETNOTIFICATION
:
474 Utils
.sendMessage(context
, replyTo
, setnotification_help
, Utils
.join(", ", toNonNull(MessageType
.values())));
477 Utils
.sendMessage(context
, replyTo
, setpassword_help
);
480 Utils
.sendMessage(context
, replyTo
, sms_help
);
483 Utils
.sendMessage(context
, replyTo
, smslog_help
);
486 Utils
.sendMessage(context
, replyTo
, speak_help
);
489 Utils
.sendMessage(context
, replyTo
, toast_help
);
492 Utils
.sendMessage(context
, replyTo
, vibrate_help
);
495 Utils
.sendMessage(context
, replyTo
, view_help
);
498 Utils
.sendMessage(context
, replyTo
, wifi_help
);
501 Utils
.sendMessage(context
, replyTo
, wipe_help
, Utils
.WIPE_CONFIRM_STRING
);
504 Utils
.sendMessage(context
, replyTo
, reboot_help
);
507 Utils
.sendMessage(context
, replyTo
, notify_help
);
510 Utils
.sendMessage(context
, replyTo
, screencap_help
);
513 Utils
.sendMessage(context
, replyTo
, torch_help
);
521 * @see #startCamera(Context, Address)
524 private static Camera camera
;
526 * Ringtone used by the {@link Utils.Command#RING RING} command.
528 * @see #setupRingtone(Context)
530 private static Ringtone ringtone
;
532 * Saved ringer volume.
534 * @see #startAlarm(Context, Address)
535 * @see #stopAlarm(Context, Address)
537 private static int savedRingVolume
;
541 * @see #startAlarm(Context, Address)
542 * @see #stopAlarm(Context, Address)
544 private static int savedRingerMode
;
546 /** Private constructor */
552 * Convert a phone number type to a string
554 * @param context Context instance
555 * @param type phone number type
556 * @param label name of a custom phone type
557 * @return the phone number type
559 private static @Nullable String
phoneNumberType(final Context context
, final int type
, final @Nullable String label
) {
561 case BaseTypes
.TYPE_CUSTOM
:
563 case Phone
.TYPE_ASSISTANT
:
564 return context
.getString(phone_numer_type_assistant
);
565 case Phone
.TYPE_CALLBACK
:
566 return context
.getString(phone_number_type_callback
);
568 return context
.getString(phone_number_type_car
);
569 case Phone
.TYPE_COMPANY_MAIN
:
570 return context
.getString(phone_number_type_company_main
);
571 case Phone
.TYPE_FAX_HOME
:
572 return context
.getString(phone_number_type_home_fax
);
573 case Phone
.TYPE_FAX_WORK
:
574 return context
.getString(phone_number_type_work_fax
);
575 case Phone
.TYPE_HOME
:
576 return context
.getString(phone_number_type_home
);
577 case Phone
.TYPE_ISDN
:
578 return context
.getString(phone_number_type_isdn
);
579 case Phone
.TYPE_MAIN
:
580 return context
.getString(phone_number_type_main
);
582 return context
.getString(phone_number_type_mms
);
583 case Phone
.TYPE_MOBILE
:
584 return context
.getString(phone_number_type_mobile
);
585 case Phone
.TYPE_OTHER
:
586 return context
.getString(phone_number_type_other
);
587 case Phone
.TYPE_OTHER_FAX
:
588 return context
.getString(phone_number_type_other_fax
);
589 case Phone
.TYPE_PAGER
:
590 return context
.getString(phone_number_type_pager
);
591 case Phone
.TYPE_RADIO
:
592 return context
.getString(phone_number_type_radio
);
593 case Phone
.TYPE_TELEX
:
594 return context
.getString(phone_number_type_telex
);
595 case Phone
.TYPE_TTY_TDD
:
596 return context
.getString(phone_number_type_textphone
);
597 case Phone
.TYPE_WORK
:
598 return context
.getString(phone_number_type_work
);
599 case Phone
.TYPE_WORK_MOBILE
:
600 return context
.getString(phone_number_type_work_mobile
);
601 case Phone
.TYPE_WORK_PAGER
:
602 return context
.getString(phone_number_type_work_pager
);
605 return context
.getString(phone_number_type_unknown
, Integer
.valueOf(type
));
609 * Setup the ringtone used by the {@link Utils.Command#RING RING} command
611 * @param context Context
613 private static void setupRingtone(final Context context
){
614 if(ringtone
==null){//NOPMD not supposed to be thread-safe
615 final Uri alert
=RingtoneManager
.getDefaultUri(RingtoneManager
.TYPE_RINGTONE
);
616 ringtone
=RingtoneManager
.getRingtone(context
, alert
);
621 * Make the phone start ringing. Turns up the volume and sets the ringer mode to NORMAL
623 * @param context Context instance
624 * @param replyTo reply Address
626 private static void startAlarm(final Context context
, final Address replyTo
){
627 Utils
.registerOngoing(context
, toNonNull(OngoingEvent
.RING
));
628 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
629 savedRingerMode
=man
.getRingerMode();
630 man
.setRingerMode(AudioManager
.RINGER_MODE_NORMAL
);
631 savedRingVolume
=man
.getStreamVolume(AudioManager
.STREAM_RING
);
632 man
.setStreamVolume(AudioManager
.STREAM_RING
, man
.getStreamMaxVolume(AudioManager
.STREAM_RING
), 0);
633 Utils
.sendMessage(context
, replyTo
, ringing
);
638 * Get a camera instance.
640 * @param context Context instance
641 * @param replyTo reply Address
643 private static void startCamera(final Context context
, final Address replyTo
, final int cameraNumber
){
648 camera
=Camera
.open(cameraNumber
);
649 } catch (Exception ex
){
650 camera
=Camera
.open();
652 } catch (Exception e
){
653 Utils
.sendMessage(context
, replyTo
, cannot_grab_camera
);
658 * Make the phone stop ringing. Restores the volume and ringer mode.
660 * @param context Context instance
661 * @param replyTo reply Address
663 private static void stopAlarm(final Context context
, final Address replyTo
){
664 Utils
.unregisterOngoing(context
, toNonNull(OngoingEvent
.RING
));
665 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
666 Utils
.sendMessage(context
, replyTo
, no_longer_ringing
);
668 man
.setStreamVolume(AudioManager
.STREAM_RING
, savedRingVolume
, 0);
669 man
.setRingerMode(savedRingerMode
);
673 * Release the previously grabbed camera instance
675 * @see #startCamera(Context, Address)
677 private static void stopCamera(){
685 * Send battery status information to an Address
687 * @param context Context instance
688 * @param replyTo destination Address
690 * @see #describeBatteryLevel(Context, Address, MessageType)
692 public static void batt(final Context context
, final Address replyTo
){
693 describeBatteryLevel(context
, replyTo
, null);
697 * Show the bluetooth radio status.
699 * @param context Context instance
700 * @param replyTo destination Address
702 public static void bluetooth(final Context context
, final Address replyTo
) {
703 final BluetoothAdapter adapter
=BluetoothAdapter
.getDefaultAdapter();
705 Utils
.sendMessage(context
, replyTo
, no_bluetooth_adapter
);
709 if(adapter
.isEnabled())
710 Utils
.sendMessage(context
, replyTo
, bluetooth_on
);
712 Utils
.sendMessage(context
, replyTo
, bluetooth_off
);
716 * Set the bluetooth radio status.
718 * @param context Context instance
719 * @param replyTo destination Address
720 * @param on the requested radio status
722 public static void bluetooth(final Context context
, final Address replyTo
, final boolean on
){
723 final BluetoothAdapter adapter
=BluetoothAdapter
.getDefaultAdapter();
725 Utils
.sendMessage(context
, replyTo
, no_bluetooth_adapter
);
731 Utils
.sendMessage(context
, replyTo
, enabling_bluetooth
);
735 Utils
.sendMessage(context
, replyTo
, disabling_bluetooth
);
740 * Cancel an ongoing event.
742 * @param context Context instance
743 * @param event the event to cancel
745 public static void cancelOngoing(final Context context
, final OngoingEvent event
){
748 nolocation(context
, toNonNull(Address
.BLACKHOLE
));
751 ring(context
, toNonNull(Address
.BLACKHOLE
), false);
757 * Send the last calls to an Address.
759 * @param context Context instance
760 * @param replyTo destination Address
761 * @param numCalls how many calls to send
763 public static void calllog(final Context context
, final Address replyTo
, final int numCalls
) {
764 final String
[] fields
= {
765 Calls
.TYPE
, Calls
.NUMBER
, Calls
.CACHED_NAME
, Calls
.DURATION
, Calls
.DATE
768 final Cursor cursor
= context
.getContentResolver().query(
776 if (cursor
.moveToFirst()) {
778 final StringBuilder sb
=new StringBuilder(50);//NOPMD different strings
779 final int type
=cursor
.getInt(0);
780 final String from
=cursor
.getString(1);
783 case Calls
.INCOMING_TYPE
:
784 sb
.append(context
.getString(incoming_call_from
, from
));
786 case Calls
.MISSED_TYPE
:
787 sb
.append(context
.getString(missed_call_from
, from
));
789 case Calls
.OUTGOING_TYPE
:
790 sb
.append(context
.getString(outgoing_call_to
, from
));
794 if (cursor
.getString(2) != null)
795 sb
.append('(').append(cursor
.getString(2)).append(") ");
797 sb
.append(context
.getString(duration_seconds_starting_at
,
798 Long
.valueOf(cursor
.getLong(3)),
799 new Date(cursor
.getLong(4))));
801 Utils
.sendMessage(context
, replyTo
, toNonNull(sb
.toString()));
802 } while (cursor
.moveToNext() && cursor
.getPosition() < numCalls
);
809 * Search for contacts by name/nickname and send matching entries to an Address.
811 * @param context Context instance
812 * @param replyTo destination Address
813 * @param name name/nickname part to search for
815 @SuppressLint("StringFormatMatches")
816 public static void contacts(final Context context
, final Address replyTo
, final String name
){
817 final Cursor cursor
=context
.getContentResolver().query(Uri
.withAppendedPath(
818 Contacts
.CONTENT_FILTER_URI
, name
),
819 new String
[]{Contacts
.DISPLAY_NAME
, BaseColumns
._ID
, Contacts
.LOOKUP_KEY
},
820 null, null, Contacts
.DISPLAY_NAME
);
822 if(cursor
.getCount()==0)
823 Utils
.sendMessage(context
, replyTo
, no_matching_contacts_found
);
825 while(cursor
.moveToNext()){
826 final String
[] fields
= {
827 CommonDataKinds
.Phone
.NUMBER
,
828 CommonDataKinds
.Phone
.TYPE
,
829 CommonDataKinds
.Phone
.LABEL
,
832 final Cursor inCursor
=context
.getContentResolver().query(Data
.CONTENT_URI
,
834 Data
.CONTACT_ID
+" = ? AND "+Data
.MIMETYPE
+ " = ?",
835 new String
[]{Long
.toString(cursor
.getLong(1)), CommonDataKinds
.Phone
.CONTENT_ITEM_TYPE
},
836 CommonDataKinds
.Phone
.LABEL
);
838 while(inCursor
.moveToNext())
839 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(contact_info
,
841 inCursor
.getString(0),
842 phoneNumberType(context
, inCursor
.getInt(1), inCursor
.getString(2)))));
851 * Send battery status information to an Address or as a notification
853 * @param context Context instance
854 * @param replyTo Address to send the information to, if sending to a direct address. Null otherwise.
855 * @param type Notification type, if sending as a notification. Null otherwise.
857 public static void describeBatteryLevel(final Context context
, final @Nullable Address replyTo
, final @Nullable MessageType type
) {
858 if(replyTo
==null&&type
==null)
860 final Intent intent
=context
.registerReceiver(null, new IntentFilter(Intent
.ACTION_BATTERY_CHANGED
));
863 final double level
=intent
.getIntExtra(BatteryManager
.EXTRA_LEVEL
, 0);
864 final int scale
=intent
.getIntExtra(BatteryManager
.EXTRA_SCALE
, 100);
865 final int plugged
=intent
.getIntExtra(BatteryManager
.EXTRA_PLUGGED
, 0);
866 final int status
=intent
.getIntExtra(BatteryManager
.EXTRA_STATUS
, BatteryManager
.BATTERY_STATUS_UNKNOWN
);
867 final int temp
=intent
.getIntExtra(BatteryManager
.EXTRA_TEMPERATURE
, 0);
868 final int volt
=intent
.getIntExtra(BatteryManager
.EXTRA_VOLTAGE
, 0);
870 final StringBuilder sb
=new StringBuilder(100);
871 sb
.append(context
.getString(battery_level
, Double
.valueOf(level
*100/scale
)));
875 sb
.append(context
.getString(not_plugged_in
));
877 case BatteryManager
.BATTERY_PLUGGED_AC
:
878 sb
.append(context
.getString(plugged_in_ac
));
880 case BatteryManager
.BATTERY_PLUGGED_USB
:
881 sb
.append(context
.getString(plugged_in_usb
));
883 case BatteryManager
.BATTERY_PLUGGED_WIRELESS
:
884 sb
.append(context
.getString(plugged_in_wireless
));
889 case BatteryManager
.BATTERY_STATUS_CHARGING
:
890 sb
.append(context
.getString(status_charging
));
892 case BatteryManager
.BATTERY_STATUS_DISCHARGING
:
893 sb
.append(context
.getString(status_discharging
));
895 case BatteryManager
.BATTERY_STATUS_FULL
:
896 sb
.append(context
.getString(status_full
));
898 case BatteryManager
.BATTERY_STATUS_NOT_CHARGING
:
899 sb
.append(context
.getString(status_not_charging
));
901 case BatteryManager
.BATTERY_STATUS_UNKNOWN
:
902 sb
.append(context
.getString(status_unknown
));
906 sb
.append(context
.getString(temperature
, Integer
.valueOf(temp
)));
908 sb
.append(context
.getString(voltage
, Integer
.valueOf(volt
)));
910 Utils
.sendMessage(context
, toNonNull(replyTo
), toNonNull(sb
.toString()));
912 Utils
.sendMessage(context
, type
, toNonNull(sb
.toString()));
916 * Dial a phone number.
918 * @param context Context instance
919 * @param replyTo reply Address
920 * @param nr phone number to dial
922 public static void dial(final Context context
, final Address replyTo
, final String nr
){
923 final Intent intent
=new Intent(Intent
.ACTION_CALL
,Uri
.parse("tel:"+nr
));
924 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
925 final String name
=Utils
.callerId(context
, nr
);
927 Utils
.sendMessage(context
, replyTo
, dialing
, nr
);
929 Utils
.sendMessage(context
, replyTo
, dialing
, nr
+" ("+name
+")");
930 context
.startActivity(intent
);
934 * Show a dialog with a message and a list of buttons.
936 * @param context Context instance
937 * @param replyTo reply Address
938 * @param message dialog message
939 * @param buttons dialog buttons
941 public static void dialog(final Context context
, final Address replyTo
, final String message
, final String
[] buttons
){
942 final Intent intent
=new Intent(context
, DialogActivity
.class);
943 intent
.putExtra(DialogActivity
.EXTRA_MESSAGE
, message
);
944 intent
.putExtra(DialogActivity
.EXTRA_BUTTONS
, buttons
);
945 intent
.putExtra(DialogActivity
.EXTRA_REPLYTO
, replyTo
.toString());
947 Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
|
948 Intent
.FLAG_ACTIVITY_NEW_TASK
|
949 Intent
.FLAG_ACTIVITY_NO_USER_ACTION
|
950 Intent
.FLAG_FROM_BACKGROUND
);
951 Utils
.sendMessage(context
, toNonNull(replyTo
), showing_dialog
);
952 context
.startActivity(intent
);
956 * Turns the flashlight on or off.
958 * @param context Context instance
959 * @param replyTo reply Address
960 * @param on requested flashlight state
962 public static void flash(final Context context
, final Address replyTo
, final boolean on
){
963 startCamera(context
, replyTo
, 0);
966 final Camera
.Parameters parms
=camera
.getParameters();
968 parms
.setFlashMode(Camera
.Parameters
.FLASH_MODE_TORCH
);
969 camera
.setParameters(parms
);
971 parms
.setFlashMode(Camera
.Parameters
.FLASH_MODE_AUTO
);
972 camera
.setParameters(parms
);
978 * Start sending location updates to an Address.
980 * @param context Context instance
981 * @param replyTo destination Address
982 * @param provider LocationProvider
983 * @param minTime minimum time between two consecutive updates (in ms)
984 * @param minDistance minimum distance between two consecutive updates (in meters)
986 * @see LocationManager#requestLocationUpdates(String, long, float, LocationListener)
988 public static void location(final Context context
, final Address replyTo
, final String provider
,final long minTime
,final float minDistance
){
989 final LocationManager man
=(LocationManager
) context
.getApplicationContext().getSystemService(Context
.LOCATION_SERVICE
);
990 if(locationListener
!=null)
991 nolocation(context
, toNonNull(Address
.BLACKHOLE
));
992 Utils
.registerOngoing(context
, toNonNull(OngoingEvent
.LOCATION
));
993 locationListener
=new FonBotLocationListener(context
, replyTo
);
994 man
.removeUpdates(locationListener
);
995 final Location lastKnownLocation
=man
.getLastKnownLocation(provider
);
996 if(lastKnownLocation
!=null){
997 Utils
.sendMessage(context
, replyTo
, last_known_location
);
998 locationListener
.onLocationChanged(lastKnownLocation
);
1000 Utils
.sendMessage(context
, replyTo
, listening_for_location_updates
);
1001 man
.requestLocationUpdates(provider
, minTime
, minDistance
, locationListener
);
1007 * @param context Context instance
1008 * @param replyTo reply Address
1010 public static void lock(final Context context
, final Address replyTo
) {
1011 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1013 Utils
.sendMessage(context
, replyTo
, device_locked
);
1017 * Send a command to a running instance of the music player
1019 * @param context Context instance
1020 * @param replyTo reply Address
1021 * @param command command to send
1023 public static void musicPlayerCommand(final Context context
, final Address replyTo
, final String command
) {
1024 final Intent intent
=new Intent("com.android.music.musicservicecommand");
1025 intent
.putExtra("command", command
);
1026 context
.sendBroadcast(intent
);
1027 Utils
.sendMessage(context
, replyTo
, command_sent
);
1031 * Send a file to a server.
1033 * @param context Context instance
1034 * @param replyTo reply Address
1035 * @param filename file to send
1036 * @param hostname server hostname
1037 * @param port server port
1039 public static void ncfile(final Context context
, final Address replyTo
, final String filename
,final String hostname
,final int port
){
1040 new AsyncTask
<Void
, Void
, Void
>() {
1042 protected @Nullable Void
doInBackground(@Nullable final Void
... params
) {
1043 final FileChannel in
;
1045 in
=new FileInputStream(filename
).getChannel();
1046 } catch (final FileNotFoundException e
){
1047 Utils
.sendMessage(context
, replyTo
, file_not_found
, filename
);
1050 final SocketChannel sock
;
1052 sock
= SocketChannel
.open(new InetSocketAddress(hostname
, port
));
1053 } catch (final IOException e
){
1054 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(
1055 cannot_connect_to_host_on_port
, hostname
, Integer
.valueOf(port
))));
1058 } catch (IOException ex
) {
1065 in
.transferTo(0, in
.size(), sock
);
1066 } catch (final IOException e
){
1067 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(
1068 io_error
, e
.getMessage())));
1072 } catch (IOException e
){
1077 } catch(IOException e
){
1085 protected void onPostExecute(@Nullable final Void result
) {
1086 Utils
.sendMessage(context
, replyTo
, file_sent
);
1092 * Stop sending location updates.
1094 * @param context Context instance
1095 * @param replyTo reply Address
1097 public static void nolocation(final Context context
, final Address replyTo
){
1098 Utils
.unregisterOngoing(context
, toNonNull(OngoingEvent
.LOCATION
));
1099 final LocationManager man
=(LocationManager
) context
.getApplicationContext().getSystemService(Context
.LOCATION_SERVICE
);
1100 man
.removeUpdates(locationListener
);
1101 locationListener
=null;
1102 Utils
.sendMessage(context
, replyTo
, no_longer_listening_for_location_updates
);
1106 * Take a photo and send it to a server.
1108 * @param context Context instance
1109 * @param replyTo reply Address
1110 * @param cameraNumber camera to take photo with
1111 * @param hostname server hostname
1112 * @param port server port
1114 public static void photo(final Context context
, final Address replyTo
, final int cameraNumber
, final String hostname
, final int port
){
1115 startCamera(context
, replyTo
, cameraNumber
);
1118 final Camera
.Parameters parms
=camera
.getParameters();
1119 parms
.setJpegQuality(70);
1120 parms
.setPictureFormat(ImageFormat
.JPEG
);
1121 camera
.setParameters(parms
);
1123 final SurfaceView fakeView
=new SurfaceView(context
);
1125 camera
.setPreviewDisplay(fakeView
.getHolder());
1126 } catch (IOException e
) {
1127 Utils
.sendMessage(context
, replyTo
, error_setting_preview_display
);
1130 camera
.startPreview();
1131 final Handler handler
=new Handler();
1133 new Thread(new Runnable() {
1138 } catch (InterruptedException e
) {
1142 handler
.post(new Runnable() {
1145 camera
.takePicture(null, null, new FonBotPictureCallback(context
, replyTo
, hostname
, port
));
1153 * Send a directory listing to an Address
1155 * @param context Context instance
1156 * @param replyTo destination Address
1157 * @param directory directory to list
1159 public static void ls(final Context context
, final Address replyTo
, final String directory
) {
1160 final File
[] files
=new File(directory
).listFiles();
1162 Utils
.sendMessage(context
, replyTo
, string_is_not_a_directory
, directory
);
1166 final StringBuilder sb
=new StringBuilder(context
.getString(files_in_directory
,directory
));
1167 for(final File file
: files
){
1168 sb
.append(file
.getName());
1169 if(file
.isDirectory())
1174 Utils
.sendMessage(context
, replyTo
, toNonNull(sb
.toString()));
1178 * Make the phone start ringing if it is not ringing or stop ringing if it is.
1180 * @param context Context instance
1181 * @param replyTo reply Address
1183 public static void ring(final Context context
, final Address replyTo
){
1184 setupRingtone(context
);
1186 Utils
.sendMessage(context
, replyTo
, no_ringtone_found
);
1189 if(ringtone
.isPlaying())
1190 stopAlarm(context
, replyTo
);
1192 startAlarm(context
, replyTo
);
1196 * Make the phone start/stop ringing.
1198 * @param context Context instance
1199 * @param replyTo reply Address
1200 * @param on true if the phone should start ringing, false otherwise
1202 public static void ring(final Context context
, final Address replyTo
, final boolean on
){
1203 setupRingtone(context
);
1205 Utils
.sendMessage(context
, replyTo
, no_ringtone_found
);
1208 if(on
&&!ringtone
.isPlaying())
1209 startAlarm(context
, replyTo
);
1210 else if(ringtone
.isPlaying()&&!on
)
1211 stopAlarm(context
, replyTo
);
1215 * Send the current ringer mode to an Address
1217 * @param context Context instance
1218 * @param replyTo destination Address
1220 public static void ringer(final Context context
, final Address replyTo
){
1221 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
1222 switch(man
.getRingerMode()){
1223 case AudioManager
.RINGER_MODE_NORMAL
:
1224 Utils
.sendMessage(context
, replyTo
, ringer_mode_normal
);
1226 case AudioManager
.RINGER_MODE_VIBRATE
:
1227 Utils
.sendMessage(context
, replyTo
, ringer_mode_vibrate
);
1229 case AudioManager
.RINGER_MODE_SILENT
:
1230 Utils
.sendMessage(context
, replyTo
, ringer_mode_silent
);
1233 Utils
.sendMessage(context
, replyTo
, unknown_ringer_mode
);
1238 * Set the ringer mode.
1240 * @param context Context instance
1241 * @param replyTo reply Address
1242 * @param ringerMode requested ringer mode
1244 * @see Utils.RingerMode
1246 public static void ringer(final Context context
, final Address replyTo
, final int ringerMode
){
1247 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
1248 man
.setRingerMode(ringerMode
);
1249 ringer(context
, replyTo
);
1253 * Remove a file or empty directory.
1255 * @param context Context instance
1256 * @param replyTo reply Address
1257 * @param filename file/empty directory to delete
1259 public static void rm(final Context context
, final Address replyTo
, final String filename
){
1260 if(new File(filename
).delete())
1261 Utils
.sendMessage(context
, replyTo
, file_deleted
);
1263 Utils
.sendMessage(context
, replyTo
, error_while_deleting_file
);
1267 * Clear the keyguard password.
1269 * @param context Context instance
1270 * @param replyTo reply Address
1271 * @throws SecurityException if FonBot does not have device administration permissions
1273 public static void setPassword(final Context context
, final Address replyTo
) throws SecurityException
{
1274 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1276 dpm
.resetPassword("", 0);
1277 Utils
.sendMessage(context
, replyTo
, password_cleared
);
1281 * Change the keyguard password.
1283 * @param context Context instance
1284 * @param replyTo reply Address
1285 * @param password new password
1286 * @throws SecurityException if FonBot does not have device administration permissions
1288 public static void setPassword(final Context context
, final Address replyTo
, final String password
) throws SecurityException
{
1289 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1291 dpm
.resetPassword(password
, 0);
1292 Utils
.sendMessage(context
, replyTo
, password_set
);
1296 * Send a text message.
1298 * @param context Context instance
1299 * @param replyTo reply Address
1300 * @param destination destination phone number
1301 * @param text text message contents
1303 public static void sms(final Context context
, final Address replyTo
, final String destination
, final String text
){
1304 final SmsManager manager
=SmsManager
.getDefault();
1305 final ArrayList
<String
> messages
=manager
.divideMessage(text
);
1306 if(messages
.size() > 1)
1307 Utils
.sendMessage(context
, replyTo
, message_was_split_into_parts
, Integer
.valueOf(messages
.size()));
1309 final ArrayList
<PendingIntent
> sents
=new ArrayList
<PendingIntent
>(messages
.size());
1310 final ArrayList
<PendingIntent
> delivereds
=new ArrayList
<PendingIntent
>(messages
.size());
1312 final String name
=Utils
.callerId(context
, destination
);
1313 final String fullDestination
;
1315 fullDestination
=destination
;
1317 fullDestination
=destination
+" ("+name
+")";
1319 for(int i
=0;i
<messages
.size();i
++){
1320 final Intent sent
=new Intent(context
,SmsStatusReceiver
.class);
1321 sent
.putExtra(SmsStatusReceiver
.EXTRA_DESTINATION
, fullDestination
);
1322 sent
.putExtra(SmsStatusReceiver
.EXTRA_PART
, i
+1);
1323 sent
.putExtra(SmsStatusReceiver
.EXTRA_TOTAL
, messages
.size());
1324 sent
.putExtra(SmsStatusReceiver
.EXTRA_REPLY_TO
, replyTo
.toString());
1325 sent
.setAction(SmsStatusReceiver
.SENT_ACTION
+i
);//actions must be unique
1326 sents
.add(PendingIntent
.getBroadcast(context
, 0, sent
, PendingIntent
.FLAG_UPDATE_CURRENT
));
1328 final Intent delivered
=new Intent(context
, SmsStatusReceiver
.class);
1329 delivered
.putExtra(SmsStatusReceiver
.EXTRA_DESTINATION
, fullDestination
);
1330 delivered
.putExtra(SmsStatusReceiver
.EXTRA_PART
, i
+1);
1331 delivered
.putExtra(SmsStatusReceiver
.EXTRA_TOTAL
, messages
.size());
1332 delivered
.putExtra(SmsStatusReceiver
.EXTRA_REPLY_TO
, replyTo
.toString());
1333 delivered
.setAction(SmsStatusReceiver
.DELIVERED_ACTION
+i
);//actions must be unique
1334 delivereds
.add(PendingIntent
.getBroadcast(context
, 0, delivered
, PendingIntent
.FLAG_UPDATE_CURRENT
));
1337 Log
.d(Heavy
.class.getName(), "Sending sms to "+destination
);
1338 manager
.sendMultipartTextMessage(destination
, null, messages
, sents
, delivereds
);
1342 * Send the last SMSes to an Address.
1344 * @param context Context instance
1345 * @param replyTo destination Address
1346 * @param numSms how many SMSes to send
1348 public static void smslog(final Context context
, final Address replyTo
, final int numSms
) {
1349 final String
[] fields
= {"type","address", "body", "date"};
1351 final Cursor cursor
= context
.getContentResolver().query (
1352 Uri
.parse("content://sms"),
1359 if (cursor
.moveToFirst()) {
1361 final String fromNumber
=cursor
.getString(1);
1363 final String name
=Utils
.callerId(context
, Utils
.toNonNull(fromNumber
));
1367 from
=fromNumber
+" ("+name
+')';
1368 final String message
=cursor
.getString(2).replace("\n", "\n ");
1369 final Date date
=new Date(cursor
.getLong(3));
1371 if(cursor
.getInt(0)==1)
1372 Utils
.sendMessage(context
, replyTo
, incoming_message
, from
, message
, date
);
1374 Utils
.sendMessage(context
, replyTo
, outgoing_message
, from
, message
, date
);
1375 } while (cursor
.moveToNext() && cursor
.getPosition() < numSms
);
1381 /** TTS instance, only used by {@link #speak(Context, Address, String)} */
1382 private static TextToSpeech tts
;
1385 * Speak a String using the text-to-speech engine.
1387 * @param context Context instance
1388 * @param replyTo reply Address
1389 * @param text text to speak
1391 public static void speak(final Context context
, final Address replyTo
, final String text
){
1392 tts
=new TextToSpeech(context
, new OnInitListener() {
1394 public void onInit(final int status
) {
1395 if(status
==TextToSpeech
.SUCCESS
){
1396 Utils
.sendMessage(context
, replyTo
, speaking
);
1397 tts
.speak(text
, TextToSpeech
.QUEUE_ADD
, null);
1399 Utils
.sendMessage(context
, replyTo
, tts_engine_not_available
);
1405 * Show a toast notification with the default duration.
1407 * @param context Context instance
1408 * @param replyTo reply Address
1409 * @param text toast text
1411 public static void toast(final Context context
, final Address replyTo
, final String text
){
1412 toast(context
, replyTo
, text
, Toast
.LENGTH_SHORT
);
1416 * Show a toast notification.
1418 * @param context Context instance
1419 * @param replyTo reply Address
1420 * @param text toast text
1421 * @param duration toast duration
1423 public static void toast(final Context context
, final Address replyTo
, final String text
, final int duration
){
1424 Toast
.makeText(context
,text
,duration
).show();
1425 Utils
.sendMessage(context
, replyTo
, toast_shown
);
1429 * Make the phone vibrate for a number of milliseconds.
1431 * @param context Context instance
1432 * @param replyTo reply Address
1433 * @param ms vibrate duration, in milliseconds
1435 public static void vibrate(final Context context
, final Address replyTo
, final long ms
){
1436 final Vibrator v
=(Vibrator
) context
.getSystemService(Context
.VIBRATOR_SERVICE
);
1437 Utils
.sendMessage(context
, replyTo
, vibrating
);
1442 * View an URI in an appropriate activity.
1444 * @param context Context instance
1445 * @param replyTo reply Address
1446 * @param uri URI to view
1448 public static void view(final Context context
, final Address replyTo
, final Uri uri
) {
1450 final Intent intent
=new Intent(Intent
.ACTION_VIEW
);
1451 intent
.setData(uri
);
1452 intent
.setFlags(Intent
.FLAG_FROM_BACKGROUND
|Intent
.FLAG_ACTIVITY_NEW_TASK
);
1453 context
.startActivity(intent
);
1454 Utils
.sendMessage(context
, replyTo
, url_opened
);
1455 } catch(ActivityNotFoundException e
){
1456 Utils
.sendMessage(context
, replyTo
, no_activity_found_for_this_url
);
1457 } catch(Exception e
){
1458 Utils
.sendMessage(context
, replyTo
, invalid_url
);
1463 * Get the current WiFi state.
1465 * @param context Context instance
1466 * @param replyTo reply Address
1468 public static void wifi(final Context context
, final Address replyTo
){
1469 final WifiManager man
=(WifiManager
) context
.getSystemService(Context
.WIFI_SERVICE
);
1470 if(man
.isWifiEnabled())
1471 Utils
.sendMessage(context
, replyTo
, wifi_on
);
1473 Utils
.sendMessage(context
, replyTo
, wifi_off
);
1477 * Set the WiFi state.
1479 * @param context Context instance
1480 * @param replyTo reply Address
1481 * @param on the requested WiFi state
1483 public static void wifi(final Context context
, final Address replyTo
, final boolean on
){
1484 final WifiManager man
=(WifiManager
) context
.getSystemService(Context
.WIFI_SERVICE
);
1485 man
.setWifiEnabled(on
);
1487 Utils
.sendMessage(context
, replyTo
, enabling_wifi
);
1489 Utils
.sendMessage(context
, replyTo
, disabling_wifi
);
1493 * Factory reset the phone, optionally deleting the SD card too.
1495 * @param context Context instance
1496 * @param type {@link Utils.WipeType} instance
1497 * @throws SecurityException if FonBot does not have device administration permissions
1499 @SuppressLint("InlinedApi")
1500 public static void wipe(final Context context
, final WipeType type
) throws SecurityException
{
1501 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1508 dpm
.wipeData(DevicePolicyManager
.WIPE_EXTERNAL_STORAGE
);
1514 * Disable a Command. The command cannot be used until enabled again with the {@link Utils.Command#ENABLE ENABLE} command.
1516 * @param context Context instance
1517 * @param replyTo reply Address
1518 * @param command Command to disable
1520 public static void disable(final Context context
, final Address replyTo
, final Command command
){
1521 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
1522 .putBoolean(command
+"disabled", true).commit();
1523 Utils
.sendMessage(context
, replyTo
, command_disabled
, command
);
1527 * Re-enable a disabled Command.
1529 * @param context Context instance
1530 * @param replyTo reply Address
1531 * @param command Command to re-enable
1533 public static void enable(final Context context
, final Address replyTo
, final Command command
){
1534 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
1535 .remove(command
+"disabled").commit();
1536 Utils
.sendMessage(context
, replyTo
, command_enabled
, command
);
1541 * Check whether a Command is disabled.
1543 * @param context Context instance
1544 * @param command Command to check
1545 * @return true if the Command is disabled, false otherwise
1547 public static boolean isCommandDisabled(final Context context
, final Command command
){
1548 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean(command
+"disabled", false);
1552 * Poll the server for pending commands.
1554 * @param context Context instance
1555 * @param replyTo reply Address
1557 public static void poll(final Context context
, final Address replyTo
) {
1558 Utils
.sendMessage(context
, replyTo
, polling_server
);
1559 context
.startService(new Intent(context
, FonBotMainService
.class));
1563 * Get an instance of {@link ITelephony}
1565 * @param context Context instance
1566 * @return an instance of {@link ITelephony}
1567 * @throws NoSuchMethodException thrown by reflection
1568 * @throws IllegalArgumentException thrown by reflection
1569 * @throws IllegalAccessException thrown by reflection
1570 * @throws InvocationTargetException thrown by reflection
1572 private static ITelephony
getITelephony(final Context context
) throws NoSuchMethodException
, IllegalArgumentException
, IllegalAccessException
, InvocationTargetException
{
1573 final TelephonyManager man
=(TelephonyManager
) context
.getSystemService(Context
.TELEPHONY_SERVICE
);
1574 final Method m
=TelephonyManager
.class.getDeclaredMethod("getITelephony");
1575 m
.setAccessible(true);
1576 return toNonNull((ITelephony
) m
.invoke(man
));
1580 * Hang up the phone.
1582 * @param context Context instance
1583 * @param replyTo reply Address
1585 public static void hangup(final Context context
, final Address replyTo
){
1587 getITelephony(context
).endCall();
1588 } catch(Exception e
){
1589 Utils
.sendMessage(context
, replyTo
, exception_while_hanging_up_call
,
1590 e
.getClass().getName(), e
.getMessage());
1595 * Answer the phone if it is ringing.
1597 * @param context Context instance
1598 * @param replyTo reply Address
1600 public static void answer(final Context context
, final Address replyTo
){
1602 getITelephony(context
).answerRingingCall();
1603 } catch(Exception e
){
1604 Utils
.sendMessage(context
, replyTo
, exception_while_answering_call
,
1605 e
.getClass().getName(), e
.getMessage());
1612 * @param context Context instance
1613 * @param replyTo reply Address
1614 * @param pkg name of the package to launch
1616 public static void launch(final Context context
, final Address replyTo
, final String pkg
){
1617 final Intent intent
=context
.getPackageManager().getLaunchIntentForPackage(pkg
);
1619 Utils
.sendMessage(context
, replyTo
, no_such_package
);
1622 context
.startActivity(intent
);
1623 Utils
.sendMessage(context
, replyTo
, app_launched
);
1627 * Get the mobile data enabled status.
1629 * @param context Context instance
1630 * @param replyTo reply Address
1632 public static void data(final Context context
, final Address replyTo
){
1634 final ConnectivityManager man
=(ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
1635 final Method m
=ConnectivityManager
.class.getDeclaredMethod("getMobileDataEnabled");
1636 m
.setAccessible(true);
1637 if(((Boolean
)m
.invoke(man
)).booleanValue())
1638 Utils
.sendMessage(context
, replyTo
, data_on
);
1640 Utils
.sendMessage(context
, replyTo
, data_off
);
1641 } catch(Exception e
){
1642 Utils
.sendMessage(context
, replyTo
, exception_while_determining_data_state
,
1643 e
.getClass().getName(), e
.getMessage());
1648 * Set the mobile data enabled status.
1650 * @param context Context instance
1651 * @param replyTo reply Address
1652 * @param enable whether to enable mobile data
1654 public static void data(final Context context
, final Address replyTo
, final boolean enable
) {
1657 getITelephony(context
).enableDataConnectivity();
1658 Utils
.sendMessage(context
, replyTo
, enabling_data
);
1660 getITelephony(context
).disableDataConnectivity();
1661 Utils
.sendMessage(context
, replyTo
, disabling_data
);
1663 } catch(Exception e
){
1664 Utils
.sendMessage(context
, replyTo
, exception_while_getting_itelephony
,
1665 e
.getClass().getName(), e
.getMessage());
1670 * Get the GPS status.
1672 * @param context Context instance
1673 * @param replyTo reply Address
1675 public static void gps(final Context context
, final Address replyTo
){
1676 if(Secure
.isLocationProviderEnabled(context
.getContentResolver(), LocationManager
.GPS_PROVIDER
))
1677 Utils
.sendMessage(context
, replyTo
, gps_on
);
1679 Utils
.sendMessage(context
, replyTo
, gps_off
);
1683 * Set the GPS status.
1685 * @param context Context instance
1686 * @param replyTo reply Address
1687 * @param enabled requested GPS status
1689 public static void gps(final Context context
, final Address replyTo
, final boolean enabled
) {
1690 Secure
.setLocationProviderEnabled(context
.getContentResolver(), LocationManager
.GPS_PROVIDER
, enabled
);
1692 Utils
.sendMessage(context
, replyTo
, enabling_gps
);
1694 Utils
.sendMessage(context
, replyTo
, disabling_gps
);
1698 * Get the Google location (aka network location) state.
1700 * @param context Context instance
1701 * @param replyTo reply Address
1703 public static void glocation(final Context context
, final Address replyTo
){
1704 if(Secure
.isLocationProviderEnabled(context
.getContentResolver(), LocationManager
.NETWORK_PROVIDER
))
1705 Utils
.sendMessage(context
, replyTo
, network_location_on
);
1707 Utils
.sendMessage(context
, replyTo
, network_location_off
);
1711 * Set the Google location (aka network location) state.
1713 * @param context Context instance
1714 * @param replyTo reply Address
1715 * @param enabled requested Google location state
1717 public static void glocation(final Context context
, final Address replyTo
, final boolean enabled
) {
1718 Secure
.setLocationProviderEnabled(context
.getContentResolver(), LocationManager
.NETWORK_PROVIDER
, enabled
);
1720 Utils
.sendMessage(context
, replyTo
, enabling_network_location
);
1722 Utils
.sendMessage(context
, replyTo
, disabling_network_location
);
1728 * @param context Context instance
1729 * @param replyTo reply Address
1730 * @param reason reboot reason
1732 * @see PowerManager#reboot(String)
1734 public static void reboot(final Context context
, final Address replyTo
, final @Nullable String reason
) {
1735 final PowerManager pm
=(PowerManager
) context
.getSystemService(Context
.POWER_SERVICE
);
1736 Utils
.sendMessage(context
, replyTo
, rebooting
);
1741 * Cancel a notification.
1743 * @param context Context instance
1744 * @param replyTo reply Address
1745 * @param id notification ID
1747 public static void notify(final Context context
, final Address replyTo
, final int id
) {
1748 final NotificationManager man
=(NotificationManager
) context
.getSystemService(Context
.NOTIFICATION_SERVICE
);
1750 Utils
.sendMessage(context
, replyTo
, notification_canceled
);
1754 * Show a notification.
1756 * @param context Context instance
1757 * @param replyTo reply Address
1758 * @param id notification ID
1759 * @param title notificationO title
1760 * @param text notification text
1762 public static void notify(final Context context
, final Address replyTo
, final int id
, final String title
, final String text
) {
1763 final NotificationManager man
=(NotificationManager
) context
.getSystemService(Context
.NOTIFICATION_SERVICE
);
1764 man
.notify(id
, new NotificationCompat
.Builder(context
).
1765 setContentTitle(title
).
1766 setContentText(text
).
1767 setSmallIcon(android
.R
.drawable
.stat_notify_sync_noanim
).
1769 Utils
.sendMessage(context
, replyTo
, notification_shown
);
1773 * Take a screen capture. Uses the screencap utility and requires root.
1775 * @param context Context instance
1776 * @param replyTo reply Address
1777 * @param filename capture file location
1779 public static void screencap(final Context context
, final Address replyTo
, final String filename
){
1780 new Thread(new ScreencapRunnable(context
, replyTo
, filename
)).start();
1784 * Toggle the torch state using the Torch (net.cactii.torch2) app.
1786 * @param context Context instance
1787 * @param replyTo reply Address
1789 public static void torch(final Context context
, final Address replyTo
){
1790 context
.sendBroadcast(new Intent("net.cactii.flash2.TOGGLE_FLASHLIGHT"));
1791 Utils
.sendMessage(context
, replyTo
, toggling_torch_state
);