1
+ package com .hoxfon .react .RNTwilioVoice ;
2
+
3
+ import android .app .ActivityManager ;
4
+ import android .content .BroadcastReceiver ;
5
+ import android .content .Context ;
6
+ import android .content .Intent ;
7
+ import android .os .Bundle ;
8
+ import android .util .Log ;
9
+
10
+ import com .facebook .react .HeadlessJsTaskService ;
11
+
12
+ import java .util .List ;
13
+ //import java.util.Random;
14
+ //
15
+ //import androidx.localbroadcastmanager.content.LocalBroadcastManager;
16
+ //
17
+ //import com.facebook.react.ReactApplication;
18
+ //import com.facebook.react.ReactInstanceManager;
19
+ //import com.facebook.react.bridge.ReactApplicationContext;
20
+ //import com.facebook.react.bridge.ReactContext;
21
+ //import com.hoxfon.react.RNTwilioVoice.CallNotificationManager;
22
+ //
23
+ //import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.ACTION_INCOMING_CALL;
24
+ //import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.INCOMING_CALL_NOTIFICATION_ID;
25
+ import static com .hoxfon .react .RNTwilioVoice .TwilioVoiceModule .TAG ;
26
+
27
+ public class BackgroundCallReceiver extends BroadcastReceiver {
28
+
29
+ @ Override
30
+ public void onReceive (final Context context , final Intent intent ) {
31
+ Log .d (TAG , "### onReceive" );
32
+ /**
33
+ This part will be called every time network connection is changed
34
+ e.g. Connected -> Not Connected
35
+ **/
36
+ if (!isAppOnForeground ((context ))) {
37
+ // ReactApplicationContext ctx = new ReactApplicationContext(context);
38
+ //
39
+ // Random randomNumberGenerator = new Random(System.currentTimeMillis());
40
+ // final int notificationId = randomNumberGenerator.nextInt();
41
+ // CallNotificationManager callNotificationManager = new CallNotificationManager();
42
+ //
43
+ // int appImportance = callNotificationManager.getApplicationImportance(ctx);
44
+ // if (BuildConfig.DEBUG) {
45
+ // Log.d(TAG, "CONTEXT not present appImportance = " + appImportance);
46
+ // }
47
+ // Intent launchIntent = callNotificationManager.getLaunchIntent(
48
+ // ctx,
49
+ // notificationId,
50
+ // intent.getStringExtra("call_sid"),
51
+ // intent.getStringExtra("call_from"),
52
+ // intent.getStringExtra("call_to"),
53
+ // true,
54
+ // ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND
55
+ // );
56
+ // context.startActivity(launchIntent);
57
+ //// Intent callInviteIntent = new Intent(ACTION_INCOMING_CALL);
58
+ //// callInviteIntent.putExtra(INCOMING_CALL_NOTIFICATION_ID, notificationId);
59
+ //// LocalBroadcastManager.getInstance(context).sendBroadcast(callInviteIntent);
60
+ //// callNotificationManager.createIncomingCallNotification(
61
+ //// ctx,
62
+ //// intent.getStringExtra("call_sid"),
63
+ //// intent.getStringExtra("call_from"),
64
+ //// notificationId,
65
+ //// launchIntent
66
+ //// );
67
+
68
+
69
+
70
+ /**
71
+ We will start our service and send extra info about
72
+ network connections
73
+ **/
74
+ Bundle extras = intent .getExtras ();
75
+ Intent serviceIntent = new Intent (context , BackgroundCallTaskService .class );
76
+ serviceIntent .putExtras (extras );
77
+ context .startService (serviceIntent );
78
+ HeadlessJsTaskService .acquireWakeLockNow (context );
79
+ }
80
+ }
81
+
82
+ private boolean isAppOnForeground (Context context ) {
83
+ /**
84
+ We need to check if app is in foreground otherwise the app will crash.
85
+ http://stackoverflow.com/questions/8489993/check-android-application-is-in-foreground-or-not
86
+ **/
87
+ ActivityManager activityManager = (ActivityManager ) context .getSystemService (Context .ACTIVITY_SERVICE );
88
+ List <ActivityManager .RunningAppProcessInfo > appProcesses =
89
+ activityManager .getRunningAppProcesses ();
90
+ if (appProcesses == null ) {
91
+ return false ;
92
+ }
93
+ final String packageName = context .getPackageName ();
94
+ for (ActivityManager .RunningAppProcessInfo appProcess : appProcesses ) {
95
+ if (appProcess .importance ==
96
+ ActivityManager .RunningAppProcessInfo .IMPORTANCE_FOREGROUND &&
97
+ appProcess .processName .equals (packageName )) {
98
+ return true ;
99
+ }
100
+ }
101
+ return false ;
102
+ }
103
+
104
+ }
0 commit comments