Implement handling RecoverableSecurityException needed for updating files in MediaStore

This commit is contained in:
darthpaul 2021-11-10 01:05:38 +00:00
parent 40d203a8bb
commit e955f96751
2 changed files with 23 additions and 1 deletions

View file

@ -3,6 +3,7 @@ package com.simplemobiletools.commons.activities
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.app.ActivityManager import android.app.ActivityManager
import android.app.RecoverableSecurityException
import android.app.role.RoleManager import android.app.role.RoleManager
import android.content.ActivityNotFoundException import android.content.ActivityNotFoundException
import android.content.Context import android.content.Context
@ -53,10 +54,12 @@ abstract class BaseSimpleActivity : AppCompatActivity() {
private val GENERIC_PERM_HANDLER = 100 private val GENERIC_PERM_HANDLER = 100
private val DELETE_FILE_SDK_30_HANDLER = 300 private val DELETE_FILE_SDK_30_HANDLER = 300
private val RECOVERABLE_SECURITY_HANDLER = 301
companion object { companion object {
var funAfterSAFPermission: ((success: Boolean) -> Unit)? = null var funAfterSAFPermission: ((success: Boolean) -> Unit)? = null
var funAfterDelete30File: ((success: Boolean) -> Unit)? = null var funAfterDelete30File: ((success: Boolean) -> Unit)? = null
var funRecoverableSecurity: ((success: Boolean) -> Unit)? = null
} }
abstract fun getAppIconIDs(): ArrayList<Int> abstract fun getAppIconIDs(): ArrayList<Int>
@ -274,6 +277,9 @@ abstract class BaseSimpleActivity : AppCompatActivity() {
exportSettingsTo(outputStream, configItemsToExport) exportSettingsTo(outputStream, configItemsToExport)
} else if (requestCode == DELETE_FILE_SDK_30_HANDLER) { } else if (requestCode == DELETE_FILE_SDK_30_HANDLER) {
funAfterDelete30File?.invoke(resultCode == Activity.RESULT_OK) 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( fun copyMoveFilesTo(
fileDirItems: ArrayList<FileDirItem>, source: String, destination: String, isCopyOperation: Boolean, copyPhotoVideoOnly: Boolean, fileDirItems: ArrayList<FileDirItem>, source: String, destination: String, isCopyOperation: Boolean, copyPhotoVideoOnly: Boolean,
copyHidden: Boolean, callback: (destinationPath: String) -> Unit copyHidden: Boolean, callback: (destinationPath: String) -> Unit

View file

@ -1194,7 +1194,7 @@ fun AppCompatActivity.showSideloadingDialog() {
} }
fun BaseSimpleActivity.getTempFile(folderName: String, fileName: String): File? { fun BaseSimpleActivity.getTempFile(folderName: String, fileName: String): File? {
val folder = File(cacheDir, fileName) val folder = File(cacheDir, folderName)
if (!folder.exists()) { if (!folder.exists()) {
if (!folder.mkdir()) { if (!folder.mkdir()) {
toast(R.string.unknown_error_occurred) toast(R.string.unknown_error_occurred)