Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Android API >= 31 #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ node_modules/

# Vim swap
*.sw?

# Android Studio files
.idea/
99 changes: 93 additions & 6 deletions src/android/PhoneCallTrap.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,125 @@
package io.gvox.phonecalltrap;

import android.Manifest;
import androidx.annotation.RequiresApi;
import android.os.Build;
import android.content.pm.PackageManager;
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 android.telephony.TelephonyCallback;

import org.json.JSONException;
import org.json.JSONArray;


public class PhoneCallTrap extends CordovaPlugin {

OldCallStateListener oldListener;
CallStateListener listener;
CallbackContext callbackContext;
boolean listenerSet = false;

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
prepareListener();
this.callbackContext = callbackContext;

listener.setCallbackContext(callbackContext);
if (listenerSet) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
listener.setCallbackContext(callbackContext);
} else {
oldListener.setCallbackContext(callbackContext);
}
}

return true;
}

@Override
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
for (int r:grantResults) {
if(r == PackageManager.PERMISSION_DENIED) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, 1));
return;
}
}
registerListener();
}

private void registerListener() {
TelephonyManager TelephonyMgr = (TelephonyManager) cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
TelephonyMgr.registerTelephonyCallback(cordova.getThreadPool(), listener);
if (!listenerSet) {
listener.setCallbackContext(callbackContext);
}
listenerSet = 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);
if (!listenerSet) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
listener = new CallStateListener();
boolean hasPhoneStatePermission = cordova.hasPermission(Manifest.permission.READ_PHONE_STATE);
if (!hasPhoneStatePermission) {
cordova.requestPermissions(this, 0, new String[]{
Manifest.permission.READ_PHONE_STATE
});
} else {
registerListener();
}
} else {
TelephonyManager TelephonyMgr = (TelephonyManager) cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
oldListener = new OldCallStateListener();
TelephonyMgr.listen(oldListener, PhoneStateListener.LISTEN_CALL_STATE);
listenerSet = true;
}
}
}
}

@RequiresApi(api = Build.VERSION_CODES.S)
class CallStateListener extends TelephonyCallback implements TelephonyCallback.CallStateListener {
private CallbackContext callbackContext;

public void setCallbackContext(CallbackContext callbackContext) {
this.callbackContext = callbackContext;
}

@Override
public void onCallStateChanged(int state) {

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;
}

PluginResult result = new PluginResult(PluginResult.Status.OK, msg);
result.setKeepCallback(true);

callbackContext.sendPluginResult(result);
}
}

class CallStateListener extends PhoneStateListener {
class OldCallStateListener extends PhoneStateListener {

private CallbackContext callbackContext;

Expand Down