Skip to content

Commit

Permalink
Merge pull request #164 from firemaples/fix/release-all-resources-aft…
Browse files Browse the repository at this point in the history
…er-screen-captured

Release all resources after screen captured
  • Loading branch information
firemaples authored Nov 4, 2021
2 parents e433228 + e51147c commit 3911092
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ abstract class FloatingView(protected val context: Context) {
try {
windowManager.updateViewLayout(rootView, params)
} catch (e: Exception) {
logger.warn(t = e)
// logger.warn(t = e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ object ScreenExtractor {
}

fun release() {
releaseAllResources()
virtualDisplay?.release()
mediaProjectionIntent = null
}
Expand All @@ -79,6 +80,10 @@ object ScreenExtractor {
}
}

private var projection: MediaProjection? = null
private var imageReader: ImageReader? = null
private var image: Image? = null

@SuppressLint("WrongConstant")
@Throws(
IllegalStateException::class,
Expand All @@ -88,6 +93,8 @@ object ScreenExtractor {
private suspend fun doCaptureScreen(): Bitmap {
var bitmap: Bitmap
withContext(Dispatchers.Default) {
releaseAllResources()

val mpIntent = mediaProjectionIntent
if (mpIntent == null) {
logger.warn("The mediaProjectionIntent is null: $mpIntent")
Expand All @@ -97,14 +104,11 @@ object ScreenExtractor {
val mpManager =
context.getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager

var projection: MediaProjection? = null
var imageReader: ImageReader? = null
var image: Image? = null

try {
projection =
mpManager.getMediaProjection(Activity.RESULT_OK, mpIntent.clone() as Intent)

val projection = projection
if (projection == null) {
logger.warn("Retrieve projection failed, projection: $projection")
throw IllegalStateException("Retrieving media projection failed")
Expand All @@ -116,6 +120,13 @@ object ScreenExtractor {

imageReader =
ImageReader.newInstance(width, height, AppPref.imageReaderFormat, 2)
val imageReader = imageReader

if (imageReader == null) {
logger.debug("The imageReader is null after initialized")
releaseAllResources()
throw IllegalStateException("No image reader initialized failed")
}

virtualDisplay = projection.createVirtualDisplay(
"screen-mirror",
Expand All @@ -131,18 +142,23 @@ object ScreenExtractor {

virtualDisplay?.release()

val image = image
if (image == null) {
logger.warn("The captured image is null")
imageReader.close()
releaseAllResources()
throw IllegalStateException("No image data found")
}

bitmap = image.decodeBitmap(size)

releaseAllResources()

logger.debug("Bitmap size: ${bitmap.width}x${bitmap.height}, screen size: ${width}x$height")
} catch (e: Exception) {
} catch (e: Throwable) {
logger.warn(t = e)

releaseAllResources()

val message = e.message ?: e.localizedMessage
if (message != null) {
val match = Constants.regexForImageReaderFormatError.find(message)
Expand All @@ -159,28 +175,35 @@ object ScreenExtractor {
}

throw e
} finally {
try {
projection?.stop()
} catch (e: Exception) {
FirebaseEvent.logException(e)
}
try {
image?.close()
} catch (e: Exception) {
FirebaseEvent.logException(e)
}
try {
imageReader?.close()
} catch (e: Exception) {
FirebaseEvent.logException(e)
}
}
}

return bitmap
}

private fun releaseAllResources() {
try {
imageReader?.setOnImageAvailableListener(null, handler)
} catch (e: Exception) {
FirebaseEvent.logException(e)
}
try {
imageReader?.close()
} catch (e: Exception) {
FirebaseEvent.logException(e)
}
try {
projection?.stop()
} catch (e: Exception) {
FirebaseEvent.logException(e)
}
try {
image?.close()
} catch (e: Exception) {
FirebaseEvent.logException(e)
}
}

@Throws(IllegalArgumentException::class)
private fun cropBitmap(bitmap: Bitmap, parentRect: Rect, cropRect: Rect): Bitmap {
logger.debug(
Expand Down

0 comments on commit 3911092

Please sign in to comment.