Skip to content

Avoid to check for reachability everytime we make a call #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions android/src/main/java/io/wazo/callkeep/RNCallKeepModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ public String getName() {

@ReactMethod
public void setup(ReadableMap options) {
Log.d(TAG, "setup");
VoiceConnectionService.setAvailable(false);
VoiceConnectionService.setInitialized(true);
this._settings = options;

if (isConnectionServiceAvailable()) {
Expand All @@ -140,6 +142,8 @@ public void registerPhoneAccount() {
return;
}

Log.d(TAG, "registerPhoneAccount");

this.registerPhoneAccount(this.getAppContext());
}

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

Log.d(TAG, "displayIncomingCall number: " + number + ", callerName: " + callerName);
Log.d(TAG, "displayIncomingCall, uuid: " + uuid + ", number: " + number + ", callerName: " + callerName);

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

@ReactMethod
public void answerIncomingCall(String uuid) {
Log.d(TAG, "answerIncomingCall, uuid: " + uuid);
if (!isConnectionServiceAvailable() || !hasPhoneAccount()) {
return;
}
Expand All @@ -188,12 +193,12 @@ public void answerIncomingCall(String uuid) {

@ReactMethod
public void startCall(String uuid, String number, String callerName) {
Log.d(TAG, "startCall called, uuid: " + uuid + ", number: " + number + ", callerName: " + callerName);

if (!isConnectionServiceAvailable() || !hasPhoneAccount() || !hasPermissions() || number == null) {
return;
}

Log.d(TAG, "startCall number: " + number + ", callerName: " + callerName);

Bundle extras = new Bundle();
Uri uri = Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null);

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

Log.d(TAG, "startCall, uuid: " + uuid);

telecomManager.placeCall(uri, extras);
}

@ReactMethod
public void endCall(String uuid) {
Log.d(TAG, "endCall called");
Log.d(TAG, "endCall called, uuid: " + uuid);
if (!isConnectionServiceAvailable() || !hasPhoneAccount()) {
return;
}
Expand All @@ -221,7 +228,7 @@ public void endCall(String uuid) {
}
conn.onDisconnect();

Log.d(TAG, "endCall executed");
Log.d(TAG, "endCall executed, uuid: " + uuid);
}

@ReactMethod
Expand Down Expand Up @@ -360,6 +367,8 @@ public void checkDefaultPhoneAccount(Promise promise) {

@ReactMethod
public void setOnHold(String uuid, boolean shouldHold) {
Log.d(TAG, "setOnHold, uuid: " + uuid + ", shouldHold: " + (shouldHold ? "true" : "false"));

Connection conn = VoiceConnectionService.getConnection(uuid);
if (conn == null) {
return;
Expand All @@ -374,6 +383,7 @@ public void setOnHold(String uuid, boolean shouldHold) {

@ReactMethod
public void reportEndCallWithUUID(String uuid, int reason) {
Log.d(TAG, "reportEndCallWithUUID, uuid: " + uuid + ", reason: " + reason);
if (!isConnectionServiceAvailable() || !hasPhoneAccount()) {
return;
}
Expand All @@ -387,6 +397,7 @@ public void reportEndCallWithUUID(String uuid, int reason) {

@ReactMethod
public void rejectCall(String uuid) {
Log.d(TAG, "rejectCall, uuid: " + uuid);
if (!isConnectionServiceAvailable() || !hasPhoneAccount()) {
return;
}
Expand All @@ -401,6 +412,7 @@ public void rejectCall(String uuid) {

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

@ReactMethod
public void sendDTMF(String uuid, String key) {
Log.d(TAG, "sendDTMF, uuid: " + uuid + ", key: " + key);
Connection conn = VoiceConnectionService.getConnection(uuid);
if (conn == null) {
return;
Expand All @@ -430,6 +443,7 @@ public void sendDTMF(String uuid, String key) {

@ReactMethod
public void updateDisplay(String uuid, String displayName, String uri) {
Log.d(TAG, "updateDisplay, uuid: " + uuid + ", displayName: " + displayName+ ", uri: " + uri);
Connection conn = VoiceConnectionService.getConnection(uuid);
if (conn == null) {
return;
Expand Down Expand Up @@ -480,6 +494,7 @@ public void setReachable() {

@ReactMethod
public void setCurrentCallActive(String uuid) {
Log.d(TAG, "setCurrentCallActive, uuid: " + uuid);
Connection conn = VoiceConnectionService.getConnection(uuid);
if (conn == null) {
return;
Expand Down
51 changes: 42 additions & 9 deletions android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@
// @see https://github.com/kbagchiGWC/voice-quickstart-android/blob/9a2aff7fbe0d0a5ae9457b48e9ad408740dfb968/exampleConnectionService/src/main/java/com/twilio/voice/examples/connectionservice/VoiceConnectionService.java
@TargetApi(Build.VERSION_CODES.M)
public class VoiceConnectionService extends ConnectionService {
private static Boolean isAvailable;
private static Boolean isInitialized;
private static Boolean isReachable;
private static Boolean isAvailable = false;
private static Boolean isInitialized = false;
private static Boolean isReachable = false;
private static Boolean canMakeMultipleCalls = true;
private static String notReachableCallUuid;
private static ConnectionRequest currentConnectionRequest;
Expand All @@ -92,9 +92,6 @@ public static Connection getConnection(String connectionId) {
public VoiceConnectionService() {
super();
Log.e(TAG, "Constructor");
isReachable = false;
isInitialized = false;
isAvailable = false;
currentConnectionRequest = null;
currentConnectionService = this;
}
Expand All @@ -106,7 +103,7 @@ public static void setPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle)
public static void setAvailable(Boolean value) {
Log.d(TAG, "setAvailable: " + (value ? "true" : "false"));
if (value) {
isInitialized = true;
setInitialized(true);
}

isAvailable = value;
Expand All @@ -126,10 +123,18 @@ public static void setReachable() {
VoiceConnectionService.currentConnectionRequest = null;
}

public static void setInitialized(boolean value) {
Log.d(TAG, "setInitialized: " + (value ? "true" : "false"));

isInitialized = value;
}

public static void deinitConnection(String connectionId) {
Log.d(TAG, "deinitConnection:" + connectionId);
VoiceConnectionService.hasOutgoingCall = false;

currentConnectionService.stopForegroundService();

if (currentConnections.containsKey(connectionId)) {
currentConnections.remove(connectionId);
}
Expand All @@ -140,6 +145,9 @@ public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManage
Bundle extra = request.getExtras();
Uri number = request.getAddress();
String name = extra.getString(EXTRA_CALLER_NAME);

Log.d(TAG, "onCreateIncomingConnection, name:" + name);

Connection incomingCallConnection = createConnection(request);
incomingCallConnection.setRinging();
incomingCallConnection.setInitialized();
Expand All @@ -154,6 +162,8 @@ public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManage
VoiceConnectionService.hasOutgoingCall = true;
String uuid = UUID.randomUUID().toString();

Log.d(TAG, "onCreateOutgoingConnection, uuid:" + uuid);

if (!isInitialized && !isReachable) {
this.notReachableCallUuid = uuid;
this.currentConnectionRequest = request;
Expand All @@ -171,7 +181,7 @@ private Connection makeOutgoingCall(ConnectionRequest request, String uuid, Bool
String displayName = extras.getString(EXTRA_CALLER_NAME);
Boolean isForeground = VoiceConnectionService.isRunning(this.getApplicationContext());

Log.d(TAG, "makeOutgoingCall:" + uuid + ", number: " + number + ", displayName:" + displayName);
Log.d(TAG, "makeOutgoingCall, uuid:" + uuid + ", number: " + number + ", displayName:" + displayName);

// Wakeup application if needed
if (!isForeground || forceWakeUp) {
Expand Down Expand Up @@ -221,6 +231,7 @@ private void startForegroundService() {
// Foreground services not required before SDK 28
return;
}
Log.d(TAG, "startForegroundService");
if (_settings == null || !_settings.hasKey("foregroundService")) {
Log.d(TAG, "Not creating foregroundService because not configured");
return;
Expand Down Expand Up @@ -251,7 +262,21 @@ private void startForegroundService() {
startForeground(FOREGROUND_SERVICE_TYPE_MICROPHONE, notification);
}

private void stopForegroundService() {
Log.d(TAG, "stopForegroundService");
if (_settings == null || !_settings.hasKey("foregroundService")) {
Log.d(TAG, "Discarding stop foreground service, no service configured");
return;
}
stopForeground(FOREGROUND_SERVICE_TYPE_MICROPHONE);
}

private void wakeUpApplication(String uuid, String number, String displayName) {
Log.d(TAG, "wakeUpApplication, uuid:" + uuid + ", number :" + number + ", displayName:" + displayName);

// Avoid to call wake up the app again in wakeUpAfterReachabilityTimeout.
this.currentConnectionRequest = null;

Intent headlessIntent = new Intent(
this.getApplicationContext(),
RNCallKeepBackgroundMessagingService.class
Expand Down Expand Up @@ -299,6 +324,8 @@ private Boolean canMakeOutgoingCall() {
}

private Connection createConnection(ConnectionRequest request) {
Log.d(TAG, "createConnection");

Bundle extras = request.getExtras();
HashMap<String, String> extrasMap = this.bundleToMap(extras);
extrasMap.put(EXTRA_CALL_NUMBER, request.getAddress().toString());
Expand All @@ -323,6 +350,7 @@ private Connection createConnection(ConnectionRequest request) {

@Override
public void onConference(Connection connection1, Connection connection2) {
Log.d(TAG, "onConference");
super.onConference(connection1, connection2);
VoiceConnection voiceConnection1 = (VoiceConnection) connection1;
VoiceConnection voiceConnection2 = (VoiceConnection) connection2;
Expand All @@ -344,6 +372,8 @@ private void sendCallRequestToActivity(final String action, @Nullable final Hash
final VoiceConnectionService instance = this;
final Handler handler = new Handler();

Log.d(TAG, "sendCallRequestToActivity, action:" + action);

handler.post(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -383,10 +413,13 @@ public static boolean isRunning(Context context) {
List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

for (RunningTaskInfo task : tasks) {
if (context.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
if (context.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName())) {
return true;
}
}

Log.d(TAG, "isRunning: no running package found.");

return false;
}
}