Skip to content

Commit

Permalink
Detect when device is going to reboot or shutdown and stop recording …
Browse files Browse the repository at this point in the history
…and playback before that happen.
  • Loading branch information
Dimowner committed Jun 1, 2024
1 parent 325642a commit 6f9bb7e
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions app/src/main/java/com/dimowner/audiorecorder/ARApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.media.AudioManager
import android.os.Build
import android.os.Handler
import android.telephony.PhoneStateListener
Expand All @@ -38,7 +39,10 @@ import timber.log.Timber.DebugTree

//import com.google.firebase.FirebaseApp;
class ARApplication : Application() {

private var audioOutputChangeReceiver: AudioOutputChangeReceiver? = null
private var rebootReceiver: RebootReceiver? = null

override fun onCreate() {
if (BuildConfig.DEBUG) {
//Timber initialization
Expand All @@ -56,10 +60,8 @@ class ARApplication : Application() {
if (!prefs.isMigratedSettings) {
prefs.migrateSettings()
}
val intentFilter = IntentFilter()
intentFilter.addAction(AUDIO_BECOMING_NOISY)
audioOutputChangeReceiver = AudioOutputChangeReceiver()
registerReceiver(audioOutputChangeReceiver, intentFilter)
registerAudioOutputChangeReceiver()
registerRebootReceiver()

// feature: pause when phone functions ringing or off-hook
try {
Expand All @@ -81,6 +83,7 @@ class ARApplication : Application() {
injector.releaseMainPresenter()
injector.closeTasks()
unregisterReceiver(audioOutputChangeReceiver)
unregisterReceiver(rebootReceiver)
}

fun pausePlayback() {
Expand All @@ -97,6 +100,21 @@ class ARApplication : Application() {
}
}

private fun registerAudioOutputChangeReceiver() {
val intentFilter = IntentFilter()
intentFilter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY)
audioOutputChangeReceiver = AudioOutputChangeReceiver()
registerReceiver(audioOutputChangeReceiver, intentFilter)
}

private fun registerRebootReceiver() {
val intentFilter = IntentFilter()
intentFilter.addAction(Intent.ACTION_REBOOT)
intentFilter.addAction(Intent.ACTION_SHUTDOWN)
rebootReceiver = RebootReceiver()
registerReceiver(rebootReceiver, intentFilter)
}

@RequiresApi(api = Build.VERSION_CODES.S)
private abstract class CallStateListener : TelephonyCallback(),
TelephonyCallback.CallStateListener {
Expand All @@ -116,13 +134,24 @@ class ARApplication : Application() {
private class AudioOutputChangeReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val actionOfIntent = intent.action
if (actionOfIntent != null && actionOfIntent == AUDIO_BECOMING_NOISY) {
if (actionOfIntent != null && actionOfIntent == AudioManager.ACTION_AUDIO_BECOMING_NOISY) {
val player = injector.provideAudioPlayer()
player.pause()
}
}
}

private class RebootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == Intent.ACTION_REBOOT || intent?.action == Intent.ACTION_SHUTDOWN) {
val appRecorder = injector.provideAppRecorder(context)
val audioPlayer = injector.provideAudioPlayer()
appRecorder.stopRecording()
audioPlayer.stop()
}
}
}

@Suppress("DEPRECATION")
fun telephonyPreAndroid12(telephonyMgr: TelephonyManager) {
telephonyMgr.listen(mPhoneStateListenerPreAndroid12, PhoneStateListener.LISTEN_CALL_STATE)
Expand All @@ -139,7 +168,6 @@ class ARApplication : Application() {
}

companion object {
const val AUDIO_BECOMING_NOISY = "android.media.AUDIO_BECOMING_NOISY"
private var PACKAGE_NAME: String? = null

@JvmField
Expand Down

0 comments on commit 6f9bb7e

Please sign in to comment.