-
Notifications
You must be signed in to change notification settings - Fork 4
/
PhoneStateChangeListener.java
116 lines (98 loc) · 3.12 KB
/
PhoneStateChangeListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* PhoneStateChangeListener cordova plugin (Android)
*
* @author Stéfano Stypulkowski Zanata
* @see http://szanata.com
* @reference https://github.com/madeinstefano/PhoneStateChangeListener
* @license MIT <http://szanata.com/MIT.txt>
* @license GNU <http://szanata.com/GNU.txt>
*
* Based upon PhoneListener by authored by Tommy-Carlos Williams <https://github.com/devgeeks>
*
*/
package com.szanata.cordova.plugins;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.TelephonyManager;
import android.util.Log;
/**
*
*
*/
public class PhoneStateChangeListener extends CordovaPlugin {
private static final String TAG = "PhoneStateChangeListener";
private final String NONE = "NONE";
private Context context;
private CallbackContext callbackContext;
private BroadcastReceiver receiver = null;
/**
* Expecting no parameters from javascript
* Expecting only success callback from javascript
*/
@Override
public boolean execute(final String action,final JSONArray args, final CallbackContext callbackContext) throws JSONException {
this.context = cordova.getActivity().getApplicationContext();
if ("start".equals(action)) {
this.callbackContext = callbackContext;
startPhoneListener();
return true;
}else if ("stop".equals(action)) {
removePhoneListener();
this.callbackContext = null;
return true;
}
return false;
}
/**
* creates a new BroadcastReceiver to listen whether the Telephony State changes
*/
public void startPhoneListener() {
if (this.receiver == null) {
this.receiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
if (intent != null && intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
String state = intent.hasExtra(TelephonyManager.EXTRA_STATE) ? intent.getStringExtra(TelephonyManager.EXTRA_STATE) : NONE;
String number = "";
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
}
if (callbackContext != null){
final JSONObject data = new JSONObject();
try{
data.put("state", state);
data.put("number", number);
}catch(final JSONException e){};
callbackContext.success(data);
}
}
}
};
this.context.registerReceiver(this.receiver, new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED));
}
}
/**
* removes the Receiver
*/
private void removePhoneListener() {
if (this.receiver != null) {
try {
this.context.unregisterReceiver(this.receiver);
this.receiver = null;
} catch (final Exception e) {
Log.e(TAG, "Error unregistering phone listener receiver: " + e.getMessage(), e);
}
}
}
@Override
public void onDestroy() {
removePhoneListener();
}
}