diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/replayer/ReplayExportService.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/replayer/ReplayExportService.kt index 7572bae5..71403f4a 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/replayer/ReplayExportService.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/replayer/ReplayExportService.kt @@ -3,11 +3,14 @@ package net.pokeranalytics.android.ui.modules.handhistory.replayer import android.app.PendingIntent import android.app.Service import android.content.Intent -import android.net.Uri import android.os.Binder import android.os.IBinder import androidx.core.content.FileProvider import io.realm.Realm +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.async +import kotlinx.coroutines.launch import net.pokeranalytics.android.R import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.model.realm.handhistory.HandHistory @@ -17,6 +20,7 @@ import net.pokeranalytics.android.util.extensions.findById import net.pokeranalytics.android.util.video.MMediaMuxer import timber.log.Timber import java.io.File +import kotlin.coroutines.CoroutineContext class ReplayExportService : Service() { @@ -25,6 +29,9 @@ class ReplayExportService : Service() { private val binder = LocalBinder() + private val coroutineContext: CoroutineContext + get() = Dispatchers.Main + override fun onBind(intent: Intent?): IBinder? { return binder } @@ -37,46 +44,51 @@ class ReplayExportService : Service() { fun export(handHistoryId: String) { this@ReplayExportService.handHistoryId = handHistoryId - export() + startExport() } - private fun export() { + private fun startExport() { - val realm = Realm.getDefaultInstance() - realm.findById(this.handHistoryId)?.let { hh -> - startExport(hh) - } ?: throw PAIllegalStateException("HandHistory not found, id: $handHistoryId") + GlobalScope.launch(coroutineContext) { + val c = GlobalScope.async { - } + val realm = Realm.getDefaultInstance() + val handHistory = realm.findById(handHistoryId) ?: throw PAIllegalStateException("HandHistory not found, id: $handHistoryId") - private fun startExport(handHistory: HandHistory) { + val context = this@ReplayExportService - val animator = ReplayerAnimator(handHistory, true) + val animator = ReplayerAnimator(handHistory, true) - val square = 1024f + val square = 1024f - val width = square - val height = square + val width = square + val height = square - animator.setDimension(width, height) - TableDrawer.configurePaints(this, animator) + animator.setDimension(width, height) + TableDrawer.configurePaints(context, animator) - val muxer = MMediaMuxer() - muxer.Init(null, width.toInt(), height.toInt(), "hhVideo", "YES!") + val muxer = MMediaMuxer() + muxer.Init(null, width.toInt(), height.toInt(), "hhVideo", "YES!") - animator.frames(this) { bitmap, count -> + animator.frames(context) { bitmap, count -> - try { - val byteArray = bitmap.toByteArray() - muxer.AddFrame(byteArray, count, false) - } catch (e: Exception) { - Timber.e("error = ${e.message}") - } + try { + val byteArray = bitmap.toByteArray() + muxer.AddFrame(byteArray, count, false) + } catch (e: Exception) { + Timber.e("error = ${e.message}") + } - } + } - muxer.CreateVideo { path -> - notifyUser(path) + realm.close() + + muxer.CreateVideo { path -> + notifyUser(path) + } + + } + c.await() } } diff --git a/app/src/main/java/net/pokeranalytics/android/util/video/MMediaMuxer.kt b/app/src/main/java/net/pokeranalytics/android/util/video/MMediaMuxer.kt index 8a75c24a..efc19afe 100644 --- a/app/src/main/java/net/pokeranalytics/android/util/video/MMediaMuxer.kt +++ b/app/src/main/java/net/pokeranalytics/android/util/video/MMediaMuxer.kt @@ -51,7 +51,7 @@ class MMediaMuxer { ShowProgressBar() } - private val aHandler = Handler() +// private val aHandler = Handler() fun AddFrame(byteFrame: ByteArray) { CheckDataListState()