From e955f967518fb2d57e87dc100afa8d128ccd354c Mon Sep 17 00:00:00 2001 From: darthpaul Date: Wed, 10 Nov 2021 01:05:38 +0000 Subject: [PATCH] Implement handling RecoverableSecurityException needed for updating files in MediaStore --- .../commons/activities/BaseSimpleActivity.kt | 22 +++++++++++++++++++ .../commons/extensions/Activity.kt | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt index 4c18286d9..c640f88d7 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.commons.activities import android.annotation.SuppressLint import android.app.Activity import android.app.ActivityManager +import android.app.RecoverableSecurityException import android.app.role.RoleManager import android.content.ActivityNotFoundException import android.content.Context @@ -53,10 +54,12 @@ abstract class BaseSimpleActivity : AppCompatActivity() { private val GENERIC_PERM_HANDLER = 100 private val DELETE_FILE_SDK_30_HANDLER = 300 + private val RECOVERABLE_SECURITY_HANDLER = 301 companion object { var funAfterSAFPermission: ((success: Boolean) -> Unit)? = null var funAfterDelete30File: ((success: Boolean) -> Unit)? = null + var funRecoverableSecurity: ((success: Boolean) -> Unit)? = null } abstract fun getAppIconIDs(): ArrayList @@ -274,6 +277,9 @@ abstract class BaseSimpleActivity : AppCompatActivity() { exportSettingsTo(outputStream, configItemsToExport) } else if (requestCode == DELETE_FILE_SDK_30_HANDLER) { funAfterDelete30File?.invoke(resultCode == Activity.RESULT_OK) + } else if (requestCode == RECOVERABLE_SECURITY_HANDLER) { + funRecoverableSecurity?.invoke(resultCode == Activity.RESULT_OK) + funRecoverableSecurity = null } } @@ -396,6 +402,22 @@ abstract class BaseSimpleActivity : AppCompatActivity() { } } + @SuppressLint("NewApi") + fun handleRecoverableSecurityException(callback: (success: Boolean) -> Unit) { + try { + callback.invoke(true) + } catch (securityException: SecurityException) { + if (isQPlus()) { + funRecoverableSecurity = callback + val recoverableSecurityException = securityException as? RecoverableSecurityException ?: throw securityException + val intentSender = recoverableSecurityException.userAction.actionIntent.intentSender + startIntentSenderForResult(intentSender, RECOVERABLE_SECURITY_HANDLER, null, 0, 0, 0) + } else { + callback(false) + } + } + } + fun copyMoveFilesTo( fileDirItems: ArrayList, source: String, destination: String, isCopyOperation: Boolean, copyPhotoVideoOnly: Boolean, copyHidden: Boolean, callback: (destinationPath: String) -> Unit diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt index 16afac95c..a84b2a4cf 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt @@ -1194,7 +1194,7 @@ fun AppCompatActivity.showSideloadingDialog() { } fun BaseSimpleActivity.getTempFile(folderName: String, fileName: String): File? { - val folder = File(cacheDir, fileName) + val folder = File(cacheDir, folderName) if (!folder.exists()) { if (!folder.mkdir()) { toast(R.string.unknown_error_occurred)