From 5cd666d949013e90f13fb811a7285d03ff75b92c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Wed, 20 Sep 2023 13:11:02 +0200 Subject: [PATCH 1/3] Handle file creation for OTG This fixes #602 --- .../pro/dialogs/CreateNewItemDialog.kt | 19 +++++++++++++++++-- .../filemanager/pro/extensions/Activity.kt | 1 - 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/CreateNewItemDialog.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/CreateNewItemDialog.kt index 821e3d4b..eab5d6e5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/CreateNewItemDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/CreateNewItemDialog.kt @@ -66,6 +66,12 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca callback(false) } } + } else if (activity.isPathOnOTG(path)) { + val parent = activity.getDocumentFile(path.getParentPath()) + val created = parent?.createDirectory(path.getFilenameFromPath()) + if (created != null) { + success(alertDialog) + } } else { if (File(path).mkdirs()) { success(alertDialog) @@ -137,8 +143,17 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca } isRPlus() || path.startsWith(activity.internalStoragePath, true) -> { - if (File(path).createNewFile()) { - success(alertDialog) + + if (activity.isPathOnOTG(path)) { + val parent = activity.getDocumentFile(path.getParentPath()) + val created = parent?.createFile(path.getMimeType(), path.getFilenameFromPath()) + if (created != null) { + success(alertDialog) + } + } else { + if (File(path).createNewFile()) { + success(alertDialog) + } } } else -> { diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/extensions/Activity.kt index 0ba36aaf..c0a8042f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/extensions/Activity.kt @@ -2,7 +2,6 @@ package com.simplemobiletools.filemanager.pro.extensions import android.app.Activity import android.content.Intent -import android.content.res.Configuration import android.net.Uri import android.view.View import androidx.appcompat.app.AppCompatActivity From c8b980bf3e6788e77433bb1b4adfe2448bacf0d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Wed, 20 Sep 2023 15:48:31 +0200 Subject: [PATCH 2/3] Ensure that `needsStupidWritePermissions` is tried before fallback methods --- .../pro/dialogs/CreateNewItemDialog.kt | 49 +++++++------------ 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/CreateNewItemDialog.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/CreateNewItemDialog.kt index eab5d6e5..b0e0feca 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/CreateNewItemDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/CreateNewItemDialog.kt @@ -51,6 +51,21 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca private fun createDirectory(path: String, alertDialog: AlertDialog, callback: (Boolean) -> Unit) { when { + activity.needsStupidWritePermissions(path) -> activity.handleSAFDialog(path) { + if (!it) { + return@handleSAFDialog + } + + val documentFile = activity.getDocumentFile(path.getParentPath()) + if (documentFile == null) { + val error = String.format(activity.getString(R.string.could_not_create_folder), path) + activity.showErrorToast(error) + callback(false) + return@handleSAFDialog + } + documentFile.createDirectory(path.getFilenameFromPath()) + success(alertDialog) + } isRPlus() || path.startsWith(activity.internalStoragePath, true) -> { if (activity.isRestrictedSAFOnlyRoot(path)) { activity.handleAndroidSAFDialog(path) { @@ -66,33 +81,12 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca callback(false) } } - } else if (activity.isPathOnOTG(path)) { - val parent = activity.getDocumentFile(path.getParentPath()) - val created = parent?.createDirectory(path.getFilenameFromPath()) - if (created != null) { - success(alertDialog) - } } else { if (File(path).mkdirs()) { success(alertDialog) } } } - activity.needsStupidWritePermissions(path) -> activity.handleSAFDialog(path) { - if (!it) { - return@handleSAFDialog - } - - val documentFile = activity.getDocumentFile(path.getParentPath()) - if (documentFile == null) { - val error = String.format(activity.getString(R.string.could_not_create_folder), path) - activity.showErrorToast(error) - callback(false) - return@handleSAFDialog - } - documentFile.createDirectory(path.getFilenameFromPath()) - success(alertDialog) - } else -> { RootHelpers(activity).createFileFolder(path, false) { if (it) { @@ -143,17 +137,8 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca } isRPlus() || path.startsWith(activity.internalStoragePath, true) -> { - - if (activity.isPathOnOTG(path)) { - val parent = activity.getDocumentFile(path.getParentPath()) - val created = parent?.createFile(path.getMimeType(), path.getFilenameFromPath()) - if (created != null) { - success(alertDialog) - } - } else { - if (File(path).createNewFile()) { - success(alertDialog) - } + if (File(path).createNewFile()) { + success(alertDialog) } } else -> { From 21bf5697d5d003fdc166cea2ef32ac9f569fd868 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Thu, 21 Sep 2023 09:17:21 +0200 Subject: [PATCH 3/3] Update simple-commons reference for OTG fixes --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d63318e9..c8bed6c5 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,7 +5,7 @@ kotlin = "1.9.0" androidx-swiperefreshlayout = "1.1.0" androidx-documentfile = "1.0.1" #Simple tools -simple-commons = "c08a97b8ec" +simple-commons = "0661d6d44a" #Other autofittextview = "0.2.1" gestureviews = "2.5.2"