1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.R
.string
.*;
4 import static ro
.ieval
.fonbot
.Utils
.toNonNull
;
6 import java
.io
.BufferedReader
;
8 import java
.io
.FileInputStream
;
9 import java
.io
.FileOutputStream
;
10 import java
.io
.FileNotFoundException
;
11 import java
.io
.InputStream
;
12 import java
.io
.InputStreamReader
;
13 import java
.io
.IOException
;
14 import java
.io
.OutputStream
;
15 import java
.lang
.reflect
.InvocationTargetException
;
16 import java
.lang
.reflect
.Method
;
17 import java
.net
.InetSocketAddress
;
18 import java
.net
.Socket
;
19 import java
.net
.UnknownHostException
;
21 import java
.net
.URLConnection
;
22 import java
.nio
.channels
.FileChannel
;
23 import java
.nio
.channels
.SocketChannel
;
24 import java
.util
.ArrayList
;
25 import java
.util
.Date
;
27 import org
.eclipse
.jdt
.annotation
.Nullable
;
29 import ro
.ieval
.fonbot
.Utils
.Command
;
30 import ro
.ieval
.fonbot
.Utils
.MessageType
;
31 import ro
.ieval
.fonbot
.Utils
.OngoingEvent
;
32 import ro
.ieval
.fonbot
.Utils
.RingerMode
;
33 import ro
.ieval
.fonbot
.Utils
.WipeType
;
34 import android
.annotation
.SuppressLint
;
35 import android
.app
.AlarmManager
;
36 import android
.app
.NotificationManager
;
37 import android
.app
.PendingIntent
;
38 import android
.app
.admin
.DevicePolicyManager
;
39 import android
.bluetooth
.BluetoothAdapter
;
40 import android
.content
.ActivityNotFoundException
;
41 import android
.content
.Context
;
42 import android
.content
.Intent
;
43 import android
.content
.IntentFilter
;
44 import android
.database
.Cursor
;
45 import android
.graphics
.ImageFormat
;
46 import android
.hardware
.Camera
;
47 import android
.hardware
.Camera
.PictureCallback
;
48 import android
.location
.Location
;
49 import android
.location
.LocationListener
;
50 import android
.location
.LocationManager
;
51 import android
.location
.LocationProvider
;
52 import android
.media
.AudioManager
;
53 import android
.media
.Ringtone
;
54 import android
.media
.RingtoneManager
;
55 import android
.net
.ConnectivityManager
;
56 import android
.net
.Uri
;
57 import android
.net
.wifi
.WifiManager
;
58 import android
.os
.AsyncTask
;
59 import android
.os
.BatteryManager
;
60 import android
.os
.Bundle
;
61 import android
.os
.Handler
;
62 import android
.os
.PowerManager
;
63 import android
.os
.SystemClock
;
64 import android
.os
.Vibrator
;
65 import android
.preference
.PreferenceManager
;
66 import android
.provider
.BaseColumns
;
67 import android
.provider
.CallLog
.Calls
;
68 import android
.provider
.ContactsContract
.CommonDataKinds
;
69 import android
.provider
.ContactsContract
.CommonDataKinds
.BaseTypes
;
70 import android
.provider
.ContactsContract
.CommonDataKinds
.Phone
;
71 import android
.provider
.ContactsContract
.Contacts
;
72 import android
.provider
.ContactsContract
.Data
;
73 import android
.provider
.Settings
.Secure
;
74 import android
.speech
.tts
.TextToSpeech
;
75 import android
.speech
.tts
.TextToSpeech
.OnInitListener
;
76 import android
.support
.v4
.app
.NotificationCompat
;
77 import android
.telephony
.SmsManager
;
78 import android
.telephony
.TelephonyManager
;
79 import android
.util
.Log
;
80 import android
.view
.SurfaceView
;
81 import android
.widget
.Toast
;
83 import com
.android
.internal
.telephony
.ITelephony
;
86 * Copyright © 2013 Marius Gavrilescu
88 * This file is part of FonBot.
90 * FonBot is free software: you can redistribute it and/or modify
91 * it under the terms of the GNU General Public License as published by
92 * the Free Software Foundation, either version 3 of the License, or
93 * (at your option) any later version.
95 * FonBot is distributed in the hope that it will be useful,
96 * but WITHOUT ANY WARRANTY; without even the implied warranty of
97 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
98 * GNU General Public License for more details.
100 * You should have received a copy of the GNU General Public License
101 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
105 * Implementation of all FonBot commands. The methods of this class do not do argument checking.
107 * @author Marius Gavrilescu <marius@ieval.ro>
111 * LocationListener that sends notifications to the user.
113 * @author Marius Gavrilescu <marius@ieval.ro>
115 private static final class FonBotLocationListener
implements LocationListener
{
116 /** Context instance */
117 private final Context context
;
118 /** Destination address for notifications */
119 private final Address replyTo
;
122 * Construct a FonBotLocationListener.
124 * @param context Context instance
125 * @param replyTo the reply address
127 FonBotLocationListener(final Context context
, final Address replyTo
) {
128 this.context
=context
;
129 this.replyTo
=replyTo
;
133 public void onLocationChanged(@Nullable final Location loc
) {
136 final StringBuilder sb
=new StringBuilder(toNonNull(context
.getString(location
)));
138 sb
.append(toNonNull(context
.getString(latitude
)));
140 sb
.append(loc
.getLatitude());
142 sb
.append(toNonNull(context
.getString(longitude
)));
144 sb
.append(loc
.getLongitude());
146 if(loc
.hasAccuracy()){
148 sb
.append(toNonNull(context
.getString(accuracy
)));
150 sb
.append(loc
.getAccuracy());
153 if(loc
.hasAltitude()){
155 sb
.append(toNonNull(context
.getString(altitude
)));
157 sb
.append(loc
.getAltitude());
160 if(loc
.hasBearing()){
162 sb
.append(toNonNull(context
.getString(bearing
)));
164 sb
.append(loc
.getBearing());
169 sb
.append(toNonNull(context
.getString(speed
)));
171 sb
.append(loc
.getSpeed());
174 final Date locationDate
=new Date(loc
.getTime());
176 sb
.append(toNonNull(context
.getString(at
)));
178 sb
.append(locationDate
.toString());
181 sb
.append("http://openstreetmap.org/?zoom=15&mlat=");
182 sb
.append(loc
.getLatitude());
184 sb
.append(loc
.getLongitude());
185 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), toNonNull(sb
.toString()));
189 public void onProviderDisabled(@Nullable final String provider
) {
190 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), location_provider_disabled
, provider
);
194 public void onProviderEnabled(@Nullable final String provider
) {
195 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), location_provider_enabled
, provider
);
199 public void onStatusChanged(@Nullable final String provider
, final int status
, @Nullable final Bundle extras
) {
202 case LocationProvider
.AVAILABLE
:
203 state
=location_provider_available
;
205 case LocationProvider
.TEMPORARILY_UNAVAILABLE
:
206 state
=location_provider_temporary_unavailable
;
208 case LocationProvider
.OUT_OF_SERVICE
:
209 state
=location_provider_out_of_service
;
212 state
=location_provider_unknown_state
;
214 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), state
, provider
);
218 * Currently active FonBotLocationListener
220 private static FonBotLocationListener locationListener
= null;
223 * AsyncTask that sends a byte[] to a server
225 * @author Marius Gavrilescu <marius@ieval.ro>
228 private static class SendDataAsyncTask
extends AsyncTask
<Void
, Void
, Void
>{
230 * Context instance used by this class
232 private final Context context
;
236 private final String hostname
;
240 private final int port
;
244 private final byte[] data
;
246 * Address for sending back errors and success messages
248 private final Address replyTo
;
251 * Constructs a SendDataAsyncTasks from its parameters
253 * @param context the context
254 * @param replyTo the reply Address
255 * @param hostname the server hostname
256 * @param port the server port
257 * @param data the data to send
259 public SendDataAsyncTask(final Context context
,final Address replyTo
, final String hostname
, final int port
, final byte[] data
) {//NOPMD array is immutable
261 this.context
=context
;
262 this.hostname
=hostname
;
265 this.replyTo
=replyTo
;
269 protected @Nullable Void
doInBackground(@Nullable final Void
... params
) {
272 sock
= new Socket(hostname
, port
);
274 sock
.getOutputStream().write(data
);
275 } catch (IOException e
) {
276 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), error_writing_to_socket
, e
.getMessage());
282 } catch (IOException e
) {
283 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), cannot_close_socket
, e
.getMessage());
286 } catch (UnknownHostException e
) {
287 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), unknown_host
, hostname
);
289 } catch (IOException e
) {
290 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), cannot_connect_to_host_on_port
, hostname
, Integer
.valueOf(port
));
293 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), photo_sent
);
299 * ExecutableRunnable that uploads a file to a given host and port, netcat-style
301 * @author Marius Gavrilescu <marius@ieval.ro>
303 private static final class NcfileExecutableRunnable
extends ExecutableRunnable
{
304 private final Context context
;
305 private final Address replyTo
;
306 private final String filename
;
307 private final String hostname
;
308 private final int port
;
311 * Construct a NcfileExecutableRunnable
313 * @param context Context instance
314 * @param replyTo reply Address
315 * @param filename filename to upload
316 * @param hostname hostname to upload to
317 * @param port port to upload to
319 NcfileExecutableRunnable(final Context context
, final Address replyTo
, final String filename
, final String hostname
, final int port
){
320 this.context
=context
;
321 this.replyTo
=replyTo
;
322 this.filename
=filename
;
323 this.hostname
=hostname
;
327 @Override public void run(){
328 final FileChannel in
;
329 final SocketChannel sock
;
331 in
=new FileInputStream(filename
).getChannel();
332 } catch (final FileNotFoundException e
){
333 Utils
.sendMessage(context
, replyTo
, file_not_found
, filename
);
338 sock
= SocketChannel
.open(new InetSocketAddress(hostname
, port
));
339 } catch (final IOException e
){
340 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(cannot_connect_to_host_on_port
, hostname
, Integer
.valueOf(port
))));
343 } catch (IOException ex
) {
350 in
.transferTo(0, in
.size(), sock
);
351 } catch (final IOException e
){
352 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(io_error
, e
.getMessage())));
356 } catch (IOException e
){
361 } catch(IOException e
){
365 Utils
.sendMessage(context
, replyTo
, file_sent
);
370 * ExecutableRunnable that downloads a file from a given host and port, netcat-style
372 * @author Marius Gavrilescu <marius@ieval.ro>
374 private static final class GetfileExecutableRunnable
extends ExecutableRunnable
{
375 private final Context context
;
376 private final Address replyTo
;
377 private final String filename
;
378 private final String hostname
;
379 private final int port
;
382 * Construct a GetfileExecutableRunnable
384 * @param context Context instance
385 * @param replyTo reply Address
386 * @param filename filename to save to
387 * @param hostname hostname to download from
388 * @param port port to download from
390 GetfileExecutableRunnable(final Context context
, final Address replyTo
, final String filename
, final String hostname
, final int port
){
391 this.context
=context
;
392 this.replyTo
=replyTo
;
393 this.filename
=filename
;
394 this.hostname
=hostname
;
398 @Override public void run(){
399 final InputStream in
;
400 final FileOutputStream out
;
403 out
=new FileOutputStream(filename
);
404 } catch (final IOException e
){
405 Utils
.sendMessage(context
, replyTo
, error_opening_file
, filename
, e
.getMessage());
410 sock
= new Socket(hostname
, port
);
411 in
= sock
.getInputStream();
412 } catch (final IOException e
){
413 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(cannot_connect_to_host_on_port
, hostname
, Integer
.valueOf(port
))));
416 } catch (IOException ex
) {
423 byte[] buffer
=new byte[1024*1024*2];
425 while((nread
= in
.read(buffer
)) > 0)
426 out
.write(buffer
, 0, nread
);
427 } catch (final IOException e
){
428 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(io_error
, e
.getMessage())));
432 } catch (IOException e
){
437 } catch(IOException e
){
441 Utils
.sendMessage(context
, replyTo
, file_received
);
446 * PictureCallback that sends the picture to a server.
448 * @author Marius Gavrilescu <marius@ieval.ro>
450 private static final class FonBotPictureCallback
implements PictureCallback
{
451 /** Server hostname */
452 private final String hostname
;
454 private final int port
;
455 /** Context instance */
456 private final Context context
;
458 private final Address replyTo
;
461 * Construct a FonBotPictureCallback.
463 * @param context Context instance
464 * @param replyTo reply Address
465 * @param hostname server hostname
466 * @param port server port
468 FonBotPictureCallback(final Context context
, final Address replyTo
, final String hostname
, final int port
) {
469 this.hostname
=hostname
;
471 this.context
=context
;
472 this.replyTo
=replyTo
;
476 @SuppressWarnings("hiding")
477 public void onPictureTaken(final @Nullable byte[] data
, final @Nullable Camera camera
) {
478 if(camera
==null || data
==null)
480 camera
.stopPreview();
482 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), sending_photo
);
483 new SendDataAsyncTask(toNonNull(context
), toNonNull(replyTo
), toNonNull(hostname
), port
, data
).execute();
488 * Runnable that takes a screen capture and stores it in a file.
490 private static final class ScreencapRunnable
implements Runnable
{
491 private final Context context
;
492 private final Address replyTo
;
493 private final String filename
;
495 ScreencapRunnable(final Context context
, final Address replyTo
, final String filename
){
496 this.context
=context
;
497 this.replyTo
=replyTo
;
498 this.filename
=filename
;
505 exitCode
=Runtime
.getRuntime().exec(new String
[]{
508 "screencap -p \"" + filename
+ "\""
510 } catch (final Exception e
){
512 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), screencap_failed
);
516 if(exitCode
== 0 && new File(filename
).exists())
517 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), screencap_successful
);
519 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), screencap_failed
);
524 * Get help for a particular command
526 * @param context Context instance
527 * @param replyTo reply Address
528 * @param command command to get help for
530 public static void help(final Context context
, final Address replyTo
, final Command command
){//NOPMD method is a big switch statement. Nothing confusing.
533 Utils
.sendMessage(context
, replyTo
, answer_help
);
536 Utils
.sendMessage(context
, replyTo
, batt_help
);
539 Utils
.sendMessage(context
, replyTo
, bluetooth_help
);
542 Utils
.sendMessage(context
, replyTo
, calllog_help
);
545 Utils
.sendMessage(context
, replyTo
, contacts_help
);
548 Utils
.sendMessage(context
, replyTo
, data_help
);
550 case DELNOTIFICATION
:
551 Utils
.sendMessage(context
, replyTo
, delnotification_help
, Utils
.join(", ", toNonNull(MessageType
.values())));
554 Utils
.sendMessage(context
, replyTo
, dial_help
);
557 Utils
.sendMessage(context
, replyTo
, dialog_help
);
560 Utils
.sendMessage(context
, replyTo
, disable_help
, Utils
.join(", ", toNonNull(Command
.values())));
563 Utils
.sendMessage(context
, replyTo
, echo_help
);
566 Utils
.sendMessage(context
, replyTo
, enable_help
, Utils
.join(", ", toNonNull(Command
.values())));
569 Utils
.sendMessage(context
, replyTo
, flash_help
);
572 Utils
.sendMessage(context
, replyTo
, glocation_help
);
575 Utils
.sendMessage(context
, replyTo
, gps_help
);
578 Utils
.sendMessage(context
, replyTo
, hangup_help
);
581 Utils
.sendMessage(context
, replyTo
, help_help
, Utils
.join(", ",toNonNull(Command
.values())));
584 Utils
.sendMessage(context
, replyTo
, launch_help
);
587 Utils
.sendMessage(context
, replyTo
, location_help
, Utils
.join(", ",toNonNull(Utils
.LocationProvider
.values())));
590 Utils
.sendMessage(context
, replyTo
, lock_help
);
593 Utils
.sendMessage(context
, replyTo
, ls_help
);
596 Utils
.sendMessage(context
, replyTo
, ncfile_help
);
599 Utils
.sendMessage(context
, replyTo
, next_help
);
602 Utils
.sendMessage(context
, replyTo
, nolocation_help
);
605 Utils
.sendMessage(context
, replyTo
, pause_help
);
608 Utils
.sendMessage(context
, replyTo
, photo_help
);
611 Utils
.sendMessage(context
, replyTo
, play_help
);
614 Utils
.sendMessage(context
, replyTo
, poll_help
);
617 Utils
.sendMessage(context
, replyTo
, prev_help
);
620 Utils
.sendMessage(context
, replyTo
, ring_help
);
623 Utils
.sendMessage(context
, replyTo
, ringer_help
, Utils
.join(", ", toNonNull(RingerMode
.values())));
626 Utils
.sendMessage(context
, replyTo
, rm_help
);
628 case SETNOTIFICATION
:
629 Utils
.sendMessage(context
, replyTo
, setnotification_help
, Utils
.join(", ", toNonNull(MessageType
.values())));
632 Utils
.sendMessage(context
, replyTo
, setpassword_help
);
635 Utils
.sendMessage(context
, replyTo
, sms_help
);
638 Utils
.sendMessage(context
, replyTo
, smslog_help
);
641 Utils
.sendMessage(context
, replyTo
, speak_help
);
644 Utils
.sendMessage(context
, replyTo
, toast_help
);
647 Utils
.sendMessage(context
, replyTo
, vibrate_help
);
650 Utils
.sendMessage(context
, replyTo
, view_help
);
653 Utils
.sendMessage(context
, replyTo
, wifi_help
);
656 Utils
.sendMessage(context
, replyTo
, wipe_help
, Utils
.WIPE_CONFIRM_STRING
);
659 Utils
.sendMessage(context
, replyTo
, reboot_help
);
662 Utils
.sendMessage(context
, replyTo
, notify_help
);
665 Utils
.sendMessage(context
, replyTo
, screencap_help
);
668 Utils
.sendMessage(context
, replyTo
, torch_help
);
671 Utils
.sendMessage(context
, replyTo
, sh_help
);
674 Utils
.sendMessage(context
, replyTo
, rootsh_help
);
682 * @see #startCamera(Context, Address)
685 private static Camera camera
;
687 * Ringtone used by the {@link Utils.Command#RING RING} command.
689 * @see #setupRingtone(Context)
691 private static Ringtone ringtone
;
693 * Saved ringer volume.
695 * @see #startAlarm(Context, Address)
696 * @see #stopAlarm(Context, Address)
698 private static int savedRingVolume
;
702 * @see #startAlarm(Context, Address)
703 * @see #stopAlarm(Context, Address)
705 private static int savedRingerMode
;
707 /** Private constructor */
713 * Convert a phone number type to a string
715 * @param context Context instance
716 * @param type phone number type
717 * @param label name of a custom phone type
718 * @return the phone number type
720 private static @Nullable String
phoneNumberType(final Context context
, final int type
, final @Nullable String label
) {
722 case BaseTypes
.TYPE_CUSTOM
:
724 case Phone
.TYPE_ASSISTANT
:
725 return context
.getString(phone_numer_type_assistant
);
726 case Phone
.TYPE_CALLBACK
:
727 return context
.getString(phone_number_type_callback
);
729 return context
.getString(phone_number_type_car
);
730 case Phone
.TYPE_COMPANY_MAIN
:
731 return context
.getString(phone_number_type_company_main
);
732 case Phone
.TYPE_FAX_HOME
:
733 return context
.getString(phone_number_type_home_fax
);
734 case Phone
.TYPE_FAX_WORK
:
735 return context
.getString(phone_number_type_work_fax
);
736 case Phone
.TYPE_HOME
:
737 return context
.getString(phone_number_type_home
);
738 case Phone
.TYPE_ISDN
:
739 return context
.getString(phone_number_type_isdn
);
740 case Phone
.TYPE_MAIN
:
741 return context
.getString(phone_number_type_main
);
743 return context
.getString(phone_number_type_mms
);
744 case Phone
.TYPE_MOBILE
:
745 return context
.getString(phone_number_type_mobile
);
746 case Phone
.TYPE_OTHER
:
747 return context
.getString(phone_number_type_other
);
748 case Phone
.TYPE_OTHER_FAX
:
749 return context
.getString(phone_number_type_other_fax
);
750 case Phone
.TYPE_PAGER
:
751 return context
.getString(phone_number_type_pager
);
752 case Phone
.TYPE_RADIO
:
753 return context
.getString(phone_number_type_radio
);
754 case Phone
.TYPE_TELEX
:
755 return context
.getString(phone_number_type_telex
);
756 case Phone
.TYPE_TTY_TDD
:
757 return context
.getString(phone_number_type_textphone
);
758 case Phone
.TYPE_WORK
:
759 return context
.getString(phone_number_type_work
);
760 case Phone
.TYPE_WORK_MOBILE
:
761 return context
.getString(phone_number_type_work_mobile
);
762 case Phone
.TYPE_WORK_PAGER
:
763 return context
.getString(phone_number_type_work_pager
);
766 return context
.getString(phone_number_type_unknown
, Integer
.valueOf(type
));
770 * Setup the ringtone used by the {@link Utils.Command#RING RING} command
772 * @param context Context
774 private static void setupRingtone(final Context context
){
775 if(ringtone
==null){//NOPMD not supposed to be thread-safe
776 final Uri alert
=RingtoneManager
.getDefaultUri(RingtoneManager
.TYPE_RINGTONE
);
777 ringtone
=RingtoneManager
.getRingtone(context
, alert
);
782 * Make the phone start ringing. Turns up the volume and sets the ringer mode to NORMAL
784 * @param context Context instance
785 * @param replyTo reply Address
787 private static void startAlarm(final Context context
, final Address replyTo
){
788 Utils
.registerOngoing(context
, toNonNull(OngoingEvent
.RING
));
789 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
790 savedRingerMode
=man
.getRingerMode();
791 man
.setRingerMode(AudioManager
.RINGER_MODE_NORMAL
);
792 savedRingVolume
=man
.getStreamVolume(AudioManager
.STREAM_RING
);
793 man
.setStreamVolume(AudioManager
.STREAM_RING
, man
.getStreamMaxVolume(AudioManager
.STREAM_RING
), 0);
794 Utils
.sendMessage(context
, replyTo
, ringing
);
799 * Get a camera instance.
801 * @param context Context instance
802 * @param replyTo reply Address
804 private static void startCamera(final Context context
, final Address replyTo
, final int cameraNumber
){
809 camera
=Camera
.open(cameraNumber
);
810 } catch (Exception ex
){
811 camera
=Camera
.open();
813 } catch (Exception e
){
814 Utils
.sendMessage(context
, replyTo
, cannot_grab_camera
);
819 * Make the phone stop ringing. Restores the volume and ringer mode.
821 * @param context Context instance
822 * @param replyTo reply Address
824 private static void stopAlarm(final Context context
, final Address replyTo
){
825 Utils
.unregisterOngoing(context
, toNonNull(OngoingEvent
.RING
));
826 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
827 Utils
.sendMessage(context
, replyTo
, no_longer_ringing
);
829 man
.setStreamVolume(AudioManager
.STREAM_RING
, savedRingVolume
, 0);
830 man
.setRingerMode(savedRingerMode
);
834 * Release the previously grabbed camera instance
836 * @see #startCamera(Context, Address)
838 private static void stopCamera(){
846 * Send battery status information to an Address
848 * @param context Context instance
849 * @param replyTo destination Address
851 * @see #describeBatteryLevel(Context, Address, MessageType)
853 public static void batt(final Context context
, final Address replyTo
){
854 describeBatteryLevel(context
, replyTo
, null);
858 * Show the bluetooth radio status.
860 * @param context Context instance
861 * @param replyTo destination Address
863 public static void bluetooth(final Context context
, final Address replyTo
) {
864 final BluetoothAdapter adapter
=BluetoothAdapter
.getDefaultAdapter();
866 Utils
.sendMessage(context
, replyTo
, no_bluetooth_adapter
);
870 if(adapter
.isEnabled())
871 Utils
.sendMessage(context
, replyTo
, bluetooth_on
);
873 Utils
.sendMessage(context
, replyTo
, bluetooth_off
);
877 * Set the bluetooth radio status.
879 * @param context Context instance
880 * @param replyTo destination Address
881 * @param on the requested radio status
883 public static void bluetooth(final Context context
, final Address replyTo
, final boolean on
){
884 final BluetoothAdapter adapter
=BluetoothAdapter
.getDefaultAdapter();
886 Utils
.sendMessage(context
, replyTo
, no_bluetooth_adapter
);
892 Utils
.sendMessage(context
, replyTo
, enabling_bluetooth
);
896 Utils
.sendMessage(context
, replyTo
, disabling_bluetooth
);
901 * Cancel an ongoing event.
903 * @param context Context instance
904 * @param event the event to cancel
906 public static void cancelOngoing(final Context context
, final OngoingEvent event
){
909 nolocation(context
, toNonNull(Address
.BLACKHOLE
));
912 ring(context
, toNonNull(Address
.BLACKHOLE
), false);
918 * Send the last calls to an Address.
920 * @param context Context instance
921 * @param replyTo destination Address
922 * @param numCalls how many calls to send
924 public static void calllog(final Context context
, final Address replyTo
, final int numCalls
) {
925 final String
[] fields
= {
926 Calls
.TYPE
, Calls
.NUMBER
, Calls
.CACHED_NAME
, Calls
.DURATION
, Calls
.DATE
929 final Cursor cursor
= context
.getContentResolver().query(
937 if (cursor
.moveToFirst()) {
939 final StringBuilder sb
=new StringBuilder(50);//NOPMD different strings
940 final int type
=cursor
.getInt(0);
941 final String from
=cursor
.getString(1);
944 case Calls
.INCOMING_TYPE
:
945 sb
.append(context
.getString(incoming_call_from
, from
));
947 case Calls
.MISSED_TYPE
:
948 sb
.append(context
.getString(missed_call_from
, from
));
950 case Calls
.OUTGOING_TYPE
:
951 sb
.append(context
.getString(outgoing_call_to
, from
));
955 if (cursor
.getString(2) != null)
956 sb
.append('(').append(cursor
.getString(2)).append(") ");
958 sb
.append(context
.getString(duration_seconds_starting_at
,
959 Long
.valueOf(cursor
.getLong(3)),
960 new Date(cursor
.getLong(4))));
962 Utils
.sendMessage(context
, replyTo
, toNonNull(sb
.toString()));
963 } while (cursor
.moveToNext() && cursor
.getPosition() < numCalls
);
970 * Search for contacts by name/nickname and send matching entries to an Address.
972 * @param context Context instance
973 * @param replyTo destination Address
974 * @param name name/nickname part to search for
976 @SuppressLint("StringFormatMatches")
977 public static void contacts(final Context context
, final Address replyTo
, final String name
){
978 final Cursor cursor
=context
.getContentResolver().query(Uri
.withAppendedPath(
979 Contacts
.CONTENT_FILTER_URI
, name
),
980 new String
[]{Contacts
.DISPLAY_NAME
, BaseColumns
._ID
, Contacts
.LOOKUP_KEY
},
981 null, null, Contacts
.DISPLAY_NAME
);
983 if(cursor
.getCount()==0)
984 Utils
.sendMessage(context
, replyTo
, no_matching_contacts_found
);
986 while(cursor
.moveToNext()){
987 final String
[] fields
= {
988 CommonDataKinds
.Phone
.NUMBER
,
989 CommonDataKinds
.Phone
.TYPE
,
990 CommonDataKinds
.Phone
.LABEL
,
993 final Cursor inCursor
=context
.getContentResolver().query(Data
.CONTENT_URI
,
995 Data
.CONTACT_ID
+" = ? AND "+Data
.MIMETYPE
+ " = ?",
996 new String
[]{Long
.toString(cursor
.getLong(1)), CommonDataKinds
.Phone
.CONTENT_ITEM_TYPE
},
997 CommonDataKinds
.Phone
.LABEL
);
999 while(inCursor
.moveToNext())
1000 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(contact_info
,
1001 cursor
.getString(0),
1002 inCursor
.getString(0),
1003 phoneNumberType(context
, inCursor
.getInt(1), inCursor
.getString(2)))));
1012 * Send battery status information to an Address or as a notification
1014 * @param context Context instance
1015 * @param replyTo Address to send the information to, if sending to a direct address. Null otherwise.
1016 * @param type Notification type, if sending as a notification. Null otherwise.
1018 public static void describeBatteryLevel(final Context context
, final @Nullable Address replyTo
, final @Nullable MessageType type
) {
1019 if(replyTo
==null&&type
==null)
1021 final Intent intent
=context
.registerReceiver(null, new IntentFilter(Intent
.ACTION_BATTERY_CHANGED
));
1024 final double level
=intent
.getIntExtra(BatteryManager
.EXTRA_LEVEL
, 0);
1025 final int scale
=intent
.getIntExtra(BatteryManager
.EXTRA_SCALE
, 100);
1026 final int plugged
=intent
.getIntExtra(BatteryManager
.EXTRA_PLUGGED
, 0);
1027 final int status
=intent
.getIntExtra(BatteryManager
.EXTRA_STATUS
, BatteryManager
.BATTERY_STATUS_UNKNOWN
);
1028 final int temp
=intent
.getIntExtra(BatteryManager
.EXTRA_TEMPERATURE
, 0);
1029 final int volt
=intent
.getIntExtra(BatteryManager
.EXTRA_VOLTAGE
, 0);
1031 final StringBuilder sb
=new StringBuilder(100);
1032 sb
.append(context
.getString(battery_level
, Double
.valueOf(level
*100/scale
)));
1036 sb
.append(context
.getString(not_plugged_in
));
1038 case BatteryManager
.BATTERY_PLUGGED_AC
:
1039 sb
.append(context
.getString(plugged_in_ac
));
1041 case BatteryManager
.BATTERY_PLUGGED_USB
:
1042 sb
.append(context
.getString(plugged_in_usb
));
1044 case BatteryManager
.BATTERY_PLUGGED_WIRELESS
:
1045 sb
.append(context
.getString(plugged_in_wireless
));
1050 case BatteryManager
.BATTERY_STATUS_CHARGING
:
1051 sb
.append(context
.getString(status_charging
));
1053 case BatteryManager
.BATTERY_STATUS_DISCHARGING
:
1054 sb
.append(context
.getString(status_discharging
));
1056 case BatteryManager
.BATTERY_STATUS_FULL
:
1057 sb
.append(context
.getString(status_full
));
1059 case BatteryManager
.BATTERY_STATUS_NOT_CHARGING
:
1060 sb
.append(context
.getString(status_not_charging
));
1062 case BatteryManager
.BATTERY_STATUS_UNKNOWN
:
1063 sb
.append(context
.getString(status_unknown
));
1067 sb
.append(context
.getString(temperature
, Integer
.valueOf(temp
)));
1069 sb
.append(context
.getString(voltage
, Integer
.valueOf(volt
)));
1071 Utils
.sendMessage(context
, toNonNull(replyTo
), toNonNull(sb
.toString()));
1073 Utils
.sendMessage(context
, type
, toNonNull(sb
.toString()));
1077 * Dial a phone number.
1079 * @param context Context instance
1080 * @param replyTo reply Address
1081 * @param nr phone number to dial
1083 public static void dial(final Context context
, final Address replyTo
, final String nr
){
1084 final Intent intent
=new Intent(Intent
.ACTION_CALL
,Uri
.parse("tel:"+nr
));
1085 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
1086 final String name
=Utils
.callerId(context
, nr
);
1088 Utils
.sendMessage(context
, replyTo
, dialing
, nr
);
1090 Utils
.sendMessage(context
, replyTo
, dialing
, nr
+" ("+name
+")");
1091 context
.startActivity(intent
);
1095 * Show a dialog with a message and a list of buttons.
1097 * @param context Context instance
1098 * @param replyTo reply Address
1099 * @param message dialog message
1100 * @param buttons dialog buttons
1102 public static void dialog(final Context context
, final Address replyTo
, final String message
, final String
[] buttons
){
1103 final Intent intent
=new Intent(context
, DialogActivity
.class);
1104 intent
.putExtra(DialogActivity
.EXTRA_MESSAGE
, message
);
1105 intent
.putExtra(DialogActivity
.EXTRA_BUTTONS
, buttons
);
1106 intent
.putExtra(DialogActivity
.EXTRA_REPLYTO
, replyTo
.toString());
1108 Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
|
1109 Intent
.FLAG_ACTIVITY_NEW_TASK
|
1110 Intent
.FLAG_ACTIVITY_NO_USER_ACTION
|
1111 Intent
.FLAG_FROM_BACKGROUND
);
1112 Utils
.sendMessage(context
, toNonNull(replyTo
), showing_dialog
);
1113 context
.startActivity(intent
);
1117 * Turns the flashlight on or off.
1119 * @param context Context instance
1120 * @param replyTo reply Address
1121 * @param on requested flashlight state
1123 public static void flash(final Context context
, final Address replyTo
, final boolean on
){
1124 startCamera(context
, replyTo
, 0);
1127 final Camera
.Parameters parms
=camera
.getParameters();
1129 parms
.setFlashMode(Camera
.Parameters
.FLASH_MODE_TORCH
);
1130 camera
.setParameters(parms
);
1132 parms
.setFlashMode(Camera
.Parameters
.FLASH_MODE_AUTO
);
1133 camera
.setParameters(parms
);
1139 * Start sending location updates to an Address.
1141 * @param context Context instance
1142 * @param replyTo destination Address
1143 * @param provider LocationProvider
1144 * @param minTime minimum time between two consecutive updates (in ms)
1145 * @param minDistance minimum distance between two consecutive updates (in meters)
1147 * @see LocationManager#requestLocationUpdates(String, long, float, LocationListener)
1149 public static void location(final Context context
, final Address replyTo
, final String provider
,final long minTime
,final float minDistance
){
1150 final LocationManager man
=(LocationManager
) context
.getApplicationContext().getSystemService(Context
.LOCATION_SERVICE
);
1151 if(locationListener
!=null)
1152 nolocation(context
, toNonNull(Address
.BLACKHOLE
));
1153 Utils
.registerOngoing(context
, toNonNull(OngoingEvent
.LOCATION
));
1154 locationListener
=new FonBotLocationListener(context
, replyTo
);
1155 man
.removeUpdates(locationListener
);
1156 final Location lastKnownLocation
=man
.getLastKnownLocation(provider
);
1157 if(lastKnownLocation
!=null){
1158 Utils
.sendMessage(context
, replyTo
, last_known_location
);
1159 locationListener
.onLocationChanged(lastKnownLocation
);
1161 Utils
.sendMessage(context
, replyTo
, listening_for_location_updates
);
1162 man
.requestLocationUpdates(provider
, minTime
, minDistance
, locationListener
);
1168 * @param context Context instance
1169 * @param replyTo reply Address
1171 public static void lock(final Context context
, final Address replyTo
) {
1172 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1174 Utils
.sendMessage(context
, replyTo
, device_locked
);
1178 * Send a command to a running instance of the music player
1180 * @param context Context instance
1181 * @param replyTo reply Address
1182 * @param command command to send
1184 public static void musicPlayerCommand(final Context context
, final Address replyTo
, final String command
) {
1185 final Intent intent
=new Intent("com.android.music.musicservicecommand");
1186 intent
.putExtra("command", command
);
1187 context
.sendBroadcast(intent
);
1188 Utils
.sendMessage(context
, replyTo
, command_sent
);
1192 * Send a file to a server.
1194 * @param context Context instance
1195 * @param replyTo reply Address
1196 * @param filename file to send
1197 * @param hostname server hostname
1198 * @param port server port
1200 public static void ncfile(final Context context
, final Address replyTo
, final String filename
,final String hostname
,final int port
){
1201 new NcfileExecutableRunnable(context
, replyTo
, filename
, hostname
, port
).execute();
1205 * Stop sending location updates.
1207 * @param context Context instance
1208 * @param replyTo reply Address
1210 public static void nolocation(final Context context
, final Address replyTo
){
1211 Utils
.unregisterOngoing(context
, toNonNull(OngoingEvent
.LOCATION
));
1212 final LocationManager man
=(LocationManager
) context
.getApplicationContext().getSystemService(Context
.LOCATION_SERVICE
);
1213 man
.removeUpdates(locationListener
);
1214 locationListener
=null;
1215 Utils
.sendMessage(context
, replyTo
, no_longer_listening_for_location_updates
);
1219 * Take a photo and send it to a server.
1221 * @param context Context instance
1222 * @param replyTo reply Address
1223 * @param cameraNumber camera to take photo with
1224 * @param hostname server hostname
1225 * @param port server port
1227 public static void photo(final Context context
, final Address replyTo
, final int cameraNumber
, final String hostname
, final int port
){
1228 startCamera(context
, replyTo
, cameraNumber
);
1231 final Camera
.Parameters parms
=camera
.getParameters();
1232 parms
.setJpegQuality(70);
1233 parms
.setPictureFormat(ImageFormat
.JPEG
);
1234 camera
.setParameters(parms
);
1236 final SurfaceView fakeView
=new SurfaceView(context
);
1238 camera
.setPreviewDisplay(fakeView
.getHolder());
1239 } catch (IOException e
) {
1240 Utils
.sendMessage(context
, replyTo
, error_setting_preview_display
);
1243 camera
.startPreview();
1244 final Handler handler
=new Handler();
1246 new Thread(new Runnable() {
1249 SystemClock
.sleep(2000);
1251 handler
.post(new Runnable() {
1255 camera
.takePicture(null, null, new FonBotPictureCallback(context
, replyTo
, hostname
, port
));
1256 } catch(Exception e
){
1257 Utils
.sendMessage(context
, replyTo
, error_while_processing_command
, e
.getClass().getName(), e
.getMessage());
1266 * Send a directory listing to an Address
1268 * @param context Context instance
1269 * @param replyTo destination Address
1270 * @param directory directory to list
1272 public static void ls(final Context context
, final Address replyTo
, final String directory
) {
1273 final File
[] files
=new File(directory
).listFiles();
1275 Utils
.sendMessage(context
, replyTo
, string_is_not_a_directory
, directory
);
1279 final StringBuilder sb
=new StringBuilder(context
.getString(files_in_directory
,directory
));
1280 for(final File file
: files
){
1281 sb
.append(file
.getName());
1282 if(file
.isDirectory())
1287 Utils
.sendMessage(context
, replyTo
, toNonNull(sb
.toString()));
1291 * Make the phone start ringing if it is not ringing or stop ringing if it is.
1293 * @param context Context instance
1294 * @param replyTo reply Address
1296 public static void ring(final Context context
, final Address replyTo
){
1297 setupRingtone(context
);
1299 Utils
.sendMessage(context
, replyTo
, no_ringtone_found
);
1302 if(ringtone
.isPlaying())
1303 stopAlarm(context
, replyTo
);
1305 startAlarm(context
, replyTo
);
1309 * Make the phone start/stop ringing.
1311 * @param context Context instance
1312 * @param replyTo reply Address
1313 * @param on true if the phone should start ringing, false otherwise
1315 public static void ring(final Context context
, final Address replyTo
, final boolean on
){
1316 setupRingtone(context
);
1318 Utils
.sendMessage(context
, replyTo
, no_ringtone_found
);
1321 if(on
&&!ringtone
.isPlaying())
1322 startAlarm(context
, replyTo
);
1323 else if(ringtone
.isPlaying()&&!on
)
1324 stopAlarm(context
, replyTo
);
1328 * Send the current ringer mode to an Address
1330 * @param context Context instance
1331 * @param replyTo destination Address
1333 public static void ringer(final Context context
, final Address replyTo
){
1334 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
1335 switch(man
.getRingerMode()){
1336 case AudioManager
.RINGER_MODE_NORMAL
:
1337 Utils
.sendMessage(context
, replyTo
, ringer_mode_normal
);
1339 case AudioManager
.RINGER_MODE_VIBRATE
:
1340 Utils
.sendMessage(context
, replyTo
, ringer_mode_vibrate
);
1342 case AudioManager
.RINGER_MODE_SILENT
:
1343 Utils
.sendMessage(context
, replyTo
, ringer_mode_silent
);
1346 Utils
.sendMessage(context
, replyTo
, unknown_ringer_mode
);
1351 * Set the ringer mode.
1353 * @param context Context instance
1354 * @param replyTo reply Address
1355 * @param ringerMode requested ringer mode
1357 * @see Utils.RingerMode
1359 public static void ringer(final Context context
, final Address replyTo
, final int ringerMode
){
1360 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
1361 man
.setRingerMode(ringerMode
);
1362 ringer(context
, replyTo
);
1366 * Remove a file or empty directory.
1368 * @param context Context instance
1369 * @param replyTo reply Address
1370 * @param filename file/empty directory to delete
1372 public static void rm(final Context context
, final Address replyTo
, final String filename
){
1373 if(new File(filename
).delete())
1374 Utils
.sendMessage(context
, replyTo
, file_deleted
);
1376 Utils
.sendMessage(context
, replyTo
, error_while_deleting_file
);
1380 * Clear the keyguard password.
1382 * @param context Context instance
1383 * @param replyTo reply Address
1384 * @throws SecurityException if FonBot does not have device administration permissions
1386 public static void setPassword(final Context context
, final Address replyTo
) throws SecurityException
{
1387 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1389 dpm
.resetPassword("", 0);
1390 Utils
.sendMessage(context
, replyTo
, password_cleared
);
1394 * Change the keyguard password.
1396 * @param context Context instance
1397 * @param replyTo reply Address
1398 * @param password new password
1399 * @throws SecurityException if FonBot does not have device administration permissions
1401 public static void setPassword(final Context context
, final Address replyTo
, final String password
) throws SecurityException
{
1402 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1404 dpm
.resetPassword(password
, 0);
1405 Utils
.sendMessage(context
, replyTo
, password_set
);
1409 * Send a text message.
1411 * @param context Context instance
1412 * @param replyTo reply Address
1413 * @param destination destination phone number
1414 * @param text text message contents
1416 public static void sms(final Context context
, final Address replyTo
, final String destination
, final String text
){
1417 final SmsManager manager
=SmsManager
.getDefault();
1418 final ArrayList
<String
> messages
=manager
.divideMessage(text
);
1419 if(messages
.size() > 1)
1420 Utils
.sendMessage(context
, replyTo
, message_was_split_into_parts
, Integer
.valueOf(messages
.size()));
1422 final ArrayList
<PendingIntent
> sents
=new ArrayList
<PendingIntent
>(messages
.size());
1423 final ArrayList
<PendingIntent
> delivereds
=new ArrayList
<PendingIntent
>(messages
.size());
1425 final String name
=Utils
.callerId(context
, destination
);
1426 final String fullDestination
;
1428 fullDestination
=destination
;
1430 fullDestination
=destination
+" ("+name
+")";
1432 for(int i
=0;i
<messages
.size();i
++){
1433 final Intent sent
=new Intent(context
,SmsStatusReceiver
.class);
1434 sent
.putExtra(SmsStatusReceiver
.EXTRA_DESTINATION
, fullDestination
);
1435 sent
.putExtra(SmsStatusReceiver
.EXTRA_PART
, i
+1);
1436 sent
.putExtra(SmsStatusReceiver
.EXTRA_TOTAL
, messages
.size());
1437 sent
.putExtra(SmsStatusReceiver
.EXTRA_REPLY_TO
, replyTo
.toString());
1438 sent
.setAction(SmsStatusReceiver
.SENT_ACTION
+i
+System
.currentTimeMillis());//actions must be unique
1439 sents
.add(PendingIntent
.getBroadcast(context
, 0, sent
, PendingIntent
.FLAG_UPDATE_CURRENT
));
1441 final Intent delivered
=new Intent(context
, SmsStatusReceiver
.class);
1442 delivered
.putExtra(SmsStatusReceiver
.EXTRA_DESTINATION
, fullDestination
);
1443 delivered
.putExtra(SmsStatusReceiver
.EXTRA_PART
, i
+1);
1444 delivered
.putExtra(SmsStatusReceiver
.EXTRA_TOTAL
, messages
.size());
1445 delivered
.putExtra(SmsStatusReceiver
.EXTRA_REPLY_TO
, replyTo
.toString());
1446 delivered
.setAction(SmsStatusReceiver
.DELIVERED_ACTION
+i
+System
.currentTimeMillis());//actions must be unique
1447 delivereds
.add(PendingIntent
.getBroadcast(context
, 0, delivered
, PendingIntent
.FLAG_UPDATE_CURRENT
));
1450 Log
.d(Heavy
.class.getName(), "Sending sms to "+destination
);
1451 manager
.sendMultipartTextMessage(destination
, null, messages
, sents
, delivereds
);
1455 * Send the last SMSes to an Address.
1457 * @param context Context instance
1458 * @param replyTo destination Address
1459 * @param numSms how many SMSes to send
1461 public static void smslog(final Context context
, final Address replyTo
, final int numSms
) {
1462 final String
[] fields
= {"type","address", "body", "date"};
1464 final Cursor cursor
= context
.getContentResolver().query (
1465 Uri
.parse("content://sms"),
1472 if (cursor
.moveToFirst()) {
1474 final String fromNumber
=cursor
.getString(1);
1476 if(fromNumber
== null)
1479 final String name
=Utils
.callerId(context
, Utils
.toNonNull(fromNumber
));
1483 from
=fromNumber
+" ("+name
+')';
1486 final String message
=cursor
.getString(2).replace("\n", "\n ");
1487 final Date date
=new Date(cursor
.getLong(3));
1489 if(cursor
.getInt(0)==1)
1490 Utils
.sendMessage(context
, replyTo
, incoming_message
, from
, message
, date
);
1492 Utils
.sendMessage(context
, replyTo
, outgoing_message
, from
, message
, date
);
1493 } while (cursor
.moveToNext() && cursor
.getPosition() < numSms
);
1499 /** TTS instance, only used by {@link #speak(Context, Address, String)} */
1500 private static TextToSpeech tts
;
1503 * Speak a String using the text-to-speech engine.
1505 * @param context Context instance
1506 * @param replyTo reply Address
1507 * @param text text to speak
1509 public static void speak(final Context context
, final Address replyTo
, final String text
){
1510 tts
=new TextToSpeech(context
, new OnInitListener() {
1512 public void onInit(final int status
) {
1513 if(status
==TextToSpeech
.SUCCESS
){
1514 Utils
.sendMessage(context
, replyTo
, speaking
);
1515 tts
.speak(text
, TextToSpeech
.QUEUE_ADD
, null);
1517 Utils
.sendMessage(context
, replyTo
, tts_engine_not_available
);
1523 * Show a toast notification with the default duration.
1525 * @param context Context instance
1526 * @param replyTo reply Address
1527 * @param text toast text
1529 public static void toast(final Context context
, final Address replyTo
, final String text
){
1530 toast(context
, replyTo
, text
, Toast
.LENGTH_SHORT
);
1534 * Show a toast notification.
1536 * @param context Context instance
1537 * @param replyTo reply Address
1538 * @param text toast text
1539 * @param duration toast duration
1541 public static void toast(final Context context
, final Address replyTo
, final String text
, final int duration
){
1542 Toast
.makeText(context
,text
,duration
).show();
1543 Utils
.sendMessage(context
, replyTo
, toast_shown
);
1547 * Make the phone vibrate for a number of milliseconds.
1549 * @param context Context instance
1550 * @param replyTo reply Address
1551 * @param ms vibrate duration, in milliseconds
1553 public static void vibrate(final Context context
, final Address replyTo
, final long ms
){
1554 final Vibrator v
=(Vibrator
) context
.getSystemService(Context
.VIBRATOR_SERVICE
);
1555 Utils
.sendMessage(context
, replyTo
, vibrating
);
1560 * View an URI in an appropriate activity.
1562 * @param context Context instance
1563 * @param replyTo reply Address
1564 * @param uri URI to view
1566 public static void view(final Context context
, final Address replyTo
, final Uri uri
) {
1568 final Intent intent
=new Intent(Intent
.ACTION_VIEW
);
1569 intent
.setData(uri
);
1570 intent
.setFlags(Intent
.FLAG_FROM_BACKGROUND
|Intent
.FLAG_ACTIVITY_NEW_TASK
);
1571 context
.startActivity(intent
);
1572 Utils
.sendMessage(context
, replyTo
, url_opened
);
1573 } catch(ActivityNotFoundException e
){
1574 Utils
.sendMessage(context
, replyTo
, no_activity_found_for_this_url
);
1575 } catch(Exception e
){
1576 Utils
.sendMessage(context
, replyTo
, invalid_url
);
1581 * Get the current WiFi state.
1583 * @param context Context instance
1584 * @param replyTo reply Address
1586 public static void wifi(final Context context
, final Address replyTo
){
1587 final WifiManager man
=(WifiManager
) context
.getSystemService(Context
.WIFI_SERVICE
);
1588 if(man
.isWifiEnabled())
1589 Utils
.sendMessage(context
, replyTo
, wifi_on
);
1591 Utils
.sendMessage(context
, replyTo
, wifi_off
);
1595 * Set the WiFi state.
1597 * @param context Context instance
1598 * @param replyTo reply Address
1599 * @param on the requested WiFi state
1601 public static void wifi(final Context context
, final Address replyTo
, final boolean on
){
1602 final WifiManager man
=(WifiManager
) context
.getSystemService(Context
.WIFI_SERVICE
);
1603 man
.setWifiEnabled(on
);
1605 Utils
.sendMessage(context
, replyTo
, enabling_wifi
);
1607 Utils
.sendMessage(context
, replyTo
, disabling_wifi
);
1611 * Factory reset the phone, optionally deleting the SD card too.
1613 * @param context Context instance
1614 * @param type {@link Utils.WipeType} instance
1615 * @throws SecurityException if FonBot does not have device administration permissions
1617 @SuppressLint("InlinedApi")
1618 public static void wipe(final Context context
, final WipeType type
) throws SecurityException
{
1619 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1626 dpm
.wipeData(DevicePolicyManager
.WIPE_EXTERNAL_STORAGE
);
1632 * Disable a Command. The command cannot be used until enabled again with the {@link Utils.Command#ENABLE ENABLE} command.
1634 * @param context Context instance
1635 * @param replyTo reply Address
1636 * @param command Command to disable
1638 public static void disable(final Context context
, final Address replyTo
, final Command command
){
1639 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
1640 .putBoolean(command
+"disabled", true).commit();
1641 Utils
.sendMessage(context
, replyTo
, command_disabled
, command
);
1645 * Re-enable a disabled Command.
1647 * @param context Context instance
1648 * @param replyTo reply Address
1649 * @param command Command to re-enable
1651 public static void enable(final Context context
, final Address replyTo
, final Command command
){
1652 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
1653 .remove(command
+"disabled").commit();
1654 Utils
.sendMessage(context
, replyTo
, command_enabled
, command
);
1659 * Check whether a Command is disabled.
1661 * @param context Context instance
1662 * @param command Command to check
1663 * @return true if the Command is disabled, false otherwise
1665 public static boolean isCommandDisabled(final Context context
, final Command command
){
1666 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean(command
+"disabled", false);
1670 * Start long polling if stopped
1672 * @param context Context instance
1673 * @param replyTo reply Address
1675 public static void poll(final Context context
, final Address replyTo
) {
1676 Utils
.sendMessage(context
, replyTo
, starting_long_polling_if_stopped
);
1677 context
.startService(new Intent(context
, FonBotMainService
.class));
1681 * Get an instance of {@link ITelephony}
1683 * @param context Context instance
1684 * @return an instance of {@link ITelephony}
1685 * @throws NoSuchMethodException thrown by reflection
1686 * @throws IllegalArgumentException thrown by reflection
1687 * @throws IllegalAccessException thrown by reflection
1688 * @throws InvocationTargetException thrown by reflection
1690 private static ITelephony
getITelephony(final Context context
) throws NoSuchMethodException
, IllegalArgumentException
, IllegalAccessException
, InvocationTargetException
{
1691 final TelephonyManager man
=(TelephonyManager
) context
.getSystemService(Context
.TELEPHONY_SERVICE
);
1692 final Method m
=TelephonyManager
.class.getDeclaredMethod("getITelephony");
1693 m
.setAccessible(true);
1694 return toNonNull((ITelephony
) m
.invoke(man
));
1698 * Hang up the phone.
1700 * @param context Context instance
1701 * @param replyTo reply Address
1703 public static void hangup(final Context context
, final Address replyTo
){
1705 getITelephony(context
).endCall();
1706 } catch(Exception e
){
1707 Utils
.sendMessage(context
, replyTo
, exception_while_hanging_up_call
,
1708 e
.getClass().getName(), e
.getMessage());
1713 * Answer the phone if it is ringing.
1715 * @param context Context instance
1716 * @param replyTo reply Address
1718 public static void answer(final Context context
, final Address replyTo
){
1720 getITelephony(context
).answerRingingCall();
1721 } catch(Exception e
){
1722 Utils
.sendMessage(context
, replyTo
, exception_while_answering_call
,
1723 e
.getClass().getName(), e
.getMessage());
1730 * @param context Context instance
1731 * @param replyTo reply Address
1732 * @param pkg name of the package to launch
1734 public static void launch(final Context context
, final Address replyTo
, final String pkg
){
1735 final Intent intent
=context
.getPackageManager().getLaunchIntentForPackage(pkg
);
1737 Utils
.sendMessage(context
, replyTo
, no_such_package
);
1740 context
.startActivity(intent
);
1741 Utils
.sendMessage(context
, replyTo
, app_launched
);
1745 * Get the mobile data enabled status.
1747 * @param context Context instance
1748 * @param replyTo reply Address
1750 public static void data(final Context context
, final Address replyTo
){
1752 final ConnectivityManager man
=(ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
1753 final Method m
=ConnectivityManager
.class.getDeclaredMethod("getMobileDataEnabled");
1754 m
.setAccessible(true);
1755 if(((Boolean
)m
.invoke(man
)).booleanValue())
1756 Utils
.sendMessage(context
, replyTo
, data_on
);
1758 Utils
.sendMessage(context
, replyTo
, data_off
);
1759 } catch(Exception e
){
1760 Utils
.sendMessage(context
, replyTo
, exception_while_determining_data_state
,
1761 e
.getClass().getName(), e
.getMessage());
1766 * Set the mobile data enabled status.
1768 * @param context Context instance
1769 * @param replyTo reply Address
1770 * @param enable whether to enable mobile data
1772 public static void data(final Context context
, final Address replyTo
, final boolean enable
) {
1775 getITelephony(context
).enableDataConnectivity();
1776 Utils
.sendMessage(context
, replyTo
, enabling_data
);
1778 getITelephony(context
).disableDataConnectivity();
1779 Utils
.sendMessage(context
, replyTo
, disabling_data
);
1781 } catch(Exception e
){
1782 Utils
.sendMessage(context
, replyTo
, exception_while_getting_itelephony
,
1783 e
.getClass().getName(), e
.getMessage());
1788 * Get the GPS status.
1790 * @param context Context instance
1791 * @param replyTo reply Address
1793 public static void gps(final Context context
, final Address replyTo
){
1794 if(Secure
.isLocationProviderEnabled(context
.getContentResolver(), LocationManager
.GPS_PROVIDER
))
1795 Utils
.sendMessage(context
, replyTo
, gps_on
);
1797 Utils
.sendMessage(context
, replyTo
, gps_off
);
1801 * Set the GPS status.
1803 * @param context Context instance
1804 * @param replyTo reply Address
1805 * @param enabled requested GPS status
1807 public static void gps(final Context context
, final Address replyTo
, final boolean enabled
) {
1808 Secure
.setLocationProviderEnabled(context
.getContentResolver(), LocationManager
.GPS_PROVIDER
, enabled
);
1810 Utils
.sendMessage(context
, replyTo
, enabling_gps
);
1812 Utils
.sendMessage(context
, replyTo
, disabling_gps
);
1816 * Get the Google location (aka network location) state.
1818 * @param context Context instance
1819 * @param replyTo reply Address
1821 public static void glocation(final Context context
, final Address replyTo
){
1822 if(Secure
.isLocationProviderEnabled(context
.getContentResolver(), LocationManager
.NETWORK_PROVIDER
))
1823 Utils
.sendMessage(context
, replyTo
, network_location_on
);
1825 Utils
.sendMessage(context
, replyTo
, network_location_off
);
1829 * Set the Google location (aka network location) state.
1831 * @param context Context instance
1832 * @param replyTo reply Address
1833 * @param enabled requested Google location state
1835 public static void glocation(final Context context
, final Address replyTo
, final boolean enabled
) {
1836 Secure
.setLocationProviderEnabled(context
.getContentResolver(), LocationManager
.NETWORK_PROVIDER
, enabled
);
1838 Utils
.sendMessage(context
, replyTo
, enabling_network_location
);
1840 Utils
.sendMessage(context
, replyTo
, disabling_network_location
);
1846 * @param context Context instance
1847 * @param replyTo reply Address
1848 * @param reason reboot reason
1850 * @see PowerManager#reboot(String)
1852 public static void reboot(final Context context
, final Address replyTo
, final @Nullable String reason
) {
1853 final PowerManager pm
=(PowerManager
) context
.getSystemService(Context
.POWER_SERVICE
);
1854 Utils
.sendMessage(context
, replyTo
, rebooting
);
1857 } catch (final Exception e
){
1858 e
.printStackTrace();
1861 Runtime
.getRuntime().exec(new String
[]{
1866 } catch (final Exception e
){
1867 e
.printStackTrace();
1869 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), reboot_failed
);
1873 * Cancel a notification.
1875 * @param context Context instance
1876 * @param replyTo reply Address
1877 * @param id notification ID
1879 public static void notify(final Context context
, final Address replyTo
, final int id
) {
1880 final NotificationManager man
=(NotificationManager
) context
.getSystemService(Context
.NOTIFICATION_SERVICE
);
1882 Utils
.sendMessage(context
, replyTo
, notification_canceled
);
1886 * Show a notification.
1888 * @param context Context instance
1889 * @param replyTo reply Address
1890 * @param id notification ID
1891 * @param title notificationO title
1892 * @param text notification text
1894 public static void notify(final Context context
, final Address replyTo
, final int id
, final String title
, final String text
) {
1895 final NotificationManager man
=(NotificationManager
) context
.getSystemService(Context
.NOTIFICATION_SERVICE
);
1896 man
.notify(id
, new NotificationCompat
.Builder(context
).
1897 setContentTitle(title
).
1898 setContentText(text
).
1899 setSmallIcon(android
.R
.drawable
.stat_notify_sync_noanim
).
1901 Utils
.sendMessage(context
, replyTo
, notification_shown
);
1905 * Take a screen capture. Uses the screencap utility and requires root.
1907 * @param context Context instance
1908 * @param replyTo reply Address
1909 * @param filename capture file location
1911 public static void screencap(final Context context
, final Address replyTo
, final String filename
){
1912 new Thread(new ScreencapRunnable(context
, replyTo
, filename
)).start();
1916 * Toggle the torch state using the Torch (net.cactii.torch2) app.
1918 * @param context Context instance
1919 * @param replyTo reply Address
1921 public static void torch(final Context context
, final Address replyTo
){
1922 context
.sendBroadcast(new Intent("net.cactii.flash2.TOGGLE_FLASHLIGHT"));
1923 Utils
.sendMessage(context
, replyTo
, toggling_torch_state
);
1927 * Download a file from a given URL to a given filename
1929 * @param context Context instance
1930 * @param replyTo reply Address
1931 * @param filename filename to save to
1932 * @param hostname hostname to download from
1933 * @param port port to download from
1935 public static void getfile(final Context context
, final Address replyTo
, final String filename
, final String hostname
, final int port
){
1936 new GetfileExecutableRunnable(context
, replyTo
, filename
, hostname
, port
).execute();
1940 * Execute a command using a given shell and reply with the output.
1942 * @param context Context instance
1943 * @param replyTo reply Address
1944 * @param shell The shell to execute with. Usually sh or su.
1945 * @param command The command to pass to the shell.
1947 public static void execute(final Context context
, final Address replyTo
, final String shell
, final String command
) {
1949 final Process proc
= Runtime
.getRuntime().exec(new String
[]{
1954 final BufferedReader br
= new BufferedReader (new InputStreamReader(proc
.getInputStream()));
1956 while((line
= br
.readLine()) != null)
1957 Utils
.sendMessage(context
, replyTo
, line
);
1959 } catch (final Exception e
){
1960 Utils
.sendMessage(context
, replyTo
, error_while_processing_command
, e
.getClass().getName(), e
.getMessage());
1961 Log
.w(Heavy
.class.getName(), "Error while processing command", e
);