From ccf51c9dbfb99d4ed2d7084c8df37db11190d44a Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Thu, 21 Nov 2024 22:43:39 +0300 Subject: [PATCH] resources: smoother audio recorder stop (fixes #4790) (#4791) Co-authored-by: dogi --- app/build.gradle | 4 +- .../ole/planet/myplanet/MainApplication.kt | 69 ++++++++++--------- .../myplanet/service/AudioRecorderService.kt | 18 +++-- 3 files changed, 50 insertions(+), 41 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 29fefec9c9..e0fa73ffc1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,8 +10,8 @@ android { applicationId "org.ole.planet.myplanet" minSdkVersion 26 targetSdkVersion 34 - versionCode 2103 - versionName "0.21.3" + versionCode 2104 + versionName "0.21.4" ndkVersion '21.3.6528147' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true diff --git a/app/src/main/java/org/ole/planet/myplanet/MainApplication.kt b/app/src/main/java/org/ole/planet/myplanet/MainApplication.kt index b0939e1ce0..2ff8e10bde 100644 --- a/app/src/main/java/org/ole/planet/myplanet/MainApplication.kt +++ b/app/src/main/java/org/ole/planet/myplanet/MainApplication.kt @@ -24,6 +24,7 @@ import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext +import org.ole.planet.myplanet.MainApplication import org.ole.planet.myplanet.base.BaseResourceFragment.Companion.backgroundDownload import org.ole.planet.myplanet.base.BaseResourceFragment.Companion.getAllLibraryList import org.ole.planet.myplanet.callback.TeamPageListener @@ -139,6 +140,40 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks { false } } + + fun handleUncaughtException(e: Throwable) { + e.printStackTrace() + applicationScope.launch(Dispatchers.IO) { + try { + val realm = Realm.getDefaultInstance() + try { + realm.executeTransaction { r -> + val log = r.createObject(RealmApkLog::class.java, "${UUID.randomUUID()}") + val model = UserProfileDbHandler(context).userModel + if (model != null) { + log.parentCode = model.parentCode + log.createdOn = model.planetCode + log.userId = model.id + } + log.time = "${Date().time}" + log.page = "" + log.version = getVersionName(context) + log.type = RealmApkLog.ERROR_TYPE_CRASH + log.setError(e) + } + } finally { + realm.close() + } + } catch (ex: Exception) { + ex.printStackTrace() + } + } + + val homeIntent = Intent(Intent.ACTION_MAIN) + homeIntent.addCategory(Intent.CATEGORY_HOME) + homeIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK + context.startActivity(homeIntent) + } } private var activityReferences = 0 @@ -288,40 +323,6 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks { private fun onAppClosed() {} - private fun handleUncaughtException(e: Throwable) { - e.printStackTrace() - applicationScope.launch(Dispatchers.IO) { - try { - val realm = Realm.getDefaultInstance() - try { - realm.executeTransaction { r -> - val log = r.createObject(RealmApkLog::class.java, "${UUID.randomUUID()}") - val model = UserProfileDbHandler(this@MainApplication).userModel - if (model != null) { - log.parentCode = model.parentCode - log.createdOn = model.planetCode - log.userId = model.id - } - log.time = "${Date().time}" - log.page = "" - log.version = getVersionName(this@MainApplication) - log.type = RealmApkLog.ERROR_TYPE_CRASH - log.setError(e) - } - } finally { - realm.close() - } - } catch (ex: Exception) { - ex.printStackTrace() - } - } - - val homeIntent = Intent(Intent.ACTION_MAIN) - homeIntent.addCategory(Intent.CATEGORY_HOME) - homeIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK - startActivity(homeIntent) - } - override fun onTerminate() { super.onTerminate() onAppClosed() diff --git a/app/src/main/java/org/ole/planet/myplanet/service/AudioRecorderService.kt b/app/src/main/java/org/ole/planet/myplanet/service/AudioRecorderService.kt index f752db1e2a..24f23be3fe 100644 --- a/app/src/main/java/org/ole/planet/myplanet/service/AudioRecorderService.kt +++ b/app/src/main/java/org/ole/planet/myplanet/service/AudioRecorderService.kt @@ -4,6 +4,7 @@ import android.media.MediaRecorder import android.os.Build import android.os.Environment import androidx.annotation.RequiresApi +import org.ole.planet.myplanet.MainApplication import org.ole.planet.myplanet.MainApplication.Companion.context import java.io.File import java.util.UUID @@ -73,11 +74,18 @@ class AudioRecorderService { } fun stopRecording() { - if (myAudioRecorder != null) { - myAudioRecorder?.stop() - myAudioRecorder?.release() - myAudioRecorder = null - audioRecordListener?.onRecordStopped(outputFile) + myAudioRecorder?.let { recorder -> + try { + if (isRecording()) { + recorder.stop() + recorder.release() + } + } catch (e: RuntimeException) { + MainApplication.handleUncaughtException(e) + } finally { + myAudioRecorder = null + audioRecordListener?.onRecordStopped(outputFile) + } } }