69
69
// @see https://github.com/kbagchiGWC/voice-quickstart-android/blob/9a2aff7fbe0d0a5ae9457b48e9ad408740dfb968/exampleConnectionService/src/main/java/com/twilio/voice/examples/connectionservice/VoiceConnectionService.java
70
70
@ TargetApi (Build .VERSION_CODES .M )
71
71
public 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 ;
75
75
private static Boolean canMakeMultipleCalls = true ;
76
76
private static String notReachableCallUuid ;
77
77
private static ConnectionRequest currentConnectionRequest ;
@@ -92,9 +92,6 @@ public static Connection getConnection(String connectionId) {
92
92
public VoiceConnectionService () {
93
93
super ();
94
94
Log .e (TAG , "Constructor" );
95
- isReachable = false ;
96
- isInitialized = false ;
97
- isAvailable = false ;
98
95
currentConnectionRequest = null ;
99
96
currentConnectionService = this ;
100
97
}
@@ -106,7 +103,7 @@ public static void setPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle)
106
103
public static void setAvailable (Boolean value ) {
107
104
Log .d (TAG , "setAvailable: " + (value ? "true" : "false" ));
108
105
if (value ) {
109
- isInitialized = true ;
106
+ setInitialized ( true ) ;
110
107
}
111
108
112
109
isAvailable = value ;
@@ -126,10 +123,18 @@ public static void setReachable() {
126
123
VoiceConnectionService .currentConnectionRequest = null ;
127
124
}
128
125
126
+ public static void setInitialized (boolean value ) {
127
+ Log .d (TAG , "setInitialized: " + (value ? "true" : "false" ));
128
+
129
+ isInitialized = value ;
130
+ }
131
+
129
132
public static void deinitConnection (String connectionId ) {
130
133
Log .d (TAG , "deinitConnection:" + connectionId );
131
134
VoiceConnectionService .hasOutgoingCall = false ;
132
135
136
+ currentConnectionService .stopForegroundService ();
137
+
133
138
if (currentConnections .containsKey (connectionId )) {
134
139
currentConnections .remove (connectionId );
135
140
}
@@ -140,6 +145,9 @@ public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManage
140
145
Bundle extra = request .getExtras ();
141
146
Uri number = request .getAddress ();
142
147
String name = extra .getString (EXTRA_CALLER_NAME );
148
+
149
+ Log .d (TAG , "onCreateIncomingConnection, name:" + name );
150
+
143
151
Connection incomingCallConnection = createConnection (request );
144
152
incomingCallConnection .setRinging ();
145
153
incomingCallConnection .setInitialized ();
@@ -154,6 +162,8 @@ public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManage
154
162
VoiceConnectionService .hasOutgoingCall = true ;
155
163
String uuid = UUID .randomUUID ().toString ();
156
164
165
+ Log .d (TAG , "onCreateOutgoingConnection, uuid:" + uuid );
166
+
157
167
if (!isInitialized && !isReachable ) {
158
168
this .notReachableCallUuid = uuid ;
159
169
this .currentConnectionRequest = request ;
@@ -171,7 +181,7 @@ private Connection makeOutgoingCall(ConnectionRequest request, String uuid, Bool
171
181
String displayName = extras .getString (EXTRA_CALLER_NAME );
172
182
Boolean isForeground = VoiceConnectionService .isRunning (this .getApplicationContext ());
173
183
174
- Log .d (TAG , "makeOutgoingCall:" + uuid + ", number: " + number + ", displayName:" + displayName );
184
+ Log .d (TAG , "makeOutgoingCall, uuid :" + uuid + ", number: " + number + ", displayName:" + displayName );
175
185
176
186
// Wakeup application if needed
177
187
if (!isForeground || forceWakeUp ) {
@@ -221,6 +231,7 @@ private void startForegroundService() {
221
231
// Foreground services not required before SDK 28
222
232
return ;
223
233
}
234
+ Log .d (TAG , "startForegroundService" );
224
235
if (_settings == null || !_settings .hasKey ("foregroundService" )) {
225
236
Log .d (TAG , "Not creating foregroundService because not configured" );
226
237
return ;
@@ -251,7 +262,21 @@ private void startForegroundService() {
251
262
startForeground (FOREGROUND_SERVICE_TYPE_MICROPHONE , notification );
252
263
}
253
264
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
+
254
274
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
+
255
280
Intent headlessIntent = new Intent (
256
281
this .getApplicationContext (),
257
282
RNCallKeepBackgroundMessagingService .class
@@ -299,6 +324,8 @@ private Boolean canMakeOutgoingCall() {
299
324
}
300
325
301
326
private Connection createConnection (ConnectionRequest request ) {
327
+ Log .d (TAG , "createConnection" );
328
+
302
329
Bundle extras = request .getExtras ();
303
330
HashMap <String , String > extrasMap = this .bundleToMap (extras );
304
331
extrasMap .put (EXTRA_CALL_NUMBER , request .getAddress ().toString ());
@@ -323,6 +350,7 @@ private Connection createConnection(ConnectionRequest request) {
323
350
324
351
@ Override
325
352
public void onConference (Connection connection1 , Connection connection2 ) {
353
+ Log .d (TAG , "onConference" );
326
354
super .onConference (connection1 , connection2 );
327
355
VoiceConnection voiceConnection1 = (VoiceConnection ) connection1 ;
328
356
VoiceConnection voiceConnection2 = (VoiceConnection ) connection2 ;
@@ -344,6 +372,8 @@ private void sendCallRequestToActivity(final String action, @Nullable final Hash
344
372
final VoiceConnectionService instance = this ;
345
373
final Handler handler = new Handler ();
346
374
375
+ Log .d (TAG , "sendCallRequestToActivity, action:" + action );
376
+
347
377
handler .post (new Runnable () {
348
378
@ Override
349
379
public void run () {
@@ -383,10 +413,13 @@ public static boolean isRunning(Context context) {
383
413
List <RunningTaskInfo > tasks = activityManager .getRunningTasks (Integer .MAX_VALUE );
384
414
385
415
for (RunningTaskInfo task : tasks ) {
386
- if (context .getPackageName ().equalsIgnoreCase (task .baseActivity .getPackageName ()))
416
+ if (context .getPackageName ().equalsIgnoreCase (task .baseActivity .getPackageName ())) {
387
417
return true ;
418
+ }
388
419
}
389
420
421
+ Log .d (TAG , "isRunning: no running package found." );
422
+
390
423
return false ;
391
424
}
392
425
}
0 commit comments