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
.FileOutputStream
;
9 import java
.io
.FileNotFoundException
;
10 import java
.io
.InputStream
;
11 import java
.io
.IOException
;
12 import java
.io
.OutputStream
;
13 import java
.lang
.reflect
.InvocationTargetException
;
14 import java
.lang
.reflect
.Method
;
15 import java
.net
.InetSocketAddress
;
16 import java
.net
.Socket
;
17 import java
.net
.UnknownHostException
;
19 import java
.net
.URLConnection
;
20 import java
.nio
.channels
.FileChannel
;
21 import java
.nio
.channels
.SocketChannel
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Date
;
25 import org
.eclipse
.jdt
.annotation
.Nullable
;
27 import ro
.ieval
.fonbot
.Utils
.Command
;
28 import ro
.ieval
.fonbot
.Utils
.MessageType
;
29 import ro
.ieval
.fonbot
.Utils
.OngoingEvent
;
30 import ro
.ieval
.fonbot
.Utils
.RingerMode
;
31 import ro
.ieval
.fonbot
.Utils
.WipeType
;
32 import android
.annotation
.SuppressLint
;
33 import android
.app
.AlarmManager
;
34 import android
.app
.NotificationManager
;
35 import android
.app
.PendingIntent
;
36 import android
.app
.admin
.DevicePolicyManager
;
37 import android
.bluetooth
.BluetoothAdapter
;
38 import android
.content
.ActivityNotFoundException
;
39 import android
.content
.Context
;
40 import android
.content
.Intent
;
41 import android
.content
.IntentFilter
;
42 import android
.database
.Cursor
;
43 import android
.graphics
.ImageFormat
;
44 import android
.hardware
.Camera
;
45 import android
.hardware
.Camera
.PictureCallback
;
46 import android
.location
.Location
;
47 import android
.location
.LocationListener
;
48 import android
.location
.LocationManager
;
49 import android
.location
.LocationProvider
;
50 import android
.media
.AudioManager
;
51 import android
.media
.Ringtone
;
52 import android
.media
.RingtoneManager
;
53 import android
.net
.ConnectivityManager
;
54 import android
.net
.Uri
;
55 import android
.net
.wifi
.WifiManager
;
56 import android
.os
.AsyncTask
;
57 import android
.os
.BatteryManager
;
58 import android
.os
.Bundle
;
59 import android
.os
.Handler
;
60 import android
.os
.PowerManager
;
61 import android
.os
.SystemClock
;
62 import android
.os
.Vibrator
;
63 import android
.preference
.PreferenceManager
;
64 import android
.provider
.BaseColumns
;
65 import android
.provider
.CallLog
.Calls
;
66 import android
.provider
.ContactsContract
.CommonDataKinds
;
67 import android
.provider
.ContactsContract
.CommonDataKinds
.BaseTypes
;
68 import android
.provider
.ContactsContract
.CommonDataKinds
.Phone
;
69 import android
.provider
.ContactsContract
.Contacts
;
70 import android
.provider
.ContactsContract
.Data
;
71 import android
.provider
.Settings
.Secure
;
72 import android
.speech
.tts
.TextToSpeech
;
73 import android
.speech
.tts
.TextToSpeech
.OnInitListener
;
74 import android
.support
.v4
.app
.NotificationCompat
;
75 import android
.telephony
.SmsManager
;
76 import android
.telephony
.TelephonyManager
;
77 import android
.util
.Log
;
78 import android
.view
.SurfaceView
;
79 import android
.widget
.Toast
;
81 import com
.android
.internal
.telephony
.ITelephony
;
84 * Copyright © 2013 Marius Gavrilescu
86 * This file is part of FonBot.
88 * FonBot is free software: you can redistribute it and/or modify
89 * it under the terms of the GNU General Public License as published by
90 * the Free Software Foundation, either version 3 of the License, or
91 * (at your option) any later version.
93 * FonBot is distributed in the hope that it will be useful,
94 * but WITHOUT ANY WARRANTY; without even the implied warranty of
95 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
96 * GNU General Public License for more details.
98 * You should have received a copy of the GNU General Public License
99 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
103 * Implementation of all FonBot commands. The methods of this class do not do argument checking.
105 * @author Marius Gavrilescu <marius@ieval.ro>
109 * LocationListener that sends notifications to the user.
111 * @author Marius Gavrilescu <marius@ieval.ro>
113 private static final class FonBotLocationListener
implements LocationListener
{
114 /** Context instance */
115 private final Context context
;
116 /** Destination address for notifications */
117 private final Address replyTo
;
120 * Construct a FonBotLocationListener.
122 * @param context Context instance
123 * @param replyTo the reply address
125 FonBotLocationListener(final Context context
, final Address replyTo
) {
126 this.context
=context
;
127 this.replyTo
=replyTo
;
131 public void onLocationChanged(@Nullable final Location loc
) {
134 final StringBuilder sb
=new StringBuilder(toNonNull(context
.getString(location
)));
136 sb
.append(toNonNull(context
.getString(latitude
)));
138 sb
.append(loc
.getLatitude());
140 sb
.append(toNonNull(context
.getString(longitude
)));
142 sb
.append(loc
.getLongitude());
144 if(loc
.hasAccuracy()){
146 sb
.append(toNonNull(context
.getString(accuracy
)));
148 sb
.append(loc
.getAccuracy());
151 if(loc
.hasAltitude()){
153 sb
.append(toNonNull(context
.getString(altitude
)));
155 sb
.append(loc
.getAltitude());
158 if(loc
.hasBearing()){
160 sb
.append(toNonNull(context
.getString(bearing
)));
162 sb
.append(loc
.getBearing());
167 sb
.append(toNonNull(context
.getString(speed
)));
169 sb
.append(loc
.getSpeed());
172 final Date locationDate
=new Date(loc
.getTime());
174 sb
.append(toNonNull(context
.getString(at
)));
176 sb
.append(locationDate
.toString());
179 sb
.append("http://openstreetmap.org/?zoom=15&mlat=");
180 sb
.append(loc
.getLatitude());
182 sb
.append(loc
.getLongitude());
183 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), toNonNull(sb
.toString()));
187 public void onProviderDisabled(@Nullable final String provider
) {
188 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), location_provider_disabled
, provider
);
192 public void onProviderEnabled(@Nullable final String provider
) {
193 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), location_provider_enabled
, provider
);
197 public void onStatusChanged(@Nullable final String provider
, final int status
, @Nullable final Bundle extras
) {
200 case LocationProvider
.AVAILABLE
:
201 state
=location_provider_available
;
203 case LocationProvider
.TEMPORARILY_UNAVAILABLE
:
204 state
=location_provider_temporary_unavailable
;
206 case LocationProvider
.OUT_OF_SERVICE
:
207 state
=location_provider_out_of_service
;
210 state
=location_provider_unknown_state
;
212 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), state
, provider
);
216 * Currently active FonBotLocationListener
218 private static FonBotLocationListener locationListener
= null;
221 * AsyncTask that sends a byte[] to a server
223 * @author Marius Gavrilescu <marius@ieval.ro>
226 private static class SendDataAsyncTask
extends AsyncTask
<Void
, Void
, Void
>{
228 * Context instance used by this class
230 private final Context context
;
234 private final String hostname
;
238 private final int port
;
242 private final byte[] data
;
244 * Address for sending back errors and success messages
246 private final Address replyTo
;
249 * Constructs a SendDataAsyncTasks from its parameters
251 * @param context the context
252 * @param replyTo the reply Address
253 * @param hostname the server hostname
254 * @param port the server port
255 * @param data the data to send
257 public SendDataAsyncTask(final Context context
,final Address replyTo
, final String hostname
, final int port
, final byte[] data
) {//NOPMD array is immutable
259 this.context
=context
;
260 this.hostname
=hostname
;
263 this.replyTo
=replyTo
;
267 protected @Nullable Void
doInBackground(@Nullable final Void
... params
) {
270 sock
= new Socket(hostname
, port
);
272 sock
.getOutputStream().write(data
);
273 } catch (IOException e
) {
274 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), error_writing_to_socket
, e
.getMessage());
280 } catch (IOException e
) {
281 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), cannot_close_socket
, e
.getMessage());
284 } catch (UnknownHostException e
) {
285 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), unknown_host
, hostname
);
287 } catch (IOException e
) {
288 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), cannot_connect_to_host_on_port
, hostname
, Integer
.valueOf(port
));
291 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), photo_sent
);
297 * ExecutableRunnable that uploads a file to a given host and port, netcat-style
299 * @author Marius Gavrilescu <marius@ieval.ro>
301 private static final class NcfileExecutableRunnable
extends ExecutableRunnable
{
302 private final Context context
;
303 private final Address replyTo
;
304 private final String filename
;
305 private final String hostname
;
306 private final int port
;
309 * Construct a NcfileExecutableRunnable
311 * @param context Context instance
312 * @param replyTo reply Address
313 * @param filename filename to upload
314 * @param hostname hostname to upload to
315 * @param port port to upload to
317 NcfileExecutableRunnable(final Context context
, final Address replyTo
, final String filename
, final String hostname
, final int port
){
318 this.context
=context
;
319 this.replyTo
=replyTo
;
320 this.filename
=filename
;
321 this.hostname
=hostname
;
325 @Override public void run(){
326 final FileChannel in
;
327 final SocketChannel sock
;
329 in
=new FileInputStream(filename
).getChannel();
330 } catch (final FileNotFoundException e
){
331 Utils
.sendMessage(context
, replyTo
, file_not_found
, filename
);
336 sock
= SocketChannel
.open(new InetSocketAddress(hostname
, port
));
337 } catch (final IOException e
){
338 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(cannot_connect_to_host_on_port
, hostname
, Integer
.valueOf(port
))));
341 } catch (IOException ex
) {
348 in
.transferTo(0, in
.size(), sock
);
349 } catch (final IOException e
){
350 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(io_error
, e
.getMessage())));
354 } catch (IOException e
){
359 } catch(IOException e
){
363 Utils
.sendMessage(context
, replyTo
, file_sent
);
368 * ExecutableRunnable that downloads a file from a given host and port, netcat-style
370 * @author Marius Gavrilescu <marius@ieval.ro>
372 private static final class GetfileExecutableRunnable
extends ExecutableRunnable
{
373 private final Context context
;
374 private final Address replyTo
;
375 private final String filename
;
376 private final String hostname
;
377 private final int port
;
380 * Construct a GetfileExecutableRunnable
382 * @param context Context instance
383 * @param replyTo reply Address
384 * @param filename filename to save to
385 * @param hostname hostname to download from
386 * @param port port to download from
388 GetfileExecutableRunnable(final Context context
, final Address replyTo
, final String filename
, final String hostname
, final int port
){
389 this.context
=context
;
390 this.replyTo
=replyTo
;
391 this.filename
=filename
;
392 this.hostname
=hostname
;
396 @Override public void run(){
397 final InputStream in
;
398 final FileOutputStream out
;
401 out
=new FileOutputStream(filename
);
402 } catch (final IOException e
){
403 Utils
.sendMessage(context
, replyTo
, error_opening_file
, filename
, e
.getMessage());
408 sock
= new Socket(hostname
, port
);
409 in
= sock
.getInputStream();
410 } catch (final IOException e
){
411 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(cannot_connect_to_host_on_port
, hostname
, Integer
.valueOf(port
))));
414 } catch (IOException ex
) {
421 byte[] buffer
=new byte[1024*1024*2];
423 while((nread
= in
.read(buffer
)) > 0)
424 out
.write(buffer
, 0, nread
);
425 } catch (final IOException e
){
426 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(io_error
, e
.getMessage())));
430 } catch (IOException e
){
435 } catch(IOException e
){
439 Utils
.sendMessage(context
, replyTo
, file_received
);
444 * PictureCallback that sends the picture to a server.
446 * @author Marius Gavrilescu <marius@ieval.ro>
448 private static final class FonBotPictureCallback
implements PictureCallback
{
449 /** Server hostname */
450 private final String hostname
;
452 private final int port
;
453 /** Context instance */
454 private final Context context
;
456 private final Address replyTo
;
459 * Construct a FonBotPictureCallback.
461 * @param context Context instance
462 * @param replyTo reply Address
463 * @param hostname server hostname
464 * @param port server port
466 FonBotPictureCallback(final Context context
, final Address replyTo
, final String hostname
, final int port
) {
467 this.hostname
=hostname
;
469 this.context
=context
;
470 this.replyTo
=replyTo
;
474 @SuppressWarnings("hiding")
475 public void onPictureTaken(final @Nullable byte[] data
, final @Nullable Camera camera
) {
476 if(camera
==null || data
==null)
478 camera
.stopPreview();
480 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), sending_photo
);
481 new SendDataAsyncTask(toNonNull(context
), toNonNull(replyTo
), toNonNull(hostname
), port
, data
).execute();
486 * Runnable that takes a screen capture and stores it in a file.
488 private static final class ScreencapRunnable
implements Runnable
{
489 private final Context context
;
490 private final Address replyTo
;
491 private final String filename
;
493 ScreencapRunnable(final Context context
, final Address replyTo
, final String filename
){
494 this.context
=context
;
495 this.replyTo
=replyTo
;
496 this.filename
=filename
;
503 exitCode
=Runtime
.getRuntime().exec(new String
[]{
506 "screencap -p \"" + filename
+ "\""
508 } catch (final Exception e
){
510 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), screencap_failed
);
514 if(exitCode
== 0 && new File(filename
).exists())
515 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), screencap_successful
);
517 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), screencap_failed
);
522 * Get help for a particular command
524 * @param context Context instance
525 * @param replyTo reply Address
526 * @param command command to get help for
528 public static void help(final Context context
, final Address replyTo
, final Command command
){//NOPMD method is a big switch statement. Nothing confusing.
531 Utils
.sendMessage(context
, replyTo
, answer_help
);
534 Utils
.sendMessage(context
, replyTo
, batt_help
);
537 Utils
.sendMessage(context
, replyTo
, bluetooth_help
);
540 Utils
.sendMessage(context
, replyTo
, calllog_help
);
543 Utils
.sendMessage(context
, replyTo
, contacts_help
);
546 Utils
.sendMessage(context
, replyTo
, data_help
);
548 case DELNOTIFICATION
:
549 Utils
.sendMessage(context
, replyTo
, delnotification_help
, Utils
.join(", ", toNonNull(MessageType
.values())));
552 Utils
.sendMessage(context
, replyTo
, dial_help
);
555 Utils
.sendMessage(context
, replyTo
, dialog_help
);
558 Utils
.sendMessage(context
, replyTo
, disable_help
, Utils
.join(", ", toNonNull(Command
.values())));
561 Utils
.sendMessage(context
, replyTo
, echo_help
);
564 Utils
.sendMessage(context
, replyTo
, enable_help
, Utils
.join(", ", toNonNull(Command
.values())));
567 Utils
.sendMessage(context
, replyTo
, flash_help
);
570 Utils
.sendMessage(context
, replyTo
, glocation_help
);
573 Utils
.sendMessage(context
, replyTo
, gps_help
);
576 Utils
.sendMessage(context
, replyTo
, hangup_help
);
579 Utils
.sendMessage(context
, replyTo
, help_help
, Utils
.join(", ",toNonNull(Command
.values())));
582 Utils
.sendMessage(context
, replyTo
, launch_help
);
585 Utils
.sendMessage(context
, replyTo
, location_help
, Utils
.join(", ",toNonNull(Utils
.LocationProvider
.values())));
588 Utils
.sendMessage(context
, replyTo
, lock_help
);
591 Utils
.sendMessage(context
, replyTo
, ls_help
);
594 Utils
.sendMessage(context
, replyTo
, ncfile_help
);
597 Utils
.sendMessage(context
, replyTo
, next_help
);
600 Utils
.sendMessage(context
, replyTo
, nolocation_help
);
603 Utils
.sendMessage(context
, replyTo
, pause_help
);
606 Utils
.sendMessage(context
, replyTo
, photo_help
);
609 Utils
.sendMessage(context
, replyTo
, play_help
);
612 Utils
.sendMessage(context
, replyTo
, poll_help
);
615 Utils
.sendMessage(context
, replyTo
, prev_help
);
618 Utils
.sendMessage(context
, replyTo
, ring_help
);
621 Utils
.sendMessage(context
, replyTo
, ringer_help
, Utils
.join(", ", toNonNull(RingerMode
.values())));
624 Utils
.sendMessage(context
, replyTo
, rm_help
);
626 case SETNOTIFICATION
:
627 Utils
.sendMessage(context
, replyTo
, setnotification_help
, Utils
.join(", ", toNonNull(MessageType
.values())));
630 Utils
.sendMessage(context
, replyTo
, setpassword_help
);
633 Utils
.sendMessage(context
, replyTo
, sms_help
);
636 Utils
.sendMessage(context
, replyTo
, smslog_help
);
639 Utils
.sendMessage(context
, replyTo
, speak_help
);
642 Utils
.sendMessage(context
, replyTo
, toast_help
);
645 Utils
.sendMessage(context
, replyTo
, vibrate_help
);
648 Utils
.sendMessage(context
, replyTo
, view_help
);
651 Utils
.sendMessage(context
, replyTo
, wifi_help
);
654 Utils
.sendMessage(context
, replyTo
, wipe_help
, Utils
.WIPE_CONFIRM_STRING
);
657 Utils
.sendMessage(context
, replyTo
, reboot_help
);
660 Utils
.sendMessage(context
, replyTo
, notify_help
);
663 Utils
.sendMessage(context
, replyTo
, screencap_help
);
666 Utils
.sendMessage(context
, replyTo
, torch_help
);
674 * @see #startCamera(Context, Address)
677 private static Camera camera
;
679 * Ringtone used by the {@link Utils.Command#RING RING} command.
681 * @see #setupRingtone(Context)
683 private static Ringtone ringtone
;
685 * Saved ringer volume.
687 * @see #startAlarm(Context, Address)
688 * @see #stopAlarm(Context, Address)
690 private static int savedRingVolume
;
694 * @see #startAlarm(Context, Address)
695 * @see #stopAlarm(Context, Address)
697 private static int savedRingerMode
;
699 /** Private constructor */
705 * Convert a phone number type to a string
707 * @param context Context instance
708 * @param type phone number type
709 * @param label name of a custom phone type
710 * @return the phone number type
712 private static @Nullable String
phoneNumberType(final Context context
, final int type
, final @Nullable String label
) {
714 case BaseTypes
.TYPE_CUSTOM
:
716 case Phone
.TYPE_ASSISTANT
:
717 return context
.getString(phone_numer_type_assistant
);
718 case Phone
.TYPE_CALLBACK
:
719 return context
.getString(phone_number_type_callback
);
721 return context
.getString(phone_number_type_car
);
722 case Phone
.TYPE_COMPANY_MAIN
:
723 return context
.getString(phone_number_type_company_main
);
724 case Phone
.TYPE_FAX_HOME
:
725 return context
.getString(phone_number_type_home_fax
);
726 case Phone
.TYPE_FAX_WORK
:
727 return context
.getString(phone_number_type_work_fax
);
728 case Phone
.TYPE_HOME
:
729 return context
.getString(phone_number_type_home
);
730 case Phone
.TYPE_ISDN
:
731 return context
.getString(phone_number_type_isdn
);
732 case Phone
.TYPE_MAIN
:
733 return context
.getString(phone_number_type_main
);
735 return context
.getString(phone_number_type_mms
);
736 case Phone
.TYPE_MOBILE
:
737 return context
.getString(phone_number_type_mobile
);
738 case Phone
.TYPE_OTHER
:
739 return context
.getString(phone_number_type_other
);
740 case Phone
.TYPE_OTHER_FAX
:
741 return context
.getString(phone_number_type_other_fax
);
742 case Phone
.TYPE_PAGER
:
743 return context
.getString(phone_number_type_pager
);
744 case Phone
.TYPE_RADIO
:
745 return context
.getString(phone_number_type_radio
);
746 case Phone
.TYPE_TELEX
:
747 return context
.getString(phone_number_type_telex
);
748 case Phone
.TYPE_TTY_TDD
:
749 return context
.getString(phone_number_type_textphone
);
750 case Phone
.TYPE_WORK
:
751 return context
.getString(phone_number_type_work
);
752 case Phone
.TYPE_WORK_MOBILE
:
753 return context
.getString(phone_number_type_work_mobile
);
754 case Phone
.TYPE_WORK_PAGER
:
755 return context
.getString(phone_number_type_work_pager
);
758 return context
.getString(phone_number_type_unknown
, Integer
.valueOf(type
));
762 * Setup the ringtone used by the {@link Utils.Command#RING RING} command
764 * @param context Context
766 private static void setupRingtone(final Context context
){
767 if(ringtone
==null){//NOPMD not supposed to be thread-safe
768 final Uri alert
=RingtoneManager
.getDefaultUri(RingtoneManager
.TYPE_RINGTONE
);
769 ringtone
=RingtoneManager
.getRingtone(context
, alert
);
774 * Make the phone start ringing. Turns up the volume and sets the ringer mode to NORMAL
776 * @param context Context instance
777 * @param replyTo reply Address
779 private static void startAlarm(final Context context
, final Address replyTo
){
780 Utils
.registerOngoing(context
, toNonNull(OngoingEvent
.RING
));
781 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
782 savedRingerMode
=man
.getRingerMode();
783 man
.setRingerMode(AudioManager
.RINGER_MODE_NORMAL
);
784 savedRingVolume
=man
.getStreamVolume(AudioManager
.STREAM_RING
);
785 man
.setStreamVolume(AudioManager
.STREAM_RING
, man
.getStreamMaxVolume(AudioManager
.STREAM_RING
), 0);
786 Utils
.sendMessage(context
, replyTo
, ringing
);
791 * Get a camera instance.
793 * @param context Context instance
794 * @param replyTo reply Address
796 private static void startCamera(final Context context
, final Address replyTo
, final int cameraNumber
){
801 camera
=Camera
.open(cameraNumber
);
802 } catch (Exception ex
){
803 camera
=Camera
.open();
805 } catch (Exception e
){
806 Utils
.sendMessage(context
, replyTo
, cannot_grab_camera
);
811 * Make the phone stop ringing. Restores the volume and ringer mode.
813 * @param context Context instance
814 * @param replyTo reply Address
816 private static void stopAlarm(final Context context
, final Address replyTo
){
817 Utils
.unregisterOngoing(context
, toNonNull(OngoingEvent
.RING
));
818 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
819 Utils
.sendMessage(context
, replyTo
, no_longer_ringing
);
821 man
.setStreamVolume(AudioManager
.STREAM_RING
, savedRingVolume
, 0);
822 man
.setRingerMode(savedRingerMode
);
826 * Release the previously grabbed camera instance
828 * @see #startCamera(Context, Address)
830 private static void stopCamera(){
838 * Send battery status information to an Address
840 * @param context Context instance
841 * @param replyTo destination Address
843 * @see #describeBatteryLevel(Context, Address, MessageType)
845 public static void batt(final Context context
, final Address replyTo
){
846 describeBatteryLevel(context
, replyTo
, null);
850 * Show the bluetooth radio status.
852 * @param context Context instance
853 * @param replyTo destination Address
855 public static void bluetooth(final Context context
, final Address replyTo
) {
856 final BluetoothAdapter adapter
=BluetoothAdapter
.getDefaultAdapter();
858 Utils
.sendMessage(context
, replyTo
, no_bluetooth_adapter
);
862 if(adapter
.isEnabled())
863 Utils
.sendMessage(context
, replyTo
, bluetooth_on
);
865 Utils
.sendMessage(context
, replyTo
, bluetooth_off
);
869 * Set the bluetooth radio status.
871 * @param context Context instance
872 * @param replyTo destination Address
873 * @param on the requested radio status
875 public static void bluetooth(final Context context
, final Address replyTo
, final boolean on
){
876 final BluetoothAdapter adapter
=BluetoothAdapter
.getDefaultAdapter();
878 Utils
.sendMessage(context
, replyTo
, no_bluetooth_adapter
);
884 Utils
.sendMessage(context
, replyTo
, enabling_bluetooth
);
888 Utils
.sendMessage(context
, replyTo
, disabling_bluetooth
);
893 * Cancel an ongoing event.
895 * @param context Context instance
896 * @param event the event to cancel
898 public static void cancelOngoing(final Context context
, final OngoingEvent event
){
901 nolocation(context
, toNonNull(Address
.BLACKHOLE
));
904 ring(context
, toNonNull(Address
.BLACKHOLE
), false);
910 * Send the last calls to an Address.
912 * @param context Context instance
913 * @param replyTo destination Address
914 * @param numCalls how many calls to send
916 public static void calllog(final Context context
, final Address replyTo
, final int numCalls
) {
917 final String
[] fields
= {
918 Calls
.TYPE
, Calls
.NUMBER
, Calls
.CACHED_NAME
, Calls
.DURATION
, Calls
.DATE
921 final Cursor cursor
= context
.getContentResolver().query(
929 if (cursor
.moveToFirst()) {
931 final StringBuilder sb
=new StringBuilder(50);//NOPMD different strings
932 final int type
=cursor
.getInt(0);
933 final String from
=cursor
.getString(1);
936 case Calls
.INCOMING_TYPE
:
937 sb
.append(context
.getString(incoming_call_from
, from
));
939 case Calls
.MISSED_TYPE
:
940 sb
.append(context
.getString(missed_call_from
, from
));
942 case Calls
.OUTGOING_TYPE
:
943 sb
.append(context
.getString(outgoing_call_to
, from
));
947 if (cursor
.getString(2) != null)
948 sb
.append('(').append(cursor
.getString(2)).append(") ");
950 sb
.append(context
.getString(duration_seconds_starting_at
,
951 Long
.valueOf(cursor
.getLong(3)),
952 new Date(cursor
.getLong(4))));
954 Utils
.sendMessage(context
, replyTo
, toNonNull(sb
.toString()));
955 } while (cursor
.moveToNext() && cursor
.getPosition() < numCalls
);
962 * Search for contacts by name/nickname and send matching entries to an Address.
964 * @param context Context instance
965 * @param replyTo destination Address
966 * @param name name/nickname part to search for
968 @SuppressLint("StringFormatMatches")
969 public static void contacts(final Context context
, final Address replyTo
, final String name
){
970 final Cursor cursor
=context
.getContentResolver().query(Uri
.withAppendedPath(
971 Contacts
.CONTENT_FILTER_URI
, name
),
972 new String
[]{Contacts
.DISPLAY_NAME
, BaseColumns
._ID
, Contacts
.LOOKUP_KEY
},
973 null, null, Contacts
.DISPLAY_NAME
);
975 if(cursor
.getCount()==0)
976 Utils
.sendMessage(context
, replyTo
, no_matching_contacts_found
);
978 while(cursor
.moveToNext()){
979 final String
[] fields
= {
980 CommonDataKinds
.Phone
.NUMBER
,
981 CommonDataKinds
.Phone
.TYPE
,
982 CommonDataKinds
.Phone
.LABEL
,
985 final Cursor inCursor
=context
.getContentResolver().query(Data
.CONTENT_URI
,
987 Data
.CONTACT_ID
+" = ? AND "+Data
.MIMETYPE
+ " = ?",
988 new String
[]{Long
.toString(cursor
.getLong(1)), CommonDataKinds
.Phone
.CONTENT_ITEM_TYPE
},
989 CommonDataKinds
.Phone
.LABEL
);
991 while(inCursor
.moveToNext())
992 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(contact_info
,
994 inCursor
.getString(0),
995 phoneNumberType(context
, inCursor
.getInt(1), inCursor
.getString(2)))));
1004 * Send battery status information to an Address or as a notification
1006 * @param context Context instance
1007 * @param replyTo Address to send the information to, if sending to a direct address. Null otherwise.
1008 * @param type Notification type, if sending as a notification. Null otherwise.
1010 public static void describeBatteryLevel(final Context context
, final @Nullable Address replyTo
, final @Nullable MessageType type
) {
1011 if(replyTo
==null&&type
==null)
1013 final Intent intent
=context
.registerReceiver(null, new IntentFilter(Intent
.ACTION_BATTERY_CHANGED
));
1016 final double level
=intent
.getIntExtra(BatteryManager
.EXTRA_LEVEL
, 0);
1017 final int scale
=intent
.getIntExtra(BatteryManager
.EXTRA_SCALE
, 100);
1018 final int plugged
=intent
.getIntExtra(BatteryManager
.EXTRA_PLUGGED
, 0);
1019 final int status
=intent
.getIntExtra(BatteryManager
.EXTRA_STATUS
, BatteryManager
.BATTERY_STATUS_UNKNOWN
);
1020 final int temp
=intent
.getIntExtra(BatteryManager
.EXTRA_TEMPERATURE
, 0);
1021 final int volt
=intent
.getIntExtra(BatteryManager
.EXTRA_VOLTAGE
, 0);
1023 final StringBuilder sb
=new StringBuilder(100);
1024 sb
.append(context
.getString(battery_level
, Double
.valueOf(level
*100/scale
)));
1028 sb
.append(context
.getString(not_plugged_in
));
1030 case BatteryManager
.BATTERY_PLUGGED_AC
:
1031 sb
.append(context
.getString(plugged_in_ac
));
1033 case BatteryManager
.BATTERY_PLUGGED_USB
:
1034 sb
.append(context
.getString(plugged_in_usb
));
1036 case BatteryManager
.BATTERY_PLUGGED_WIRELESS
:
1037 sb
.append(context
.getString(plugged_in_wireless
));
1042 case BatteryManager
.BATTERY_STATUS_CHARGING
:
1043 sb
.append(context
.getString(status_charging
));
1045 case BatteryManager
.BATTERY_STATUS_DISCHARGING
:
1046 sb
.append(context
.getString(status_discharging
));
1048 case BatteryManager
.BATTERY_STATUS_FULL
:
1049 sb
.append(context
.getString(status_full
));
1051 case BatteryManager
.BATTERY_STATUS_NOT_CHARGING
:
1052 sb
.append(context
.getString(status_not_charging
));
1054 case BatteryManager
.BATTERY_STATUS_UNKNOWN
:
1055 sb
.append(context
.getString(status_unknown
));
1059 sb
.append(context
.getString(temperature
, Integer
.valueOf(temp
)));
1061 sb
.append(context
.getString(voltage
, Integer
.valueOf(volt
)));
1063 Utils
.sendMessage(context
, toNonNull(replyTo
), toNonNull(sb
.toString()));
1065 Utils
.sendMessage(context
, type
, toNonNull(sb
.toString()));
1069 * Dial a phone number.
1071 * @param context Context instance
1072 * @param replyTo reply Address
1073 * @param nr phone number to dial
1075 public static void dial(final Context context
, final Address replyTo
, final String nr
){
1076 final Intent intent
=new Intent(Intent
.ACTION_CALL
,Uri
.parse("tel:"+nr
));
1077 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
1078 final String name
=Utils
.callerId(context
, nr
);
1080 Utils
.sendMessage(context
, replyTo
, dialing
, nr
);
1082 Utils
.sendMessage(context
, replyTo
, dialing
, nr
+" ("+name
+")");
1083 context
.startActivity(intent
);
1087 * Show a dialog with a message and a list of buttons.
1089 * @param context Context instance
1090 * @param replyTo reply Address
1091 * @param message dialog message
1092 * @param buttons dialog buttons
1094 public static void dialog(final Context context
, final Address replyTo
, final String message
, final String
[] buttons
){
1095 final Intent intent
=new Intent(context
, DialogActivity
.class);
1096 intent
.putExtra(DialogActivity
.EXTRA_MESSAGE
, message
);
1097 intent
.putExtra(DialogActivity
.EXTRA_BUTTONS
, buttons
);
1098 intent
.putExtra(DialogActivity
.EXTRA_REPLYTO
, replyTo
.toString());
1100 Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
|
1101 Intent
.FLAG_ACTIVITY_NEW_TASK
|
1102 Intent
.FLAG_ACTIVITY_NO_USER_ACTION
|
1103 Intent
.FLAG_FROM_BACKGROUND
);
1104 Utils
.sendMessage(context
, toNonNull(replyTo
), showing_dialog
);
1105 context
.startActivity(intent
);
1109 * Turns the flashlight on or off.
1111 * @param context Context instance
1112 * @param replyTo reply Address
1113 * @param on requested flashlight state
1115 public static void flash(final Context context
, final Address replyTo
, final boolean on
){
1116 startCamera(context
, replyTo
, 0);
1119 final Camera
.Parameters parms
=camera
.getParameters();
1121 parms
.setFlashMode(Camera
.Parameters
.FLASH_MODE_TORCH
);
1122 camera
.setParameters(parms
);
1124 parms
.setFlashMode(Camera
.Parameters
.FLASH_MODE_AUTO
);
1125 camera
.setParameters(parms
);
1131 * Start sending location updates to an Address.
1133 * @param context Context instance
1134 * @param replyTo destination Address
1135 * @param provider LocationProvider
1136 * @param minTime minimum time between two consecutive updates (in ms)
1137 * @param minDistance minimum distance between two consecutive updates (in meters)
1139 * @see LocationManager#requestLocationUpdates(String, long, float, LocationListener)
1141 public static void location(final Context context
, final Address replyTo
, final String provider
,final long minTime
,final float minDistance
){
1142 final LocationManager man
=(LocationManager
) context
.getApplicationContext().getSystemService(Context
.LOCATION_SERVICE
);
1143 if(locationListener
!=null)
1144 nolocation(context
, toNonNull(Address
.BLACKHOLE
));
1145 Utils
.registerOngoing(context
, toNonNull(OngoingEvent
.LOCATION
));
1146 locationListener
=new FonBotLocationListener(context
, replyTo
);
1147 man
.removeUpdates(locationListener
);
1148 final Location lastKnownLocation
=man
.getLastKnownLocation(provider
);
1149 if(lastKnownLocation
!=null){
1150 Utils
.sendMessage(context
, replyTo
, last_known_location
);
1151 locationListener
.onLocationChanged(lastKnownLocation
);
1153 Utils
.sendMessage(context
, replyTo
, listening_for_location_updates
);
1154 man
.requestLocationUpdates(provider
, minTime
, minDistance
, locationListener
);
1160 * @param context Context instance
1161 * @param replyTo reply Address
1163 public static void lock(final Context context
, final Address replyTo
) {
1164 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1166 Utils
.sendMessage(context
, replyTo
, device_locked
);
1170 * Send a command to a running instance of the music player
1172 * @param context Context instance
1173 * @param replyTo reply Address
1174 * @param command command to send
1176 public static void musicPlayerCommand(final Context context
, final Address replyTo
, final String command
) {
1177 final Intent intent
=new Intent("com.android.music.musicservicecommand");
1178 intent
.putExtra("command", command
);
1179 context
.sendBroadcast(intent
);
1180 Utils
.sendMessage(context
, replyTo
, command_sent
);
1184 * Send a file to a server.
1186 * @param context Context instance
1187 * @param replyTo reply Address
1188 * @param filename file to send
1189 * @param hostname server hostname
1190 * @param port server port
1192 public static void ncfile(final Context context
, final Address replyTo
, final String filename
,final String hostname
,final int port
){
1193 new NcfileExecutableRunnable(context
, replyTo
, filename
, hostname
, port
).execute();
1197 * Stop sending location updates.
1199 * @param context Context instance
1200 * @param replyTo reply Address
1202 public static void nolocation(final Context context
, final Address replyTo
){
1203 Utils
.unregisterOngoing(context
, toNonNull(OngoingEvent
.LOCATION
));
1204 final LocationManager man
=(LocationManager
) context
.getApplicationContext().getSystemService(Context
.LOCATION_SERVICE
);
1205 man
.removeUpdates(locationListener
);
1206 locationListener
=null;
1207 Utils
.sendMessage(context
, replyTo
, no_longer_listening_for_location_updates
);
1211 * Take a photo and send it to a server.
1213 * @param context Context instance
1214 * @param replyTo reply Address
1215 * @param cameraNumber camera to take photo with
1216 * @param hostname server hostname
1217 * @param port server port
1219 public static void photo(final Context context
, final Address replyTo
, final int cameraNumber
, final String hostname
, final int port
){
1220 startCamera(context
, replyTo
, cameraNumber
);
1223 final Camera
.Parameters parms
=camera
.getParameters();
1224 parms
.setJpegQuality(70);
1225 parms
.setPictureFormat(ImageFormat
.JPEG
);
1226 camera
.setParameters(parms
);
1228 final SurfaceView fakeView
=new SurfaceView(context
);
1230 camera
.setPreviewDisplay(fakeView
.getHolder());
1231 } catch (IOException e
) {
1232 Utils
.sendMessage(context
, replyTo
, error_setting_preview_display
);
1235 camera
.startPreview();
1236 final Handler handler
=new Handler();
1238 new Thread(new Runnable() {
1241 SystemClock
.sleep(2000);
1243 handler
.post(new Runnable() {
1247 camera
.takePicture(null, null, new FonBotPictureCallback(context
, replyTo
, hostname
, port
));
1248 } catch(Exception e
){
1249 Utils
.sendMessage(context
, replyTo
, error_while_processing_command
, e
.getClass().getName(), e
.getMessage());
1258 * Send a directory listing to an Address
1260 * @param context Context instance
1261 * @param replyTo destination Address
1262 * @param directory directory to list
1264 public static void ls(final Context context
, final Address replyTo
, final String directory
) {
1265 final File
[] files
=new File(directory
).listFiles();
1267 Utils
.sendMessage(context
, replyTo
, string_is_not_a_directory
, directory
);
1271 final StringBuilder sb
=new StringBuilder(context
.getString(files_in_directory
,directory
));
1272 for(final File file
: files
){
1273 sb
.append(file
.getName());
1274 if(file
.isDirectory())
1279 Utils
.sendMessage(context
, replyTo
, toNonNull(sb
.toString()));
1283 * Make the phone start ringing if it is not ringing or stop ringing if it is.
1285 * @param context Context instance
1286 * @param replyTo reply Address
1288 public static void ring(final Context context
, final Address replyTo
){
1289 setupRingtone(context
);
1291 Utils
.sendMessage(context
, replyTo
, no_ringtone_found
);
1294 if(ringtone
.isPlaying())
1295 stopAlarm(context
, replyTo
);
1297 startAlarm(context
, replyTo
);
1301 * Make the phone start/stop ringing.
1303 * @param context Context instance
1304 * @param replyTo reply Address
1305 * @param on true if the phone should start ringing, false otherwise
1307 public static void ring(final Context context
, final Address replyTo
, final boolean on
){
1308 setupRingtone(context
);
1310 Utils
.sendMessage(context
, replyTo
, no_ringtone_found
);
1313 if(on
&&!ringtone
.isPlaying())
1314 startAlarm(context
, replyTo
);
1315 else if(ringtone
.isPlaying()&&!on
)
1316 stopAlarm(context
, replyTo
);
1320 * Send the current ringer mode to an Address
1322 * @param context Context instance
1323 * @param replyTo destination Address
1325 public static void ringer(final Context context
, final Address replyTo
){
1326 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
1327 switch(man
.getRingerMode()){
1328 case AudioManager
.RINGER_MODE_NORMAL
:
1329 Utils
.sendMessage(context
, replyTo
, ringer_mode_normal
);
1331 case AudioManager
.RINGER_MODE_VIBRATE
:
1332 Utils
.sendMessage(context
, replyTo
, ringer_mode_vibrate
);
1334 case AudioManager
.RINGER_MODE_SILENT
:
1335 Utils
.sendMessage(context
, replyTo
, ringer_mode_silent
);
1338 Utils
.sendMessage(context
, replyTo
, unknown_ringer_mode
);
1343 * Set the ringer mode.
1345 * @param context Context instance
1346 * @param replyTo reply Address
1347 * @param ringerMode requested ringer mode
1349 * @see Utils.RingerMode
1351 public static void ringer(final Context context
, final Address replyTo
, final int ringerMode
){
1352 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
1353 man
.setRingerMode(ringerMode
);
1354 ringer(context
, replyTo
);
1358 * Remove a file or empty directory.
1360 * @param context Context instance
1361 * @param replyTo reply Address
1362 * @param filename file/empty directory to delete
1364 public static void rm(final Context context
, final Address replyTo
, final String filename
){
1365 if(new File(filename
).delete())
1366 Utils
.sendMessage(context
, replyTo
, file_deleted
);
1368 Utils
.sendMessage(context
, replyTo
, error_while_deleting_file
);
1372 * Clear the keyguard password.
1374 * @param context Context instance
1375 * @param replyTo reply Address
1376 * @throws SecurityException if FonBot does not have device administration permissions
1378 public static void setPassword(final Context context
, final Address replyTo
) throws SecurityException
{
1379 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1381 dpm
.resetPassword("", 0);
1382 Utils
.sendMessage(context
, replyTo
, password_cleared
);
1386 * Change the keyguard password.
1388 * @param context Context instance
1389 * @param replyTo reply Address
1390 * @param password new password
1391 * @throws SecurityException if FonBot does not have device administration permissions
1393 public static void setPassword(final Context context
, final Address replyTo
, final String password
) throws SecurityException
{
1394 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1396 dpm
.resetPassword(password
, 0);
1397 Utils
.sendMessage(context
, replyTo
, password_set
);
1401 * Send a text message.
1403 * @param context Context instance
1404 * @param replyTo reply Address
1405 * @param destination destination phone number
1406 * @param text text message contents
1408 public static void sms(final Context context
, final Address replyTo
, final String destination
, final String text
){
1409 final SmsManager manager
=SmsManager
.getDefault();
1410 final ArrayList
<String
> messages
=manager
.divideMessage(text
);
1411 if(messages
.size() > 1)
1412 Utils
.sendMessage(context
, replyTo
, message_was_split_into_parts
, Integer
.valueOf(messages
.size()));
1414 final ArrayList
<PendingIntent
> sents
=new ArrayList
<PendingIntent
>(messages
.size());
1415 final ArrayList
<PendingIntent
> delivereds
=new ArrayList
<PendingIntent
>(messages
.size());
1417 final String name
=Utils
.callerId(context
, destination
);
1418 final String fullDestination
;
1420 fullDestination
=destination
;
1422 fullDestination
=destination
+" ("+name
+")";
1424 for(int i
=0;i
<messages
.size();i
++){
1425 final Intent sent
=new Intent(context
,SmsStatusReceiver
.class);
1426 sent
.putExtra(SmsStatusReceiver
.EXTRA_DESTINATION
, fullDestination
);
1427 sent
.putExtra(SmsStatusReceiver
.EXTRA_PART
, i
+1);
1428 sent
.putExtra(SmsStatusReceiver
.EXTRA_TOTAL
, messages
.size());
1429 sent
.putExtra(SmsStatusReceiver
.EXTRA_REPLY_TO
, replyTo
.toString());
1430 sent
.setAction(SmsStatusReceiver
.SENT_ACTION
+i
+System
.currentTimeMillis());//actions must be unique
1431 sents
.add(PendingIntent
.getBroadcast(context
, 0, sent
, PendingIntent
.FLAG_UPDATE_CURRENT
));
1433 final Intent delivered
=new Intent(context
, SmsStatusReceiver
.class);
1434 delivered
.putExtra(SmsStatusReceiver
.EXTRA_DESTINATION
, fullDestination
);
1435 delivered
.putExtra(SmsStatusReceiver
.EXTRA_PART
, i
+1);
1436 delivered
.putExtra(SmsStatusReceiver
.EXTRA_TOTAL
, messages
.size());
1437 delivered
.putExtra(SmsStatusReceiver
.EXTRA_REPLY_TO
, replyTo
.toString());
1438 delivered
.setAction(SmsStatusReceiver
.DELIVERED_ACTION
+i
+System
.currentTimeMillis());//actions must be unique
1439 delivereds
.add(PendingIntent
.getBroadcast(context
, 0, delivered
, PendingIntent
.FLAG_UPDATE_CURRENT
));
1442 Log
.d(Heavy
.class.getName(), "Sending sms to "+destination
);
1443 manager
.sendMultipartTextMessage(destination
, null, messages
, sents
, delivereds
);
1447 * Send the last SMSes to an Address.
1449 * @param context Context instance
1450 * @param replyTo destination Address
1451 * @param numSms how many SMSes to send
1453 public static void smslog(final Context context
, final Address replyTo
, final int numSms
) {
1454 final String
[] fields
= {"type","address", "body", "date"};
1456 final Cursor cursor
= context
.getContentResolver().query (
1457 Uri
.parse("content://sms"),
1464 if (cursor
.moveToFirst()) {
1466 final String fromNumber
=cursor
.getString(1);
1468 if(fromNumber
== null)
1471 final String name
=Utils
.callerId(context
, Utils
.toNonNull(fromNumber
));
1475 from
=fromNumber
+" ("+name
+')';
1478 final String message
=cursor
.getString(2).replace("\n", "\n ");
1479 final Date date
=new Date(cursor
.getLong(3));
1481 if(cursor
.getInt(0)==1)
1482 Utils
.sendMessage(context
, replyTo
, incoming_message
, from
, message
, date
);
1484 Utils
.sendMessage(context
, replyTo
, outgoing_message
, from
, message
, date
);
1485 } while (cursor
.moveToNext() && cursor
.getPosition() < numSms
);
1491 /** TTS instance, only used by {@link #speak(Context, Address, String)} */
1492 private static TextToSpeech tts
;
1495 * Speak a String using the text-to-speech engine.
1497 * @param context Context instance
1498 * @param replyTo reply Address
1499 * @param text text to speak
1501 public static void speak(final Context context
, final Address replyTo
, final String text
){
1502 tts
=new TextToSpeech(context
, new OnInitListener() {
1504 public void onInit(final int status
) {
1505 if(status
==TextToSpeech
.SUCCESS
){
1506 Utils
.sendMessage(context
, replyTo
, speaking
);
1507 tts
.speak(text
, TextToSpeech
.QUEUE_ADD
, null);
1509 Utils
.sendMessage(context
, replyTo
, tts_engine_not_available
);
1515 * Show a toast notification with the default duration.
1517 * @param context Context instance
1518 * @param replyTo reply Address
1519 * @param text toast text
1521 public static void toast(final Context context
, final Address replyTo
, final String text
){
1522 toast(context
, replyTo
, text
, Toast
.LENGTH_SHORT
);
1526 * Show a toast notification.
1528 * @param context Context instance
1529 * @param replyTo reply Address
1530 * @param text toast text
1531 * @param duration toast duration
1533 public static void toast(final Context context
, final Address replyTo
, final String text
, final int duration
){
1534 Toast
.makeText(context
,text
,duration
).show();
1535 Utils
.sendMessage(context
, replyTo
, toast_shown
);
1539 * Make the phone vibrate for a number of milliseconds.
1541 * @param context Context instance
1542 * @param replyTo reply Address
1543 * @param ms vibrate duration, in milliseconds
1545 public static void vibrate(final Context context
, final Address replyTo
, final long ms
){
1546 final Vibrator v
=(Vibrator
) context
.getSystemService(Context
.VIBRATOR_SERVICE
);
1547 Utils
.sendMessage(context
, replyTo
, vibrating
);
1552 * View an URI in an appropriate activity.
1554 * @param context Context instance
1555 * @param replyTo reply Address
1556 * @param uri URI to view
1558 public static void view(final Context context
, final Address replyTo
, final Uri uri
) {
1560 final Intent intent
=new Intent(Intent
.ACTION_VIEW
);
1561 intent
.setData(uri
);
1562 intent
.setFlags(Intent
.FLAG_FROM_BACKGROUND
|Intent
.FLAG_ACTIVITY_NEW_TASK
);
1563 context
.startActivity(intent
);
1564 Utils
.sendMessage(context
, replyTo
, url_opened
);
1565 } catch(ActivityNotFoundException e
){
1566 Utils
.sendMessage(context
, replyTo
, no_activity_found_for_this_url
);
1567 } catch(Exception e
){
1568 Utils
.sendMessage(context
, replyTo
, invalid_url
);
1573 * Get the current WiFi state.
1575 * @param context Context instance
1576 * @param replyTo reply Address
1578 public static void wifi(final Context context
, final Address replyTo
){
1579 final WifiManager man
=(WifiManager
) context
.getSystemService(Context
.WIFI_SERVICE
);
1580 if(man
.isWifiEnabled())
1581 Utils
.sendMessage(context
, replyTo
, wifi_on
);
1583 Utils
.sendMessage(context
, replyTo
, wifi_off
);
1587 * Set the WiFi state.
1589 * @param context Context instance
1590 * @param replyTo reply Address
1591 * @param on the requested WiFi state
1593 public static void wifi(final Context context
, final Address replyTo
, final boolean on
){
1594 final WifiManager man
=(WifiManager
) context
.getSystemService(Context
.WIFI_SERVICE
);
1595 man
.setWifiEnabled(on
);
1597 Utils
.sendMessage(context
, replyTo
, enabling_wifi
);
1599 Utils
.sendMessage(context
, replyTo
, disabling_wifi
);
1603 * Factory reset the phone, optionally deleting the SD card too.
1605 * @param context Context instance
1606 * @param type {@link Utils.WipeType} instance
1607 * @throws SecurityException if FonBot does not have device administration permissions
1609 @SuppressLint("InlinedApi")
1610 public static void wipe(final Context context
, final WipeType type
) throws SecurityException
{
1611 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1618 dpm
.wipeData(DevicePolicyManager
.WIPE_EXTERNAL_STORAGE
);
1624 * Disable a Command. The command cannot be used until enabled again with the {@link Utils.Command#ENABLE ENABLE} command.
1626 * @param context Context instance
1627 * @param replyTo reply Address
1628 * @param command Command to disable
1630 public static void disable(final Context context
, final Address replyTo
, final Command command
){
1631 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
1632 .putBoolean(command
+"disabled", true).commit();
1633 Utils
.sendMessage(context
, replyTo
, command_disabled
, command
);
1637 * Re-enable a disabled Command.
1639 * @param context Context instance
1640 * @param replyTo reply Address
1641 * @param command Command to re-enable
1643 public static void enable(final Context context
, final Address replyTo
, final Command command
){
1644 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
1645 .remove(command
+"disabled").commit();
1646 Utils
.sendMessage(context
, replyTo
, command_enabled
, command
);
1651 * Check whether a Command is disabled.
1653 * @param context Context instance
1654 * @param command Command to check
1655 * @return true if the Command is disabled, false otherwise
1657 public static boolean isCommandDisabled(final Context context
, final Command command
){
1658 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean(command
+"disabled", false);
1662 * Start long polling if stopped
1664 * @param context Context instance
1665 * @param replyTo reply Address
1667 public static void poll(final Context context
, final Address replyTo
) {
1668 Utils
.sendMessage(context
, replyTo
, starting_long_polling_if_stopped
);
1669 context
.startService(new Intent(context
, FonBotMainService
.class));
1673 * Get an instance of {@link ITelephony}
1675 * @param context Context instance
1676 * @return an instance of {@link ITelephony}
1677 * @throws NoSuchMethodException thrown by reflection
1678 * @throws IllegalArgumentException thrown by reflection
1679 * @throws IllegalAccessException thrown by reflection
1680 * @throws InvocationTargetException thrown by reflection
1682 private static ITelephony
getITelephony(final Context context
) throws NoSuchMethodException
, IllegalArgumentException
, IllegalAccessException
, InvocationTargetException
{
1683 final TelephonyManager man
=(TelephonyManager
) context
.getSystemService(Context
.TELEPHONY_SERVICE
);
1684 final Method m
=TelephonyManager
.class.getDeclaredMethod("getITelephony");
1685 m
.setAccessible(true);
1686 return toNonNull((ITelephony
) m
.invoke(man
));
1690 * Hang up the phone.
1692 * @param context Context instance
1693 * @param replyTo reply Address
1695 public static void hangup(final Context context
, final Address replyTo
){
1697 getITelephony(context
).endCall();
1698 } catch(Exception e
){
1699 Utils
.sendMessage(context
, replyTo
, exception_while_hanging_up_call
,
1700 e
.getClass().getName(), e
.getMessage());
1705 * Answer the phone if it is ringing.
1707 * @param context Context instance
1708 * @param replyTo reply Address
1710 public static void answer(final Context context
, final Address replyTo
){
1712 getITelephony(context
).answerRingingCall();
1713 } catch(Exception e
){
1714 Utils
.sendMessage(context
, replyTo
, exception_while_answering_call
,
1715 e
.getClass().getName(), e
.getMessage());
1722 * @param context Context instance
1723 * @param replyTo reply Address
1724 * @param pkg name of the package to launch
1726 public static void launch(final Context context
, final Address replyTo
, final String pkg
){
1727 final Intent intent
=context
.getPackageManager().getLaunchIntentForPackage(pkg
);
1729 Utils
.sendMessage(context
, replyTo
, no_such_package
);
1732 context
.startActivity(intent
);
1733 Utils
.sendMessage(context
, replyTo
, app_launched
);
1737 * Get the mobile data enabled status.
1739 * @param context Context instance
1740 * @param replyTo reply Address
1742 public static void data(final Context context
, final Address replyTo
){
1744 final ConnectivityManager man
=(ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
1745 final Method m
=ConnectivityManager
.class.getDeclaredMethod("getMobileDataEnabled");
1746 m
.setAccessible(true);
1747 if(((Boolean
)m
.invoke(man
)).booleanValue())
1748 Utils
.sendMessage(context
, replyTo
, data_on
);
1750 Utils
.sendMessage(context
, replyTo
, data_off
);
1751 } catch(Exception e
){
1752 Utils
.sendMessage(context
, replyTo
, exception_while_determining_data_state
,
1753 e
.getClass().getName(), e
.getMessage());
1758 * Set the mobile data enabled status.
1760 * @param context Context instance
1761 * @param replyTo reply Address
1762 * @param enable whether to enable mobile data
1764 public static void data(final Context context
, final Address replyTo
, final boolean enable
) {
1767 getITelephony(context
).enableDataConnectivity();
1768 Utils
.sendMessage(context
, replyTo
, enabling_data
);
1770 getITelephony(context
).disableDataConnectivity();
1771 Utils
.sendMessage(context
, replyTo
, disabling_data
);
1773 } catch(Exception e
){
1774 Utils
.sendMessage(context
, replyTo
, exception_while_getting_itelephony
,
1775 e
.getClass().getName(), e
.getMessage());
1780 * Get the GPS status.
1782 * @param context Context instance
1783 * @param replyTo reply Address
1785 public static void gps(final Context context
, final Address replyTo
){
1786 if(Secure
.isLocationProviderEnabled(context
.getContentResolver(), LocationManager
.GPS_PROVIDER
))
1787 Utils
.sendMessage(context
, replyTo
, gps_on
);
1789 Utils
.sendMessage(context
, replyTo
, gps_off
);
1793 * Set the GPS status.
1795 * @param context Context instance
1796 * @param replyTo reply Address
1797 * @param enabled requested GPS status
1799 public static void gps(final Context context
, final Address replyTo
, final boolean enabled
) {
1800 Secure
.setLocationProviderEnabled(context
.getContentResolver(), LocationManager
.GPS_PROVIDER
, enabled
);
1802 Utils
.sendMessage(context
, replyTo
, enabling_gps
);
1804 Utils
.sendMessage(context
, replyTo
, disabling_gps
);
1808 * Get the Google location (aka network location) state.
1810 * @param context Context instance
1811 * @param replyTo reply Address
1813 public static void glocation(final Context context
, final Address replyTo
){
1814 if(Secure
.isLocationProviderEnabled(context
.getContentResolver(), LocationManager
.NETWORK_PROVIDER
))
1815 Utils
.sendMessage(context
, replyTo
, network_location_on
);
1817 Utils
.sendMessage(context
, replyTo
, network_location_off
);
1821 * Set the Google location (aka network location) state.
1823 * @param context Context instance
1824 * @param replyTo reply Address
1825 * @param enabled requested Google location state
1827 public static void glocation(final Context context
, final Address replyTo
, final boolean enabled
) {
1828 Secure
.setLocationProviderEnabled(context
.getContentResolver(), LocationManager
.NETWORK_PROVIDER
, enabled
);
1830 Utils
.sendMessage(context
, replyTo
, enabling_network_location
);
1832 Utils
.sendMessage(context
, replyTo
, disabling_network_location
);
1838 * @param context Context instance
1839 * @param replyTo reply Address
1840 * @param reason reboot reason
1842 * @see PowerManager#reboot(String)
1844 public static void reboot(final Context context
, final Address replyTo
, final @Nullable String reason
) {
1845 final PowerManager pm
=(PowerManager
) context
.getSystemService(Context
.POWER_SERVICE
);
1846 Utils
.sendMessage(context
, replyTo
, rebooting
);
1849 } catch (final Exception e
){
1850 e
.printStackTrace();
1853 Runtime
.getRuntime().exec(new String
[]{
1858 } catch (final Exception e
){
1859 e
.printStackTrace();
1861 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), reboot_failed
);
1865 * Cancel a notification.
1867 * @param context Context instance
1868 * @param replyTo reply Address
1869 * @param id notification ID
1871 public static void notify(final Context context
, final Address replyTo
, final int id
) {
1872 final NotificationManager man
=(NotificationManager
) context
.getSystemService(Context
.NOTIFICATION_SERVICE
);
1874 Utils
.sendMessage(context
, replyTo
, notification_canceled
);
1878 * Show a notification.
1880 * @param context Context instance
1881 * @param replyTo reply Address
1882 * @param id notification ID
1883 * @param title notificationO title
1884 * @param text notification text
1886 public static void notify(final Context context
, final Address replyTo
, final int id
, final String title
, final String text
) {
1887 final NotificationManager man
=(NotificationManager
) context
.getSystemService(Context
.NOTIFICATION_SERVICE
);
1888 man
.notify(id
, new NotificationCompat
.Builder(context
).
1889 setContentTitle(title
).
1890 setContentText(text
).
1891 setSmallIcon(android
.R
.drawable
.stat_notify_sync_noanim
).
1893 Utils
.sendMessage(context
, replyTo
, notification_shown
);
1897 * Take a screen capture. Uses the screencap utility and requires root.
1899 * @param context Context instance
1900 * @param replyTo reply Address
1901 * @param filename capture file location
1903 public static void screencap(final Context context
, final Address replyTo
, final String filename
){
1904 new Thread(new ScreencapRunnable(context
, replyTo
, filename
)).start();
1908 * Toggle the torch state using the Torch (net.cactii.torch2) app.
1910 * @param context Context instance
1911 * @param replyTo reply Address
1913 public static void torch(final Context context
, final Address replyTo
){
1914 context
.sendBroadcast(new Intent("net.cactii.flash2.TOGGLE_FLASHLIGHT"));
1915 Utils
.sendMessage(context
, replyTo
, toggling_torch_state
);
1919 * Download a file from a given URL to a given filename
1921 * @param context Context instance
1922 * @param replyTo reply Address
1923 * @param filename filename to save to
1924 * @param hostname hostname to download from
1925 * @param port port to download from
1927 public static void getfile(final Context context
, final Address replyTo
, final String filename
, final String hostname
, final int port
){
1928 new GetfileExecutableRunnable(context
, replyTo
, filename
, hostname
, port
).execute();