access Android/[data|obb] on all volumes

- passing EXTRA_URI with the actual Android/[data|obb] URI on the intent will open it directly and it is selectable on Android 11 and 12.
This commit is contained in:
darthpaul 2021-11-13 09:45:57 +00:00
parent 32d2a6b97b
commit 3a6d59485a
4 changed files with 44 additions and 34 deletions

View file

@ -154,7 +154,7 @@ fun BaseSimpleActivity.isShowingSAFPrimaryAndroidDialog(path: String): Boolean {
WritePermissionDialog(this, false) {
Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
putExtra("android.content.extra.SHOW_ADVANCED", true)
putExtra(DocumentsContract.EXTRA_INITIAL_URI, createDocumentUri("${path.getBasePath(this@isShowingSAFPrimaryAndroidDialog).trimEnd('/')}/Android"))
putExtra(DocumentsContract.EXTRA_INITIAL_URI, createDocumentUri(path))
try {
startActivityForResult(this, OPEN_DOCUMENT_TREE_PRIMARY)
checkedDocumentPath = path

View file

@ -153,10 +153,14 @@ fun Context.isPathOnOTG(path: String) = otgPath.isNotEmpty() && path.startsWith(
fun Context.isPathOnInternalStorage(path: String) = internalStoragePath.isNotEmpty() && path.startsWith(internalStoragePath)
val DIRS_ACCESSIBLE_ONLY_WITH_SAF = listOf("/Android/data", "/Android/obb")
const val ANDROID_DATA_DIR = "/Android/data"
const val ANDROID_OBB_DIR = "/Android/obb"
val DIRS_ACCESSIBLE_ONLY_WITH_SAF = listOf(ANDROID_DATA_DIR, ANDROID_OBB_DIR)
fun Context.getSAFOnlyDirs(): List<String> {
return DIRS_ACCESSIBLE_ONLY_WITH_SAF.map { "$internalStoragePath$it" }
return DIRS_ACCESSIBLE_ONLY_WITH_SAF.map { "$internalStoragePath$it" }.toMutableList() +
DIRS_ACCESSIBLE_ONLY_WITH_SAF.map { "$sdCardPath$it" }.toMutableList() +
DIRS_ACCESSIBLE_ONLY_WITH_SAF.map { "$otgPath$it" }.toMutableList()
}
fun Context.isSAFOnlyRoot(path: String): Boolean {
@ -196,25 +200,21 @@ fun Context.hasProperStoredAndroidDirTreeUri(path: String): Boolean {
fun Context.getAndroidTreeUri(path: String): String {
return when {
isPathOnOTG(path) -> baseConfig.otgAndroidTreeUri
isPathOnSD(path) -> baseConfig.sdAndroidTreeUri
else -> baseConfig.primaryAndroidTreeUri
isPathOnOTG(path) -> if (isAndroidDataDir(path)) baseConfig.otgAndroidDataTreeUri else baseConfig.otgAndroidObbTreeUri
isPathOnSD(path) -> if (isAndroidDataDir(path)) baseConfig.sdAndroidDataTreeUri else baseConfig.sdAndroidTreeObbUri
else -> if (isAndroidDataDir(path)) baseConfig.primaryAndroidDataTreeUri else baseConfig.primaryAndroidObbTreeUri
}
}
fun Context.getTreeUriFromRoot(path: String): String {
return when {
isPathOnOTG(path) -> baseConfig.otgAndroidTreeUri
isPathOnSD(path) -> baseConfig.sdAndroidTreeUri
else -> baseConfig.primaryAndroidTreeUri
}
fun isAndroidDataDir(path: String): Boolean {
return path.contains(ANDROID_DATA_DIR)
}
fun Context.storeAndroidTreeUri(path: String, treeUri: String) {
return when {
isPathOnOTG(path) -> baseConfig.otgAndroidTreeUri = treeUri
isPathOnSD(path) -> baseConfig.sdAndroidTreeUri = treeUri
else -> baseConfig.primaryAndroidTreeUri = treeUri
isPathOnOTG(path) -> if (isAndroidDataDir(path)) baseConfig.otgAndroidDataTreeUri = treeUri else baseConfig.otgAndroidObbTreeUri = treeUri
isPathOnSD(path) -> if (isAndroidDataDir(path)) baseConfig.sdAndroidDataTreeUri = treeUri else baseConfig.otgAndroidObbTreeUri = treeUri
else -> if (isAndroidDataDir(path)) baseConfig.primaryAndroidDataTreeUri = treeUri else baseConfig.primaryAndroidObbTreeUri = treeUri
}
}
@ -236,13 +236,8 @@ fun Context.createDocumentUri(fullPath: String): Uri {
return DocumentsContract.buildDocumentUriUsingTree(treeUri, documentId)
}
fun Context.getStorageRootId(path: String): String {
//content://com.android.externalstorage.documents/tree/1E17-301C%3A
//content://com.android.externalstorage.documents/tree/primary%3A
val androidTreeUri = getAndroidTreeUri(path)
val rootId = androidTreeUri.removeSuffix("%3AAndroid").substringAfterLast('/').trimEnd('/')
return rootId
}
fun Context.getStorageRootId(path: String) =
getAndroidTreeUri(path).removeSuffix(if (isAndroidDataDir(path)) "%3AAndroid%2Fdata" else "%3AAndroid%2Fobb").substringAfterLast('/').trimEnd('/')
fun Context.isAStorageRootFolder(path: String): Boolean {
val trimmed = path.trimEnd('/')

View file

@ -24,17 +24,29 @@ open class BaseConfig(val context: Context) {
get() = prefs.getInt(LAST_VERSION, 0)
set(lastVersion) = prefs.edit().putInt(LAST_VERSION, lastVersion).apply()
var primaryAndroidTreeUri: String
get() = prefs.getString(PRIMARY_ANDROID_TREE_URI, "")!!
set(uri) = prefs.edit().putString(PRIMARY_ANDROID_TREE_URI, uri).apply()
var primaryAndroidDataTreeUri: String
get() = prefs.getString(PRIMARY_ANDROID_DATA_TREE_URI, "")!!
set(uri) = prefs.edit().putString(PRIMARY_ANDROID_DATA_TREE_URI, uri).apply()
var sdAndroidTreeUri: String
get() = prefs.getString(SD_ANDROID_TREE_URI, "")!!
set(uri) = prefs.edit().putString(SD_ANDROID_TREE_URI, uri).apply()
var sdAndroidDataTreeUri: String
get() = prefs.getString(SD_ANDROID_DATA_TREE_URI, "")!!
set(uri) = prefs.edit().putString(SD_ANDROID_DATA_TREE_URI, uri).apply()
var otgAndroidTreeUri: String
get() = prefs.getString(OTG_ANDROID_TREE_URI, "")!!
set(uri) = prefs.edit().putString(OTG_ANDROID_TREE_URI, uri).apply()
var otgAndroidDataTreeUri: String
get() = prefs.getString(OTG_ANDROID_DATA_TREE_URI, "")!!
set(uri) = prefs.edit().putString(OTG_ANDROID_DATA_TREE_URI, uri).apply()
var primaryAndroidObbTreeUri: String
get() = prefs.getString(PRIMARY_ANDROID_OBB_TREE_URI, "")!!
set(uri) = prefs.edit().putString(PRIMARY_ANDROID_OBB_TREE_URI, uri).apply()
var sdAndroidTreeObbUri: String
get() = prefs.getString(SD_ANDROID_OBB_TREE_URI, "")!!
set(uri) = prefs.edit().putString(SD_ANDROID_OBB_TREE_URI, uri).apply()
var otgAndroidObbTreeUri: String
get() = prefs.getString(OTG_ANDROID_OBB_TREE_URI, "")!!
set(uri) = prefs.edit().putString(OTG_ANDROID_OBB_TREE_URI, uri).apply()
var sdTreeUri: String
get() = prefs.getString(SD_TREE_URI, "")!!

View file

@ -65,9 +65,12 @@ const val PREFS_KEY = "Prefs"
const val APP_RUN_COUNT = "app_run_count"
const val LAST_VERSION = "last_version"
const val SD_TREE_URI = "tree_uri_2"
const val PRIMARY_ANDROID_TREE_URI = "primary_android_tree_uri_2"
const val OTG_ANDROID_TREE_URI = "otg_android_tree_uri_2"
const val SD_ANDROID_TREE_URI = "sd_android_tree_uri_2"
const val PRIMARY_ANDROID_DATA_TREE_URI = "primary_android_data_tree_uri_2"
const val OTG_ANDROID_DATA_TREE_URI = "otg_android_data_tree__uri_2"
const val SD_ANDROID_DATA_TREE_URI = "sd_android_data_tree_uri_2"
const val PRIMARY_ANDROID_OBB_TREE_URI = "primary_android_obb_tree_uri_2"
const val OTG_ANDROID_OBB_TREE_URI = "otg_android_obb_tree_uri_2"
const val SD_ANDROID_OBB_TREE_URI = "sd_android_obb_tree_uri_2"
const val OTG_TREE_URI = "otg_tree_uri_2"
const val SD_CARD_PATH = "sd_card_path_2"
const val OTG_REAL_PATH = "otg_real_path_2"