Cleanup issues

This commit is contained in:
tzugen 2021-10-30 15:59:51 +02:00
parent e5f7ca6310
commit 5fe1921ca5
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
2 changed files with 60 additions and 56 deletions

View File

@ -16,6 +16,8 @@ import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
import java.io.File
import kotlin.math.ceil
import org.koin.core.component.KoinComponent
import org.koin.java.KoinJavaComponent.get
import org.koin.java.KoinJavaComponent.inject
@ -40,7 +42,6 @@ import org.moire.ultrasonic.util.FileUtil.ultrasonicDirectory
import org.moire.ultrasonic.util.MediaSessionHandler
import org.moire.ultrasonic.util.PermissionUtil
import org.moire.ultrasonic.util.PermissionUtil.Companion.requestInitialPermission
import org.moire.ultrasonic.util.PermissionUtil.PermissionRequestFinishedCallback
import org.moire.ultrasonic.util.Settings
import org.moire.ultrasonic.util.Settings.preferences
import org.moire.ultrasonic.util.Settings.shareGreeting
@ -50,13 +51,14 @@ import org.moire.ultrasonic.util.TimeSpanPreference
import org.moire.ultrasonic.util.TimeSpanPreferenceDialogFragmentCompat
import org.moire.ultrasonic.util.Util.toast
import timber.log.Timber
import java.io.File
import kotlin.math.ceil
/**
* Shows main app settings.
*/
class SettingsFragment : PreferenceFragmentCompat(), OnSharedPreferenceChangeListener, KoinComponent {
class SettingsFragment :
PreferenceFragmentCompat(),
OnSharedPreferenceChangeListener,
KoinComponent {
private var theme: ListPreference? = null
private var maxBitrateWifi: ListPreference? = null
private var maxBitrateMobile: ListPreference? = null
@ -227,30 +229,29 @@ class SettingsFragment : PreferenceFragmentCompat(), OnSharedPreferenceChangeLis
cacheLocation!!.summary = Settings.cacheLocation
cacheLocation!!.onPreferenceClickListener =
Preference.OnPreferenceClickListener {
// If the user tries to change the cache location, we must first check to see if we have write access.
// If the user tries to change the cache location,
// we must first check to see if we have write access.
requestInitialPermission(
requireActivity(),
object : PermissionRequestFinishedCallback {
override fun onPermissionRequestFinished(hasPermission: Boolean) {
if (hasPermission) {
val filePickerDialog = createFilePickerDialog(
context!!
)
filePickerDialog.setDefaultDirectory(defaultMusicDirectory.path)
filePickerDialog.setInitialDirectory(cacheLocation!!.summary.toString())
filePickerDialog.setOnFileSelectedListener(object :
OnFileSelectedListener {
override fun onFileSelected(file: File?, path: String?) {
if (path != null) {
Settings.cacheLocation = path
setCacheLocation(path)
}
requireActivity()
) {
if (it) {
val filePickerDialog = createFilePickerDialog(
requireContext()
)
filePickerDialog.setDefaultDirectory(defaultMusicDirectory.path)
filePickerDialog.setInitialDirectory(cacheLocation!!.summary.toString())
filePickerDialog.setOnFileSelectedListener(object :
OnFileSelectedListener {
override fun onFileSelected(file: File?, path: String?) {
if (path != null) {
Settings.cacheLocation = path
setCacheLocation(path)
}
})
filePickerDialog.show()
}
}
})
}
})
filePickerDialog.show()
}
}
true
}
}
@ -308,9 +309,15 @@ class SettingsFragment : PreferenceFragmentCompat(), OnSharedPreferenceChangeLis
private fun bluetoothDevicePreferenceToString(preferenceValue: Int): String {
return when (preferenceValue) {
Constants.PREFERENCE_VALUE_ALL -> getString(R.string.settings_playback_bluetooth_all)
Constants.PREFERENCE_VALUE_A2DP -> getString(R.string.settings_playback_bluetooth_a2dp)
Constants.PREFERENCE_VALUE_DISABLED -> getString(R.string.settings_playback_bluetooth_disabled)
Constants.PREFERENCE_VALUE_ALL -> {
getString(R.string.settings_playback_bluetooth_all)
}
Constants.PREFERENCE_VALUE_A2DP -> {
getString(R.string.settings_playback_bluetooth_a2dp)
}
Constants.PREFERENCE_VALUE_DISABLED -> {
getString(R.string.settings_playback_bluetooth_disabled)
}
else -> ""
}
}
@ -415,12 +422,10 @@ class SettingsFragment : PreferenceFragmentCompat(), OnSharedPreferenceChangeLis
private fun setCacheLocation(path: String) {
val dir = File(path)
if (!ensureDirectoryExistsAndIsReadWritable(dir)) {
permissionUtil.value.handlePermissionFailed(object : PermissionRequestFinishedCallback {
override fun onPermissionRequestFinished(hasPermission: Boolean) {
val currentPath = Settings.cacheLocation
cacheLocation!!.summary = currentPath
}
})
permissionUtil.value.handlePermissionFailed {
val currentPath = Settings.cacheLocation
cacheLocation!!.summary = currentPath
}
} else {
cacheLocation!!.summary = path
}
@ -444,19 +449,22 @@ class SettingsFragment : PreferenceFragmentCompat(), OnSharedPreferenceChangeLis
ceil(fileSize / 1000 * 1000.0).toString(),
ultrasonicDirectory
)
val keep = R.string.settings_debug_log_keep
val delete = R.string.settings_debug_log_delete
AlertDialog.Builder(activity)
.setMessage(message)
.setIcon(android.R.drawable.ic_dialog_info)
.setNegativeButton(R.string.settings_debug_log_keep) { dialogInterface: DialogInterface, _: Int ->
dialogInterface.cancel()
.setNegativeButton(keep) { dIf: DialogInterface, _: Int ->
dIf.cancel()
}
.setPositiveButton(R.string.settings_debug_log_delete) { dialogInterface: DialogInterface, _: Int ->
.setPositiveButton(delete) { dIf: DialogInterface, _: Int ->
deleteLogFiles()
Timber.i("Deleted debug log files")
dialogInterface.dismiss()
AlertDialog.Builder(activity).setMessage(R.string.settings_debug_log_deleted)
.setPositiveButton(R.string.common_ok) { dialogInterface1: DialogInterface, _: Int ->
dialogInterface1.dismiss()
dIf.dismiss()
AlertDialog.Builder(activity)
.setMessage(R.string.settings_debug_log_deleted)
.setPositiveButton(R.string.common_ok) { dIf2: DialogInterface, _: Int ->
dIf2.dismiss()
}
.create().show()
}

View File

@ -22,10 +22,6 @@ import timber.log.Timber
class PermissionUtil(private val applicationContext: Context) {
private var activityContext: Context? = null
interface PermissionRequestFinishedCallback {
fun onPermissionRequestFinished(hasPermission: Boolean)
}
fun onForegroundApplicationStarted(context: Context?) {
activityContext = context
}
@ -42,7 +38,7 @@ class PermissionUtil(private val applicationContext: Context) {
*
* @param callback callback function to execute after the permission request is finished
*/
fun handlePermissionFailed(callback: PermissionRequestFinishedCallback?) {
fun handlePermissionFailed(callback: ((Boolean) -> Unit)?) {
val currentCachePath = Settings.cacheLocation
val defaultCachePath = defaultMusicDirectory.path
@ -76,7 +72,7 @@ class PermissionUtil(private val applicationContext: Context) {
)
}
}
callback?.onPermissionRequestFinished(false)
callback?.invoke(false)
}
}
@ -91,7 +87,7 @@ class PermissionUtil(private val applicationContext: Context) {
@JvmStatic
fun requestInitialPermission(
context: Context,
callback: PermissionRequestFinishedCallback?
callback: ((Boolean) -> Unit)?
) {
Dexter.withContext(context)
.withPermissions(
@ -102,7 +98,7 @@ class PermissionUtil(private val applicationContext: Context) {
override fun onPermissionsChecked(report: MultiplePermissionsReport) {
if (report.areAllPermissionsGranted()) {
Timber.i("R/W permission granted for external storage")
callback?.onPermissionRequestFinished(true)
callback?.invoke(true)
return
}
if (report.isAnyPermissionPermanentlyDenied) {
@ -110,7 +106,7 @@ class PermissionUtil(private val applicationContext: Context) {
"R/W permission is permanently denied for external storage"
)
showSettingsDialog(context)
callback?.onPermissionRequestFinished(false)
callback?.invoke(false)
return
}
Timber.i("R/W permission is missing for external storage")
@ -120,7 +116,7 @@ class PermissionUtil(private val applicationContext: Context) {
context.getString(R.string.permissions_rationale_description_initial),
null
)
callback?.onPermissionRequestFinished(false)
callback?.invoke(false)
}
override fun onPermissionRationaleShouldBeShown(
@ -146,7 +142,7 @@ class PermissionUtil(private val applicationContext: Context) {
private fun requestFailedPermission(
context: Context,
cacheLocation: String?,
callback: PermissionRequestFinishedCallback?
callback: ((Boolean) -> Unit)?
) {
Dexter.withContext(context)
.withPermissions(
@ -161,7 +157,7 @@ class PermissionUtil(private val applicationContext: Context) {
if (cacheLocation != null) {
Settings.cacheLocation = cacheLocation
}
callback?.onPermissionRequestFinished(true)
callback?.invoke(true)
return
}
if (report.isAnyPermissionPermanentlyDenied) {
@ -170,7 +166,7 @@ class PermissionUtil(private val applicationContext: Context) {
cacheLocation
)
showSettingsDialog(context)
callback?.onPermissionRequestFinished(false)
callback?.invoke(false)
return
}
Timber.i(
@ -182,7 +178,7 @@ class PermissionUtil(private val applicationContext: Context) {
context, context.getString(R.string.permissions_message_box_title),
context.getString(R.string.permissions_permission_missing), null
)
callback?.onPermissionRequestFinished(false)
callback?.invoke(false)
}
override fun onPermissionRationaleShouldBeShown(