From f8d2739a1dba01a198ddbbe23000ce7865a7f2c3 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 26 Mar 2019 23:12:47 +0100 Subject: [PATCH] improve OTG path detection --- build.gradle | 2 +- .../commons/activities/BaseSimpleActivity.kt | 2 +- .../commons/extensions/Context-storage.kt | 13 +++++++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 68842bd70..f64d8ae6a 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { propMinSdkVersion = 21 propTargetSdkVersion = propCompileSdkVersion propVersionCode = 1 - propVersionName = '5.10.19' + propVersionName = '5.10.21' kotlin_version = '1.3.21' } 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 0c0fc8e29..0bf5681f0 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt @@ -166,7 +166,7 @@ abstract class BaseSimpleActivity : AppCompatActivity() { } baseConfig.OTGTreeUri = resultData.dataString baseConfig.OTGPartition = baseConfig.OTGTreeUri.removeSuffix("%3A").substringAfterLast('/').trimEnd('/') - baseConfig.OTGPath = "/storage/${baseConfig.OTGPartition}" + updateOTGPathFromPartition() val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION applicationContext.contentResolver.takePersistableUriPermission(resultData.data, takeFlags) 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 7ec20a102..60f491126 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 @@ -30,7 +30,8 @@ fun Context.getSDCardPath(): String { } val fullSDpattern = Pattern.compile("^/storage/[A-Za-z0-9]{4}-[A-Za-z0-9]{4}$") - var sdCardPath = directories.firstOrNull { fullSDpattern.matcher(it).matches() } ?: directories.firstOrNull { !physicalPaths.contains(it.toLowerCase()) } ?: "" + var sdCardPath = directories.firstOrNull { fullSDpattern.matcher(it).matches() } + ?: directories.firstOrNull { !physicalPaths.contains(it.toLowerCase()) } ?: "" // on some devices no method retrieved any SD card path, so test if its not sdcard1 by any chance. It happened on an Android 5.1 if (sdCardPath.trimEnd('/').isEmpty()) { @@ -200,7 +201,7 @@ fun Context.getOTGFastDocumentFile(path: String): DocumentFile? { if (baseConfig.OTGPartition.isEmpty()) { baseConfig.OTGPartition = baseConfig.OTGTreeUri.removeSuffix("%3A").substringAfterLast('/').trimEnd('/') - baseConfig.OTGPath = "/storage/${baseConfig.OTGPartition}" + updateOTGPathFromPartition() } val relativePath = Uri.encode(path.substring(baseConfig.OTGPath.length).trim('/')) @@ -435,6 +436,14 @@ fun Context.trySAFFileDelete(fileDirItem: FileDirItem, allowDeleteFolder: Boolea } } +fun Context.updateOTGPathFromPartition() { + baseConfig.OTGPath = if (File("/storage/${baseConfig.OTGPartition}").exists()) { + "/storage/${baseConfig.OTGPartition}" + } else { + "/mnt/media_rw/${baseConfig.OTGPartition}" + } +} + // avoid these being set as SD card paths private val physicalPaths = arrayListOf( "/storage/sdcard1", // Motorola Xoom