Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziomoscon committed Nov 13, 2020
1 parent 64c59fa commit add201a
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.hoxfon.react.RNTwilioVoice;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.facebook.react.HeadlessJsTaskService;

import java.util.List;
//import java.util.Random;
//
//import androidx.localbroadcastmanager.content.LocalBroadcastManager;
//
//import com.facebook.react.ReactApplication;
//import com.facebook.react.ReactInstanceManager;
//import com.facebook.react.bridge.ReactApplicationContext;
//import com.facebook.react.bridge.ReactContext;
//import com.hoxfon.react.RNTwilioVoice.CallNotificationManager;
//
//import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.ACTION_INCOMING_CALL;
//import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.INCOMING_CALL_NOTIFICATION_ID;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.TAG;

public class BackgroundCallReceiver extends BroadcastReceiver {

@Override
public void onReceive(final Context context, final Intent intent) {
Log.d(TAG, "### onReceive");
/**
This part will be called every time network connection is changed
e.g. Connected -> Not Connected
**/
if (!isAppOnForeground((context))) {
// ReactApplicationContext ctx = new ReactApplicationContext(context);
//
// Random randomNumberGenerator = new Random(System.currentTimeMillis());
// final int notificationId = randomNumberGenerator.nextInt();
// CallNotificationManager callNotificationManager = new CallNotificationManager();
//
// int appImportance = callNotificationManager.getApplicationImportance(ctx);
// if (BuildConfig.DEBUG) {
// Log.d(TAG, "CONTEXT not present appImportance = " + appImportance);
// }
// Intent launchIntent = callNotificationManager.getLaunchIntent(
// ctx,
// notificationId,
// intent.getStringExtra("call_sid"),
// intent.getStringExtra("call_from"),
// intent.getStringExtra("call_to"),
// true,
// ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND
// );
// context.startActivity(launchIntent);
//// Intent callInviteIntent = new Intent(ACTION_INCOMING_CALL);
//// callInviteIntent.putExtra(INCOMING_CALL_NOTIFICATION_ID, notificationId);
//// LocalBroadcastManager.getInstance(context).sendBroadcast(callInviteIntent);
//// callNotificationManager.createIncomingCallNotification(
//// ctx,
//// intent.getStringExtra("call_sid"),
//// intent.getStringExtra("call_from"),
//// notificationId,
//// launchIntent
//// );



/**
We will start our service and send extra info about
network connections
**/
Bundle extras = intent.getExtras();
Intent serviceIntent = new Intent(context, BackgroundCallTaskService.class);
serviceIntent.putExtras(extras);
context.startService(serviceIntent);
HeadlessJsTaskService.acquireWakeLockNow(context);
}
}

private boolean isAppOnForeground(Context context) {
/**
We need to check if app is in foreground otherwise the app will crash.
http://stackoverflow.com/questions/8489993/check-android-application-is-in-foreground-or-not
**/
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses =
activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance ==
ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.hoxfon.react.RNTwilioVoice;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;

import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.TAG;

public class BackgroundCallTaskService extends HeadlessJsTaskService {
@Override
protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
Bundle extras = intent.getExtras();
Log.d(TAG, "###getTaskConfig"+ extras.toString());
if (extras != null) {
return new HeadlessJsTaskConfig(
"BackgroundCallTaskService",
Arguments.fromBundle(extras),
5000, // timeout for the task
false // optional: defines whether or not the task is allowed in foreground. Default is false
);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.ACTION_HANGUP_CALL;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.ACTION_INCOMING_CALL;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.ACTION_MISSED_CALL;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.INCOMING_CALL_INVITE;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.INCOMING_CALL_NOTIFICATION_ID;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.NOTIFICATION_TYPE;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.CALL_SID_KEY;
Expand Down Expand Up @@ -86,7 +85,9 @@ public Class getMainActivityClass(ReactApplicationContext context) {

public Intent getLaunchIntent(ReactApplicationContext context,
int notificationId,
CallInvite callInvite,
String callInviteSid,
String callInviteFrom,
String callInviteTo,
Boolean shouldStartNewTask,
int appImportance
) {
Expand All @@ -107,14 +108,15 @@ public Intent getLaunchIntent(ReactApplicationContext context,
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
);

if (callInvite != null) {
launchIntent.putExtra(INCOMING_CALL_INVITE, callInvite);
}
launchIntent.putExtra("call_sid", callInviteSid);
launchIntent.putExtra("call_from", callInviteFrom);
launchIntent.putExtra("call_to", callInviteTo);
return launchIntent;
}

public void createIncomingCallNotification(ReactApplicationContext context,
CallInvite callInvite,
String callInviteSid,
String callInviteFrom,
int notificationId,
Intent launchIntent)
{
Expand All @@ -131,7 +133,7 @@ public void createIncomingCallNotification(ReactApplicationContext context,
*/
Bundle extras = new Bundle();
extras.putInt(INCOMING_CALL_NOTIFICATION_ID, notificationId);
extras.putString(CALL_SID_KEY, callInvite.getCallSid());
extras.putString(CALL_SID_KEY, callInviteSid);
extras.putString(NOTIFICATION_TYPE, ACTION_INCOMING_CALL);
/*
* Create the notification shown in the notification drawer
Expand All @@ -145,7 +147,7 @@ public void createIncomingCallNotification(ReactApplicationContext context,
.setCategory(NotificationCompat.CATEGORY_CALL)
.setSmallIcon(R.drawable.ic_call_white_24dp)
.setContentTitle("Incoming call")
.setContentText(callInvite.getFrom() + " is calling")
.setContentText(callInviteFrom + " is calling")
.setOngoing(true)
.setAutoCancel(true)
.setExtras(extras)
Expand Down Expand Up @@ -180,7 +182,7 @@ public void createIncomingCallNotification(ReactApplicationContext context,
notificationBuilder.addAction(R.drawable.ic_call_white_24dp, "ANSWER", pendingAnswerIntent);

notificationManager.notify(notificationId, notificationBuilder.build());
TwilioVoiceModule.callNotificationMap.put(INCOMING_NOTIFICATION_PREFIX+callInvite.getCallSid(), notificationId);
TwilioVoiceModule.callNotificationMap.put(INCOMING_NOTIFICATION_PREFIX+callInviteSid, notificationId);
}

public void initCallNotificationsChannel(NotificationManager notificationManager) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public class TwilioVoiceModule extends ReactContextBaseJavaModule implements Act
// Empty HashMap, contains parameters for the Outbound call
private HashMap<String, String> twiMLParams = new HashMap<>();

public static final String INCOMING_CALL_INVITE = "INCOMING_CALL_INVITE";
public static final String INCOMING_CALL_NOTIFICATION_ID = "INCOMING_CALL_NOTIFICATION_ID";
public static final String NOTIFICATION_TYPE = "NOTIFICATION_TYPE";
public static final String CANCELLED_CALL_INVITE = "CANCELLED_CALL_INVITE";
Expand Down Expand Up @@ -461,8 +460,7 @@ private void handleIncomingCallIntent(Intent intent) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "handleIncomingCallIntent");
}
activeCallInvite = intent.getParcelableExtra(INCOMING_CALL_INVITE);
if (activeCallInvite != null) {
if (intent != null) {
callAccepted = false;
SoundPoolManager.getInstance(getReactApplicationContext()).playRinging();

Expand All @@ -479,9 +477,9 @@ private void handleIncomingCallIntent(Intent intent) {
appImportance == RunningAppProcessInfo.IMPORTANCE_SERVICE) {

WritableMap params = Arguments.createMap();
params.putString("call_sid", activeCallInvite.getCallSid());
params.putString("call_from", activeCallInvite.getFrom());
params.putString("call_to", activeCallInvite.getTo()); // TODO check if needed
params.putString("call_sid", intent.getStringExtra("call_sid"));
params.putString("call_from", intent.getStringExtra("call_from"));
params.putString("call_to", intent.getStringExtra("call_to"));
eventManager.sendEvent(EVENT_DEVICE_DID_RECEIVE_INCOMING, params);
}
} else {
Expand Down
Loading

0 comments on commit add201a

Please sign in to comment.