From 1aa92e77fe68d9be68552bd296ea5969519acacb Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 3 Sep 2019 18:30:57 +0200 Subject: [PATCH] adding some extra SD/OTG path related checks --- .../commons/activities/BaseSimpleActivity.kt | 20 +++++++++++++++---- .../commons/extensions/Activity.kt | 8 +++++--- .../commons/extensions/Context-storage.kt | 8 +++----- .../commons/helpers/Constants.kt | 2 ++ 4 files changed, 26 insertions(+), 12 deletions(-) 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 d57f10477..ee0418f55 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt @@ -28,12 +28,14 @@ import com.simplemobiletools.commons.models.FAQItem import com.simplemobiletools.commons.models.FileDirItem import java.io.File import java.util.* +import java.util.regex.Pattern abstract class BaseSimpleActivity : AppCompatActivity() { var copyMoveCallback: ((destinationPath: String) -> Unit)? = null var actionOnPermission: ((granted: Boolean) -> Unit)? = null var isAskingPermissions = false var useDynamicTheme = true + var checkedDocumentPath = "" private val GENERIC_PERM_HANDLER = 100 @@ -173,12 +175,21 @@ abstract class BaseSimpleActivity : AppCompatActivity() { override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { super.onActivityResult(requestCode, resultCode, resultData) - if (requestCode == OPEN_DOCUMENT_TREE && resultCode == Activity.RESULT_OK && resultData != null) { - if (isProperSDFolder(resultData.data)) { + val partition = try { + checkedDocumentPath.substring(9, 18) + } catch (e: Exception) { + "" + } + val sdOtgPattern = Pattern.compile(SD_OTG_SHORT) + + if (requestCode == OPEN_DOCUMENT_TREE && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) { + val isProperPartition = partition.isEmpty() || (sdOtgPattern.matcher(partition).matches() && resultData.dataString!!.contains(partition)) + if (isProperSDFolder(resultData.data!!) && isProperPartition) { if (resultData.dataString == baseConfig.OTGTreeUri) { toast(R.string.sd_card_usb_same) return } + saveTreeUri(resultData) funAfterSAFPermission?.invoke(true) funAfterSAFPermission = null @@ -187,8 +198,9 @@ abstract class BaseSimpleActivity : AppCompatActivity() { val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) startActivityForResult(intent, requestCode) } - } else if (requestCode == OPEN_DOCUMENT_TREE_OTG && resultCode == Activity.RESULT_OK && resultData != null) { - if (isProperOTGFolder(resultData.data)) { + } else if (requestCode == OPEN_DOCUMENT_TREE_OTG && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) { + val isProperPartition = partition.isEmpty() || (sdOtgPattern.matcher(partition).matches() && resultData.dataString!!.contains(partition)) + if (isProperOTGFolder(resultData.data!!) && isProperPartition) { if (resultData.dataString == baseConfig.treeUri) { funAfterSAFPermission?.invoke(false) toast(R.string.sd_card_usb_same) 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 520c2aaf6..8edad6fc5 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt @@ -96,7 +96,7 @@ fun Activity.isAppInstalledOnSDCard(): Boolean = try { false } -fun Activity.isShowingSAFDialog(path: String): Boolean { +fun BaseSimpleActivity.isShowingSAFDialog(path: String): Boolean { return if (isPathOnSD(path) && (baseConfig.treeUri.isEmpty() || !hasProperStoredTreeUri(false))) { runOnUiThread { if (!isDestroyed && !isFinishing) { @@ -108,6 +108,7 @@ fun Activity.isShowingSAFDialog(path: String): Boolean { } if (resolveActivity(packageManager) != null) { + checkedDocumentPath = path startActivityForResult(this, OPEN_DOCUMENT_TREE) } else { toast(R.string.unknown_error_occurred) @@ -124,14 +125,14 @@ fun Activity.isShowingSAFDialog(path: String): Boolean { fun BaseSimpleActivity.isShowingOTGDialog(path: String): Boolean { return if (isPathOnOTG(path) && (baseConfig.OTGTreeUri.isEmpty() || !hasProperStoredTreeUri(true))) { - showOTGPermissionDialog() + showOTGPermissionDialog(path) true } else { false } } -fun Activity.showOTGPermissionDialog() { +fun BaseSimpleActivity.showOTGPermissionDialog(path: String) { runOnUiThread { if (!isDestroyed && !isFinishing) { WritePermissionDialog(this, true) { @@ -141,6 +142,7 @@ fun Activity.showOTGPermissionDialog() { } if (resolveActivity(packageManager) != null) { + checkedDocumentPath = path startActivityForResult(this, OPEN_DOCUMENT_TREE_OTG) } else { toast(R.string.unknown_error_occurred) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt index 157518a2b..f60a9d5dc 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt @@ -13,9 +13,7 @@ import android.text.TextUtils import androidx.core.content.FileProvider import androidx.documentfile.provider.DocumentFile import com.simplemobiletools.commons.R -import com.simplemobiletools.commons.helpers.ensureBackgroundThread -import com.simplemobiletools.commons.helpers.isMarshmallowPlus -import com.simplemobiletools.commons.helpers.isNougatPlus +import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.models.FileDirItem import java.io.File import java.net.URLDecoder @@ -28,7 +26,7 @@ fun Context.getSDCardPath(): String { it != getInternalStoragePath() && (baseConfig.OTGPartition.isEmpty() || !it.endsWith(baseConfig.OTGPartition)) } - val fullSDpattern = Pattern.compile("^/storage/[A-Za-z0-9]{4}-[A-Za-z0-9]{4}$") + val fullSDpattern = Pattern.compile(SD_OTG_PATTERN) var sdCardPath = directories.firstOrNull { fullSDpattern.matcher(it).matches() } ?: directories.firstOrNull { !physicalPaths.contains(it.toLowerCase()) } ?: "" @@ -43,7 +41,7 @@ fun Context.getSDCardPath(): String { } if (sdCardPath.isEmpty()) { - val SDpattern = Pattern.compile("^[A-Za-z0-9]{4}-[A-Za-z0-9]{4}$") + val SDpattern = Pattern.compile(SD_OTG_SHORT) try { File("/storage").listFiles()?.forEach { if (SDpattern.matcher(it.name).matches()) { diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt index 71f7bbdb3..f696a15a9 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt @@ -26,6 +26,8 @@ const val INVALID_NAVIGATION_BAR_COLOR = -1 const val CHOPPED_LIST_DEFAULT_SIZE = 50 const val SAVE_DISCARD_PROMPT_INTERVAL = 1000L val DEFAULT_WIDGET_BG_COLOR = Color.parseColor("#33000000") +const val SD_OTG_PATTERN = "^/storage/[A-Za-z0-9]{4}-[A-Za-z0-9]{4}$" +const val SD_OTG_SHORT = "^[A-Za-z0-9]{4}-[A-Za-z0-9]{4}$" const val HOUR_MINUTES = 60 const val DAY_MINUTES = 24 * HOUR_MINUTES