Skip to content

Commit

Permalink
Android - native call sendCall result fix, limit active connections t…
Browse files Browse the repository at this point in the history
…o one, add dtmf support
  • Loading branch information
Andrzej Muzacz committed Sep 7, 2018
1 parent b8ccb0d commit 6a81aa9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/android/CordovaCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
callbackContextMap.put("hangup",new ArrayList<CallbackContext>());
callbackContextMap.put("sendCall",new ArrayList<CallbackContext>());
callbackContextMap.put("receiveCall",new ArrayList<CallbackContext>());
callbackContextMap.put("DTMF",new ArrayList<CallbackContext>());
}

@Override
Expand Down
63 changes: 61 additions & 2 deletions src/android/MyConnectionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
import org.json.JSONObject;
import org.json.JSONException;
import android.content.Intent;
import android.content.Context;
import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.telecom.Connection;
Expand All @@ -16,6 +19,7 @@
import android.net.Uri;
import java.util.ArrayList;
import android.util.Log;
import android.widget.Toast;

public class MyConnectionService extends ConnectionService {

Expand All @@ -30,6 +34,22 @@ public static void deinitConnection() {
conn = null;
}

public static void notifyCordovaCall(String handler, PluginResult result) {
ArrayList<CallbackContext> callbackContexts = CordovaCall.getCallbackContexts().get(handler);
if (callbackContexts == null) {
return;
}

for (final CallbackContext callbackContext : callbackContexts) {
CordovaCall.getCordova().getThreadPool().execute(new Runnable() {
public void run() {
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
}
});
}
}

@Override
public Connection onCreateIncomingConnection(final PhoneAccountHandle connectionManagerPhoneAccount, final ConnectionRequest request) {
final Connection connection = new Connection() {
Expand Down Expand Up @@ -74,6 +94,13 @@ public void onAbort() {
super.onAbort();
}

@Override
public void onPlayDtmfTone(char c) {
super.onPlayDtmfTone(c);

MyConnectionService.notifyCordovaCall("DTMF", new PluginResult(PluginResult.Status.OK, String.valueOf(c)));
}

@Override
public void onDisconnect() {
DisconnectCause cause = new DisconnectCause(DisconnectCause.LOCAL);
Expand Down Expand Up @@ -114,6 +141,13 @@ public void run() {

@Override
public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
final Context context = this.getApplicationContext();

if (conn != null) {
Toast.makeText(context, "You can't make a call right now because you're already in a call", Toast.LENGTH_SHORT).show();
return null;
}

final Connection connection = new Connection() {
@Override
public void onAnswer() {
Expand All @@ -130,13 +164,24 @@ public void onAbort() {
super.onAbort();
}

@Override
public void onPlayDtmfTone(char c) {
super.onPlayDtmfTone(c);

MyConnectionService.notifyCordovaCall("DTMF", new PluginResult(PluginResult.Status.OK, String.valueOf(c)));
}

@Override
public void onDisconnect() {
DisconnectCause cause = new DisconnectCause(DisconnectCause.LOCAL);
this.setDisconnected(cause);
this.destroy();
conn = null;
ArrayList<CallbackContext> callbackContexts = CordovaCall.getCallbackContexts().get("hangup");
if (callbackContexts == null) {
return;
}

for (final CallbackContext callbackContext : callbackContexts) {
CordovaCall.getCordova().getThreadPool().execute(new Runnable() {
public void run() {
Expand All @@ -151,10 +196,17 @@ public void run() {
@Override
public void onStateChanged(int state) {
if(state == Connection.STATE_DIALING) {
final Connection self = this;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (CordovaCall.getCordova() == null) {
Toast.makeText(context, "Please open VoIPstudio first", Toast.LENGTH_SHORT).show();
self.onDisconnect();
return;
}

Intent intent = new Intent(CordovaCall.getCordova().getActivity().getApplicationContext(), CordovaCall.getCordova().getActivity().getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
CordovaCall.getCordova().getActivity().getApplicationContext().startActivity(intent);
Expand All @@ -163,7 +215,8 @@ public void run() {
}
}
};
connection.setAddress(Uri.parse(request.getExtras().getString("to")), TelecomManager.PRESENTATION_ALLOWED);
Uri address = request.getAddress();
connection.setAddress(address, TelecomManager.PRESENTATION_ALLOWED);
Icon icon = CordovaCall.getIcon();
if(icon != null) {
StatusHints statusHints = new StatusHints((CharSequence)"", icon, new Bundle());
Expand All @@ -176,7 +229,13 @@ public void run() {
for (final CallbackContext callbackContext : callbackContexts) {
CordovaCall.getCordova().getThreadPool().execute(new Runnable() {
public void run() {
PluginResult result = new PluginResult(PluginResult.Status.OK, "sendCall event called successfully");
JSONObject info = new JSONObject();
try {
info.put("callId", address.getSchemeSpecificPart());
} catch (JSONException e) {

}
PluginResult result = new PluginResult(PluginResult.Status.OK, info);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
}
Expand Down

0 comments on commit 6a81aa9

Please sign in to comment.