1818package io .wazo .callkeep ;
1919
2020import android .annotation .TargetApi ;
21+ import android .app .ActivityManager ;
22+ import android .app .ActivityManager .RunningTaskInfo ;
23+ import android .app .Notification ;
24+ import android .app .NotificationChannel ;
25+ import android .app .NotificationManager ;
2126import android .content .Intent ;
2227import android .content .Context ;
2328import android .content .ComponentName ;
2732import android .os .Handler ;
2833import android .speech .tts .Voice ;
2934import androidx .annotation .Nullable ;
35+ import android .support .v4 .app .NotificationCompat ;
3036import androidx .localbroadcastmanager .content .LocalBroadcastManager ;
3137import android .telecom .CallAudioState ;
3238import android .telecom .Connection ;
3743import android .telecom .TelecomManager ;
3844import android .util .Log ;
3945
40- import android .app .ActivityManager ;
41- import android .app .ActivityManager .RunningTaskInfo ;
42-
4346import com .facebook .react .HeadlessJsTaskService ;
47+ import com .facebook .react .bridge .ReadableMap ;
4448
4549import java .util .ArrayList ;
4650import java .util .HashMap ;
5155import java .util .UUID ;
5256import java .util .stream .Collectors ;
5357
58+ import static android .content .pm .ServiceInfo .FOREGROUND_SERVICE_TYPE_MICROPHONE ;
5459import static io .wazo .callkeep .Constants .ACTION_AUDIO_SESSION ;
5560import static io .wazo .callkeep .Constants .ACTION_ONGOING_CALL ;
5661import static io .wazo .callkeep .Constants .ACTION_CHECK_REACHABILITY ;
@@ -70,6 +75,7 @@ public class VoiceConnectionService extends ConnectionService {
7075 private static String notReachableCallUuid ;
7176 private static ConnectionRequest currentConnectionRequest ;
7277 private static PhoneAccountHandle phoneAccountHandle ;
78+ private static ReadableMap _settings ;
7379 private static String TAG = "RNCK:VoiceConnectionService" ;
7480 public static Map <String , VoiceConnection > currentConnections = new HashMap <>();
7581 public static Boolean hasOutgoingCall = false ;
@@ -105,6 +111,10 @@ public static void setAvailable(Boolean value) {
105111 isAvailable = value ;
106112 }
107113
114+ public static void setSettings (ReadableMap settings ) {
115+ _settings = settings ;
116+ }
117+
108118 public static void setCanMakeMultipleCalls (Boolean allow ) {
109119 VoiceConnectionService .canMakeMultipleCalls = allow ;
110120 }
@@ -133,6 +143,8 @@ public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManage
133143 incomingCallConnection .setRinging ();
134144 incomingCallConnection .setInitialized ();
135145
146+ startForegroundService ();
147+
136148 return incomingCallConnection ;
137149 }
138150
@@ -185,6 +197,8 @@ private Connection makeOutgoingCall(ConnectionRequest request, String uuid, Bool
185197 outgoingCallConnection .setAudioModeIsVoip (true );
186198 outgoingCallConnection .setCallerDisplayName (displayName , TelecomManager .PRESENTATION_ALLOWED );
187199
200+ startForegroundService ();
201+
188202 // ️Weirdly on some Samsung phones (A50, S9...) using `setInitialized` will not display the native UI ...
189203 // when making a call from the native Phone application. The call will still be displayed correctly without it.
190204 if (!Build .MANUFACTURER .equalsIgnoreCase ("Samsung" )) {
@@ -201,6 +215,28 @@ private Connection makeOutgoingCall(ConnectionRequest request, String uuid, Bool
201215 return outgoingCallConnection ;
202216 }
203217
218+ private void startForegroundService () {
219+ if (_settings == null || !_settings .hasKey ("foregroundService" )) {
220+ return ;
221+ }
222+ ReadableMap foregroundSettings = _settings .getMap ("foregroundService" );
223+ String NOTIFICATION_CHANNEL_ID = foregroundSettings .getString ("channelId" );
224+ String channelName = foregroundSettings .getString ("channelName" );
225+ NotificationChannel chan = new NotificationChannel (NOTIFICATION_CHANNEL_ID , channelName , NotificationManager .IMPORTANCE_NONE );
226+ chan .setLockscreenVisibility (Notification .VISIBILITY_PRIVATE );
227+ NotificationManager manager = (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
228+ assert manager != null ;
229+ manager .createNotificationChannel (chan );
230+
231+ NotificationCompat .Builder notificationBuilder = new NotificationCompat .Builder (this , NOTIFICATION_CHANNEL_ID );
232+ Notification notification = notificationBuilder .setOngoing (true )
233+ .setContentTitle (foregroundSettings .getString ("notificationTitle" ))
234+ .setPriority (NotificationManager .IMPORTANCE_MIN )
235+ .setCategory (Notification .CATEGORY_SERVICE )
236+ .build ();
237+ startForeground (FOREGROUND_SERVICE_TYPE_MICROPHONE , notification );
238+ }
239+
204240 private void wakeUpApplication (String uuid , String number , String displayName ) {
205241 Intent headlessIntent = new Intent (
206242 this .getApplicationContext (),
0 commit comments