1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.R
.string
.*;
4 import static ro
.ieval
.fonbot
.Utils
.toNonNull
;
7 import java
.io
.FileInputStream
;
8 import java
.io
.FileNotFoundException
;
9 import java
.io
.IOException
;
10 import java
.lang
.reflect
.InvocationTargetException
;
11 import java
.lang
.reflect
.Method
;
12 import java
.net
.InetSocketAddress
;
13 import java
.net
.Socket
;
14 import java
.net
.UnknownHostException
;
15 import java
.nio
.channels
.FileChannel
;
16 import java
.nio
.channels
.SocketChannel
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Date
;
20 import org
.eclipse
.jdt
.annotation
.Nullable
;
22 import ro
.ieval
.fonbot
.Utils
.Command
;
23 import ro
.ieval
.fonbot
.Utils
.MessageType
;
24 import ro
.ieval
.fonbot
.Utils
.OngoingEvent
;
25 import ro
.ieval
.fonbot
.Utils
.RingerMode
;
26 import ro
.ieval
.fonbot
.Utils
.WipeType
;
27 import android
.annotation
.SuppressLint
;
28 import android
.app
.AlarmManager
;
29 import android
.app
.NotificationManager
;
30 import android
.app
.PendingIntent
;
31 import android
.app
.admin
.DevicePolicyManager
;
32 import android
.bluetooth
.BluetoothAdapter
;
33 import android
.content
.ActivityNotFoundException
;
34 import android
.content
.Context
;
35 import android
.content
.Intent
;
36 import android
.content
.IntentFilter
;
37 import android
.database
.Cursor
;
38 import android
.graphics
.ImageFormat
;
39 import android
.hardware
.Camera
;
40 import android
.hardware
.Camera
.PictureCallback
;
41 import android
.location
.Location
;
42 import android
.location
.LocationListener
;
43 import android
.location
.LocationManager
;
44 import android
.location
.LocationProvider
;
45 import android
.media
.AudioManager
;
46 import android
.media
.Ringtone
;
47 import android
.media
.RingtoneManager
;
48 import android
.net
.ConnectivityManager
;
49 import android
.net
.Uri
;
50 import android
.net
.wifi
.WifiManager
;
51 import android
.os
.AsyncTask
;
52 import android
.os
.BatteryManager
;
53 import android
.os
.Bundle
;
54 import android
.os
.Handler
;
55 import android
.os
.PowerManager
;
56 import android
.os
.Vibrator
;
57 import android
.preference
.PreferenceManager
;
58 import android
.provider
.BaseColumns
;
59 import android
.provider
.CallLog
.Calls
;
60 import android
.provider
.ContactsContract
.CommonDataKinds
;
61 import android
.provider
.ContactsContract
.CommonDataKinds
.BaseTypes
;
62 import android
.provider
.ContactsContract
.CommonDataKinds
.Phone
;
63 import android
.provider
.ContactsContract
.Contacts
;
64 import android
.provider
.ContactsContract
.Data
;
65 import android
.provider
.Settings
.Secure
;
66 import android
.speech
.tts
.TextToSpeech
;
67 import android
.speech
.tts
.TextToSpeech
.OnInitListener
;
68 import android
.support
.v4
.app
.NotificationCompat
;
69 import android
.telephony
.SmsManager
;
70 import android
.telephony
.TelephonyManager
;
71 import android
.util
.Log
;
72 import android
.view
.SurfaceView
;
73 import android
.widget
.Toast
;
75 import com
.android
.internal
.telephony
.ITelephony
;
78 * Copyright © 2013 Marius Gavrilescu
80 * This file is part of FonBot.
82 * FonBot is free software: you can redistribute it and/or modify
83 * it under the terms of the GNU General Public License as published by
84 * the Free Software Foundation, either version 3 of the License, or
85 * (at your option) any later version.
87 * FonBot is distributed in the hope that it will be useful,
88 * but WITHOUT ANY WARRANTY; without even the implied warranty of
89 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
90 * GNU General Public License for more details.
92 * You should have received a copy of the GNU General Public License
93 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
97 * Implementation of all FonBot commands. The methods of this class do not do argument checking.
99 * @author Marius Gavrilescu <marius@ieval.ro>
103 * LocationListener that sends notifications to the user.
105 * @author Marius Gavrilescu <marius@ieval.ro>
107 private static final class FonBotLocationListener
implements LocationListener
{
108 /** Context instance */
109 private final Context context
;
110 /** Destination address for notifications */
111 private final Address replyTo
;
114 * Construct a FonBotLocationListener.
116 * @param context Context instance
117 * @param replyTo the reply address
119 FonBotLocationListener(final Context context
, final Address replyTo
) {
120 this.context
=context
;
121 this.replyTo
=replyTo
;
125 public void onLocationChanged(@Nullable final Location loc
) {
128 final StringBuilder sb
=new StringBuilder(toNonNull(context
.getString(location
)));
130 sb
.append(toNonNull(context
.getString(latitude
)));
132 sb
.append(loc
.getLatitude());
134 sb
.append(toNonNull(context
.getString(longitude
)));
136 sb
.append(loc
.getLongitude());
138 if(loc
.hasAccuracy()){
140 sb
.append(toNonNull(context
.getString(accuracy
)));
142 sb
.append(loc
.getAccuracy());
145 if(loc
.hasAltitude()){
147 sb
.append(toNonNull(context
.getString(altitude
)));
149 sb
.append(loc
.getAltitude());
152 if(loc
.hasBearing()){
154 sb
.append(toNonNull(context
.getString(bearing
)));
156 sb
.append(loc
.getBearing());
161 sb
.append(toNonNull(context
.getString(speed
)));
163 sb
.append(loc
.getSpeed());
166 final Date locationDate
=new Date(loc
.getTime());
168 sb
.append(toNonNull(context
.getString(at
)));
170 sb
.append(locationDate
.toString());
173 sb
.append("http://openstreetmap.org/?zoom=15&mlat=");
174 sb
.append(loc
.getLatitude());
176 sb
.append(loc
.getLongitude());
177 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), toNonNull(sb
.toString()));
181 public void onProviderDisabled(@Nullable final String provider
) {
182 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), location_provider_disabled
, provider
);
186 public void onProviderEnabled(@Nullable final String provider
) {
187 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), location_provider_enabled
, provider
);
191 public void onStatusChanged(@Nullable final String provider
, final int status
, @Nullable final Bundle extras
) {
194 case LocationProvider
.AVAILABLE
:
195 state
=location_provider_available
;
197 case LocationProvider
.TEMPORARILY_UNAVAILABLE
:
198 state
=location_provider_temporary_unavailable
;
200 case LocationProvider
.OUT_OF_SERVICE
:
201 state
=location_provider_out_of_service
;
204 state
=location_provider_unknown_state
;
206 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), state
, provider
);
210 * Currently active FonBotLocationListener
212 private static FonBotLocationListener locationListener
= null;
215 * AsyncTask that sends a byte[] to a server
217 * @author Marius Gavrilescu <marius@ieval.ro>
220 private static class SendDataAsyncTask
extends AsyncTask
<Void
, Void
, Void
>{
222 * Context instance used by this class
224 private final Context context
;
228 private final String hostname
;
232 private final int port
;
236 private final byte[] data
;
238 * Address for sending back errors and success messages
240 private final Address replyTo
;
243 * Constructs a SendDataAsyncTasks from its parameters
245 * @param context the context
246 * @param replyTo the reply Address
247 * @param hostname the server hostname
248 * @param port the server port
249 * @param data the data to send
251 public SendDataAsyncTask(final Context context
,final Address replyTo
, final String hostname
, final int port
, final byte[] data
) {//NOPMD array is immutable
253 this.context
=context
;
254 this.hostname
=hostname
;
257 this.replyTo
=replyTo
;
261 protected @Nullable Void
doInBackground(@Nullable final Void
... params
) {
264 sock
= new Socket(hostname
, port
);
266 sock
.getOutputStream().write(data
);
267 } catch (IOException e
) {
268 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), error_writing_to_socket
, e
.getMessage());
274 } catch (IOException e
) {
275 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), cannot_close_socket
, e
.getMessage());
278 } catch (UnknownHostException e
) {
279 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), unknown_host
, hostname
);
281 } catch (IOException e
) {
282 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), cannot_connect_to_host_on_port
, hostname
, Integer
.valueOf(port
));
285 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), photo_sent
);
291 * PictureCallback that sends the picture to a server.
293 * @author Marius Gavrilescu <marius@ieval.ro>
295 private static final class FonBotPictureCallback
implements PictureCallback
{
296 /** Server hostname */
297 private final String hostname
;
299 private final int port
;
300 /** Context instance */
301 private final Context context
;
303 private final Address replyTo
;
306 * Construct a FonBotPictureCallback.
308 * @param context Context instance
309 * @param replyTo reply Address
310 * @param hostname server hostname
311 * @param port server port
313 FonBotPictureCallback(final Context context
, final Address replyTo
, final String hostname
, final int port
) {
314 this.hostname
=hostname
;
316 this.context
=context
;
317 this.replyTo
=replyTo
;
321 @SuppressWarnings("hiding")
322 public void onPictureTaken(final @Nullable byte[] data
, final @Nullable Camera camera
) {
323 if(camera
==null || data
==null)
325 camera
.stopPreview();
327 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), sending_photo
);
328 new SendDataAsyncTask(toNonNull(context
), toNonNull(replyTo
), toNonNull(hostname
), port
, data
).execute();
333 * Runnable that takes a screen capture and stores it in a file.
335 private static final class ScreencapRunnable
implements Runnable
{
336 private final Context context
;
337 private final Address replyTo
;
338 private final String filename
;
340 ScreencapRunnable(final Context context
, final Address replyTo
, final String filename
){
341 this.context
=context
;
342 this.replyTo
=replyTo
;
343 this.filename
=filename
;
350 exitCode
=Runtime
.getRuntime().exec(new String
[]{
353 "screencap -p \"" + filename
+ "\""
355 } catch (final Exception e
){
357 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), screencap_failed
);
361 if(exitCode
== 0 && new File(filename
).exists())
362 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), screencap_successful
);
364 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), screencap_failed
);
369 * Get help for a particular command
371 * @param context Context instance
372 * @param replyTo reply Address
373 * @param command command to get help for
375 public static void help(final Context context
, final Address replyTo
, final Command command
){//NOPMD method is a big switch statement. Nothing confusing.
378 Utils
.sendMessage(context
, replyTo
, answer_help
);
381 Utils
.sendMessage(context
, replyTo
, batt_help
);
384 Utils
.sendMessage(context
, replyTo
, bluetooth_help
);
387 Utils
.sendMessage(context
, replyTo
, calllog_help
);
390 Utils
.sendMessage(context
, replyTo
, contacts_help
);
393 Utils
.sendMessage(context
, replyTo
, data_help
);
395 case DELNOTIFICATION
:
396 Utils
.sendMessage(context
, replyTo
, delnotification_help
, Utils
.join(", ", toNonNull(MessageType
.values())));
399 Utils
.sendMessage(context
, replyTo
, dial_help
);
402 Utils
.sendMessage(context
, replyTo
, dialog_help
);
405 Utils
.sendMessage(context
, replyTo
, disable_help
, Utils
.join(", ", toNonNull(Command
.values())));
408 Utils
.sendMessage(context
, replyTo
, echo_help
);
411 Utils
.sendMessage(context
, replyTo
, enable_help
, Utils
.join(", ", toNonNull(Command
.values())));
414 Utils
.sendMessage(context
, replyTo
, flash_help
);
417 Utils
.sendMessage(context
, replyTo
, glocation_help
);
420 Utils
.sendMessage(context
, replyTo
, gps_help
);
423 Utils
.sendMessage(context
, replyTo
, hangup_help
);
426 Utils
.sendMessage(context
, replyTo
, help_help
, Utils
.join(", ",toNonNull(Command
.values())));
429 Utils
.sendMessage(context
, replyTo
, launch_help
);
432 Utils
.sendMessage(context
, replyTo
, location_help
, Utils
.join(", ",toNonNull(Utils
.LocationProvider
.values())));
435 Utils
.sendMessage(context
, replyTo
, lock_help
);
438 Utils
.sendMessage(context
, replyTo
, ls_help
);
441 Utils
.sendMessage(context
, replyTo
, ncfile_help
);
444 Utils
.sendMessage(context
, replyTo
, next_help
);
447 Utils
.sendMessage(context
, replyTo
, nolocation_help
);
450 Utils
.sendMessage(context
, replyTo
, pause_help
);
453 Utils
.sendMessage(context
, replyTo
, photo_help
);
456 Utils
.sendMessage(context
, replyTo
, play_help
);
459 Utils
.sendMessage(context
, replyTo
, poll_help
);
462 Utils
.sendMessage(context
, replyTo
, prev_help
);
465 Utils
.sendMessage(context
, replyTo
, ring_help
);
468 Utils
.sendMessage(context
, replyTo
, ringer_help
, Utils
.join(", ", toNonNull(RingerMode
.values())));
471 Utils
.sendMessage(context
, replyTo
, rm_help
);
473 case SETNOTIFICATION
:
474 Utils
.sendMessage(context
, replyTo
, setnotification_help
, Utils
.join(", ", toNonNull(MessageType
.values())));
477 Utils
.sendMessage(context
, replyTo
, setpassword_help
);
480 Utils
.sendMessage(context
, replyTo
, sms_help
);
483 Utils
.sendMessage(context
, replyTo
, smslog_help
);
486 Utils
.sendMessage(context
, replyTo
, speak_help
);
489 Utils
.sendMessage(context
, replyTo
, toast_help
);
492 Utils
.sendMessage(context
, replyTo
, vibrate_help
);
495 Utils
.sendMessage(context
, replyTo
, view_help
);
498 Utils
.sendMessage(context
, replyTo
, wifi_help
);
501 Utils
.sendMessage(context
, replyTo
, wipe_help
, Utils
.WIPE_CONFIRM_STRING
);
504 Utils
.sendMessage(context
, replyTo
, reboot_help
);
507 Utils
.sendMessage(context
, replyTo
, notify_help
);
510 Utils
.sendMessage(context
, replyTo
, screencap_help
);
513 Utils
.sendMessage(context
, replyTo
, torch_help
);
521 * @see #startCamera(Context, Address)
524 private static Camera camera
;
526 * Ringtone used by the {@link Utils.Command#RING RING} command.
528 * @see #setupRingtone(Context)
530 private static Ringtone ringtone
;
532 * Saved ringer volume.
534 * @see #startAlarm(Context, Address)
535 * @see #stopAlarm(Context, Address)
537 private static int savedRingVolume
;
541 * @see #startAlarm(Context, Address)
542 * @see #stopAlarm(Context, Address)
544 private static int savedRingerMode
;
546 /** Private constructor */
552 * Convert a phone number type to a string
554 * @param context Context instance
555 * @param type phone number type
556 * @param label name of a custom phone type
557 * @return the phone number type
559 private static @Nullable String
phoneNumberType(final Context context
, final int type
, final @Nullable String label
) {
561 case BaseTypes
.TYPE_CUSTOM
:
563 case Phone
.TYPE_ASSISTANT
:
564 return context
.getString(phone_numer_type_assistant
);
565 case Phone
.TYPE_CALLBACK
:
566 return context
.getString(phone_number_type_callback
);
568 return context
.getString(phone_number_type_car
);
569 case Phone
.TYPE_COMPANY_MAIN
:
570 return context
.getString(phone_number_type_company_main
);
571 case Phone
.TYPE_FAX_HOME
:
572 return context
.getString(phone_number_type_home_fax
);
573 case Phone
.TYPE_FAX_WORK
:
574 return context
.getString(phone_number_type_work_fax
);
575 case Phone
.TYPE_HOME
:
576 return context
.getString(phone_number_type_home
);
577 case Phone
.TYPE_ISDN
:
578 return context
.getString(phone_number_type_isdn
);
579 case Phone
.TYPE_MAIN
:
580 return context
.getString(phone_number_type_main
);
582 return context
.getString(phone_number_type_mms
);
583 case Phone
.TYPE_MOBILE
:
584 return context
.getString(phone_number_type_mobile
);
585 case Phone
.TYPE_OTHER
:
586 return context
.getString(phone_number_type_other
);
587 case Phone
.TYPE_OTHER_FAX
:
588 return context
.getString(phone_number_type_other_fax
);
589 case Phone
.TYPE_PAGER
:
590 return context
.getString(phone_number_type_pager
);
591 case Phone
.TYPE_RADIO
:
592 return context
.getString(phone_number_type_radio
);
593 case Phone
.TYPE_TELEX
:
594 return context
.getString(phone_number_type_telex
);
595 case Phone
.TYPE_TTY_TDD
:
596 return context
.getString(phone_number_type_textphone
);
597 case Phone
.TYPE_WORK
:
598 return context
.getString(phone_number_type_work
);
599 case Phone
.TYPE_WORK_MOBILE
:
600 return context
.getString(phone_number_type_work_mobile
);
601 case Phone
.TYPE_WORK_PAGER
:
602 return context
.getString(phone_number_type_work_pager
);
605 return context
.getString(phone_number_type_unknown
, Integer
.valueOf(type
));
609 * Setup the ringtone used by the {@link Utils.Command#RING RING} command
611 * @param context Context
613 private static void setupRingtone(final Context context
){
614 if(ringtone
==null){//NOPMD not supposed to be thread-safe
615 final Uri alert
=RingtoneManager
.getDefaultUri(RingtoneManager
.TYPE_RINGTONE
);
616 ringtone
=RingtoneManager
.getRingtone(context
, alert
);
621 * Make the phone start ringing. Turns up the volume and sets the ringer mode to NORMAL
623 * @param context Context instance
624 * @param replyTo reply Address
626 private static void startAlarm(final Context context
, final Address replyTo
){
627 Utils
.registerOngoing(context
, toNonNull(OngoingEvent
.RING
));
628 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
629 savedRingerMode
=man
.getRingerMode();
630 man
.setRingerMode(AudioManager
.RINGER_MODE_NORMAL
);
631 savedRingVolume
=man
.getStreamVolume(AudioManager
.STREAM_RING
);
632 man
.setStreamVolume(AudioManager
.STREAM_RING
, man
.getStreamMaxVolume(AudioManager
.STREAM_RING
), 0);
633 Utils
.sendMessage(context
, replyTo
, ringing
);
638 * Get a camera instance.
640 * @param context Context instance
641 * @param replyTo reply Address
643 private static void startCamera(final Context context
, final Address replyTo
){
647 camera
=Camera
.open();
648 } catch (Exception e
){
649 Utils
.sendMessage(context
, replyTo
, cannot_grab_camera
);
654 * Make the phone stop ringing. Restores the volume and ringer mode.
656 * @param context Context instance
657 * @param replyTo reply Address
659 private static void stopAlarm(final Context context
, final Address replyTo
){
660 Utils
.unregisterOngoing(context
, toNonNull(OngoingEvent
.RING
));
661 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
662 Utils
.sendMessage(context
, replyTo
, no_longer_ringing
);
664 man
.setStreamVolume(AudioManager
.STREAM_RING
, savedRingVolume
, 0);
665 man
.setRingerMode(savedRingerMode
);
669 * Release the previously grabbed camera instance
671 * @see #startCamera(Context, Address)
673 private static void stopCamera(){
681 * Send battery status information to an Address
683 * @param context Context instance
684 * @param replyTo destination Address
686 * @see #describeBatteryLevel(Context, Address, MessageType)
688 public static void batt(final Context context
, final Address replyTo
){
689 describeBatteryLevel(context
, replyTo
, null);
693 * Show the bluetooth radio status.
695 * @param context Context instance
696 * @param replyTo destination Address
698 public static void bluetooth(final Context context
, final Address replyTo
) {
699 final BluetoothAdapter adapter
=BluetoothAdapter
.getDefaultAdapter();
701 Utils
.sendMessage(context
, replyTo
, no_bluetooth_adapter
);
705 if(adapter
.isEnabled())
706 Utils
.sendMessage(context
, replyTo
, bluetooth_on
);
708 Utils
.sendMessage(context
, replyTo
, bluetooth_off
);
712 * Set the bluetooth radio status.
714 * @param context Context instance
715 * @param replyTo destination Address
716 * @param on the requested radio status
718 public static void bluetooth(final Context context
, final Address replyTo
, final boolean on
){
719 final BluetoothAdapter adapter
=BluetoothAdapter
.getDefaultAdapter();
721 Utils
.sendMessage(context
, replyTo
, no_bluetooth_adapter
);
727 Utils
.sendMessage(context
, replyTo
, enabling_bluetooth
);
731 Utils
.sendMessage(context
, replyTo
, disabling_bluetooth
);
736 * Cancel an ongoing event.
738 * @param context Context instance
739 * @param event the event to cancel
741 public static void cancelOngoing(final Context context
, final OngoingEvent event
){
744 nolocation(context
, toNonNull(Address
.BLACKHOLE
));
747 ring(context
, toNonNull(Address
.BLACKHOLE
), false);
753 * Send the last calls to an Address.
755 * @param context Context instance
756 * @param replyTo destination Address
757 * @param numCalls how many calls to send
759 public static void calllog(final Context context
, final Address replyTo
, final int numCalls
) {
760 final String
[] fields
= {
761 Calls
.TYPE
, Calls
.NUMBER
, Calls
.CACHED_NAME
, Calls
.DURATION
, Calls
.DATE
764 final Cursor cursor
= context
.getContentResolver().query(
772 if (cursor
.moveToFirst()) {
774 final StringBuilder sb
=new StringBuilder(50);//NOPMD different strings
775 final int type
=cursor
.getInt(0);
776 final String from
=cursor
.getString(1);
779 case Calls
.INCOMING_TYPE
:
780 sb
.append(context
.getString(incoming_call_from
, from
));
782 case Calls
.MISSED_TYPE
:
783 sb
.append(context
.getString(missed_call_from
, from
));
785 case Calls
.OUTGOING_TYPE
:
786 sb
.append(context
.getString(outgoing_call_to
, from
));
790 if (cursor
.getString(2) != null)
791 sb
.append('(').append(cursor
.getString(2)).append(") ");
793 sb
.append(context
.getString(duration_seconds_starting_at
,
794 Long
.valueOf(cursor
.getLong(3)),
795 new Date(cursor
.getLong(4))));
797 Utils
.sendMessage(context
, replyTo
, toNonNull(sb
.toString()));
798 } while (cursor
.moveToNext() && cursor
.getPosition() < numCalls
);
805 * Search for contacts by name/nickname and send matching entries to an Address.
807 * @param context Context instance
808 * @param replyTo destination Address
809 * @param name name/nickname part to search for
811 @SuppressLint("StringFormatMatches")
812 public static void contacts(final Context context
, final Address replyTo
, final String name
){
813 final Cursor cursor
=context
.getContentResolver().query(Uri
.withAppendedPath(
814 Contacts
.CONTENT_FILTER_URI
, name
),
815 new String
[]{Contacts
.DISPLAY_NAME
, BaseColumns
._ID
, Contacts
.LOOKUP_KEY
},
816 null, null, Contacts
.DISPLAY_NAME
);
818 if(cursor
.getCount()==0)
819 Utils
.sendMessage(context
, replyTo
, no_matching_contacts_found
);
821 while(cursor
.moveToNext()){
822 final String
[] fields
= {
823 CommonDataKinds
.Phone
.NUMBER
,
824 CommonDataKinds
.Phone
.TYPE
,
825 CommonDataKinds
.Phone
.LABEL
,
828 final Cursor inCursor
=context
.getContentResolver().query(Data
.CONTENT_URI
,
830 Data
.CONTACT_ID
+" = ? AND "+Data
.MIMETYPE
+ " = ?",
831 new String
[]{Long
.toString(cursor
.getLong(1)), CommonDataKinds
.Phone
.CONTENT_ITEM_TYPE
},
832 CommonDataKinds
.Phone
.LABEL
);
834 while(inCursor
.moveToNext())
835 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(contact_info
,
837 inCursor
.getString(0),
838 phoneNumberType(context
, inCursor
.getInt(1), inCursor
.getString(2)))));
847 * Send battery status information to an Address or as a notification
849 * @param context Context instance
850 * @param replyTo Address to send the information to, if sending to a direct address. Null otherwise.
851 * @param type Notification type, if sending as a notification. Null otherwise.
853 public static void describeBatteryLevel(final Context context
, final @Nullable Address replyTo
, final @Nullable MessageType type
) {
854 if(replyTo
==null&&type
==null)
856 final Intent intent
=context
.registerReceiver(null, new IntentFilter(Intent
.ACTION_BATTERY_CHANGED
));
859 final double level
=intent
.getIntExtra(BatteryManager
.EXTRA_LEVEL
, 0);
860 final int scale
=intent
.getIntExtra(BatteryManager
.EXTRA_SCALE
, 100);
861 final int plugged
=intent
.getIntExtra(BatteryManager
.EXTRA_PLUGGED
, 0);
862 final int status
=intent
.getIntExtra(BatteryManager
.EXTRA_STATUS
, BatteryManager
.BATTERY_STATUS_UNKNOWN
);
863 final int temp
=intent
.getIntExtra(BatteryManager
.EXTRA_TEMPERATURE
, 0);
864 final int volt
=intent
.getIntExtra(BatteryManager
.EXTRA_VOLTAGE
, 0);
866 final StringBuilder sb
=new StringBuilder(100);
867 sb
.append(context
.getString(battery_level
, Double
.valueOf(level
*100/scale
)));
871 sb
.append(context
.getString(not_plugged_in
));
873 case BatteryManager
.BATTERY_PLUGGED_AC
:
874 sb
.append(context
.getString(plugged_in_ac
));
876 case BatteryManager
.BATTERY_PLUGGED_USB
:
877 sb
.append(context
.getString(plugged_in_usb
));
879 case BatteryManager
.BATTERY_PLUGGED_WIRELESS
:
880 sb
.append(context
.getString(plugged_in_wireless
));
885 case BatteryManager
.BATTERY_STATUS_CHARGING
:
886 sb
.append(context
.getString(status_charging
));
888 case BatteryManager
.BATTERY_STATUS_DISCHARGING
:
889 sb
.append(context
.getString(status_discharging
));
891 case BatteryManager
.BATTERY_STATUS_FULL
:
892 sb
.append(context
.getString(status_full
));
894 case BatteryManager
.BATTERY_STATUS_NOT_CHARGING
:
895 sb
.append(context
.getString(status_not_charging
));
897 case BatteryManager
.BATTERY_STATUS_UNKNOWN
:
898 sb
.append(context
.getString(status_unknown
));
902 sb
.append(context
.getString(temperature
, Integer
.valueOf(temp
)));
904 sb
.append(context
.getString(voltage
, Integer
.valueOf(volt
)));
906 Utils
.sendMessage(context
, toNonNull(replyTo
), toNonNull(sb
.toString()));
908 Utils
.sendMessage(context
, type
, toNonNull(sb
.toString()));
912 * Dial a phone number.
914 * @param context Context instance
915 * @param replyTo reply Address
916 * @param nr phone number to dial
918 public static void dial(final Context context
, final Address replyTo
, final String nr
){
919 final Intent intent
=new Intent(Intent
.ACTION_CALL
,Uri
.parse("tel:"+nr
));
920 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
921 final String name
=Utils
.callerId(context
, nr
);
923 Utils
.sendMessage(context
, replyTo
, dialing
, nr
);
925 Utils
.sendMessage(context
, replyTo
, dialing
, nr
+" ("+name
+")");
926 context
.startActivity(intent
);
930 * Show a dialog with a message and a list of buttons.
932 * @param context Context instance
933 * @param replyTo reply Address
934 * @param message dialog message
935 * @param buttons dialog buttons
937 public static void dialog(final Context context
, final Address replyTo
, final String message
, final String
[] buttons
){
938 final Intent intent
=new Intent(context
, DialogActivity
.class);
939 intent
.putExtra(DialogActivity
.EXTRA_MESSAGE
, message
);
940 intent
.putExtra(DialogActivity
.EXTRA_BUTTONS
, buttons
);
941 intent
.putExtra(DialogActivity
.EXTRA_REPLYTO
, replyTo
.toString());
943 Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
|
944 Intent
.FLAG_ACTIVITY_NEW_TASK
|
945 Intent
.FLAG_ACTIVITY_NO_USER_ACTION
|
946 Intent
.FLAG_FROM_BACKGROUND
);
947 Utils
.sendMessage(context
, toNonNull(replyTo
), showing_dialog
);
948 context
.startActivity(intent
);
952 * Turns the flashlight on or off.
954 * @param context Context instance
955 * @param replyTo reply Address
956 * @param on requested flashlight state
958 public static void flash(final Context context
, final Address replyTo
, final boolean on
){
959 startCamera(context
, replyTo
);
962 final Camera
.Parameters parms
=camera
.getParameters();
964 parms
.setFlashMode(Camera
.Parameters
.FLASH_MODE_TORCH
);
965 camera
.setParameters(parms
);
967 parms
.setFlashMode(Camera
.Parameters
.FLASH_MODE_AUTO
);
968 camera
.setParameters(parms
);
974 * Start sending location updates to an Address.
976 * @param context Context instance
977 * @param replyTo destination Address
978 * @param provider LocationProvider
979 * @param minTime minimum time between two consecutive updates (in ms)
980 * @param minDistance minimum distance between two consecutive updates (in meters)
982 * @see LocationManager#requestLocationUpdates(String, long, float, LocationListener)
984 public static void location(final Context context
, final Address replyTo
, final String provider
,final long minTime
,final float minDistance
){
985 final LocationManager man
=(LocationManager
) context
.getApplicationContext().getSystemService(Context
.LOCATION_SERVICE
);
986 if(locationListener
!=null)
987 nolocation(context
, toNonNull(Address
.BLACKHOLE
));
988 Utils
.registerOngoing(context
, toNonNull(OngoingEvent
.LOCATION
));
989 locationListener
=new FonBotLocationListener(context
, replyTo
);
990 man
.removeUpdates(locationListener
);
991 final Location lastKnownLocation
=man
.getLastKnownLocation(provider
);
992 if(lastKnownLocation
!=null){
993 Utils
.sendMessage(context
, replyTo
, last_known_location
);
994 locationListener
.onLocationChanged(lastKnownLocation
);
996 Utils
.sendMessage(context
, replyTo
, listening_for_location_updates
);
997 man
.requestLocationUpdates(provider
, minTime
, minDistance
, locationListener
);
1003 * @param context Context instance
1004 * @param replyTo reply Address
1006 public static void lock(final Context context
, final Address replyTo
) {
1007 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1009 Utils
.sendMessage(context
, replyTo
, device_locked
);
1013 * Send a command to a running instance of the music player
1015 * @param context Context instance
1016 * @param replyTo reply Address
1017 * @param command command to send
1019 public static void musicPlayerCommand(final Context context
, final Address replyTo
, final String command
) {
1020 final Intent intent
=new Intent("com.android.music.musicservicecommand");
1021 intent
.putExtra("command", command
);
1022 context
.sendBroadcast(intent
);
1023 Utils
.sendMessage(context
, replyTo
, command_sent
);
1027 * Send a file to a server.
1029 * @param context Context instance
1030 * @param replyTo reply Address
1031 * @param filename file to send
1032 * @param hostname server hostname
1033 * @param port server port
1035 public static void ncfile(final Context context
, final Address replyTo
, final String filename
,final String hostname
,final int port
){
1036 new AsyncTask
<Void
, Void
, Void
>() {
1038 protected @Nullable Void
doInBackground(@Nullable final Void
... params
) {
1039 final FileChannel in
;
1041 in
=new FileInputStream(filename
).getChannel();
1042 } catch (final FileNotFoundException e
){
1043 Utils
.sendMessage(context
, replyTo
, file_not_found
, filename
);
1046 final SocketChannel sock
;
1048 sock
= SocketChannel
.open(new InetSocketAddress(hostname
, port
));
1049 } catch (final IOException e
){
1050 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(
1051 cannot_connect_to_host_on_port
, hostname
, Integer
.valueOf(port
))));
1054 } catch (IOException ex
) {
1061 in
.transferTo(0, in
.size(), sock
);
1062 } catch (final IOException e
){
1063 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(
1064 io_error
, e
.getMessage())));
1068 } catch (IOException e
){
1073 } catch(IOException e
){
1081 protected void onPostExecute(@Nullable final Void result
) {
1082 Utils
.sendMessage(context
, replyTo
, file_sent
);
1088 * Stop sending location updates.
1090 * @param context Context instance
1091 * @param replyTo reply Address
1093 public static void nolocation(final Context context
, final Address replyTo
){
1094 Utils
.unregisterOngoing(context
, toNonNull(OngoingEvent
.LOCATION
));
1095 final LocationManager man
=(LocationManager
) context
.getApplicationContext().getSystemService(Context
.LOCATION_SERVICE
);
1096 man
.removeUpdates(locationListener
);
1097 locationListener
=null;
1098 Utils
.sendMessage(context
, replyTo
, no_longer_listening_for_location_updates
);
1102 * Take a photo and send it to a server.
1104 * @param context Context instance
1105 * @param replyTo reply Address
1106 * @param hostname server hostname
1107 * @param port server port
1109 public static void photo(final Context context
, final Address replyTo
, final String hostname
, final int port
){
1110 startCamera(context
, replyTo
);
1113 final Camera
.Parameters parms
=camera
.getParameters();
1114 parms
.setJpegQuality(70);
1115 parms
.setPictureFormat(ImageFormat
.JPEG
);
1116 camera
.setParameters(parms
);
1118 final SurfaceView fakeView
=new SurfaceView(context
);
1120 camera
.setPreviewDisplay(fakeView
.getHolder());
1121 } catch (IOException e
) {
1122 Utils
.sendMessage(context
, replyTo
, error_setting_preview_display
);
1125 camera
.startPreview();
1126 final Handler handler
=new Handler();
1128 new Thread(new Runnable() {
1133 } catch (InterruptedException e
) {
1137 handler
.post(new Runnable() {
1140 camera
.takePicture(null, null, new FonBotPictureCallback(context
, replyTo
, hostname
, port
));
1148 * Send a directory listing to an Address
1150 * @param context Context instance
1151 * @param replyTo destination Address
1152 * @param directory directory to list
1154 public static void ls(final Context context
, final Address replyTo
, final String directory
) {
1155 final File
[] files
=new File(directory
).listFiles();
1157 Utils
.sendMessage(context
, replyTo
, string_is_not_a_directory
, directory
);
1161 final StringBuilder sb
=new StringBuilder(context
.getString(files_in_directory
,directory
));
1162 for(final File file
: files
){
1163 sb
.append(file
.getName());
1164 if(file
.isDirectory())
1169 Utils
.sendMessage(context
, replyTo
, toNonNull(sb
.toString()));
1173 * Make the phone start ringing if it is not ringing or stop ringing if it is.
1175 * @param context Context instance
1176 * @param replyTo reply Address
1178 public static void ring(final Context context
, final Address replyTo
){
1179 setupRingtone(context
);
1181 Utils
.sendMessage(context
, replyTo
, no_ringtone_found
);
1184 if(ringtone
.isPlaying())
1185 stopAlarm(context
, replyTo
);
1187 startAlarm(context
, replyTo
);
1191 * Make the phone start/stop ringing.
1193 * @param context Context instance
1194 * @param replyTo reply Address
1195 * @param on true if the phone should start ringing, false otherwise
1197 public static void ring(final Context context
, final Address replyTo
, final boolean on
){
1198 setupRingtone(context
);
1200 Utils
.sendMessage(context
, replyTo
, no_ringtone_found
);
1203 if(on
&&!ringtone
.isPlaying())
1204 startAlarm(context
, replyTo
);
1205 else if(ringtone
.isPlaying()&&!on
)
1206 stopAlarm(context
, replyTo
);
1210 * Send the current ringer mode to an Address
1212 * @param context Context instance
1213 * @param replyTo destination Address
1215 public static void ringer(final Context context
, final Address replyTo
){
1216 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
1217 switch(man
.getRingerMode()){
1218 case AudioManager
.RINGER_MODE_NORMAL
:
1219 Utils
.sendMessage(context
, replyTo
, ringer_mode_normal
);
1221 case AudioManager
.RINGER_MODE_VIBRATE
:
1222 Utils
.sendMessage(context
, replyTo
, ringer_mode_vibrate
);
1224 case AudioManager
.RINGER_MODE_SILENT
:
1225 Utils
.sendMessage(context
, replyTo
, ringer_mode_silent
);
1228 Utils
.sendMessage(context
, replyTo
, unknown_ringer_mode
);
1233 * Set the ringer mode.
1235 * @param context Context instance
1236 * @param replyTo reply Address
1237 * @param ringerMode requested ringer mode
1239 * @see Utils.RingerMode
1241 public static void ringer(final Context context
, final Address replyTo
, final int ringerMode
){
1242 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
1243 man
.setRingerMode(ringerMode
);
1244 ringer(context
, replyTo
);
1248 * Remove a file or empty directory.
1250 * @param context Context instance
1251 * @param replyTo reply Address
1252 * @param filename file/empty directory to delete
1254 public static void rm(final Context context
, final Address replyTo
, final String filename
){
1255 if(new File(filename
).delete())
1256 Utils
.sendMessage(context
, replyTo
, file_deleted
);
1258 Utils
.sendMessage(context
, replyTo
, error_while_deleting_file
);
1262 * Clear the keyguard password.
1264 * @param context Context instance
1265 * @param replyTo reply Address
1266 * @throws SecurityException if FonBot does not have device administration permissions
1268 public static void setPassword(final Context context
, final Address replyTo
) throws SecurityException
{
1269 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1271 dpm
.resetPassword("", 0);
1272 Utils
.sendMessage(context
, replyTo
, password_cleared
);
1276 * Change the keyguard password.
1278 * @param context Context instance
1279 * @param replyTo reply Address
1280 * @param password new password
1281 * @throws SecurityException if FonBot does not have device administration permissions
1283 public static void setPassword(final Context context
, final Address replyTo
, final String password
) throws SecurityException
{
1284 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1286 dpm
.resetPassword(password
, 0);
1287 Utils
.sendMessage(context
, replyTo
, password_set
);
1291 * Send a text message.
1293 * @param context Context instance
1294 * @param replyTo reply Address
1295 * @param destination destination phone number
1296 * @param text text message contents
1298 public static void sms(final Context context
, final Address replyTo
, final String destination
, final String text
){
1299 final SmsManager manager
=SmsManager
.getDefault();
1300 final ArrayList
<String
> messages
=manager
.divideMessage(text
);
1301 if(messages
.size() > 1)
1302 Utils
.sendMessage(context
, replyTo
, message_was_split_into_parts
, Integer
.valueOf(messages
.size()));
1304 final ArrayList
<PendingIntent
> sents
=new ArrayList
<PendingIntent
>(messages
.size());
1305 final ArrayList
<PendingIntent
> delivereds
=new ArrayList
<PendingIntent
>(messages
.size());
1307 final String name
=Utils
.callerId(context
, destination
);
1308 final String fullDestination
;
1310 fullDestination
=destination
;
1312 fullDestination
=destination
+" ("+name
+")";
1314 for(int i
=0;i
<messages
.size();i
++){
1315 final Intent sent
=new Intent(context
,SmsStatusReceiver
.class);
1316 sent
.putExtra(SmsStatusReceiver
.EXTRA_DESTINATION
, fullDestination
);
1317 sent
.putExtra(SmsStatusReceiver
.EXTRA_PART
, i
+1);
1318 sent
.putExtra(SmsStatusReceiver
.EXTRA_TOTAL
, messages
.size());
1319 sent
.putExtra(SmsStatusReceiver
.EXTRA_REPLY_TO
, replyTo
.toString());
1320 sent
.setAction(SmsStatusReceiver
.SENT_ACTION
+i
);//actions must be unique
1321 sents
.add(PendingIntent
.getBroadcast(context
, 0, sent
, PendingIntent
.FLAG_UPDATE_CURRENT
));
1323 final Intent delivered
=new Intent(context
, SmsStatusReceiver
.class);
1324 delivered
.putExtra(SmsStatusReceiver
.EXTRA_DESTINATION
, fullDestination
);
1325 delivered
.putExtra(SmsStatusReceiver
.EXTRA_PART
, i
+1);
1326 delivered
.putExtra(SmsStatusReceiver
.EXTRA_TOTAL
, messages
.size());
1327 delivered
.putExtra(SmsStatusReceiver
.EXTRA_REPLY_TO
, replyTo
.toString());
1328 delivered
.setAction(SmsStatusReceiver
.DELIVERED_ACTION
+i
);//actions must be unique
1329 delivereds
.add(PendingIntent
.getBroadcast(context
, 0, delivered
, PendingIntent
.FLAG_UPDATE_CURRENT
));
1332 Log
.d(Heavy
.class.getName(), "Sending sms to "+destination
);
1333 manager
.sendMultipartTextMessage(destination
, null, messages
, sents
, delivereds
);
1337 * Send the last SMSes to an Address.
1339 * @param context Context instance
1340 * @param replyTo destination Address
1341 * @param numSms how many SMSes to send
1343 public static void smslog(final Context context
, final Address replyTo
, final int numSms
) {
1344 final String
[] fields
= {"type","address", "body", "date"};
1346 final Cursor cursor
= context
.getContentResolver().query (
1347 Uri
.parse("content://sms"),
1354 if (cursor
.moveToFirst()) {
1356 final String fromNumber
=cursor
.getString(1);
1358 final String name
=Utils
.callerId(context
, Utils
.toNonNull(fromNumber
));
1362 from
=fromNumber
+" ("+name
+')';
1363 final String message
=cursor
.getString(2).replace("\n", "\n ");
1364 final Date date
=new Date(cursor
.getLong(3));
1366 if(cursor
.getInt(0)==1)
1367 Utils
.sendMessage(context
, replyTo
, incoming_message
, from
, message
, date
);
1369 Utils
.sendMessage(context
, replyTo
, outgoing_message
, from
, message
, date
);
1370 } while (cursor
.moveToNext() && cursor
.getPosition() < numSms
);
1376 /** TTS instance, only used by {@link #speak(Context, Address, String)} */
1377 private static TextToSpeech tts
;
1380 * Speak a String using the text-to-speech engine.
1382 * @param context Context instance
1383 * @param replyTo reply Address
1384 * @param text text to speak
1386 public static void speak(final Context context
, final Address replyTo
, final String text
){
1387 tts
=new TextToSpeech(context
, new OnInitListener() {
1389 public void onInit(final int status
) {
1390 if(status
==TextToSpeech
.SUCCESS
){
1391 Utils
.sendMessage(context
, replyTo
, speaking
);
1392 tts
.speak(text
, TextToSpeech
.QUEUE_ADD
, null);
1394 Utils
.sendMessage(context
, replyTo
, tts_engine_not_available
);
1400 * Show a toast notification with the default duration.
1402 * @param context Context instance
1403 * @param replyTo reply Address
1404 * @param text toast text
1406 public static void toast(final Context context
, final Address replyTo
, final String text
){
1407 toast(context
, replyTo
, text
, Toast
.LENGTH_SHORT
);
1411 * Show a toast notification.
1413 * @param context Context instance
1414 * @param replyTo reply Address
1415 * @param text toast text
1416 * @param duration toast duration
1418 public static void toast(final Context context
, final Address replyTo
, final String text
, final int duration
){
1419 Toast
.makeText(context
,text
,duration
).show();
1420 Utils
.sendMessage(context
, replyTo
, toast_shown
);
1424 * Make the phone vibrate for a number of milliseconds.
1426 * @param context Context instance
1427 * @param replyTo reply Address
1428 * @param ms vibrate duration, in milliseconds
1430 public static void vibrate(final Context context
, final Address replyTo
, final long ms
){
1431 final Vibrator v
=(Vibrator
) context
.getSystemService(Context
.VIBRATOR_SERVICE
);
1432 Utils
.sendMessage(context
, replyTo
, vibrating
);
1437 * View an URI in an appropriate activity.
1439 * @param context Context instance
1440 * @param replyTo reply Address
1441 * @param uri URI to view
1443 public static void view(final Context context
, final Address replyTo
, final Uri uri
) {
1445 final Intent intent
=new Intent(Intent
.ACTION_VIEW
);
1446 intent
.setData(uri
);
1447 intent
.setFlags(Intent
.FLAG_FROM_BACKGROUND
|Intent
.FLAG_ACTIVITY_NEW_TASK
);
1448 context
.startActivity(intent
);
1449 Utils
.sendMessage(context
, replyTo
, url_opened
);
1450 } catch(ActivityNotFoundException e
){
1451 Utils
.sendMessage(context
, replyTo
, no_activity_found_for_this_url
);
1452 } catch(Exception e
){
1453 Utils
.sendMessage(context
, replyTo
, invalid_url
);
1458 * Get the current WiFi state.
1460 * @param context Context instance
1461 * @param replyTo reply Address
1463 public static void wifi(final Context context
, final Address replyTo
){
1464 final WifiManager man
=(WifiManager
) context
.getSystemService(Context
.WIFI_SERVICE
);
1465 if(man
.isWifiEnabled())
1466 Utils
.sendMessage(context
, replyTo
, wifi_on
);
1468 Utils
.sendMessage(context
, replyTo
, wifi_off
);
1472 * Set the WiFi state.
1474 * @param context Context instance
1475 * @param replyTo reply Address
1476 * @param on the requested WiFi state
1478 public static void wifi(final Context context
, final Address replyTo
, final boolean on
){
1479 final WifiManager man
=(WifiManager
) context
.getSystemService(Context
.WIFI_SERVICE
);
1480 man
.setWifiEnabled(on
);
1482 Utils
.sendMessage(context
, replyTo
, enabling_wifi
);
1484 Utils
.sendMessage(context
, replyTo
, disabling_wifi
);
1488 * Factory reset the phone, optionally deleting the SD card too.
1490 * @param context Context instance
1491 * @param type {@link Utils.WipeType} instance
1492 * @throws SecurityException if FonBot does not have device administration permissions
1494 @SuppressLint("InlinedApi")
1495 public static void wipe(final Context context
, final WipeType type
) throws SecurityException
{
1496 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1503 dpm
.wipeData(DevicePolicyManager
.WIPE_EXTERNAL_STORAGE
);
1509 * Disable a Command. The command cannot be used until enabled again with the {@link Utils.Command#ENABLE ENABLE} command.
1511 * @param context Context instance
1512 * @param replyTo reply Address
1513 * @param command Command to disable
1515 public static void disable(final Context context
, final Address replyTo
, final Command command
){
1516 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
1517 .putBoolean(command
+"disabled", true).commit();
1518 Utils
.sendMessage(context
, replyTo
, command_disabled
, command
);
1522 * Re-enable a disabled Command.
1524 * @param context Context instance
1525 * @param replyTo reply Address
1526 * @param command Command to re-enable
1528 public static void enable(final Context context
, final Address replyTo
, final Command command
){
1529 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
1530 .remove(command
+"disabled").commit();
1531 Utils
.sendMessage(context
, replyTo
, command_enabled
, command
);
1536 * Check whether a Command is disabled.
1538 * @param context Context instance
1539 * @param command Command to check
1540 * @return true if the Command is disabled, false otherwise
1542 public static boolean isCommandDisabled(final Context context
, final Command command
){
1543 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean(command
+"disabled", false);
1547 * Poll the server for pending commands.
1549 * @param context Context instance
1550 * @param replyTo reply Address
1552 public static void poll(final Context context
, final Address replyTo
) {
1553 Utils
.sendMessage(context
, replyTo
, polling_server
);
1554 context
.startService(new Intent(context
, FonBotMainService
.class));
1558 * Get an instance of {@link ITelephony}
1560 * @param context Context instance
1561 * @return an instance of {@link ITelephony}
1562 * @throws NoSuchMethodException thrown by reflection
1563 * @throws IllegalArgumentException thrown by reflection
1564 * @throws IllegalAccessException thrown by reflection
1565 * @throws InvocationTargetException thrown by reflection
1567 private static ITelephony
getITelephony(final Context context
) throws NoSuchMethodException
, IllegalArgumentException
, IllegalAccessException
, InvocationTargetException
{
1568 final TelephonyManager man
=(TelephonyManager
) context
.getSystemService(Context
.TELEPHONY_SERVICE
);
1569 final Method m
=TelephonyManager
.class.getDeclaredMethod("getITelephony");
1570 m
.setAccessible(true);
1571 return toNonNull((ITelephony
) m
.invoke(man
));
1575 * Hang up the phone.
1577 * @param context Context instance
1578 * @param replyTo reply Address
1580 public static void hangup(final Context context
, final Address replyTo
){
1582 getITelephony(context
).endCall();
1583 } catch(Exception e
){
1584 Utils
.sendMessage(context
, replyTo
, exception_while_hanging_up_call
,
1585 e
.getClass().getName(), e
.getMessage());
1590 * Answer the phone if it is ringing.
1592 * @param context Context instance
1593 * @param replyTo reply Address
1595 public static void answer(final Context context
, final Address replyTo
){
1597 getITelephony(context
).answerRingingCall();
1598 } catch(Exception e
){
1599 Utils
.sendMessage(context
, replyTo
, exception_while_answering_call
,
1600 e
.getClass().getName(), e
.getMessage());
1607 * @param context Context instance
1608 * @param replyTo reply Address
1609 * @param pkg name of the package to launch
1611 public static void launch(final Context context
, final Address replyTo
, final String pkg
){
1612 final Intent intent
=context
.getPackageManager().getLaunchIntentForPackage(pkg
);
1614 Utils
.sendMessage(context
, replyTo
, no_such_package
);
1617 context
.startActivity(intent
);
1618 Utils
.sendMessage(context
, replyTo
, app_launched
);
1622 * Get the mobile data enabled status.
1624 * @param context Context instance
1625 * @param replyTo reply Address
1627 public static void data(final Context context
, final Address replyTo
){
1629 final ConnectivityManager man
=(ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
1630 final Method m
=ConnectivityManager
.class.getDeclaredMethod("getMobileDataEnabled");
1631 m
.setAccessible(true);
1632 if(((Boolean
)m
.invoke(man
)).booleanValue())
1633 Utils
.sendMessage(context
, replyTo
, data_on
);
1635 Utils
.sendMessage(context
, replyTo
, data_off
);
1636 } catch(Exception e
){
1637 Utils
.sendMessage(context
, replyTo
, exception_while_determining_data_state
,
1638 e
.getClass().getName(), e
.getMessage());
1643 * Set the mobile data enabled status.
1645 * @param context Context instance
1646 * @param replyTo reply Address
1647 * @param enable whether to enable mobile data
1649 public static void data(final Context context
, final Address replyTo
, final boolean enable
) {
1652 getITelephony(context
).enableDataConnectivity();
1653 Utils
.sendMessage(context
, replyTo
, enabling_data
);
1655 getITelephony(context
).disableDataConnectivity();
1656 Utils
.sendMessage(context
, replyTo
, disabling_data
);
1658 } catch(Exception e
){
1659 Utils
.sendMessage(context
, replyTo
, exception_while_getting_itelephony
,
1660 e
.getClass().getName(), e
.getMessage());
1665 * Get the GPS status.
1667 * @param context Context instance
1668 * @param replyTo reply Address
1670 public static void gps(final Context context
, final Address replyTo
){
1671 if(Secure
.isLocationProviderEnabled(context
.getContentResolver(), LocationManager
.GPS_PROVIDER
))
1672 Utils
.sendMessage(context
, replyTo
, gps_on
);
1674 Utils
.sendMessage(context
, replyTo
, gps_off
);
1678 * Set the GPS status.
1680 * @param context Context instance
1681 * @param replyTo reply Address
1682 * @param enabled requested GPS status
1684 public static void gps(final Context context
, final Address replyTo
, final boolean enabled
) {
1685 Secure
.setLocationProviderEnabled(context
.getContentResolver(), LocationManager
.GPS_PROVIDER
, enabled
);
1687 Utils
.sendMessage(context
, replyTo
, enabling_gps
);
1689 Utils
.sendMessage(context
, replyTo
, disabling_gps
);
1693 * Get the Google location (aka network location) state.
1695 * @param context Context instance
1696 * @param replyTo reply Address
1698 public static void glocation(final Context context
, final Address replyTo
){
1699 if(Secure
.isLocationProviderEnabled(context
.getContentResolver(), LocationManager
.NETWORK_PROVIDER
))
1700 Utils
.sendMessage(context
, replyTo
, network_location_on
);
1702 Utils
.sendMessage(context
, replyTo
, network_location_off
);
1706 * Set the Google location (aka network location) state.
1708 * @param context Context instance
1709 * @param replyTo reply Address
1710 * @param enabled requested Google location state
1712 public static void glocation(final Context context
, final Address replyTo
, final boolean enabled
) {
1713 Secure
.setLocationProviderEnabled(context
.getContentResolver(), LocationManager
.NETWORK_PROVIDER
, enabled
);
1715 Utils
.sendMessage(context
, replyTo
, enabling_network_location
);
1717 Utils
.sendMessage(context
, replyTo
, disabling_network_location
);
1723 * @param context Context instance
1724 * @param replyTo reply Address
1725 * @param reason reboot reason
1727 * @see PowerManager#reboot(String)
1729 public static void reboot(final Context context
, final Address replyTo
, final @Nullable String reason
) {
1730 final PowerManager pm
=(PowerManager
) context
.getSystemService(Context
.POWER_SERVICE
);
1731 Utils
.sendMessage(context
, replyTo
, rebooting
);
1736 * Cancel a notification.
1738 * @param context Context instance
1739 * @param replyTo reply Address
1740 * @param id notification ID
1742 public static void notify(final Context context
, final Address replyTo
, final int id
) {
1743 final NotificationManager man
=(NotificationManager
) context
.getSystemService(Context
.NOTIFICATION_SERVICE
);
1745 Utils
.sendMessage(context
, replyTo
, notification_canceled
);
1749 * Show a notification.
1751 * @param context Context instance
1752 * @param replyTo reply Address
1753 * @param id notification ID
1754 * @param title notificationO title
1755 * @param text notification text
1757 public static void notify(final Context context
, final Address replyTo
, final int id
, final String title
, final String text
) {
1758 final NotificationManager man
=(NotificationManager
) context
.getSystemService(Context
.NOTIFICATION_SERVICE
);
1759 man
.notify(id
, new NotificationCompat
.Builder(context
).
1760 setContentTitle(title
).
1761 setContentText(text
).
1762 setSmallIcon(android
.R
.drawable
.stat_notify_sync_noanim
).
1764 Utils
.sendMessage(context
, replyTo
, notification_shown
);
1768 * Take a screen capture. Uses the screencap utility and requires root.
1770 * @param context Context instance
1771 * @param replyTo reply Address
1772 * @param filename capture file location
1774 public static void screencap(final Context context
, final Address replyTo
, final String filename
){
1775 new Thread(new ScreencapRunnable(context
, replyTo
, filename
)).start();
1779 * Toggle the torch state using the Torch (net.cactii.torch2) app.
1781 * @param context Context instance
1782 * @param replyTo reply Address
1784 public static void torch(final Context context
, final Address replyTo
){
1785 context
.sendBroadcast(new Intent("net.cactii.flash2.TOGGLE_FLASHLIGHT"));
1786 Utils
.sendMessage(context
, replyTo
, toggling_torch_state
);