From 5800d6c76b0714ad027460c9fb5b06f91f5f2200 Mon Sep 17 00:00:00 2001 From: Hai Zhang Date: Fri, 19 Apr 2024 14:04:18 -0700 Subject: [PATCH] Fix: Use last path segment as default name for external storage shortcuts --- .../android/files/file/ExternalStorageUri.kt | 19 ++++++++++--------- .../AddExternalStorageShortcutFragment.kt | 10 ++++++++-- .../files/storage/AddStorageDialogFragment.kt | 2 ++ ...itExternalStorageShortcutDialogFragment.kt | 8 +++----- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/me/zhanghai/android/files/file/ExternalStorageUri.kt b/app/src/main/java/me/zhanghai/android/files/file/ExternalStorageUri.kt index 35607d206..2448615c8 100644 --- a/app/src/main/java/me/zhanghai/android/files/file/ExternalStorageUri.kt +++ b/app/src/main/java/me/zhanghai/android/files/file/ExternalStorageUri.kt @@ -12,15 +12,21 @@ import kotlinx.parcelize.Parcelize import kotlinx.parcelize.WriteWith import me.zhanghai.android.files.compat.DocumentsContractCompat import me.zhanghai.android.files.util.StableUriParceler +import me.zhanghai.android.files.util.takeIfNotEmpty @Parcelize @JvmInline value class ExternalStorageUri(val value: @WriteWith Uri) : Parcelable { - constructor(rootId: String, path: String) : this( + constructor( + rootId: String, + path: String + ) : this( DocumentsContract.buildDocumentUriUsingTree( DocumentsContract.buildTreeDocumentUri( - DocumentsContractCompat.EXTERNAL_STORAGE_PROVIDER_AUTHORITY, rootId - ), "$rootId:$path" + DocumentsContractCompat.EXTERNAL_STORAGE_PROVIDER_AUTHORITY, + rootId + ), + "$rootId:$path" ) ) @@ -47,9 +53,4 @@ private val Uri.isExternalStorageUri: Boolean pathSegments.size == 4 val ExternalStorageUri.displayName: String - get() = - if (rootId == DocumentsContractCompat.EXTERNAL_STORAGE_PRIMARY_EMULATED_ROOT_ID) { - path - } else { - DocumentsContract.getDocumentId(value) - } + get() = path.takeLastWhile { it != '/' }.takeIfNotEmpty() ?: "/" diff --git a/app/src/main/java/me/zhanghai/android/files/storage/AddExternalStorageShortcutFragment.kt b/app/src/main/java/me/zhanghai/android/files/storage/AddExternalStorageShortcutFragment.kt index eeaefa1e6..6674ab050 100644 --- a/app/src/main/java/me/zhanghai/android/files/storage/AddExternalStorageShortcutFragment.kt +++ b/app/src/main/java/me/zhanghai/android/files/storage/AddExternalStorageShortcutFragment.kt @@ -6,6 +6,7 @@ package me.zhanghai.android.files.storage import android.os.Bundle +import androidx.annotation.StringRes import androidx.fragment.app.Fragment import kotlinx.parcelize.Parcelize import me.zhanghai.android.files.R @@ -27,7 +28,9 @@ class AddExternalStorageShortcutFragment : Fragment() { val hasDocumentsUi = uri.value.createDocumentsUiViewDirectoryIntent() .resolveActivity(packageManager) != null if (hasDocumentsUi) { - val externalStorageShortcut = ExternalStorageShortcut(null, null, uri) + val externalStorageShortcut = ExternalStorageShortcut( + null, args.customNameRes?.let { getString(it) }, uri + ) Storages.addOrReplace(externalStorageShortcut) } else { showToast(R.string.activity_not_found) @@ -36,5 +39,8 @@ class AddExternalStorageShortcutFragment : Fragment() { } @Parcelize - class Args(val uri: ExternalStorageUri) : ParcelableArgs + class Args( + @StringRes val customNameRes: Int?, + val uri: ExternalStorageUri + ) : ParcelableArgs } diff --git a/app/src/main/java/me/zhanghai/android/files/storage/AddStorageDialogFragment.kt b/app/src/main/java/me/zhanghai/android/files/storage/AddStorageDialogFragment.kt index d0f3085ae..74a3d568f 100644 --- a/app/src/main/java/me/zhanghai/android/files/storage/AddStorageDialogFragment.kt +++ b/app/src/main/java/me/zhanghai/android/files/storage/AddStorageDialogFragment.kt @@ -44,6 +44,7 @@ class AddStorageDialogFragment : AppCompatDialogFragment() { R.string.storage_add_storage_android_data to AddExternalStorageShortcutActivity ::class.createIntent().putArgs( AddExternalStorageShortcutFragment.Args( + R.string.storage_add_storage_android_data, ExternalStorageProviderHacks.DOCUMENT_URI_ANDROID_DATA .asExternalStorageUri() ) @@ -53,6 +54,7 @@ class AddStorageDialogFragment : AppCompatDialogFragment() { R.string.storage_add_storage_android_obb to AddExternalStorageShortcutActivity ::class.createIntent().putArgs( AddExternalStorageShortcutFragment.Args( + R.string.storage_add_storage_android_obb, ExternalStorageProviderHacks.DOCUMENT_URI_ANDROID_OBB .asExternalStorageUri() ) diff --git a/app/src/main/java/me/zhanghai/android/files/storage/EditExternalStorageShortcutDialogFragment.kt b/app/src/main/java/me/zhanghai/android/files/storage/EditExternalStorageShortcutDialogFragment.kt index 9fd868ea0..37cd7cf66 100644 --- a/app/src/main/java/me/zhanghai/android/files/storage/EditExternalStorageShortcutDialogFragment.kt +++ b/app/src/main/java/me/zhanghai/android/files/storage/EditExternalStorageShortcutDialogFragment.kt @@ -16,6 +16,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.textfield.TextInputEditText import kotlinx.parcelize.Parcelize import me.zhanghai.android.files.R +import me.zhanghai.android.files.compat.DocumentsContractCompat import me.zhanghai.android.files.databinding.EditExternalStorageShortcutDialogBinding import me.zhanghai.android.files.file.ExternalStorageUri import me.zhanghai.android.files.file.displayName @@ -65,12 +66,9 @@ class EditExternalStorageShortcutDialogFragment : AppCompatDialogFragment() { private fun updateNamePlaceholder() { val rootId = binding.rootIdEdit.text.toString().takeIfNotEmpty() + ?: DocumentsContractCompat.EXTERNAL_STORAGE_PRIMARY_EMULATED_ROOT_ID val path = binding.pathEdit.text.toString().dropWhile { it == '/' } - binding.nameLayout.placeholderText = if (rootId != null) { - ExternalStorageUri(rootId, path).displayName - } else { - path - } + binding.nameLayout.placeholderText = ExternalStorageUri(rootId, path).displayName } private fun save() {