-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneCallTrap.java
More file actions
73 lines (52 loc) · 2.01 KB
/
PhoneCallTrap.java
File metadata and controls
73 lines (52 loc) · 2.01 KB
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
package io.gvox.phonecalltrap;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import org.json.JSONException;
import org.json.JSONArray;
public class PhoneCallTrap extends CordovaPlugin {
CallStateListener listener;
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
prepareListener();
listener.setCallbackContext(callbackContext);
return true;
}
private void prepareListener() {
if (listener == null) {
listener = new CallStateListener();
TelephonyManager TelephonyMgr = (TelephonyManager) cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
TelephonyMgr.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
}
class CallStateListener extends PhoneStateListener {
private CallbackContext callbackContext;
public void setCallbackContext(CallbackContext callbackContext) {
this.callbackContext = callbackContext;
}
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
if (callbackContext == null) return;
String msg = "";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
msg = "IDLE";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
msg = "OFFHOOK";
break;
case TelephonyManager.CALL_STATE_RINGING:
msg = "RINGING";
break;
}
JSONArray msgJSON = new JSONArray();
msgJSON.put(msg);
msgJSON.put(incomingNumber);
PluginResult result = new PluginResult(PluginResult.Status.OK, msgJSON);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
}
}