1919
2020import android .annotation .TargetApi ;
2121import android .content .Intent ;
22+ import android .content .Context ;
23+ import android .content .ComponentName ;
2224import android .net .Uri ;
2325import android .os .Build ;
2426import android .os .Bundle ;
3537import android .telecom .TelecomManager ;
3638import android .util .Log ;
3739
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+
3847import java .util .ArrayList ;
3948import java .util .HashMap ;
4049import java .util .Iterator ;
@@ -103,29 +112,46 @@ public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManage
103112
104113 @ Override
105114 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-
111115 Bundle extras = request .getExtras ();
112116 Connection outgoingCallConnection = null ;
113117 String number = request .getAddress ().getSchemeSpecificPart ();
114118 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+ }
116141
142+ // TODO: Hold all other calls
117143 if (extrasNumber != null && extrasNumber .equals (number )) {
118144 outgoingCallConnection = createConnection (request );
119145 } else {
120- String uuid = UUID .randomUUID ().toString ();
121146 extras .putString (EXTRA_CALL_UUID , uuid );
122- extras .putString (EXTRA_CALLER_NAME , name );
147+ extras .putString (EXTRA_CALLER_NAME , displayName );
123148 extras .putString (EXTRA_CALL_NUMBER , number );
124149 outgoingCallConnection = createConnection (request );
125150 }
151+
126152 outgoingCallConnection .setDialing ();
127153 outgoingCallConnection .setAudioModeIsVoip (true );
128- outgoingCallConnection .setCallerDisplayName (name , TelecomManager .PRESENTATION_ALLOWED );
154+ outgoingCallConnection .setCallerDisplayName (displayName , TelecomManager .PRESENTATION_ALLOWED );
129155 outgoingCallConnection .setInitialized ();
130156
131157 HashMap <String , String > extrasMap = this .bundleToMap (extras );
@@ -197,8 +223,8 @@ public void run() {
197223 Bundle extras = new Bundle ();
198224 extras .putSerializable ("attributeMap" , attributeMap );
199225 intent .putExtras (extras );
200- LocalBroadcastManager .getInstance (instance ).sendBroadcast (intent );
201226 }
227+ LocalBroadcastManager .getInstance (instance ).sendBroadcast (intent );
202228 }
203229 });
204230 }
@@ -216,4 +242,22 @@ private HashMap<String, String> bundleToMap(Bundle extras) {
216242 }
217243 return extrasMap ;
218244 }
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+ }
219263}
0 commit comments