From f0cda280b746c098f6d2e7ef5bb6c8279ee5a936 Mon Sep 17 00:00:00 2001 From: Andrew Gunnerson Date: Sun, 5 Jun 2022 22:05:14 -0400 Subject: [PATCH] SettingsActivity: Show user-friendly path for output directory Signed-off-by: Andrew Gunnerson --- .../java/com/chiller3/bcr/SettingsActivity.kt | 23 +++++++++++++++++-- .../com/chiller3/bcr/format/SampleRates.kt | 1 - 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/chiller3/bcr/SettingsActivity.kt b/app/src/main/java/com/chiller3/bcr/SettingsActivity.kt index 282e9f181..73dcdbd06 100644 --- a/app/src/main/java/com/chiller3/bcr/SettingsActivity.kt +++ b/app/src/main/java/com/chiller3/bcr/SettingsActivity.kt @@ -1,9 +1,11 @@ package com.chiller3.bcr +import android.content.ContentResolver import android.content.Intent import android.content.SharedPreferences import android.net.Uri import android.os.Bundle +import android.provider.DocumentsContract import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.preference.Preference @@ -105,9 +107,26 @@ class SettingsActivity : AppCompatActivity() { } private fun refreshOutputDir() { - val outputDir = Preferences.getOutputDir(requireContext()) + val context = requireContext() + val outputDirUri = Preferences.getOutputDir(context) + val outputDirFormatted = when { + outputDirUri.scheme == ContentResolver.SCHEME_FILE -> outputDirUri.path + outputDirUri.scheme == ContentResolver.SCHEME_CONTENT + && outputDirUri.authority == "com.android.externalstorage.documents" -> { + val treeDocumentId = DocumentsContract.getTreeDocumentId(outputDirUri) + val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree( + outputDirUri, treeDocumentId) + + DocumentsContract.findDocumentPath(context.contentResolver, childrenUri) + ?.path + ?.joinToString("/") + ?: outputDirUri.toString() + } + else -> outputDirUri.toString() + } + val summary = getString(R.string.pref_output_dir_desc) - prefOutputDir.summary = "${summary}\n\n${outputDir}" + prefOutputDir.summary = "${summary}\n\n${outputDirFormatted}" } private fun refreshOutputFormat() { diff --git a/app/src/main/java/com/chiller3/bcr/format/SampleRates.kt b/app/src/main/java/com/chiller3/bcr/format/SampleRates.kt index ab6dd4d59..0c71ae155 100644 --- a/app/src/main/java/com/chiller3/bcr/format/SampleRates.kt +++ b/app/src/main/java/com/chiller3/bcr/format/SampleRates.kt @@ -5,7 +5,6 @@ package com.chiller3.bcr.format import android.content.Context import com.chiller3.bcr.Preferences -import com.chiller3.bcr.R object SampleRates { /**