19
19
20
20
import android .annotation .TargetApi ;
21
21
import android .content .Intent ;
22
+ import android .content .Context ;
23
+ import android .content .ComponentName ;
22
24
import android .net .Uri ;
23
25
import android .os .Build ;
24
26
import android .os .Bundle ;
35
37
import android .telecom .TelecomManager ;
36
38
import android .util .Log ;
37
39
40
+ import android .app .ActivityManager ;
41
+ import android .app .ActivityManager .RunningTaskInfo ;
42
+
43
+ import com .facebook .react .HeadlessJsTaskService ;
44
+ import com .facebook .react .bridge .ReactContext ;
45
+ import com .facebook .react .common .LifecycleState ;
46
+
38
47
import java .util .ArrayList ;
39
48
import java .util .HashMap ;
40
49
import java .util .Iterator ;
@@ -103,29 +112,46 @@ public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManage
103
112
104
113
@ Override
105
114
public Connection onCreateOutgoingConnection (PhoneAccountHandle connectionManagerPhoneAccount , ConnectionRequest request ) {
106
- if (!this .canMakeOutgoingCall ()) {
107
- return Connection .createFailedConnection (new DisconnectCause (DisconnectCause .LOCAL ));
108
- }
109
- // TODO: Hold all other calls
110
-
111
115
Bundle extras = request .getExtras ();
112
116
Connection outgoingCallConnection = null ;
113
117
String number = request .getAddress ().getSchemeSpecificPart ();
114
118
String extrasNumber = extras .getString (EXTRA_CALL_NUMBER );
115
- String name = extras .getString (EXTRA_CALLER_NAME );
119
+ String displayName = extras .getString (EXTRA_CALLER_NAME );
120
+ String uuid = UUID .randomUUID ().toString ();
121
+
122
+ Log .d (TAG , "onCreateOutgoingConnection:" + uuid + ", number: " + number );
123
+
124
+ // Wakeup application if needed
125
+ if (!VoiceConnectionService .isRunning (this .getApplicationContext ())) {
126
+ Log .d (TAG , "onCreateOutgoingConnection: Waking up application" );
127
+ Intent headlessIntent = new Intent (
128
+ this .getApplicationContext (),
129
+ RNCallKeepBackgroundMessagingService .class
130
+ );
131
+ headlessIntent .putExtra ("callUUID" , uuid );
132
+ headlessIntent .putExtra ("name" , displayName );
133
+ headlessIntent .putExtra ("handle" , number );
134
+ ComponentName name = this .getApplicationContext ().startService (headlessIntent );
135
+ if (name != null ) {
136
+ HeadlessJsTaskService .acquireWakeLockNow (this .getApplicationContext ());
137
+ }
138
+ } else if (!this .canMakeOutgoingCall ()) {
139
+ return Connection .createFailedConnection (new DisconnectCause (DisconnectCause .LOCAL ));
140
+ }
116
141
142
+ // TODO: Hold all other calls
117
143
if (extrasNumber != null && extrasNumber .equals (number )) {
118
144
outgoingCallConnection = createConnection (request );
119
145
} else {
120
- String uuid = UUID .randomUUID ().toString ();
121
146
extras .putString (EXTRA_CALL_UUID , uuid );
122
- extras .putString (EXTRA_CALLER_NAME , name );
147
+ extras .putString (EXTRA_CALLER_NAME , displayName );
123
148
extras .putString (EXTRA_CALL_NUMBER , number );
124
149
outgoingCallConnection = createConnection (request );
125
150
}
151
+
126
152
outgoingCallConnection .setDialing ();
127
153
outgoingCallConnection .setAudioModeIsVoip (true );
128
- outgoingCallConnection .setCallerDisplayName (name , TelecomManager .PRESENTATION_ALLOWED );
154
+ outgoingCallConnection .setCallerDisplayName (displayName , TelecomManager .PRESENTATION_ALLOWED );
129
155
outgoingCallConnection .setInitialized ();
130
156
131
157
HashMap <String , String > extrasMap = this .bundleToMap (extras );
@@ -197,8 +223,8 @@ public void run() {
197
223
Bundle extras = new Bundle ();
198
224
extras .putSerializable ("attributeMap" , attributeMap );
199
225
intent .putExtras (extras );
200
- LocalBroadcastManager .getInstance (instance ).sendBroadcast (intent );
201
226
}
227
+ LocalBroadcastManager .getInstance (instance ).sendBroadcast (intent );
202
228
}
203
229
});
204
230
}
@@ -216,4 +242,22 @@ private HashMap<String, String> bundleToMap(Bundle extras) {
216
242
}
217
243
return extrasMap ;
218
244
}
245
+
246
+ /**
247
+ * https://stackoverflow.com/questions/5446565/android-how-do-i-check-if-activity-is-running
248
+ *
249
+ * @param context Context
250
+ * @return boolean
251
+ */
252
+ public static boolean isRunning (Context context ) {
253
+ ActivityManager activityManager = (ActivityManager ) context .getSystemService (Context .ACTIVITY_SERVICE );
254
+ List <RunningTaskInfo > tasks = activityManager .getRunningTasks (Integer .MAX_VALUE );
255
+
256
+ for (RunningTaskInfo task : tasks ) {
257
+ if (context .getPackageName ().equalsIgnoreCase (task .baseActivity .getPackageName ()))
258
+ return true ;
259
+ }
260
+
261
+ return false ;
262
+ }
219
263
}
0 commit comments