Skip to content

Commit

Permalink
Retry upon media projection instantiation failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Nain57 committed Aug 25, 2024
1 parent 528fc25 commit e14cca4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,20 @@ class DisplayRecorder @Inject internal constructor() {
suspend fun startProjection(context: Context, resultCode: Int, data: Intent, stoppedListener: () -> Unit) = mutex.withLock {
if (projection != null) {
Log.w(TAG, "Attempting to start media projection while already started.")
return
return@withLock
}

Log.d(TAG, "Start media projection")

stopListener = stoppedListener
val projectionManager = context.getSystemService(Context.MEDIA_PROJECTION_SERVICE)
as MediaProjectionManager
projection = projectionManager.getMediaProjection(resultCode, data).apply {
registerCallback(projectionCallback, Handler(Looper.getMainLooper()))

try {
projection = context.getMediaProjection(resultCode, data).apply {
registerCallback(projectionCallback, Handler(Looper.getMainLooper()))
}
} catch (sEx: SecurityException) {
Log.e(TAG, "Failed to start media projection")
stopListener?.invoke()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2024 Kevin Buzeau
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.buzbuz.smartautoclicker.core.display

import android.content.Context
import android.content.Intent
import android.media.projection.MediaProjection
import android.media.projection.MediaProjectionManager
import android.util.Log
import kotlinx.coroutines.delay

/**
* Get the media projection and retries if the foreground service isn't started yet.
* @throws SecurityException after [RETRY_MAX_COUNT] retries without success.
*/
internal suspend fun Context.getMediaProjection(resultCode: Int, data: Intent): MediaProjection {
Log.d(TAG, "getMediaProjection")

return (getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager)
.getMediaProjectionWithCurrentDelay(resultCode, data)
}


internal suspend fun MediaProjectionManager.getMediaProjectionWithCurrentDelay(
resultCode: Int,
data: Intent,
retryCount: Int = 0,
): MediaProjection {
if (retryCount > 0) delay(RETRY_DELAY)

try {
return getMediaProjection(resultCode, data)
} catch (sEx: SecurityException) {
if (retryCount >= RETRY_MAX_COUNT) throw sEx

Log.w(TAG, "Foreground service is not started yet, retrying...")
return getMediaProjectionWithCurrentDelay(resultCode, data, retryCount + 1)
}
}

private const val RETRY_DELAY = 500L
private const val RETRY_MAX_COUNT = 10

private const val TAG = "ProjectionManagerExt"
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,13 @@ class LocalService(
displayConfigManager.startMonitoring(context)
tileRepository.setTileScenario(scenarioId = scenario.id.databaseId, isSmart = true)
startJob = serviceScope.launch {
delay(500)

detectionRepository.setScenarioId(scenario.id)

overlayManager.navigateTo(
context = context,
newOverlay = MainMenu { stop() },
)

// If we start too quickly, there is a chance of crash because the service isn't in foreground state yet
// That's not really an issue as the user just clicked the permission button and the activity is closing
delay(1000)
detectionRepository.startScreenRecord(
context = context,
resultCode = resultCode,
Expand Down

0 comments on commit e14cca4

Please sign in to comment.