Skip to content

Commit 255b5f4

Browse files
authored
Merge pull request #376 from react-native-webrtc/fix_reachability
Avoid to check for reachability everytime we make a call
2 parents b22521f + b760af9 commit 255b5f4

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed

android/src/main/java/io/wazo/callkeep/RNCallKeepModule.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ public String getName() {
122122

123123
@ReactMethod
124124
public void setup(ReadableMap options) {
125+
Log.d(TAG, "setup");
125126
VoiceConnectionService.setAvailable(false);
127+
VoiceConnectionService.setInitialized(true);
126128
this._settings = options;
127129

128130
if (isConnectionServiceAvailable()) {
@@ -140,6 +142,8 @@ public void registerPhoneAccount() {
140142
return;
141143
}
142144

145+
Log.d(TAG, "registerPhoneAccount");
146+
143147
this.registerPhoneAccount(this.getAppContext());
144148
}
145149

@@ -160,7 +164,7 @@ public void displayIncomingCall(String uuid, String number, String callerName) {
160164
return;
161165
}
162166

163-
Log.d(TAG, "displayIncomingCall number: " + number + ", callerName: " + callerName);
167+
Log.d(TAG, "displayIncomingCall, uuid: " + uuid + ", number: " + number + ", callerName: " + callerName);
164168

165169
Bundle extras = new Bundle();
166170
Uri uri = Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null);
@@ -174,6 +178,7 @@ public void displayIncomingCall(String uuid, String number, String callerName) {
174178

175179
@ReactMethod
176180
public void answerIncomingCall(String uuid) {
181+
Log.d(TAG, "answerIncomingCall, uuid: " + uuid);
177182
if (!isConnectionServiceAvailable() || !hasPhoneAccount()) {
178183
return;
179184
}
@@ -188,12 +193,12 @@ public void answerIncomingCall(String uuid) {
188193

189194
@ReactMethod
190195
public void startCall(String uuid, String number, String callerName) {
196+
Log.d(TAG, "startCall called, uuid: " + uuid + ", number: " + number + ", callerName: " + callerName);
197+
191198
if (!isConnectionServiceAvailable() || !hasPhoneAccount() || !hasPermissions() || number == null) {
192199
return;
193200
}
194201

195-
Log.d(TAG, "startCall number: " + number + ", callerName: " + callerName);
196-
197202
Bundle extras = new Bundle();
198203
Uri uri = Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null);
199204

@@ -205,12 +210,14 @@ public void startCall(String uuid, String number, String callerName) {
205210
extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, handle);
206211
extras.putParcelable(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS, callExtras);
207212

213+
Log.d(TAG, "startCall, uuid: " + uuid);
214+
208215
telecomManager.placeCall(uri, extras);
209216
}
210217

211218
@ReactMethod
212219
public void endCall(String uuid) {
213-
Log.d(TAG, "endCall called");
220+
Log.d(TAG, "endCall called, uuid: " + uuid);
214221
if (!isConnectionServiceAvailable() || !hasPhoneAccount()) {
215222
return;
216223
}
@@ -221,7 +228,7 @@ public void endCall(String uuid) {
221228
}
222229
conn.onDisconnect();
223230

224-
Log.d(TAG, "endCall executed");
231+
Log.d(TAG, "endCall executed, uuid: " + uuid);
225232
}
226233

227234
@ReactMethod
@@ -360,6 +367,8 @@ public void checkDefaultPhoneAccount(Promise promise) {
360367

361368
@ReactMethod
362369
public void setOnHold(String uuid, boolean shouldHold) {
370+
Log.d(TAG, "setOnHold, uuid: " + uuid + ", shouldHold: " + (shouldHold ? "true" : "false"));
371+
363372
Connection conn = VoiceConnectionService.getConnection(uuid);
364373
if (conn == null) {
365374
return;
@@ -374,6 +383,7 @@ public void setOnHold(String uuid, boolean shouldHold) {
374383

375384
@ReactMethod
376385
public void reportEndCallWithUUID(String uuid, int reason) {
386+
Log.d(TAG, "reportEndCallWithUUID, uuid: " + uuid + ", reason: " + reason);
377387
if (!isConnectionServiceAvailable() || !hasPhoneAccount()) {
378388
return;
379389
}
@@ -387,6 +397,7 @@ public void reportEndCallWithUUID(String uuid, int reason) {
387397

388398
@ReactMethod
389399
public void rejectCall(String uuid) {
400+
Log.d(TAG, "rejectCall, uuid: " + uuid);
390401
if (!isConnectionServiceAvailable() || !hasPhoneAccount()) {
391402
return;
392403
}
@@ -401,6 +412,7 @@ public void rejectCall(String uuid) {
401412

402413
@ReactMethod
403414
public void setMutedCall(String uuid, boolean shouldMute) {
415+
Log.d(TAG, "setMutedCall, uuid: " + uuid + ", shouldMute: " + (shouldMute ? "true" : "false"));
404416
Connection conn = VoiceConnectionService.getConnection(uuid);
405417
if (conn == null) {
406418
return;
@@ -420,6 +432,7 @@ public void setMutedCall(String uuid, boolean shouldMute) {
420432

421433
@ReactMethod
422434
public void sendDTMF(String uuid, String key) {
435+
Log.d(TAG, "sendDTMF, uuid: " + uuid + ", key: " + key);
423436
Connection conn = VoiceConnectionService.getConnection(uuid);
424437
if (conn == null) {
425438
return;
@@ -430,6 +443,7 @@ public void sendDTMF(String uuid, String key) {
430443

431444
@ReactMethod
432445
public void updateDisplay(String uuid, String displayName, String uri) {
446+
Log.d(TAG, "updateDisplay, uuid: " + uuid + ", displayName: " + displayName+ ", uri: " + uri);
433447
Connection conn = VoiceConnectionService.getConnection(uuid);
434448
if (conn == null) {
435449
return;
@@ -480,6 +494,7 @@ public void setReachable() {
480494

481495
@ReactMethod
482496
public void setCurrentCallActive(String uuid) {
497+
Log.d(TAG, "setCurrentCallActive, uuid: " + uuid);
483498
Connection conn = VoiceConnectionService.getConnection(uuid);
484499
if (conn == null) {
485500
return;

android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@
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)
7171
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;
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

Comments
 (0)