Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Android 14 related fixes (#271)
Browse files Browse the repository at this point in the history
* Android 14 related fixes

* Use FOREGROUND_SERVICE_MICROPHONE permission
* Similarly how ScreenRecorderService overrides fgServiceType property to FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION make both AudioRecorderService and LosslessRecorderService inline with this on android 14 and newer by overriding its fgServiceType to FOREGROUND_SERVICE_TYPE_MICROPHONE.
* RecorderService: When registering receivers use the RECEIVER_EXPORTED flag to avoid security exceptions on Android 14
* ScreenRecorderService: added onStop callback to the MediaProjection. It may possibly fix #258.

* Review: use ContextCompat.registerReceiver instead of own conditions

* Update app/src/main/AndroidManifest.xml

Co-authored-by: Bnyro <[email protected]>

* Update app/src/main/AndroidManifest.xml

Co-authored-by: Bnyro <[email protected]>

---------

Co-authored-by: Bnyro <[email protected]>
  • Loading branch information
pvagner and Bnyro authored Mar 11, 2024
1 parent 2497ca6 commit 1aee6a9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
9 changes: 6 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

Expand Down Expand Up @@ -46,11 +47,13 @@

<service
android:name=".services.AudioRecorderService"
android:exported="false" />
android:exported="false"
android:foregroundServiceType="microphone" />

<service
android:name=".services.LosslessRecorderService"
android:exported="false" />
android:exported="false"
android:foregroundServiceType="microphone" />

<receiver
android:name=".receivers.FinishedNotificationReceiver"
Expand Down Expand Up @@ -95,4 +98,4 @@

</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.bnyro.recorder.services

import android.content.pm.ServiceInfo
import android.media.MediaRecorder
import android.os.Build
import android.widget.Toast
import com.bnyro.recorder.App
import com.bnyro.recorder.R
Expand All @@ -14,6 +16,13 @@ class AudioRecorderService : RecorderService() {
override val notificationTitle: String
get() = getString(R.string.recording_audio)

override val fgServiceType: Int?
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE
} else {
null
}

override fun start() {
val audioFormat = AudioFormat.getCurrent()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bnyro.recorder.services

import android.annotation.SuppressLint
import android.content.pm.ServiceInfo
import android.media.AudioFormat
import android.media.AudioRecord
import android.media.MediaRecorder
Expand All @@ -22,6 +23,13 @@ class LosslessRecorderService : RecorderService() {
override val notificationTitle: String
get() = getString(R.string.recording_audio)

override val fgServiceType: Int?
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE
} else {
null
}

private var audioRecorder: AudioRecord? = null
private var recorderThread: Thread? = null
private var pcmConverter: PcmConverter? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.ServiceCompat
import androidx.core.content.ContextCompat
import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -91,16 +92,18 @@ abstract class RecorderService : LifecycleService() {
runCatching {
unregisterReceiver(bluetoothReceiver)
}
registerReceiver(
ContextCompat.registerReceiver(
this,
bluetoothReceiver,
IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)
IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED),
RECEIVER_EXPORTED
)
audioManager.startBluetoothSco()

runCatching {
unregisterReceiver(recorderReceiver)
}
registerReceiver(recorderReceiver, IntentFilter(RECORDER_INTENT_ACTION))
ContextCompat.registerReceiver(this, recorderReceiver, IntentFilter(RECORDER_INTENT_ACTION), RECEIVER_EXPORTED)

super.onCreate()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ class ScreenRecorderService : RecorderService() {
Log.e("Media Projection Error", e.toString())
onDestroy()
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
mediaProjection!!.registerCallback(object : MediaProjection.Callback() {
override fun onStop() {
onDestroy()
}
}, null)
}
}

override fun start() {
Expand Down

0 comments on commit 1aee6a9

Please sign in to comment.