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
)));
169 sb
.append(locationDate
.toString());
170 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), toNonNull(sb
.toString()));
174 public void onProviderDisabled(@Nullable final String provider
) {
175 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), location_provider_disabled
, provider
);
179 public void onProviderEnabled(@Nullable final String provider
) {
180 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), location_provider_enabled
, provider
);
184 public void onStatusChanged(@Nullable final String provider
, final int status
, @Nullable final Bundle extras
) {
187 case LocationProvider
.AVAILABLE
:
188 state
=location_provider_available
;
190 case LocationProvider
.TEMPORARILY_UNAVAILABLE
:
191 state
=location_provider_temporary_unavailable
;
193 case LocationProvider
.OUT_OF_SERVICE
:
194 state
=location_provider_out_of_service
;
197 state
=location_provider_unknown_state
;
199 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), state
, provider
);
203 * Currently active FonBotLocationListener
205 private static FonBotLocationListener locationListener
= null;
208 * AsyncTask that sends a byte[] to a server
210 * @author Marius Gavrilescu <marius@ieval.ro>
213 private static class SendDataAsyncTask
extends AsyncTask
<Void
, Void
, Void
>{
215 * Context instance used by this class
217 private final Context context
;
221 private final String hostname
;
225 private final int port
;
229 private final byte[] data
;
231 * Address for sending back errors and success messages
233 private final Address replyTo
;
236 * Constructs a SendDataAsyncTasks from its parameters
238 * @param context the context
239 * @param replyTo the reply Address
240 * @param hostname the server hostname
241 * @param port the server port
242 * @param data the data to send
244 public SendDataAsyncTask(final Context context
,final Address replyTo
, final String hostname
, final int port
, final byte[] data
) {//NOPMD array is immutable
246 this.context
=context
;
247 this.hostname
=hostname
;
250 this.replyTo
=replyTo
;
254 protected @Nullable Void
doInBackground(@Nullable final Void
... params
) {
257 sock
= new Socket(hostname
, port
);
259 sock
.getOutputStream().write(data
);
260 } catch (IOException e
) {
261 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), error_writing_to_socket
, e
.getMessage());
267 } catch (IOException e
) {
268 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), cannot_close_socket
, e
.getMessage());
271 } catch (UnknownHostException e
) {
272 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), unknown_host
, hostname
);
274 } catch (IOException e
) {
275 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), cannot_connect_to_host_on_port
, hostname
, Integer
.valueOf(port
));
278 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), photo_sent
);
284 * PictureCallback that sends the picture to a server.
286 * @author Marius Gavrilescu <marius@ieval.ro>
288 private static final class FonBotPictureCallback
implements PictureCallback
{
289 /** Server hostname */
290 private final String hostname
;
292 private final int port
;
293 /** Context instance */
294 private final Context context
;
296 private final Address replyTo
;
299 * Construct a FonBotPictureCallback.
301 * @param context Context instance
302 * @param replyTo reply Address
303 * @param hostname server hostname
304 * @param port server port
306 FonBotPictureCallback(final Context context
, final Address replyTo
, final String hostname
, final int port
) {
307 this.hostname
=hostname
;
309 this.context
=context
;
310 this.replyTo
=replyTo
;
314 @SuppressWarnings("hiding")
315 public void onPictureTaken(final @Nullable byte[] data
, final @Nullable Camera camera
) {
316 if(camera
==null || data
==null)
318 camera
.stopPreview();
320 Utils
.sendMessage(toNonNull(context
), toNonNull(replyTo
), sending_photo
);
321 new SendDataAsyncTask(toNonNull(context
), toNonNull(replyTo
), toNonNull(hostname
), port
, data
).execute();
326 * Get help for a particular command
328 * @param context Context instance
329 * @param replyTo reply Address
330 * @param command command to get help for
332 public static void help(final Context context
, final Address replyTo
, final Command command
){//NOPMD method is a big switch statement. Nothing confusing.
335 Utils
.sendMessage(context
, replyTo
, answer_help
);
338 Utils
.sendMessage(context
, replyTo
, batt_help
);
341 Utils
.sendMessage(context
, replyTo
, bluetooth_help
);
344 Utils
.sendMessage(context
, replyTo
, calllog_help
);
347 Utils
.sendMessage(context
, replyTo
, contacts_help
);
350 Utils
.sendMessage(context
, replyTo
, data_help
);
352 case DELNOTIFICATION
:
353 Utils
.sendMessage(context
, replyTo
, delnotification_help
, Utils
.join(", ", toNonNull(MessageType
.values())));
356 Utils
.sendMessage(context
, replyTo
, dial_help
);
359 Utils
.sendMessage(context
, replyTo
, dialog_help
);
362 Utils
.sendMessage(context
, replyTo
, disable_help
, Utils
.join(", ", toNonNull(Command
.values())));
365 Utils
.sendMessage(context
, replyTo
, echo_help
);
368 Utils
.sendMessage(context
, replyTo
, enable_help
, Utils
.join(", ", toNonNull(Command
.values())));
371 Utils
.sendMessage(context
, replyTo
, flash_help
);
374 Utils
.sendMessage(context
, replyTo
, glocation_help
);
377 Utils
.sendMessage(context
, replyTo
, gps_help
);
380 Utils
.sendMessage(context
, replyTo
, hangup_help
);
383 Utils
.sendMessage(context
, replyTo
, help_help
, Utils
.join(", ",toNonNull(Command
.values())));
386 Utils
.sendMessage(context
, replyTo
, launch_help
);
389 Utils
.sendMessage(context
, replyTo
, location_help
, Utils
.join(", ",toNonNull(Utils
.LocationProvider
.values())));
392 Utils
.sendMessage(context
, replyTo
, lock_help
);
395 Utils
.sendMessage(context
, replyTo
, ls_help
);
398 Utils
.sendMessage(context
, replyTo
, ncfile_help
);
401 Utils
.sendMessage(context
, replyTo
, next_help
);
404 Utils
.sendMessage(context
, replyTo
, nolocation_help
);
407 Utils
.sendMessage(context
, replyTo
, pause_help
);
410 Utils
.sendMessage(context
, replyTo
, photo_help
);
413 Utils
.sendMessage(context
, replyTo
, play_help
);
416 Utils
.sendMessage(context
, replyTo
, poll_help
);
419 Utils
.sendMessage(context
, replyTo
, prev_help
);
422 Utils
.sendMessage(context
, replyTo
, ring_help
);
425 Utils
.sendMessage(context
, replyTo
, ringer_help
, Utils
.join(", ", toNonNull(RingerMode
.values())));
428 Utils
.sendMessage(context
, replyTo
, rm_help
);
430 case SETNOTIFICATION
:
431 Utils
.sendMessage(context
, replyTo
, setnotification_help
, Utils
.join(", ", toNonNull(MessageType
.values())));
434 Utils
.sendMessage(context
, replyTo
, setpassword_help
);
437 Utils
.sendMessage(context
, replyTo
, sms_help
);
440 Utils
.sendMessage(context
, replyTo
, smslog_help
);
443 Utils
.sendMessage(context
, replyTo
, speak_help
);
446 Utils
.sendMessage(context
, replyTo
, toast_help
);
449 Utils
.sendMessage(context
, replyTo
, vibrate_help
);
452 Utils
.sendMessage(context
, replyTo
, view_help
);
455 Utils
.sendMessage(context
, replyTo
, wifi_help
);
458 Utils
.sendMessage(context
, replyTo
, wipe_help
, Utils
.WIPE_CONFIRM_STRING
);
461 Utils
.sendMessage(context
, replyTo
, reboot_help
);
464 Utils
.sendMessage(context
, replyTo
, shutdown_help
);
467 Utils
.sendMessage(context
, replyTo
, notify_help
);
474 * @see #startCamera(Context, Address)
477 private static Camera camera
;
479 * Ringtone used by the {@link Utils.Command#RING RING} command.
481 * @see #setupRingtone(Context)
483 private static Ringtone ringtone
;
485 * Saved ringer volume.
487 * @see #startAlarm(Context, Address)
488 * @see #stopAlarm(Context, Address)
490 private static int savedRingVolume
;
494 * @see #startAlarm(Context, Address)
495 * @see #stopAlarm(Context, Address)
497 private static int savedRingerMode
;
499 /** Private constructor */
505 * Convert a phone number type to a string
507 * @param context Context instance
508 * @param type phone number type
509 * @param label name of a custom phone type
510 * @return the phone number type
512 private static @Nullable String
phoneNumberType(final Context context
, final int type
, final @Nullable String label
) {
514 case BaseTypes
.TYPE_CUSTOM
:
516 case Phone
.TYPE_ASSISTANT
:
517 return context
.getString(phone_numer_type_assistant
);
518 case Phone
.TYPE_CALLBACK
:
519 return context
.getString(phone_number_type_callback
);
521 return context
.getString(phone_number_type_car
);
522 case Phone
.TYPE_COMPANY_MAIN
:
523 return context
.getString(phone_number_type_company_main
);
524 case Phone
.TYPE_FAX_HOME
:
525 return context
.getString(phone_number_type_home_fax
);
526 case Phone
.TYPE_FAX_WORK
:
527 return context
.getString(phone_number_type_work_fax
);
528 case Phone
.TYPE_HOME
:
529 return context
.getString(phone_number_type_home
);
530 case Phone
.TYPE_ISDN
:
531 return context
.getString(phone_number_type_isdn
);
532 case Phone
.TYPE_MAIN
:
533 return context
.getString(phone_number_type_main
);
535 return context
.getString(phone_number_type_mms
);
536 case Phone
.TYPE_MOBILE
:
537 return context
.getString(phone_number_type_mobile
);
538 case Phone
.TYPE_OTHER
:
539 return context
.getString(phone_number_type_other
);
540 case Phone
.TYPE_OTHER_FAX
:
541 return context
.getString(phone_number_type_other_fax
);
542 case Phone
.TYPE_PAGER
:
543 return context
.getString(phone_number_type_pager
);
544 case Phone
.TYPE_RADIO
:
545 return context
.getString(phone_number_type_radio
);
546 case Phone
.TYPE_TELEX
:
547 return context
.getString(phone_number_type_telex
);
548 case Phone
.TYPE_TTY_TDD
:
549 return context
.getString(phone_number_type_textphone
);
550 case Phone
.TYPE_WORK
:
551 return context
.getString(phone_number_type_work
);
552 case Phone
.TYPE_WORK_MOBILE
:
553 return context
.getString(phone_number_type_work_mobile
);
554 case Phone
.TYPE_WORK_PAGER
:
555 return context
.getString(phone_number_type_work_pager
);
558 return context
.getString(phone_number_type_unknown
, Integer
.valueOf(type
));
562 * Setup the ringtone used by the {@link Utils.Command#RING RING} command
564 * @param context Context
566 private static void setupRingtone(final Context context
){
567 if(ringtone
==null){//NOPMD not supposed to be thread-safe
568 final Uri alert
=RingtoneManager
.getDefaultUri(RingtoneManager
.TYPE_RINGTONE
);
569 ringtone
=RingtoneManager
.getRingtone(context
, alert
);
574 * Make the phone start ringing. Turns up the volume and sets the ringer mode to NORMAL
576 * @param context Context instance
577 * @param replyTo reply Address
579 private static void startAlarm(final Context context
, final Address replyTo
){
580 Utils
.registerOngoing(context
, toNonNull(OngoingEvent
.RING
));
581 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
582 savedRingerMode
=man
.getRingerMode();
583 man
.setRingerMode(AudioManager
.RINGER_MODE_NORMAL
);
584 savedRingVolume
=man
.getStreamVolume(AudioManager
.STREAM_RING
);
585 man
.setStreamVolume(AudioManager
.STREAM_RING
, man
.getStreamMaxVolume(AudioManager
.STREAM_RING
), 0);
586 Utils
.sendMessage(context
, replyTo
, ringing
);
591 * Get a camera instance.
593 * @param context Context instance
594 * @param replyTo reply Address
596 private static void startCamera(final Context context
, final Address replyTo
){
600 camera
=Camera
.open();
601 } catch (Exception e
){
602 Utils
.sendMessage(context
, replyTo
, cannot_grab_camera
);
607 * Make the phone stop ringing. Restores the volume and ringer mode.
609 * @param context Context instance
610 * @param replyTo reply Address
612 private static void stopAlarm(final Context context
, final Address replyTo
){
613 Utils
.unregisterOngoing(context
, toNonNull(OngoingEvent
.RING
));
614 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
615 Utils
.sendMessage(context
, replyTo
, no_longer_ringing
);
617 man
.setStreamVolume(AudioManager
.STREAM_RING
, savedRingVolume
, 0);
618 man
.setRingerMode(savedRingerMode
);
622 * Release the previously grabbed camera instance
624 * @see #startCamera(Context, Address)
626 private static void stopCamera(){
634 * Send battery status information to an Address
636 * @param context Context instance
637 * @param replyTo destination Address
639 * @see #describeBatteryLevel(Context, Address, MessageType)
641 public static void batt(final Context context
, final Address replyTo
){
642 describeBatteryLevel(context
, replyTo
, null);
646 * Show the bluetooth radio status.
648 * @param context Context instance
649 * @param replyTo destination Address
651 public static void bluetooth(final Context context
, final Address replyTo
) {
652 final BluetoothAdapter adapter
=BluetoothAdapter
.getDefaultAdapter();
654 Utils
.sendMessage(context
, replyTo
, no_bluetooth_adapter
);
658 if(adapter
.isEnabled())
659 Utils
.sendMessage(context
, replyTo
, bluetooth_on
);
661 Utils
.sendMessage(context
, replyTo
, bluetooth_off
);
665 * Set the bluetooth radio status.
667 * @param context Context instance
668 * @param replyTo destination Address
669 * @param on the requested radio status
671 public static void bluetooth(final Context context
, final Address replyTo
, final boolean on
){
672 final BluetoothAdapter adapter
=BluetoothAdapter
.getDefaultAdapter();
674 Utils
.sendMessage(context
, replyTo
, no_bluetooth_adapter
);
680 Utils
.sendMessage(context
, replyTo
, enabling_bluetooth
);
684 Utils
.sendMessage(context
, replyTo
, disabling_bluetooth
);
689 * Cancel an ongoing event.
691 * @param context Context instance
692 * @param event the event to cancel
694 public static void cancelOngoing(final Context context
, final OngoingEvent event
){
697 nolocation(context
, toNonNull(Address
.BLACKHOLE
));
700 poll(context
, toNonNull(Address
.BLACKHOLE
), 0);
703 ring(context
, toNonNull(Address
.BLACKHOLE
), false);
709 * Send the last calls to an Address.
711 * @param context Context instance
712 * @param replyTo destination Address
713 * @param numCalls how many calls to send
715 public static void calllog(final Context context
, final Address replyTo
, final int numCalls
) {
716 final String
[] fields
= {
717 Calls
.TYPE
, Calls
.NUMBER
, Calls
.CACHED_NAME
, Calls
.DURATION
, Calls
.DATE
720 final Cursor cursor
= context
.getContentResolver().query(
728 if (cursor
.moveToFirst()) {
730 final StringBuilder sb
=new StringBuilder(50);//NOPMD different strings
731 final int type
=cursor
.getInt(0);
732 final String from
=cursor
.getString(1);
735 case Calls
.INCOMING_TYPE
:
736 sb
.append(context
.getString(incoming_call_from
, from
));
738 case Calls
.MISSED_TYPE
:
739 sb
.append(context
.getString(missed_call_from
, from
));
741 case Calls
.OUTGOING_TYPE
:
742 sb
.append(context
.getString(outgoing_call_to
, from
));
746 if (cursor
.getString(2) != null)
747 sb
.append('(').append(cursor
.getString(2)).append(") ");
749 sb
.append(context
.getString(duration_seconds_starting_at
,
750 Long
.valueOf(cursor
.getLong(3)),
751 new Date(cursor
.getLong(4))));
753 Utils
.sendMessage(context
, replyTo
, toNonNull(sb
.toString()));
754 } while (cursor
.moveToNext() && cursor
.getPosition() < numCalls
);
761 * Search for contacts by name/nickname and send matching entries to an Address.
763 * @param context Context instance
764 * @param replyTo destination Address
765 * @param name name/nickname part to search for
767 @SuppressLint("StringFormatMatches")
768 public static void contacts(final Context context
, final Address replyTo
, final String name
){
769 final Cursor cursor
=context
.getContentResolver().query(Uri
.withAppendedPath(
770 Contacts
.CONTENT_FILTER_URI
, name
),
771 new String
[]{Contacts
.DISPLAY_NAME
, BaseColumns
._ID
, Contacts
.LOOKUP_KEY
},
772 null, null, Contacts
.DISPLAY_NAME
);
774 if(cursor
.getCount()==0)
775 Utils
.sendMessage(context
, replyTo
, no_matching_contacts_found
);
777 while(cursor
.moveToNext()){
778 final String
[] fields
= {
779 CommonDataKinds
.Phone
.NUMBER
,
780 CommonDataKinds
.Phone
.TYPE
,
781 CommonDataKinds
.Phone
.LABEL
,
784 final Cursor inCursor
=context
.getContentResolver().query(Data
.CONTENT_URI
,
786 Data
.CONTACT_ID
+" = ? AND "+Data
.MIMETYPE
+ " = ?",
787 new String
[]{Long
.toString(cursor
.getLong(1)), CommonDataKinds
.Phone
.CONTENT_ITEM_TYPE
},
788 CommonDataKinds
.Phone
.LABEL
);
790 while(inCursor
.moveToNext())
791 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(contact_info
,
793 inCursor
.getString(0),
794 phoneNumberType(context
, inCursor
.getInt(1), inCursor
.getString(2)))));
803 * Send battery status information to an Address or as a notification
805 * @param context Context instance
806 * @param replyTo Address to send the information to, if sending to a direct address. Null otherwise.
807 * @param type Notification type, if sending as a notification. Null otherwise.
809 public static void describeBatteryLevel(final Context context
, final @Nullable Address replyTo
, final @Nullable MessageType type
) {
810 if(replyTo
==null&&type
==null)
812 final Intent intent
=context
.registerReceiver(null, new IntentFilter(Intent
.ACTION_BATTERY_CHANGED
));
815 final double level
=intent
.getIntExtra(BatteryManager
.EXTRA_LEVEL
, 0);
816 final int scale
=intent
.getIntExtra(BatteryManager
.EXTRA_SCALE
, 100);
817 final int plugged
=intent
.getIntExtra(BatteryManager
.EXTRA_PLUGGED
, 0);
818 final int status
=intent
.getIntExtra(BatteryManager
.EXTRA_STATUS
, BatteryManager
.BATTERY_STATUS_UNKNOWN
);
819 final int temp
=intent
.getIntExtra(BatteryManager
.EXTRA_TEMPERATURE
, 0);
820 final int volt
=intent
.getIntExtra(BatteryManager
.EXTRA_VOLTAGE
, 0);
822 final StringBuilder sb
=new StringBuilder(100);
823 sb
.append(context
.getString(battery_level
, Double
.valueOf(level
*100/scale
)));
827 sb
.append(context
.getString(not_plugged_in
));
829 case BatteryManager
.BATTERY_PLUGGED_AC
:
830 sb
.append(context
.getString(plugged_in_ac
));
832 case BatteryManager
.BATTERY_PLUGGED_USB
:
833 sb
.append(context
.getString(plugged_in_usb
));
835 case BatteryManager
.BATTERY_PLUGGED_WIRELESS
:
836 sb
.append(context
.getString(plugged_in_wireless
));
841 case BatteryManager
.BATTERY_STATUS_CHARGING
:
842 sb
.append(context
.getString(status_charging
));
844 case BatteryManager
.BATTERY_STATUS_DISCHARGING
:
845 sb
.append(context
.getString(status_discharging
));
847 case BatteryManager
.BATTERY_STATUS_FULL
:
848 sb
.append(context
.getString(status_full
));
850 case BatteryManager
.BATTERY_STATUS_NOT_CHARGING
:
851 sb
.append(context
.getString(status_not_charging
));
853 case BatteryManager
.BATTERY_STATUS_UNKNOWN
:
854 sb
.append(context
.getString(status_unknown
));
858 sb
.append(context
.getString(temperature
, Integer
.valueOf(temp
)));
860 sb
.append(context
.getString(voltage
, Integer
.valueOf(volt
)));
862 Utils
.sendMessage(context
, toNonNull(replyTo
), toNonNull(sb
.toString()));
864 Utils
.sendMessage(context
, type
, toNonNull(sb
.toString()));
868 * Dial a phone number.
870 * @param context Context instance
871 * @param replyTo reply Address
872 * @param nr phone number to dial
874 public static void dial(final Context context
, final Address replyTo
, final String nr
){
875 final Intent intent
=new Intent(Intent
.ACTION_CALL
,Uri
.parse("tel:"+nr
));
876 intent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
877 final String name
=Utils
.callerId(context
, nr
);
879 Utils
.sendMessage(context
, replyTo
, dialing
, nr
);
881 Utils
.sendMessage(context
, replyTo
, dialing
, nr
+" ("+name
+")");
882 context
.startActivity(intent
);
886 * Show a dialog with a message and a list of buttons.
888 * @param context Context instance
889 * @param replyTo reply Address
890 * @param message dialog message
891 * @param buttons dialog buttons
893 public static void dialog(final Context context
, final Address replyTo
, final String message
, final String
[] buttons
){
894 final Intent intent
=new Intent(context
, DialogActivity
.class);
895 intent
.putExtra(DialogActivity
.EXTRA_MESSAGE
, message
);
896 intent
.putExtra(DialogActivity
.EXTRA_BUTTONS
, buttons
);
897 intent
.putExtra(DialogActivity
.EXTRA_REPLYTO
, replyTo
.toString());
899 Intent
.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
|
900 Intent
.FLAG_ACTIVITY_NEW_TASK
|
901 Intent
.FLAG_ACTIVITY_NO_USER_ACTION
|
902 Intent
.FLAG_FROM_BACKGROUND
);
903 Utils
.sendMessage(context
, toNonNull(replyTo
), showing_dialog
);
904 context
.startActivity(intent
);
908 * Turns the flashlight on or off.
910 * @param context Context instance
911 * @param replyTo reply Address
912 * @param on requested flashlight state
914 public static void flash(final Context context
, final Address replyTo
, final boolean on
){
915 startCamera(context
, replyTo
);
918 final Camera
.Parameters parms
=camera
.getParameters();
920 parms
.setFlashMode(Camera
.Parameters
.FLASH_MODE_TORCH
);
921 camera
.setParameters(parms
);
923 parms
.setFlashMode(Camera
.Parameters
.FLASH_MODE_AUTO
);
924 camera
.setParameters(parms
);
930 * Start sending location updates to an Address.
932 * @param context Context instance
933 * @param replyTo destination Address
934 * @param provider LocationProvider
935 * @param minTime minimum time between two consecutive updates (in ms)
936 * @param minDistance minimum distance between two consecutive updates (in meters)
938 * @see LocationManager#requestLocationUpdates(String, long, float, LocationListener)
940 public static void location(final Context context
, final Address replyTo
, final String provider
,final long minTime
,final float minDistance
){
941 final LocationManager man
=(LocationManager
) context
.getSystemService(Context
.LOCATION_SERVICE
);
942 if(locationListener
!=null)
943 nolocation(context
, toNonNull(Address
.BLACKHOLE
));
944 Utils
.registerOngoing(context
, toNonNull(OngoingEvent
.LOCATION
));
945 locationListener
=new FonBotLocationListener(context
, replyTo
);
946 man
.removeUpdates(locationListener
);
947 final Location lastKnownLocation
=man
.getLastKnownLocation(provider
);
948 if(lastKnownLocation
!=null){
949 Utils
.sendMessage(context
, replyTo
, last_known_location
);
950 locationListener
.onLocationChanged(lastKnownLocation
);
952 Utils
.sendMessage(context
, replyTo
, listening_for_location_updates
);
953 man
.requestLocationUpdates(provider
, minTime
, minDistance
, locationListener
);
959 * @param context Context instance
960 * @param replyTo reply Address
962 public static void lock(final Context context
, final Address replyTo
) {
963 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
965 Utils
.sendMessage(context
, replyTo
, device_locked
);
969 * Send a command to a running instance of the music player
971 * @param context Context instance
972 * @param replyTo reply Address
973 * @param command command to send
975 public static void musicPlayerCommand(final Context context
, final Address replyTo
, final String command
) {
976 final Intent intent
=new Intent("com.android.music.musicservicecommand");
977 intent
.putExtra("command", command
);
978 context
.sendBroadcast(intent
);
979 Utils
.sendMessage(context
, replyTo
, command_sent
);
983 * Send a file to a server.
985 * @param context Context instance
986 * @param replyTo reply Address
987 * @param filename file to send
988 * @param hostname server hostname
989 * @param port server port
991 public static void ncfile(final Context context
, final Address replyTo
, final String filename
,final String hostname
,final int port
){
992 new AsyncTask
<Void
, Void
, Void
>() {
994 protected @Nullable Void
doInBackground(@Nullable final Void
... params
) {
995 final FileChannel in
;
997 in
=new FileInputStream(filename
).getChannel();
998 } catch (final FileNotFoundException e
){
999 Utils
.sendMessage(context
, replyTo
, file_not_found
, filename
);
1002 final SocketChannel sock
;
1004 sock
= SocketChannel
.open(new InetSocketAddress(hostname
, port
));
1005 } catch (final IOException e
){
1006 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(
1007 cannot_connect_to_host_on_port
, hostname
, Integer
.valueOf(port
))));
1010 } catch (IOException ex
) {
1017 in
.transferTo(0, in
.size(), sock
);
1018 } catch (final IOException e
){
1019 Utils
.sendMessage(context
, replyTo
, toNonNull(context
.getString(
1020 io_error
, e
.getMessage())));
1024 } catch (IOException e
){
1029 } catch(IOException e
){
1037 protected void onPostExecute(@Nullable final Void result
) {
1038 Utils
.sendMessage(context
, replyTo
, file_sent
);
1044 * Stop sending location updates.
1046 * @param context Context instance
1047 * @param replyTo reply Address
1049 public static void nolocation(final Context context
, final Address replyTo
){
1050 Utils
.unregisterOngoing(context
, toNonNull(OngoingEvent
.LOCATION
));
1051 final LocationManager man
=(LocationManager
) context
.getSystemService(Context
.LOCATION_SERVICE
);
1052 man
.removeUpdates(locationListener
);
1053 locationListener
=null;
1054 Utils
.sendMessage(context
, replyTo
, no_longer_listening_for_location_updates
);
1058 * Take a photo and send it to a server.
1060 * @param context Context instance
1061 * @param replyTo reply Address
1062 * @param hostname server hostname
1063 * @param port server port
1065 public static void photo(final Context context
, final Address replyTo
, final String hostname
, final int port
){
1066 startCamera(context
, replyTo
);
1069 final Camera
.Parameters parms
=camera
.getParameters();
1070 parms
.setJpegQuality(70);
1071 parms
.setPictureFormat(ImageFormat
.JPEG
);
1072 camera
.setParameters(parms
);
1074 final SurfaceView fakeView
=new SurfaceView(context
);
1076 camera
.setPreviewDisplay(fakeView
.getHolder());
1077 } catch (IOException e
) {
1078 Utils
.sendMessage(context
, replyTo
, error_setting_preview_display
);
1081 camera
.startPreview();
1082 final Handler handler
=new Handler();
1084 new Thread(new Runnable() {
1089 } catch (InterruptedException e
) {
1093 handler
.post(new Runnable() {
1096 camera
.takePicture(null, null, new FonBotPictureCallback(context
, replyTo
, hostname
, port
));
1104 * Send a directory listing to an Address
1106 * @param context Context instance
1107 * @param replyTo destination Address
1108 * @param directory directory to list
1110 public static void ls(final Context context
, final Address replyTo
, final String directory
) {
1111 final File
[] files
=new File(directory
).listFiles();
1113 Utils
.sendMessage(context
, replyTo
, string_is_not_a_directory
, directory
);
1117 final StringBuilder sb
=new StringBuilder(context
.getString(files_in_directory
,directory
));
1118 for(final File file
: files
){
1119 sb
.append(file
.getName());
1120 if(file
.isDirectory())
1125 Utils
.sendMessage(context
, replyTo
, toNonNull(sb
.toString()));
1129 * Make the phone start ringing if it is not ringing or stop ringing if it is.
1131 * @param context Context instance
1132 * @param replyTo reply Address
1134 public static void ring(final Context context
, final Address replyTo
){
1135 setupRingtone(context
);
1137 Utils
.sendMessage(context
, replyTo
, no_ringtone_found
);
1140 if(ringtone
.isPlaying())
1141 stopAlarm(context
, replyTo
);
1143 startAlarm(context
, replyTo
);
1147 * Make the phone start/stop ringing.
1149 * @param context Context instance
1150 * @param replyTo reply Address
1151 * @param on true if the phone should start ringing, false otherwise
1153 public static void ring(final Context context
, final Address replyTo
, final boolean on
){
1154 setupRingtone(context
);
1156 Utils
.sendMessage(context
, replyTo
, no_ringtone_found
);
1159 if(on
&&!ringtone
.isPlaying())
1160 startAlarm(context
, replyTo
);
1161 else if(ringtone
.isPlaying()&&!on
)
1162 stopAlarm(context
, replyTo
);
1166 * Send the current ringer mode to an Address
1168 * @param context Context instance
1169 * @param replyTo destination Address
1171 public static void ringer(final Context context
, final Address replyTo
){
1172 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
1173 switch(man
.getRingerMode()){
1174 case AudioManager
.RINGER_MODE_NORMAL
:
1175 Utils
.sendMessage(context
, replyTo
, ringer_mode_normal
);
1177 case AudioManager
.RINGER_MODE_VIBRATE
:
1178 Utils
.sendMessage(context
, replyTo
, ringer_mode_vibrate
);
1180 case AudioManager
.RINGER_MODE_SILENT
:
1181 Utils
.sendMessage(context
, replyTo
, ringer_mode_silent
);
1184 Utils
.sendMessage(context
, replyTo
, unknown_ringer_mode
);
1189 * Set the ringer mode.
1191 * @param context Context instance
1192 * @param replyTo reply Address
1193 * @param ringerMode requested ringer mode
1195 * @see Utils.RingerMode
1197 public static void ringer(final Context context
, final Address replyTo
, final int ringerMode
){
1198 final AudioManager man
=(AudioManager
) context
.getSystemService(Context
.AUDIO_SERVICE
);
1199 man
.setRingerMode(ringerMode
);
1200 ringer(context
, replyTo
);
1204 * Remove a file or empty directory.
1206 * @param context Context instance
1207 * @param replyTo reply Address
1208 * @param filename file/empty directory to delete
1210 public static void rm(final Context context
, final Address replyTo
, final String filename
){
1211 if(new File(filename
).delete())
1212 Utils
.sendMessage(context
, replyTo
, file_deleted
);
1214 Utils
.sendMessage(context
, replyTo
, error_while_deleting_file
);
1218 * Clear the keyguard password.
1220 * @param context Context instance
1221 * @param replyTo reply Address
1222 * @throws SecurityException if FonBot does not have device administration permissions
1224 public static void setPassword(final Context context
, final Address replyTo
) throws SecurityException
{
1225 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1227 dpm
.resetPassword("", 0);
1228 Utils
.sendMessage(context
, replyTo
, password_cleared
);
1232 * Change the keyguard password.
1234 * @param context Context instance
1235 * @param replyTo reply Address
1236 * @param password new password
1237 * @throws SecurityException if FonBot does not have device administration permissions
1239 public static void setPassword(final Context context
, final Address replyTo
, final String password
) throws SecurityException
{
1240 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1242 dpm
.resetPassword(password
, 0);
1243 Utils
.sendMessage(context
, replyTo
, password_set
);
1247 * Send a text message.
1249 * @param context Context instance
1250 * @param replyTo reply Address
1251 * @param destination destination phone number
1252 * @param text text message contents
1254 public static void sms(final Context context
, final Address replyTo
, final String destination
, final String text
){
1255 final SmsManager manager
=SmsManager
.getDefault();
1256 final ArrayList
<String
> messages
=manager
.divideMessage(text
);
1257 if(messages
.size() > 1)
1258 Utils
.sendMessage(context
, replyTo
, message_was_split_into_parts
, Integer
.valueOf(messages
.size()));
1260 final ArrayList
<PendingIntent
> sents
=new ArrayList
<PendingIntent
>(messages
.size());
1261 final ArrayList
<PendingIntent
> delivereds
=new ArrayList
<PendingIntent
>(messages
.size());
1263 final String name
=Utils
.callerId(context
, destination
);
1264 final String fullDestination
;
1266 fullDestination
=destination
;
1268 fullDestination
=destination
+" ("+name
+")";
1270 for(int i
=0;i
<messages
.size();i
++){
1271 final Intent sent
=new Intent(context
,SmsStatusReceiver
.class);
1272 sent
.putExtra(SmsStatusReceiver
.EXTRA_DESTINATION
, fullDestination
);
1273 sent
.putExtra(SmsStatusReceiver
.EXTRA_PART
, i
+1);
1274 sent
.putExtra(SmsStatusReceiver
.EXTRA_TOTAL
, messages
.size());
1275 sent
.putExtra(SmsStatusReceiver
.EXTRA_REPLY_TO
, replyTo
.toString());
1276 sent
.setAction(SmsStatusReceiver
.SENT_ACTION
+i
);//actions must be unique
1277 sents
.add(PendingIntent
.getBroadcast(context
, 0, sent
, PendingIntent
.FLAG_UPDATE_CURRENT
));
1279 final Intent delivered
=new Intent(context
, SmsStatusReceiver
.class);
1280 delivered
.putExtra(SmsStatusReceiver
.EXTRA_DESTINATION
, fullDestination
);
1281 delivered
.putExtra(SmsStatusReceiver
.EXTRA_PART
, i
+1);
1282 delivered
.putExtra(SmsStatusReceiver
.EXTRA_TOTAL
, messages
.size());
1283 delivered
.putExtra(SmsStatusReceiver
.EXTRA_REPLY_TO
, replyTo
.toString());
1284 delivered
.setAction(SmsStatusReceiver
.DELIVERED_ACTION
+i
);//actions must be unique
1285 delivereds
.add(PendingIntent
.getBroadcast(context
, 0, delivered
, PendingIntent
.FLAG_UPDATE_CURRENT
));
1288 Log
.d(Heavy
.class.getName(), "Sending sms to "+destination
);
1289 manager
.sendMultipartTextMessage(destination
, null, messages
, sents
, delivereds
);
1293 * Send the last SMSes to an Address.
1295 * @param context Context instance
1296 * @param replyTo destination Address
1297 * @param numSms how many SMSes to send
1299 public static void smslog(final Context context
, final Address replyTo
, final int numSms
) {
1300 final String
[] fields
= {"type","address", "body", "date"};
1302 final Cursor cursor
= context
.getContentResolver().query (
1303 Uri
.parse("content://sms"),
1310 if (cursor
.moveToFirst()) {
1312 final String fromNumber
=cursor
.getString(1);
1314 final String name
=Utils
.callerId(context
, Utils
.toNonNull(fromNumber
));
1318 from
=fromNumber
+" ("+name
+')';
1319 final String message
=cursor
.getString(2).replace("\n", "\n ");
1320 final Date date
=new Date(cursor
.getLong(3));
1322 if(cursor
.getInt(0)==1)
1323 Utils
.sendMessage(context
, replyTo
, incoming_message
, from
, message
, date
);
1325 Utils
.sendMessage(context
, replyTo
, outgoing_message
, from
, message
, date
);
1326 } while (cursor
.moveToNext() && cursor
.getPosition() < numSms
);
1332 /** TTS instance, only used by {@link #speak(Context, Address, String)} */
1333 private static TextToSpeech tts
;
1336 * Speak a String using the text-to-speech engine.
1338 * @param context Context instance
1339 * @param replyTo reply Address
1340 * @param text text to speak
1342 public static void speak(final Context context
, final Address replyTo
, final String text
){
1343 tts
=new TextToSpeech(context
, new OnInitListener() {
1345 public void onInit(final int status
) {
1346 if(status
==TextToSpeech
.SUCCESS
){
1347 Utils
.sendMessage(context
, replyTo
, speaking
);
1348 tts
.speak(text
, TextToSpeech
.QUEUE_ADD
, null);
1350 Utils
.sendMessage(context
, replyTo
, tts_engine_not_available
);
1356 * Show a toast notification with the default duration.
1358 * @param context Context instance
1359 * @param replyTo reply Address
1360 * @param text toast text
1362 public static void toast(final Context context
, final Address replyTo
, final String text
){
1363 toast(context
, replyTo
, text
, Toast
.LENGTH_SHORT
);
1367 * Show a toast notification.
1369 * @param context Context instance
1370 * @param replyTo reply Address
1371 * @param text toast text
1372 * @param duration toast duration
1374 public static void toast(final Context context
, final Address replyTo
, final String text
, final int duration
){
1375 Toast
.makeText(context
,text
,duration
).show();
1376 Utils
.sendMessage(context
, replyTo
, toast_shown
);
1380 * Make the phone vibrate for a number of milliseconds.
1382 * @param context Context instance
1383 * @param replyTo reply Address
1384 * @param ms vibrate duration, in milliseconds
1386 public static void vibrate(final Context context
, final Address replyTo
, final long ms
){
1387 final Vibrator v
=(Vibrator
) context
.getSystemService(Context
.VIBRATOR_SERVICE
);
1388 Utils
.sendMessage(context
, replyTo
, vibrating
);
1393 * View an URI in an appropriate activity.
1395 * @param context Context instance
1396 * @param replyTo reply Address
1397 * @param uri URI to view
1399 public static void view(final Context context
, final Address replyTo
, final Uri uri
) {
1401 final Intent intent
=new Intent(Intent
.ACTION_VIEW
);
1402 intent
.setData(uri
);
1403 intent
.setFlags(Intent
.FLAG_FROM_BACKGROUND
|Intent
.FLAG_ACTIVITY_NEW_TASK
);
1404 context
.startActivity(intent
);
1405 Utils
.sendMessage(context
, replyTo
, url_opened
);
1406 } catch(ActivityNotFoundException e
){
1407 Utils
.sendMessage(context
, replyTo
, no_activity_found_for_this_url
);
1408 } catch(Exception e
){
1409 Utils
.sendMessage(context
, replyTo
, invalid_url
);
1414 * Get the current WiFi state.
1416 * @param context Context instance
1417 * @param replyTo reply Address
1419 public static void wifi(final Context context
, final Address replyTo
){
1420 final WifiManager man
=(WifiManager
) context
.getSystemService(Context
.WIFI_SERVICE
);
1421 if(man
.isWifiEnabled())
1422 Utils
.sendMessage(context
, replyTo
, wifi_on
);
1424 Utils
.sendMessage(context
, replyTo
, wifi_off
);
1428 * Set the WiFi state.
1430 * @param context Context instance
1431 * @param replyTo reply Address
1432 * @param on the requested WiFi state
1434 public static void wifi(final Context context
, final Address replyTo
, final boolean on
){
1435 final WifiManager man
=(WifiManager
) context
.getSystemService(Context
.WIFI_SERVICE
);
1436 man
.setWifiEnabled(on
);
1438 Utils
.sendMessage(context
, replyTo
, enabling_wifi
);
1440 Utils
.sendMessage(context
, replyTo
, disabling_wifi
);
1444 * Factory reset the phone, optionally deleting the SD card too.
1446 * @param context Context instance
1447 * @param type {@link Utils.WipeType} instance
1448 * @throws SecurityException if FonBot does not have device administration permissions
1450 @SuppressLint("InlinedApi")
1451 public static void wipe(final Context context
, final WipeType type
) throws SecurityException
{
1452 final DevicePolicyManager dpm
=(DevicePolicyManager
) context
.getSystemService(Context
.DEVICE_POLICY_SERVICE
);
1459 dpm
.wipeData(DevicePolicyManager
.WIPE_EXTERNAL_STORAGE
);
1465 * Disable a Command. The command cannot be used until enabled again with the {@link Utils.Command#ENABLE ENABLE} command.
1467 * @param context Context instance
1468 * @param replyTo reply Address
1469 * @param command Command to disable
1471 public static void disable(final Context context
, final Address replyTo
, final Command command
){
1472 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
1473 .putBoolean(command
+"disabled", true).commit();
1474 Utils
.sendMessage(context
, replyTo
, command_disabled
, command
);
1478 * Re-enable a disabled Command.
1480 * @param context Context instance
1481 * @param replyTo reply Address
1482 * @param command Command to re-enable
1484 public static void enable(final Context context
, final Address replyTo
, final Command command
){
1485 PreferenceManager
.getDefaultSharedPreferences(context
).edit()
1486 .remove(command
+"disabled").commit();
1487 Utils
.sendMessage(context
, replyTo
, command_enabled
, command
);
1492 * Check whether a Command is disabled.
1494 * @param context Context instance
1495 * @param command Command to check
1496 * @return true if the Command is disabled, false otherwise
1498 public static boolean isCommandDisabled(final Context context
, final Command command
){
1499 return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean(command
+"disabled", false);
1503 * Poll the server for pending commands.
1505 * @param context Context instance
1506 * @param replyTo reply Address
1508 public static void poll(final Context context
, final Address replyTo
) {
1509 Utils
.sendMessage(context
, replyTo
, polling_server
);
1510 Utils
.pollServer(context
);
1514 * Change the server poll interval.
1516 * @param context Context instance
1517 * @param replyTo reply Address
1518 * @param ms server poll interval in milliseconds. If 0, server poll is disabled
1520 public static void poll(final Context context
, final Address replyTo
, final long ms
){
1521 final AlarmManager man
=(AlarmManager
) context
.getSystemService(Context
.ALARM_SERVICE
);
1522 final Intent pollAlarm
=new Intent(context
, LocalBroadcastReceiver
.class);
1523 pollAlarm
.setAction(LocalBroadcastReceiver
.ACTION_POLL_ALARM
);
1524 final PendingIntent intent
=PendingIntent
.getBroadcast(context
, 0, pollAlarm
, 0);
1526 Utils
.unregisterOngoing(context
, toNonNull(OngoingEvent
.POLL
));
1528 Utils
.sendMessage(context
, replyTo
, polling_stopped
);
1530 Utils
.registerOngoing(context
, toNonNull(OngoingEvent
.POLL
));
1531 man
.setRepeating(AlarmManager
.RTC_WAKEUP
, 0, ms
, intent
);
1532 Utils
.sendMessage(context
, replyTo
, polling_every_milliseconds
, Long
.valueOf(ms
));
1537 * Get an instance of {@link ITelephony}
1539 * @param context Context instance
1540 * @return an instance of {@link ITelephony}
1541 * @throws NoSuchMethodException thrown by reflection
1542 * @throws IllegalArgumentException thrown by reflection
1543 * @throws IllegalAccessException thrown by reflection
1544 * @throws InvocationTargetException thrown by reflection
1546 private static ITelephony
getITelephony(final Context context
) throws NoSuchMethodException
, IllegalArgumentException
, IllegalAccessException
, InvocationTargetException
{
1547 final TelephonyManager man
=(TelephonyManager
) context
.getSystemService(Context
.TELEPHONY_SERVICE
);
1548 final Method m
=TelephonyManager
.class.getDeclaredMethod("getITelephony");
1549 m
.setAccessible(true);
1550 return toNonNull((ITelephony
) m
.invoke(man
));
1554 * Hang up the phone.
1556 * @param context Context instance
1557 * @param replyTo reply Address
1559 public static void hangup(final Context context
, final Address replyTo
){
1561 getITelephony(context
).endCall();
1562 } catch(Exception e
){
1563 Utils
.sendMessage(context
, replyTo
, exception_while_hanging_up_call
,
1564 e
.getClass().getName(), e
.getMessage());
1569 * Answer the phone if it is ringing.
1571 * @param context Context instance
1572 * @param replyTo reply Address
1574 public static void answer(final Context context
, final Address replyTo
){
1576 getITelephony(context
).answerRingingCall();
1577 } catch(Exception e
){
1578 Utils
.sendMessage(context
, replyTo
, exception_while_answering_call
,
1579 e
.getClass().getName(), e
.getMessage());
1586 * @param context Context instance
1587 * @param replyTo reply Address
1588 * @param pkg name of the package to launch
1590 public static void launch(final Context context
, final Address replyTo
, final String pkg
){
1591 final Intent intent
=context
.getPackageManager().getLaunchIntentForPackage(pkg
);
1593 Utils
.sendMessage(context
, replyTo
, no_such_package
);
1596 context
.startActivity(intent
);
1597 Utils
.sendMessage(context
, replyTo
, app_launched
);
1601 * Get the mobile data enabled status.
1603 * @param context Context instance
1604 * @param replyTo reply Address
1606 public static void data(final Context context
, final Address replyTo
){
1608 final ConnectivityManager man
=(ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
1609 final Method m
=ConnectivityManager
.class.getDeclaredMethod("getMobileDataEnabled");
1610 m
.setAccessible(true);
1611 if(((Boolean
)m
.invoke(man
)).booleanValue())
1612 Utils
.sendMessage(context
, replyTo
, data_on
);
1614 Utils
.sendMessage(context
, replyTo
, data_off
);
1615 } catch(Exception e
){
1616 Utils
.sendMessage(context
, replyTo
, exception_while_determining_data_state
,
1617 e
.getClass().getName(), e
.getMessage());
1622 * Set the mobile data enabled status.
1624 * @param context Context instance
1625 * @param replyTo reply Address
1626 * @param enable whether to enable mobile data
1628 public static void data(final Context context
, final Address replyTo
, final boolean enable
) {
1631 getITelephony(context
).enableDataConnectivity();
1632 Utils
.sendMessage(context
, replyTo
, enabling_data
);
1634 getITelephony(context
).disableDataConnectivity();
1635 Utils
.sendMessage(context
, replyTo
, disabling_data
);
1637 } catch(Exception e
){
1638 Utils
.sendMessage(context
, replyTo
, exception_while_getting_itelephony
,
1639 e
.getClass().getName(), e
.getMessage());
1644 * Get the GPS status.
1646 * @param context Context instance
1647 * @param replyTo reply Address
1649 public static void gps(final Context context
, final Address replyTo
){
1650 if(Secure
.isLocationProviderEnabled(context
.getContentResolver(), LocationManager
.GPS_PROVIDER
))
1651 Utils
.sendMessage(context
, replyTo
, gps_on
);
1653 Utils
.sendMessage(context
, replyTo
, gps_off
);
1657 * Set the GPS status.
1659 * @param context Context instance
1660 * @param replyTo reply Address
1661 * @param enabled requested GPS status
1663 public static void gps(final Context context
, final Address replyTo
, final boolean enabled
) {
1664 Secure
.setLocationProviderEnabled(context
.getContentResolver(), LocationManager
.GPS_PROVIDER
, enabled
);
1666 Utils
.sendMessage(context
, replyTo
, enabling_gps
);
1668 Utils
.sendMessage(context
, replyTo
, disabling_gps
);
1672 * Get the Google location (aka network location) state.
1674 * @param context Context instance
1675 * @param replyTo reply Address
1677 public static void glocation(final Context context
, final Address replyTo
){
1678 if(Secure
.isLocationProviderEnabled(context
.getContentResolver(), LocationManager
.NETWORK_PROVIDER
))
1679 Utils
.sendMessage(context
, replyTo
, network_location_on
);
1681 Utils
.sendMessage(context
, replyTo
, network_location_off
);
1685 * Set the Google location (aka network location) state.
1687 * @param context Context instance
1688 * @param replyTo reply Address
1689 * @param enabled requested Google location state
1691 public static void glocation(final Context context
, final Address replyTo
, final boolean enabled
) {
1692 Secure
.setLocationProviderEnabled(context
.getContentResolver(), LocationManager
.NETWORK_PROVIDER
, enabled
);
1694 Utils
.sendMessage(context
, replyTo
, enabling_network_location
);
1696 Utils
.sendMessage(context
, replyTo
, disabling_network_location
);
1702 * @param context Context instance
1703 * @param replyTo reply Address
1704 * @param reason reboot reason
1706 * @see PowerManager#reboot(String)
1708 public static void reboot(final Context context
, final Address replyTo
, final @Nullable String reason
) {
1709 final PowerManager pm
=(PowerManager
) context
.getSystemService(Context
.POWER_SERVICE
);
1710 Utils
.sendMessage(context
, replyTo
, rebooting
);
1715 * Cancel a notification.
1717 * @param context Context instance
1718 * @param replyTo reply Address
1719 * @param id notification ID
1721 public static void notify(final Context context
, final Address replyTo
, final int id
) {
1722 final NotificationManager man
=(NotificationManager
) context
.getSystemService(Context
.NOTIFICATION_SERVICE
);
1724 Utils
.sendMessage(context
, replyTo
, notification_canceled
);
1728 * Show a notification.
1730 * @param context Context instance
1731 * @param replyTo reply Address
1732 * @param id notification ID
1733 * @param title notificationO title
1734 * @param text notification text
1736 public static void notify(final Context context
, final Address replyTo
, final int id
, final String title
, final String text
) {
1737 final NotificationManager man
=(NotificationManager
) context
.getSystemService(Context
.NOTIFICATION_SERVICE
);
1738 man
.notify(id
, new NotificationCompat
.Builder(context
).
1739 setContentTitle(title
).
1740 setContentText(text
).
1741 setSmallIcon(android
.R
.drawable
.stat_notify_sync_noanim
).
1743 Utils
.sendMessage(context
, replyTo
, notification_shown
);