From e96d4724d370db66360c974988932b0742beeba8 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Tue, 19 Oct 2021 15:54:38 +0200 Subject: [PATCH] Adapt to write access at Android/data/sushi.hardcore.droidfs on SD cards --- .../droidfs/ChangePasswordActivity.kt | 28 +----- .../sushi/hardcore/droidfs/CreateActivity.kt | 87 ++++++------------- .../sushi/hardcore/droidfs/OpenActivity.kt | 22 ++--- .../hardcore/droidfs/VolumeActionActivity.kt | 29 ++++++- .../sushi/hardcore/droidfs/util/PathUtils.kt | 11 +-- .../main/res/layout/dialog_sdcard_error.xml | 30 +++++++ app/src/main/res/values-pt-rBR/strings.xml | 3 - app/src/main/res/values-ru/strings.xml | 3 - app/src/main/res/values/strings.xml | 6 +- 9 files changed, 98 insertions(+), 121 deletions(-) create mode 100644 app/src/main/res/layout/dialog_sdcard_error.xml diff --git a/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt index 9ab884d..d9dca82 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt @@ -1,6 +1,5 @@ package sushi.hardcore.droidfs -import android.net.Uri import android.os.Build import android.os.Bundle import android.text.Editable @@ -84,27 +83,6 @@ class ChangePasswordActivity : VolumeActionActivity() { } } - override fun onDirectoryPicked(uri: Uri) { - if (PathUtils.isTreeUriOnPrimaryStorage(uri)){ - val path = PathUtils.getFullPathFromTreeUri(uri, this) - if (path != null){ - editVolumePath.setText(path) - } else { - ColoredAlertDialogBuilder(this) - .setTitle(R.string.error) - .setMessage(R.string.path_from_uri_null_error_msg) - .setPositiveButton(R.string.ok, null) - .show() - } - } else { - ColoredAlertDialogBuilder(this) - .setTitle(R.string.warning) - .setMessage(R.string.change_pwd_on_sdcard_error_msg) - .setPositiveButton(R.string.ok, null) - .show() - } - } - fun checkVolumePathThenChangePassword() { loadVolumePath { val volumeFile = File(currentVolumePath) @@ -115,11 +93,7 @@ class ChangePasswordActivity : VolumeActionActivity() { .setPositiveButton(R.string.ok, null) .show() } else if (!volumeFile.canWrite()){ - ColoredAlertDialogBuilder(this) - .setTitle(R.string.warning) - .setMessage(R.string.change_pwd_cant_write_error_msg) - .setPositiveButton(R.string.ok, null) - .show() + errorDirectoryNotWritable(R.string.change_pwd_cant_write_error_msg) } else { changePassword() } diff --git a/app/src/main/java/sushi/hardcore/droidfs/CreateActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/CreateActivity.kt index afa4f80..c57dbfb 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/CreateActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/CreateActivity.kt @@ -1,7 +1,6 @@ package sushi.hardcore.droidfs import android.content.Intent -import android.net.Uri import android.os.Build import android.os.Bundle import android.view.View @@ -9,7 +8,6 @@ import android.widget.* import androidx.appcompat.app.AppCompatActivity import sushi.hardcore.droidfs.databinding.ActivityCreateBinding import sushi.hardcore.droidfs.explorers.ExplorerActivity -import sushi.hardcore.droidfs.util.PathUtils import sushi.hardcore.droidfs.util.Wiper import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder import java.io.File @@ -65,27 +63,6 @@ class CreateActivity : VolumeActionActivity() { } } - override fun onDirectoryPicked(uri: Uri) { - if (PathUtils.isTreeUriOnPrimaryStorage(uri)){ - val path = PathUtils.getFullPathFromTreeUri(uri, this) - if (path != null){ - editVolumePath.setText(path) - } else { - ColoredAlertDialogBuilder(this) - .setTitle(R.string.error) - .setMessage(R.string.path_from_uri_null_error_msg) - .setPositiveButton(R.string.ok, null) - .show() - } - } else { - ColoredAlertDialogBuilder(this) - .setTitle(R.string.warning) - .setMessage(R.string.create_on_sdcard_error_msg) - .setPositiveButton(R.string.ok, null) - .show() - } - } - fun createVolume() { loadVolumePath { val password = binding.editPassword.text.toString().toCharArray() @@ -93,45 +70,33 @@ class CreateActivity : VolumeActionActivity() { if (!password.contentEquals(passwordConfirm)) { Toast.makeText(this, R.string.passwords_mismatch, Toast.LENGTH_SHORT).show() } else { - object: LoadingTask(this, R.string.loading_msg_create){ - override fun doTask(activity: AppCompatActivity) { - val volumeFile = File(currentVolumePath) - var goodDirectory = false - if (!volumeFile.isDirectory) { - if (volumeFile.mkdirs()) { + val volumeFile = File(currentVolumePath) + var goodDirectory = false + if (!volumeFile.isDirectory) { + if (volumeFile.mkdirs()) { + goodDirectory = true + } else { + errorDirectoryNotWritable(R.string.create_cant_write_error_msg) + } + } else { + val dirContent = volumeFile.list() + if (dirContent != null) { + if (dirContent.isEmpty()) { + if (volumeFile.canWrite()) { goodDirectory = true } else { - stopTask { - ColoredAlertDialogBuilder(activity) - .setTitle(R.string.warning) - .setMessage(R.string.create_cant_write_error_msg) - .setPositiveButton(R.string.ok, null) - .show() - } + errorDirectoryNotWritable(R.string.create_cant_write_error_msg) } } else { - val dirContent = volumeFile.list() - if (dirContent != null){ - if (dirContent.isEmpty()) { - if (volumeFile.canWrite()){ - goodDirectory = true - } else { - stopTask { - ColoredAlertDialogBuilder(activity) - .setTitle(R.string.warning) - .setMessage(R.string.create_cant_write_error_msg) - .setPositiveButton(R.string.ok, null) - .show() - } - } - } else { - stopTaskWithToast(R.string.dir_not_empty) - } - } else { - stopTaskWithToast(R.string.listdir_null_error_msg) - } + Toast.makeText(this, R.string.dir_not_empty, Toast.LENGTH_SHORT).show() } - if (goodDirectory) { + } else { + Toast.makeText(this, R.string.listdir_null_error_msg, Toast.LENGTH_SHORT).show() + } + } + if (goodDirectory) { + object: LoadingTask(this, R.string.loading_msg_create){ + override fun doTask(activity: AppCompatActivity) { val xchacha = when (binding.spinnerXchacha.selectedItemPosition) { 0 -> 0 1 -> 1 @@ -172,10 +137,10 @@ class CreateActivity : VolumeActionActivity() { } } } - } - override fun doFinally(activity: AppCompatActivity) { - Arrays.fill(password, 0.toChar()) - Arrays.fill(passwordConfirm, 0.toChar()) + override fun doFinally(activity: AppCompatActivity) { + Arrays.fill(password, 0.toChar()) + Arrays.fill(passwordConfirm, 0.toChar()) + } } } } diff --git a/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt index 7c7b415..19c6337 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt @@ -1,13 +1,13 @@ package sushi.hardcore.droidfs import android.content.Intent -import android.net.Uri import android.os.Build import android.os.Bundle import android.text.Editable import android.text.TextWatcher import android.view.MenuItem import android.widget.AdapterView.OnItemClickListener +import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import sushi.hardcore.droidfs.adapters.SavedVolumesAdapter import sushi.hardcore.droidfs.content_providers.RestrictedFileProvider @@ -112,19 +112,6 @@ class OpenActivity : VolumeActionActivity() { isStartingActivity = true } - override fun onDirectoryPicked(uri: Uri) { - val path = PathUtils.getFullPathFromTreeUri(uri, this) - if (path != null){ - editVolumePath.setText(path) - } else { - ColoredAlertDialogBuilder(this) - .setTitle(R.string.error) - .setMessage(R.string.path_from_uri_null_error_msg) - .setPositiveButton(R.string.ok, null) - .show() - } - } - fun checkVolumePathThenOpen() { loadVolumePath { val volumeFile = File(currentVolumePath) @@ -147,7 +134,12 @@ class OpenActivity : VolumeActionActivity() { .setCancelable(false) .setPositiveButton(R.string.ok) { _, _ -> openVolume() } if (PathUtils.isPathOnExternalStorage(currentVolumeName, this)){ - dialog.setMessage(R.string.open_on_sdcard_warning) + dialog.setView( + layoutInflater.inflate(R.layout.dialog_sdcard_error, null).apply { + findViewById(R.id.path).text = PathUtils.getPackageDataFolder(this@OpenActivity) + findViewById(R.id.footer).text = getString(R.string.open_read_only) + } + ) } else { dialog.setMessage(R.string.open_cant_write_warning) } diff --git a/app/src/main/java/sushi/hardcore/droidfs/VolumeActionActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/VolumeActionActivity.kt index bd4aac4..d187218 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/VolumeActionActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/VolumeActionActivity.kt @@ -118,7 +118,18 @@ abstract class VolumeActionActivity : BaseActivity() { } protected open fun onPickingDirectory() {} - protected abstract fun onDirectoryPicked(uri: Uri) + protected fun onDirectoryPicked(uri: Uri) { + val path = PathUtils.getFullPathFromTreeUri(uri, this) + if (path != null) { + editVolumePath.setText(path) + } else { + ColoredAlertDialogBuilder(this) + .setTitle(R.string.error) + .setMessage(R.string.path_from_uri_null_error_msg) + .setPositiveButton(R.string.ok, null) + .show() + } + } private fun safePickDirectory() { try { @@ -368,4 +379,20 @@ abstract class VolumeActionActivity : BaseActivity() { callback() } } + + fun errorDirectoryNotWritable(errorMsg: Int) { + val dialog = ColoredAlertDialogBuilder(this) + .setTitle(R.string.error) + .setPositiveButton(R.string.ok, null) + if (PathUtils.isPathOnExternalStorage(currentVolumePath, this)) { + dialog.setView( + layoutInflater.inflate(R.layout.dialog_sdcard_error, null).apply { + findViewById(R.id.path).text = PathUtils.getPackageDataFolder(this@VolumeActionActivity) + } + ) + } else { + dialog.setMessage(errorMsg) + } + dialog.show() + } } \ No newline at end of file diff --git a/app/src/main/java/sushi/hardcore/droidfs/util/PathUtils.kt b/app/src/main/java/sushi/hardcore/droidfs/util/PathUtils.kt index 0faac4c..b893989 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/util/PathUtils.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/util/PathUtils.kt @@ -94,19 +94,14 @@ object PathUtils { ) + " " + units[digitGroups] } - fun isTreeUriOnPrimaryStorage(treeUri: Uri): Boolean { - val volumeId = getVolumeIdFromTreeUri(treeUri) - return if (volumeId != null) { - volumeId == PRIMARY_VOLUME_NAME || volumeId == "home" || volumeId == "downloads" - } else { - false - } + fun getPackageDataFolder(context: Context): String { + return "Android/data/${context.packageName}/" } private fun getExternalStoragePath(context: Context): List { val externalPaths: MutableList = ArrayList() ContextCompat.getExternalFilesDirs(context, null).forEach { - val rootPath = it.path.substring(0, it.path.indexOf(pathJoin("Android/data/", context.packageName, "files"))) + val rootPath = it.path.substring(0, it.path.indexOf(getPackageDataFolder(context)+"files")) if (!rootPath.endsWith("/0/")){ //not primary storage externalPaths.add(rootPath) } diff --git a/app/src/main/res/layout/dialog_sdcard_error.xml b/app/src/main/res/layout/dialog_sdcard_error.xml new file mode 100644 index 0000000..a04214f --- /dev/null +++ b/app/src/main/res/layout/dialog_sdcard_error.xml @@ -0,0 +1,30 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 4e35406..3571918 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -159,12 +159,9 @@ Por favor, digite um valor numérico Falha ao carregar o local selecionado. DroidFS não pode salvar neste local. Por favor, tente algum outro. - DroidFS não pode salvar em cartões SD externos, por favor selecione um local no armazenamento interno. - DroidFS não pode salvar em cartões SD exteros. Abrindo o volume apenas com acesso de leitura. DroidFS não tem permissão para salvar neste local. Abrindo o volume apenas com acesso de leitura. DroidFS não tem permissão para salvar neste local. Por favor, tente outro volume. DroidFS não tem permissão para salvar neste local. Pode tentar mover o volume para um local com permissão de escrita. - DroidFS não pode salvar em cartões SD externos, por favor transfira o volume ao armazenamento interno. Apresentação parou Apresentação começou A imagem foi alternada. Você deseja salvar estas mudanças e substituir a imagem original? diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index f9e907c..4c6bc54 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -156,12 +156,9 @@ Введите числовое значение. Невозможно получить выбранный путь. DroidFS не имеет доступа на запись по этому пути. Попробуйте найти другое место. - DroidFS не может записывать на съёмные SD-карты, выберите путь во внутренней памяти. - DroidFS не может писать на съёмные SD-карты. Том открывается только для чтения. DroidFS не имеет доступа на запись по этому пути. Том открывается только для чтения. DroidFS не имеет доступа на запись по этому пути. Попробуйте другой том. DroidFS не имеет доступа на запись по этому пути. Попробуйте переместить том в место, доступное для записи. - DroidFS не может писать на съёмные SD-карты, переместите том во внутреннюю память. Слайдшоу остановлено Слайдшоу началось Изображение было повёрнуто. Сохранить изменения и перезаписать исходное изображение? diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 36873d1..6b064fe 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -162,12 +162,12 @@ Please enter a numeric value Failed to retrieve the selected path. DroidFS doesn\'t have write access to this path. Please try another location. - DroidFS can\'t write on removable SD cards, please select a path on internal storage. - DroidFS can\'t write on removable SD cards. Opening volume with read-only access. + Opening volume with read-only access. DroidFS doesn\'t have write access to this path. Opening volume with read-only access. DroidFS doesn\'t have write access to this path. Please try another volume. + DroidFS can only write to removable SD cards under: + Please use a subdirectory of this path or internal storage. DroidFS doesn\'t have write access to this path. You can try to move the volume to a writable location. - DroidFS can\'t write on removable SD cards, please move the volume to internal storage. Slideshow stopped Slideshow started The image has been rotated. Do you want to save these changes and overwrite the original image ?