improve OTG path detection

This commit is contained in:
tibbi 2019-03-26 23:12:47 +01:00
parent 3b864b58a6
commit f8d2739a1d
3 changed files with 13 additions and 4 deletions

View file

@ -7,7 +7,7 @@ buildscript {
propMinSdkVersion = 21
propTargetSdkVersion = propCompileSdkVersion
propVersionCode = 1
propVersionName = '5.10.19'
propVersionName = '5.10.21'
kotlin_version = '1.3.21'
}

View file

@ -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)

View file

@ -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