parent
a5c95d2131
commit
1907b97d59
@ -0,0 +1,107 @@ |
||||
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 net.pokeranalytics.android.R |
||||
import net.pokeranalytics.android.exceptions.PAIllegalStateException |
||||
import net.pokeranalytics.android.model.realm.handhistory.HandHistory |
||||
import net.pokeranalytics.android.ui.extensions.toByteArray |
||||
import net.pokeranalytics.android.util.TriggerNotification |
||||
import net.pokeranalytics.android.util.extensions.findById |
||||
import net.pokeranalytics.android.util.video.MMediaMuxer |
||||
import timber.log.Timber |
||||
import java.io.File |
||||
|
||||
|
||||
class ReplayExportService : Service() { |
||||
|
||||
private lateinit var handHistoryId: String |
||||
|
||||
private val binder = LocalBinder() |
||||
|
||||
override fun onBind(intent: Intent?): IBinder? { |
||||
return binder |
||||
} |
||||
|
||||
inner class LocalBinder : Binder() { |
||||
|
||||
fun getService(): ReplayExportService = this@ReplayExportService |
||||
|
||||
} |
||||
|
||||
fun export(handHistoryId: String) { |
||||
this@ReplayExportService.handHistoryId = handHistoryId |
||||
export() |
||||
} |
||||
|
||||
private fun export() { |
||||
|
||||
val realm = Realm.getDefaultInstance() |
||||
realm.findById<HandHistory>(this.handHistoryId)?.let { hh -> |
||||
startExport(hh) |
||||
} ?: throw PAIllegalStateException("HandHistory not found, id: $handHistoryId") |
||||
|
||||
} |
||||
|
||||
private fun startExport(handHistory: HandHistory) { |
||||
|
||||
val animator = ReplayerAnimator(handHistory, true) |
||||
|
||||
val square = 1024f |
||||
|
||||
val width = square |
||||
val height = square |
||||
|
||||
animator.setDimension(width, height) |
||||
TableDrawer.configurePaints(this, animator) |
||||
|
||||
val muxer = MMediaMuxer() |
||||
muxer.Init(null, width.toInt(), height.toInt(), "hhVideo", "YES!") |
||||
|
||||
animator.frames(this) { bitmap, count -> |
||||
|
||||
try { |
||||
val byteArray = bitmap.toByteArray() |
||||
muxer.AddFrame(byteArray, count, false) |
||||
} catch (e: Exception) { |
||||
Timber.e("error = ${e.message}") |
||||
} |
||||
|
||||
} |
||||
|
||||
muxer.CreateVideo { path -> |
||||
notifyUser(path) |
||||
} |
||||
|
||||
} |
||||
|
||||
private fun notifyUser(path: String) { |
||||
|
||||
val title = getString(R.string.video_available) |
||||
val body = getString(R.string.video_retrieval_message) + ": " + path |
||||
|
||||
val uri = FileProvider.getUriForFile( |
||||
this, |
||||
this.applicationContext.packageName.toString() + ".provider", |
||||
File(path) |
||||
) |
||||
|
||||
val intent = Intent(Intent.ACTION_VIEW) |
||||
intent.setDataAndType(uri, "video/*") |
||||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK |
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) |
||||
val chooser = Intent.createChooser(intent, getString(R.string.open_file_with)) |
||||
|
||||
val pendingIntent = PendingIntent.getActivity(this, 0, chooser, PendingIntent.FLAG_CANCEL_CURRENT) |
||||
|
||||
TriggerNotification(this, title, body, pendingIntent) |
||||
|
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue