Skip to content

Commit

Permalink
resources: smoother audio recorder stop (fixes #4790) (#4791)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Nov 21, 2024
1 parent 1ef8874 commit ccf51c9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 41 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 35 additions & 34 deletions app/src/main/java/org/ole/planet/myplanet/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}

Expand Down

0 comments on commit ccf51c9

Please sign in to comment.