Music capture no longer crashes the app when the car connects while Gearslip is in the background

This commit is contained in:
Seb3thehacker committed 2026-11-23 23:59:27 -08:00
1 parent 4e3f8e6657
commit 10db44f681
1 file changed
+12 -3
@@ -88,10 +88,19 @@ class AudioCaptureService : Service() {
// Android silences the microphone for apps in the background unless a running service // Android silences the microphone for apps in the background unless a running service
// claims it. Claiming it here lets voice replies hear the driver with the phone locked. // claims it. Claiming it here lets voice replies hear the driver with the phone locked.
// Without the permission, claiming it would crash, so it's left off. // Without the permission, claiming it would crash, so it's left off.
// Android also refuses the claim while Gearslip is in the background, as when the car
// connects with the phone in a pocket, and that refusal crashed the app (issue #104).
// Then the capture starts without it, and only voice replies lose the microphone.
val micGranted = checkSelfPermission(android.Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED val micGranted = checkSelfPermission(android.Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
val type = ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION or val projectionOnly = ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
(if (micGranted) ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE else 0) val withMic = projectionOnly or ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE
startForeground(NOTIFICATION_ID, notification(), type) try {
startForeground(NOTIFICATION_ID, notification(), if (micGranted) withMic else projectionOnly)
} catch (e: SecurityException) {
if (!micGranted) throw e
GearslipLog.w("audio: Android won't let Gearslip claim the microphone from the background, so voice replies can't hear the driver with the phone locked this drive")
startForeground(NOTIFICATION_ID, notification(), projectionOnly)
}
ScreenShareGuard.lift(this) ScreenShareGuard.lift(this)
projection = try { projection = try {
getSystemService(MediaProjectionManager::class.java).getMediaProjection(resultCode, data) getSystemService(MediaProjectionManager::class.java).getMediaProjection(resultCode, data)