Skip to content

Instantly share code, notes, and snippets.

@geraintwhite
Last active September 2, 2020 17:01
Show Gist options
  • Save geraintwhite/597440c6a12243c6eecfc1b13c11833c to your computer and use it in GitHub Desktop.
Save geraintwhite/597440c6a12243c6eecfc1b13c11833c to your computer and use it in GitHub Desktop.
diff --git a/node_modules/react-native-callkeep/android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java b/node_modules/react-native-callkeep/android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java
index cae0399..164d772 100644
--- a/node_modules/react-native-callkeep/android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java
+++ b/node_modules/react-native-callkeep/android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java
@@ -18,17 +18,22 @@
package io.wazo.callkeep;
import android.annotation.TargetApi;
-import android.content.Intent;
-import android.content.Context;
+import android.app.ActivityManager;
+import android.app.ActivityManager.RunningTaskInfo;
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
-import android.speech.tts.Voice;
import android.support.annotation.Nullable;
+import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
-import android.telecom.CallAudioState;
import android.telecom.Connection;
import android.telecom.ConnectionRequest;
import android.telecom.ConnectionService;
@@ -37,9 +42,6 @@ import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.util.Log;
-import android.app.ActivityManager;
-import android.app.ActivityManager.RunningTaskInfo;
-
import com.facebook.react.HeadlessJsTaskService;
import java.util.ArrayList;
@@ -49,12 +51,11 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
-import java.util.stream.Collectors;
+import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE;
import static io.wazo.callkeep.Constants.ACTION_AUDIO_SESSION;
-import static io.wazo.callkeep.Constants.ACTION_ONGOING_CALL;
import static io.wazo.callkeep.Constants.ACTION_CHECK_REACHABILITY;
-import static io.wazo.callkeep.Constants.ACTION_WAKE_APP;
+import static io.wazo.callkeep.Constants.ACTION_ONGOING_CALL;
import static io.wazo.callkeep.Constants.EXTRA_CALLER_NAME;
import static io.wazo.callkeep.Constants.EXTRA_CALL_NUMBER;
import static io.wazo.callkeep.Constants.EXTRA_CALL_UUID;
@@ -118,6 +119,25 @@ public class VoiceConnectionService extends ConnectionService {
}
}
+ private void startMyOwnForeground() {
+ String NOTIFICATION_CHANNEL_ID = "io.wazo.callkeep";
+ String channelName = "CallKeep Background Service";
+ NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
+ chan.setLightColor(Color.BLUE);
+ chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
+ NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+ assert manager != null;
+ manager.createNotificationChannel(chan);
+
+ NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
+ Notification notification = notificationBuilder.setOngoing(true)
+ .setContentTitle("App is running in background")
+ .setPriority(NotificationManager.IMPORTANCE_MIN)
+ .setCategory(Notification.CATEGORY_SERVICE)
+ .build();
+ startForeground(FOREGROUND_SERVICE_TYPE_MICROPHONE, notification);
+ }
+
@Override
public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
Bundle extra = request.getExtras();
@@ -127,6 +147,8 @@ public class VoiceConnectionService extends ConnectionService {
incomingCallConnection.setRinging();
incomingCallConnection.setInitialized();
+ startMyOwnForeground();
+
return incomingCallConnection;
}
@@ -175,6 +197,8 @@ public class VoiceConnectionService extends ConnectionService {
outgoingCallConnection.setAudioModeIsVoip(true);
outgoingCallConnection.setCallerDisplayName(displayName, TelecomManager.PRESENTATION_ALLOWED);
+ startMyOwnForeground();
+
// ‍️Weirdly on some Samsung phones (A50, S9...) using `setInitialized` will not display the native UI ...
// when making a call from the native Phone application. The call will still be displayed correctly without it.
if (!Build.MANUFACTURER.equalsIgnoreCase("Samsung")) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment