-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64c59fa
commit add201a
Showing
5 changed files
with
220 additions
and
45 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
android/src/main/java/com/hoxfon/react/RNTwilioVoice/BackgroundCallReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
android/src/main/java/com/hoxfon/react/RNTwilioVoice/BackgroundCallTaskService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.