Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,24 @@ class ScreenshotServiceHolder @Inject constructor(

screenshotService = try {
if (prefs.wantsMediaProjectionToken) {
// Cloning the Intent allows reuse.
// Otherwise, the Intent gets consumed and MediaProjection cannot be started multiple times.
val token = ScriptRunnerService.mediaProjectionToken?.clone() as Intent
if (ScriptRunnerService.mediaProjectionToken == null) {
throw IllegalStateException("Media projection token is null")
}
val token = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
ScriptRunnerService.mediaProjectionToken!!
} else {
try {
// Cloning the Intent allows reuse.
// Otherwise, the Intent gets consumed and MediaProjection cannot be started multiple times.
ScriptRunnerService.mediaProjectionToken?.clone() as Intent
} catch (e: Exception) {
ScriptRunnerService.mediaProjectionToken!!
}
}

val mediaProjection =
mediaProjectionManager.getMediaProjection(Activity.RESULT_OK, token)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
// not allowed to reuse tokens on Android 14
ScriptRunnerService.mediaProjectionToken = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ package io.github.fate_grand_automata.ui
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.media.projection.MediaProjectionConfig
import android.media.projection.MediaProjectionManager
import android.os.Build
import androidx.activity.result.contract.ActivityResultContract

class StartMediaProjection : ActivityResultContract<Unit, Intent?>() {
override fun createIntent(context: Context, input: Unit): Intent {
val mediaProjectionManager = context.getSystemService(MediaProjectionManager::class.java)

return mediaProjectionManager.createScreenCaptureIntent()
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
mediaProjectionManager.createScreenCaptureIntent(MediaProjectionConfig.createConfigForDefaultDisplay())
} else {
mediaProjectionManager.createScreenCaptureIntent()
}
}

override fun parseResult(resultCode: Int, intent: Intent?) =
Expand Down
Loading