Promote call FGS asap (#4377)

* Promote call FGS as soon as possible

* Add resource strings for call notification

* Launch with only phone call type on 14+
This commit is contained in:
wchen342
2026-04-27 14:34:51 -04:00
committed by GitHub
parent 52263150c8
commit 917e764b6f
4 changed files with 69 additions and 10 deletions
@@ -215,7 +215,8 @@ public class CallCoordinator implements DcEventCenter.DcEventDelegate {
calleeName = "Unknown";
}
showOrUpdateOngoingNotification("Calling " + calleeName + "...");
showOrUpdateOngoingNotification(
appContext.getString(R.string.calling_person, calleeName));
}
// Initialize call
@@ -402,6 +403,15 @@ public class CallCoordinator implements DcEventCenter.DcEventDelegate {
callService.stopRingtone();
}
// Promote the service to foreground immediately. Waiting until onIncomingCallAccepted
// on executor pool thread is too late on stricter OEM.
//
// Do not cancel() but use showOrUpdateOngoingNotification to replace incoming
// notification without a gap.
String callerName = displayName.getValue();
if (callerName == null) callerName = "Unknown";
showOrUpdateOngoingNotification(appContext.getString(R.string.call_with, callerName));
// Notify Android system with CallControlScope
CallControlScope scope = activeCallControlScope;
if (scope != null) {
@@ -425,8 +435,6 @@ public class CallCoordinator implements DcEventCenter.DcEventDelegate {
}
});
}
notificationManager.cancel(NOTIFICATION_ID_CALL);
}
public void answerWebRTC() {
@@ -921,7 +929,7 @@ public class CallCoordinator implements DcEventCenter.DcEventDelegate {
callerName = "Unknown";
}
showOrUpdateOngoingNotification("Call with " + callerName);
showOrUpdateOngoingNotification(appContext.getString(R.string.call_with, callerName));
}
private synchronized void onCallAnsweredOnOtherDevice() {
@@ -1008,7 +1016,7 @@ public class CallCoordinator implements DcEventCenter.DcEventDelegate {
if (calleeName == null) {
calleeName = "Unknown";
}
showOrUpdateOngoingNotification("Call with " + calleeName);
showOrUpdateOngoingNotification(appContext.getString(R.string.call_with, calleeName));
}
private synchronized void onCallEnded(int accId, int callId) {
@@ -1101,6 +1109,14 @@ public class CallCoordinator implements DcEventCenter.DcEventDelegate {
return;
}
if (callService != null) {
try {
callService.stopForegroundAndDismiss();
} catch (Exception e) {
Log.w(TAG, "stopForegroundAndDismiss failed", e);
}
}
// Clear state
this.activeAccId = null;
this.activeCallId = null;
@@ -4,6 +4,7 @@ import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.media.AudioAttributes;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
@@ -13,7 +14,9 @@ import android.media.ToneGenerator;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
@@ -432,6 +435,12 @@ public class CallService extends Service implements WebRTCClient.Callbacks {
disposeWebRTC();
try {
stopForeground(STOP_FOREGROUND_REMOVE);
} catch (Exception e) {
Log.w(TAG, "stopForeground failed", e);
}
stopService();
}
@@ -494,13 +503,44 @@ public class CallService extends Service implements WebRTCClient.Callbacks {
// Foreground Notification
public void startForegroundWithNotification(int id, Notification notification) {
Log.d(TAG, "Starting foreground with notification id: " + id);
startForeground(id, notification);
// Always run on main thread
if (Looper.myLooper() != Looper.getMainLooper()) {
new Handler(Looper.getMainLooper())
.post(() -> startForegroundWithNotification(id, notification));
return;
}
Log.d(TAG, "Starting call FGS with notification id: " + id);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
startForeground(id, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
int types =
ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE
| ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA;
startForeground(id, notification, types);
} else {
startForeground(id, notification);
}
} catch (Exception e) {
Log.e(TAG, "startForeground failed", e);
if (callCoordinator != null) {
callCoordinator.reportError("Failed to activate call FGS: " + e.getMessage());
}
}
}
public void stopForegroundAndDismiss() {
Log.d(TAG, "Stopping foreground and dismissing notification");
stopForeground(STOP_FOREGROUND_REMOVE);
if (Looper.myLooper() != Looper.getMainLooper()) {
new Handler(Looper.getMainLooper()).post(this::stopForegroundAndDismiss);
return;
}
Log.d(TAG, "Stopping call FGS and dismissing notification");
try {
stopForeground(STOP_FOREGROUND_REMOVE);
} catch (Exception e) {
Log.w(TAG, "stopForeground failed", e);
}
}
// Cleanup