ScreenCaptureHelper

Helper utilities for screen capture permission flow.

This object provides utility methods to simplify the process of requesting MediaProjection permission and building constraints for WebRtcDevice.getDisplayMedia().

Usage in Activity:

class MyActivity : AppCompatActivity() {
companion object {
const val SCREEN_CAPTURE_REQUEST_CODE = 1001
}

private fun startScreenShare() {
// Request permission
ScreenCaptureHelper.requestPermission(this, SCREEN_CAPTURE_REQUEST_CODE)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SCREEN_CAPTURE_REQUEST_CODE) {
if (resultCode == RESULT_OK && data != null) {
val permissionData = ScreenCapturePermissionData(resultCode, data)
val constraints = ScreenCaptureHelper.buildConstraints(permissionData)

lifecycleScope.launch {
try {
val stream = webRtcDevice.getDisplayMedia(constraints)
// Use stream for screen sharing
} catch (e: Exception) {
// Handle error
}
}
} else {
// User denied permission
}
}
}
}

Usage with ActivityResultLauncher (Recommended):

class MyActivity : ComponentActivity() {
private val screenCaptureLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == RESULT_OK && result.data != null) {
val permissionData = ScreenCapturePermissionData(result.resultCode, result.data!!)
// Use permissionData with getDisplayMedia
}
}

private fun startScreenShare() {
val intent = ScreenCaptureHelper.createPermissionIntent(this)
screenCaptureLauncher.launch(intent)
}
}

Functions

Link copied to clipboard
fun buildConstraints(permissionData: ScreenCapturePermissionData, width: Int = 1920, height: Int = 1080, frameRate: Int = 15, maxFrameRate: Int = 30): Map<String, Any?>

Builds constraints map for getDisplayMedia from permission data.

fun buildConstraints(resultCode: Int, data: Intent?, width: Int = 1920, height: Int = 1080, frameRate: Int = 15, maxFrameRate: Int = 30): Map<String, Any?>

Builds constraints map directly from onActivityResult parameters.

Link copied to clipboard

Creates the Intent to request screen capture permission.

Link copied to clipboard
fun isPermissionGranted(resultCode: Int, data: Intent?): Boolean

Validates that the result from onActivityResult represents successful permission.

Link copied to clipboard

Checks if screen capture is supported on this device.

Link copied to clipboard
fun requestPermission(activity: Activity, requestCode: Int)

Requests screen capture permission using deprecated startActivityForResult.