Cancel queued photo enhancement task

This commit is contained in:
Ming Ming 2022-05-11 02:08:17 +08:00
parent 91d4d3be81
commit d1aa3ffe7f

View file

@ -31,6 +31,8 @@ class ImageProcessorService : Service() {
const val EXTRA_HEADERS = "headers" const val EXTRA_HEADERS = "headers"
const val EXTRA_FILENAME = "filename" const val EXTRA_FILENAME = "filename"
private const val ACTION_CANCEL = "cancel"
private const val NOTIFICATION_ID = private const val NOTIFICATION_ID =
K.IMAGE_PROCESSOR_SERVICE_NOTIFICATION_ID K.IMAGE_PROCESSOR_SERVICE_NOTIFICATION_ID
private const val RESULT_NOTIFICATION_ID = private const val RESULT_NOTIFICATION_ID =
@ -60,6 +62,20 @@ class ImageProcessorService : Service() {
} }
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
when (intent.action) {
ACTION_CANCEL -> onCancel(startId)
else -> onNewImage(intent, startId)
}
return START_REDELIVER_INTENT
}
private fun onCancel(startId: Int) {
Log.i(TAG, "[onCancel] Cancel requested")
cmdTask?.cancel(false)
stopSelf(startId)
}
private fun onNewImage(intent: Intent, startId: Int) {
assert(intent.hasExtra(EXTRA_METHOD)) assert(intent.hasExtra(EXTRA_METHOD))
assert(intent.hasExtra(EXTRA_FILE_URL)) assert(intent.hasExtra(EXTRA_FILE_URL))
if (!isForeground) { if (!isForeground) {
@ -82,7 +98,6 @@ class ImageProcessorService : Service() {
addCommand(ImageProcessorCommand(startId, "null", "", null, "")) addCommand(ImageProcessorCommand(startId, "null", "", null, ""))
} }
} }
return START_REDELIVER_INTENT
} }
private fun onZeroDce(startId: Int, extras: Bundle) { private fun onZeroDce(startId: Int, extras: Bundle) {
@ -111,10 +126,20 @@ class ImageProcessorService : Service() {
} }
private fun buildNotification(content: String? = null): Notification { private fun buildNotification(content: String? = null): Notification {
val cancelIntent =
Intent(this, ImageProcessorService::class.java).apply {
action = ACTION_CANCEL
}
val cancelPendingIntent = PendingIntent.getService(
this, 0, cancelIntent, getPendingIntentFlagImmutable()
)
return NotificationCompat.Builder(this, CHANNEL_ID).run { return NotificationCompat.Builder(this, CHANNEL_ID).run {
setSmallIcon(R.drawable.outline_auto_fix_high_white_24) setSmallIcon(R.drawable.outline_auto_fix_high_white_24)
setContentTitle("Processing image") setContentTitle("Processing image")
if (content != null) setContentText(content) if (content != null) setContentText(content)
addAction(
0, getString(android.R.string.cancel), cancelPendingIntent
)
build() build()
} }
} }
@ -171,7 +196,8 @@ class ImageProcessorService : Service() {
notifyResult(result) notifyResult(result)
cmds.removeFirst() cmds.removeFirst()
stopSelf(cmd.startId) stopSelf(cmd.startId)
if (cmds.isNotEmpty()) { @Suppress("Deprecation")
if (cmds.isNotEmpty() && !isCancelled) {
runCommand() runCommand()
} else { } else {
cmdTask = null cmdTask = null
@ -363,6 +389,7 @@ private open class ImageProcessorCommandTask(context: Context) :
private fun handleCommand(cmd: ImageProcessorCommand): Uri { private fun handleCommand(cmd: ImageProcessorCommand): Uri {
val file = downloadFile(cmd.fileUrl, cmd.headers) val file = downloadFile(cmd.fileUrl, cmd.headers)
handleCancel()
return try { return try {
val fileUri = Uri.fromFile(file) val fileUri = Uri.fromFile(file)
val output = when (cmd.method) { val output = when (cmd.method) {
@ -373,6 +400,7 @@ private open class ImageProcessorCommandTask(context: Context) :
"Unknown method: ${cmd.method}" "Unknown method: ${cmd.method}"
) )
} }
handleCancel()
saveBitmap(output, cmd.filename, file) saveBitmap(output, cmd.filename, file)
} finally { } finally {
file.delete() file.delete()
@ -450,6 +478,13 @@ private open class ImageProcessorCommandTask(context: Context) :
} }
} }
private fun handleCancel() {
if (isCancelled) {
Log.i(TAG, "[handleCancel] Canceled")
throw InterruptedException()
}
}
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
private val context = context private val context = context
} }