mirror of
https://gitlab.com/ultrasonic/ultrasonic.git
synced 2025-06-07 02:53:05 +03:00
Merge branch 'prefs' into 'develop'
Modernize Activity launching to set custom cache location See merge request ultrasonic/ultrasonic!1015
This commit is contained in:
commit
0cfd8e8240
@ -1,14 +1,11 @@
|
|||||||
package org.moire.ultrasonic.fragment
|
package org.moire.ultrasonic.fragment
|
||||||
|
|
||||||
import android.app.Activity
|
|
||||||
import android.content.DialogInterface
|
import android.content.DialogInterface
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
|
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.provider.DocumentsContract
|
|
||||||
import android.provider.SearchRecentSuggestions
|
import android.provider.SearchRecentSuggestions
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.annotation.StringRes
|
import androidx.annotation.StringRes
|
||||||
@ -38,6 +35,7 @@ import org.moire.ultrasonic.util.Constants
|
|||||||
import org.moire.ultrasonic.util.ErrorDialog
|
import org.moire.ultrasonic.util.ErrorDialog
|
||||||
import org.moire.ultrasonic.util.FileUtil.ultrasonicDirectory
|
import org.moire.ultrasonic.util.FileUtil.ultrasonicDirectory
|
||||||
import org.moire.ultrasonic.util.InfoDialog
|
import org.moire.ultrasonic.util.InfoDialog
|
||||||
|
import org.moire.ultrasonic.util.SelectCacheActivityContract
|
||||||
import org.moire.ultrasonic.util.Settings
|
import org.moire.ultrasonic.util.Settings
|
||||||
import org.moire.ultrasonic.util.Settings.id3TagsEnabledOnline
|
import org.moire.ultrasonic.util.Settings.id3TagsEnabledOnline
|
||||||
import org.moire.ultrasonic.util.Settings.preferences
|
import org.moire.ultrasonic.util.Settings.preferences
|
||||||
@ -100,64 +98,14 @@ class SettingsFragment :
|
|||||||
updateCustomPreferences()
|
updateCustomPreferences()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This function will be called when we return from the file picker
|
|
||||||
* with a new custom cache location
|
|
||||||
*
|
|
||||||
* TODO: This method has been deprecated in favor of using the Activity Result API
|
|
||||||
* which brings increased type safety via an ActivityResultContract and the prebuilt
|
|
||||||
* contracts for common intents available in
|
|
||||||
* androidx.activity.result.contract.ActivityResultContracts,
|
|
||||||
* provides hooks for testing, and allow receiving results in separate,
|
|
||||||
* testable classes independent from your fragment.
|
|
||||||
* Use registerForActivityResult(ActivityResultContract, ActivityResultCallback) with the
|
|
||||||
* appropriate ActivityResultContract and handling the result in the callback.
|
|
||||||
*/
|
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
|
|
||||||
if (
|
|
||||||
requestCode == SELECT_CACHE_ACTIVITY &&
|
|
||||||
resultCode == Activity.RESULT_OK &&
|
|
||||||
resultData != null
|
|
||||||
) {
|
|
||||||
val read = (resultData.flags and Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0
|
|
||||||
val write = (resultData.flags and Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0
|
|
||||||
val persist = (resultData.flags and Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0
|
|
||||||
|
|
||||||
if (read && write && persist) {
|
|
||||||
if (resultData.data != null) {
|
|
||||||
// The result data contains a URI for the document or directory that
|
|
||||||
// the user selected.
|
|
||||||
val uri = resultData.data!!
|
|
||||||
val contentResolver = UApp.applicationContext().contentResolver
|
|
||||||
|
|
||||||
contentResolver.takePersistableUriPermission(uri, RW_FLAG)
|
|
||||||
setCacheLocation(uri.toString())
|
|
||||||
setupCacheLocationPreference()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ErrorDialog.Builder(requireContext())
|
|
||||||
.setMessage(R.string.settings_cache_location_error)
|
|
||||||
.show()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Settings.cacheLocationUri == "") {
|
|
||||||
Settings.customCacheLocation = false
|
|
||||||
customCacheLocation?.isChecked = false
|
|
||||||
setupCacheLocationPreference()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
val preferences = preferences
|
|
||||||
preferences.registerOnSharedPreferenceChangeListener(this)
|
preferences.registerOnSharedPreferenceChangeListener(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
super.onPause()
|
super.onPause()
|
||||||
val prefs = preferences
|
preferences.unregisterOnSharedPreferenceChangeListener(this)
|
||||||
prefs.unregisterOnSharedPreferenceChangeListener(this)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
|
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
|
||||||
@ -249,19 +197,31 @@ class SettingsFragment :
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun selectCacheLocation() {
|
private fun selectCacheLocation() {
|
||||||
// Choose a directory using the system's file picker.
|
// Start the activity to pick a directory using the system's file picker.
|
||||||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
|
selectCacheActivityContract.launch(Settings.cacheLocationUri)
|
||||||
|
|
||||||
if (Settings.cacheLocationUri != "" && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
||||||
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, Settings.cacheLocationUri)
|
|
||||||
}
|
|
||||||
|
|
||||||
intent.addFlags(RW_FLAG)
|
|
||||||
intent.addFlags(PERSISTABLE_FLAG)
|
|
||||||
|
|
||||||
startActivityForResult(intent, SELECT_CACHE_ACTIVITY)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Custom activity result contract
|
||||||
|
private val selectCacheActivityContract =
|
||||||
|
registerForActivityResult(SelectCacheActivityContract()) { uri ->
|
||||||
|
// parseResult will return the chosen path as an Uri
|
||||||
|
if (uri != null) {
|
||||||
|
val contentResolver = UApp.applicationContext().contentResolver
|
||||||
|
contentResolver.takePersistableUriPermission(uri, RW_FLAG)
|
||||||
|
setCacheLocation(uri.toString())
|
||||||
|
setupCacheLocationPreference()
|
||||||
|
} else {
|
||||||
|
ErrorDialog.Builder(requireContext())
|
||||||
|
.setMessage(R.string.settings_cache_location_error)
|
||||||
|
.show()
|
||||||
|
if (Settings.cacheLocationUri == "") {
|
||||||
|
Settings.customCacheLocation = false
|
||||||
|
customCacheLocation?.isChecked = false
|
||||||
|
setupCacheLocationPreference()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun setupBluetoothDevicePreferences() {
|
private fun setupBluetoothDevicePreferences() {
|
||||||
val resumeSetting = Settings.resumeOnBluetoothDevice
|
val resumeSetting = Settings.resumeOnBluetoothDevice
|
||||||
val pauseSetting = Settings.pauseOnBluetoothDevice
|
val pauseSetting = Settings.pauseOnBluetoothDevice
|
||||||
@ -425,7 +385,6 @@ class SettingsFragment :
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val SELECT_CACHE_ACTIVITY = 161161
|
|
||||||
const val RW_FLAG = Intent.FLAG_GRANT_READ_URI_PERMISSION or
|
const val RW_FLAG = Intent.FLAG_GRANT_READ_URI_PERMISSION or
|
||||||
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
||||||
const val PERSISTABLE_FLAG = Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
|
const val PERSISTABLE_FLAG = Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* SelectCacheActivityContract.kt
|
||||||
|
* Copyright (C) 2009-2023 Ultrasonic developers
|
||||||
|
*
|
||||||
|
* Distributed under terms of the GNU GPLv3 license.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.moire.ultrasonic.util
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
|
import android.os.Build
|
||||||
|
import android.provider.DocumentsContract
|
||||||
|
import androidx.activity.result.contract.ActivityResultContract
|
||||||
|
import org.moire.ultrasonic.fragment.SettingsFragment
|
||||||
|
|
||||||
|
class SelectCacheActivityContract : ActivityResultContract<String?, Uri?>() {
|
||||||
|
override fun createIntent(context: Context, input: String?): Intent {
|
||||||
|
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
|
||||||
|
if (Settings.cacheLocationUri != "" && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, input)
|
||||||
|
}
|
||||||
|
intent.addFlags(SettingsFragment.RW_FLAG)
|
||||||
|
intent.addFlags(SettingsFragment.PERSISTABLE_FLAG)
|
||||||
|
return intent
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
|
||||||
|
if (
|
||||||
|
resultCode == Activity.RESULT_OK &&
|
||||||
|
intent != null
|
||||||
|
) {
|
||||||
|
val read = (intent.flags and Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0
|
||||||
|
val write = (intent.flags and Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0
|
||||||
|
val persist = (intent.flags and Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0
|
||||||
|
|
||||||
|
if (read && write && persist) {
|
||||||
|
if (intent.data != null) {
|
||||||
|
// The result data contains a URI for the document or directory that
|
||||||
|
// the user selected.
|
||||||
|
return intent.data!!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
@ -48,7 +48,6 @@ object Settings {
|
|||||||
@JvmStatic
|
@JvmStatic
|
||||||
val preloadCount: Int
|
val preloadCount: Int
|
||||||
get() {
|
get() {
|
||||||
val preferences = preferences
|
|
||||||
val preloadCount =
|
val preloadCount =
|
||||||
preferences.getString(getKey(R.string.setting_key_preload_count), "-1")!!
|
preferences.getString(getKey(R.string.setting_key_preload_count), "-1")!!
|
||||||
.toInt()
|
.toInt()
|
||||||
@ -60,7 +59,6 @@ object Settings {
|
|||||||
@JvmStatic
|
@JvmStatic
|
||||||
val cacheSizeMB: Int
|
val cacheSizeMB: Int
|
||||||
get() {
|
get() {
|
||||||
val preferences = preferences
|
|
||||||
val cacheSize = preferences.getString(
|
val cacheSize = preferences.getString(
|
||||||
getKey(R.string.setting_key_cache_size),
|
getKey(R.string.setting_key_cache_size),
|
||||||
"-1"
|
"-1"
|
||||||
@ -209,7 +207,6 @@ object Settings {
|
|||||||
@JvmStatic
|
@JvmStatic
|
||||||
val shareGreeting: String?
|
val shareGreeting: String?
|
||||||
get() {
|
get() {
|
||||||
val preferences = preferences
|
|
||||||
val context = Util.appContext()
|
val context = Util.appContext()
|
||||||
val defaultVal = String.format(
|
val defaultVal = String.format(
|
||||||
context.resources.getString(R.string.share_default_greeting),
|
context.resources.getString(R.string.share_default_greeting),
|
||||||
@ -278,8 +275,7 @@ object Settings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun getAllKeys(): List<String> {
|
fun getAllKeys(): List<String> {
|
||||||
val prefs = PreferenceManager.getDefaultSharedPreferences(UApp.applicationContext())
|
return preferences.all.keys.toList()
|
||||||
return prefs.all.keys.toList()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private val appContext: Context
|
private val appContext: Context
|
||||||
|
@ -833,6 +833,7 @@ object Util {
|
|||||||
Timber.d("Current user preferences")
|
Timber.d("Current user preferences")
|
||||||
Timber.d("========================")
|
Timber.d("========================")
|
||||||
val keys = Settings.preferences.all
|
val keys = Settings.preferences.all
|
||||||
|
|
||||||
keys.forEach {
|
keys.forEach {
|
||||||
Timber.d("${it.key}: ${it.value}")
|
Timber.d("${it.key}: ${it.value}")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user