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 24edcf73f..90e4aef7e 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt @@ -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 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 50ed03cd2..a7c6a15ea 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 @@ -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 { - 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('/') diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/BaseConfig.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/BaseConfig.kt index ab06dc098..4578124d4 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/BaseConfig.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/BaseConfig.kt @@ -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, "")!! 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 107932497..bdd498d71 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt @@ -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"