6969// @see https://github.com/kbagchiGWC/voice-quickstart-android/blob/9a2aff7fbe0d0a5ae9457b48e9ad408740dfb968/exampleConnectionService/src/main/java/com/twilio/voice/examples/connectionservice/VoiceConnectionService.java 
7070@ TargetApi (Build .VERSION_CODES .M )
7171public  class  VoiceConnectionService  extends  ConnectionService  {
72-     private  static  Boolean  isAvailable ;
73-     private  static  Boolean  isInitialized ;
74-     private  static  Boolean  isReachable ;
72+     private  static  Boolean  isAvailable  =  false ;
73+     private  static  Boolean  isInitialized  =  false ;
74+     private  static  Boolean  isReachable  =  false ;
7575    private  static  Boolean  canMakeMultipleCalls  = true ;
7676    private  static  String  notReachableCallUuid ;
7777    private  static  ConnectionRequest  currentConnectionRequest ;
@@ -92,9 +92,6 @@ public static Connection getConnection(String connectionId) {
9292    public  VoiceConnectionService () {
9393        super ();
9494        Log .e (TAG , "Constructor" );
95-         isReachable  = false ;
96-         isInitialized  = false ;
97-         isAvailable  = false ;
9895        currentConnectionRequest  = null ;
9996        currentConnectionService  = this ;
10097    }
@@ -106,7 +103,7 @@ public static void setPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle)
106103    public  static  void  setAvailable (Boolean  value ) {
107104        Log .d (TAG , "setAvailable: "  + (value  ? "true"  : "false" ));
108105        if  (value ) {
109-             isInitialized  =  true ;
106+             setInitialized ( true ) ;
110107        }
111108
112109        isAvailable  = value ;
@@ -126,10 +123,18 @@ public static void setReachable() {
126123        VoiceConnectionService .currentConnectionRequest  = null ;
127124    }
128125
126+     public  static  void  setInitialized (boolean  value ) {
127+         Log .d (TAG , "setInitialized: "  + (value  ? "true"  : "false" ));
128+ 
129+         isInitialized  = value ;
130+     }
131+ 
129132    public  static  void  deinitConnection (String  connectionId ) {
130133        Log .d (TAG , "deinitConnection:"  + connectionId );
131134        VoiceConnectionService .hasOutgoingCall  = false ;
132135
136+         currentConnectionService .stopForegroundService ();
137+ 
133138        if  (currentConnections .containsKey (connectionId )) {
134139            currentConnections .remove (connectionId );
135140        }
@@ -140,6 +145,9 @@ public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManage
140145        Bundle  extra  = request .getExtras ();
141146        Uri  number  = request .getAddress ();
142147        String  name  = extra .getString (EXTRA_CALLER_NAME );
148+ 
149+         Log .d (TAG , "onCreateIncomingConnection, name:"  + name );
150+ 
143151        Connection  incomingCallConnection  = createConnection (request );
144152        incomingCallConnection .setRinging ();
145153        incomingCallConnection .setInitialized ();
@@ -154,6 +162,8 @@ public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManage
154162        VoiceConnectionService .hasOutgoingCall  = true ;
155163        String  uuid  = UUID .randomUUID ().toString ();
156164
165+         Log .d (TAG , "onCreateOutgoingConnection, uuid:"  + uuid );
166+ 
157167        if  (!isInitialized  && !isReachable ) {
158168            this .notReachableCallUuid  = uuid ;
159169            this .currentConnectionRequest  = request ;
@@ -171,7 +181,7 @@ private Connection makeOutgoingCall(ConnectionRequest request, String uuid, Bool
171181        String  displayName  = extras .getString (EXTRA_CALLER_NAME );
172182        Boolean  isForeground  = VoiceConnectionService .isRunning (this .getApplicationContext ());
173183
174-         Log .d (TAG , "makeOutgoingCall:"  + uuid  + ", number: "  + number  + ", displayName:"  + displayName );
184+         Log .d (TAG , "makeOutgoingCall, uuid :"  + uuid  + ", number: "  + number  + ", displayName:"  + displayName );
175185
176186        // Wakeup application if needed 
177187        if  (!isForeground  || forceWakeUp ) {
@@ -221,6 +231,7 @@ private void startForegroundService() {
221231            // Foreground services not required before SDK 28 
222232            return ;
223233        }
234+         Log .d (TAG , "startForegroundService" );
224235        if  (_settings  == null  || !_settings .hasKey ("foregroundService" )) {
225236            Log .d (TAG , "Not creating foregroundService because not configured" );
226237            return ;
@@ -251,7 +262,21 @@ private void startForegroundService() {
251262        startForeground (FOREGROUND_SERVICE_TYPE_MICROPHONE , notification );
252263    }
253264
265+     private  void  stopForegroundService () {
266+         Log .d (TAG , "stopForegroundService" );
267+         if  (_settings  == null  || !_settings .hasKey ("foregroundService" )) {
268+             Log .d (TAG , "Discarding stop foreground service, no service configured" );
269+             return ;
270+         }
271+         stopForeground (FOREGROUND_SERVICE_TYPE_MICROPHONE );
272+     }
273+ 
254274    private  void  wakeUpApplication (String  uuid , String  number , String  displayName ) {
275+          Log .d (TAG , "wakeUpApplication, uuid:"  + uuid  + ", number :"  + number  + ", displayName:"  + displayName );
276+ 
277+         // Avoid to call wake up the app again in wakeUpAfterReachabilityTimeout. 
278+         this .currentConnectionRequest  = null ;
279+ 
255280        Intent  headlessIntent  = new  Intent (
256281            this .getApplicationContext (),
257282            RNCallKeepBackgroundMessagingService .class 
@@ -299,6 +324,8 @@ private Boolean canMakeOutgoingCall() {
299324    }
300325
301326    private  Connection  createConnection (ConnectionRequest  request ) {
327+         Log .d (TAG , "createConnection" );
328+ 
302329        Bundle  extras  = request .getExtras ();
303330        HashMap <String , String > extrasMap  = this .bundleToMap (extras );
304331        extrasMap .put (EXTRA_CALL_NUMBER , request .getAddress ().toString ());
@@ -323,6 +350,7 @@ private Connection createConnection(ConnectionRequest request) {
323350
324351    @ Override 
325352    public  void  onConference (Connection  connection1 , Connection  connection2 ) {
353+         Log .d (TAG , "onConference" );
326354        super .onConference (connection1 , connection2 );
327355        VoiceConnection  voiceConnection1  = (VoiceConnection ) connection1 ;
328356        VoiceConnection  voiceConnection2  = (VoiceConnection ) connection2 ;
@@ -344,6 +372,8 @@ private void sendCallRequestToActivity(final String action, @Nullable final Hash
344372        final  VoiceConnectionService  instance  = this ;
345373        final  Handler  handler  = new  Handler ();
346374
375+         Log .d (TAG , "sendCallRequestToActivity, action:"  + action );
376+ 
347377        handler .post (new  Runnable () {
348378            @ Override 
349379            public  void  run () {
@@ -383,10 +413,13 @@ public static boolean isRunning(Context context) {
383413        List <RunningTaskInfo > tasks  = activityManager .getRunningTasks (Integer .MAX_VALUE );
384414
385415        for  (RunningTaskInfo  task  : tasks ) {
386-             if  (context .getPackageName ().equalsIgnoreCase (task .baseActivity .getPackageName ()))
416+             if  (context .getPackageName ().equalsIgnoreCase (task .baseActivity .getPackageName ())) { 
387417                return  true ;
418+             }
388419        }
389420
421+         Log .d (TAG , "isRunning: no running package found." );
422+ 
390423        return  false ;
391424    }
392425}
0 commit comments