Skip to content

Commit

Permalink
#8 save changed sound in SharedPreferences
Browse files Browse the repository at this point in the history
  • Loading branch information
arburk committed Jul 8, 2023
1 parent b3ce882 commit 2fbd041
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,24 @@ class AppSettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPref
class SettingsFragment : PreferenceFragmentCompat() {

private val soundSelectionEnabler = SoundSelectionEnabler()
private var ringtoneUriNextLevel: Uri? = null
private var ringtoneUriWarning: Uri? = null

private val nextLevelPicker =
registerForActivityResult(PickRingtone().apply { selectedRingtone = ringtoneUriNextLevel }) { uri: Uri? ->
registerForActivityResult(PickRingtoneContract().apply {
sharedPrefKeyRingtone = pref_key_sound_next_round
}) { uri: Uri? ->
if (uri != null) {
ringtoneUriNextLevel = uri
Log.v("AppSettingsActivity", "set nextLevel sound to $ringtoneUriNextLevel")
applySoundSelection(pref_key_sound_next_round, uri)
Log.v("AppSettingsActivity", "set nextLevel sound to $uri")
}
}

private val warningPicker =
registerForActivityResult(PickRingtone().apply { selectedRingtone = ringtoneUriWarning }) { uri: Uri? ->
registerForActivityResult(PickRingtoneContract().apply {
sharedPrefKeyRingtone = pref_key_sound_warning_of_next_round
}) { uri: Uri? ->
if (uri != null) {
ringtoneUriWarning = uri
Log.v("AppSettingsActivity", "set warning sound to $ringtoneUriWarning")
applySoundSelection(pref_key_sound_warning_of_next_round, uri)
Log.v("AppSettingsActivity", "set warning sound to $uri")
}
}

Expand All @@ -83,7 +85,12 @@ class AppSettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPref

findPreference<NotificationPreference>(pref_key_notify_settings)?.isVisible =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
}

private fun applySoundSelection(prefName: String, uri: Uri) {
findPreference<NotificationSoundSelector>(prefName)?.apply {
updateRingtone(uri)
}
}

inner class SoundSelectionEnabler : OnPreferenceChangeListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,54 @@ package com.github.arburk.vscp.app.settings

import android.content.Context
import android.media.RingtoneManager
import android.net.Uri
import android.util.AttributeSet
import android.util.Log
import androidx.activity.result.ActivityResultLauncher
import androidx.preference.Preference
import androidx.preference.PreferenceManager


class NotificationSoundSelector(context: Context, attrs: AttributeSet?) : Preference(context, attrs),
Preference.OnPreferenceClickListener {
Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener {

private var _ringtone: Uri? = null
val ringtone: Uri? get() = _ringtone

var ringtoneSelectorLauncher: ActivityResultLauncher<Int>? = null

init {
Log.v("NotificationSoundSelector", "configure $key")
onPreferenceClickListener = this

// TODO add a PreferenceManagerClass handling those things

PreferenceManager.getDefaultSharedPreferences(context)
.getString(key, _ringtone?.toString())?.also {
if (it.isNotBlank()) {
_ringtone = Uri.parse(it)
Log.v("NotificationSoundSelector", "initialized to $_ringtone")
}
}
}

fun updateRingtone(uri: Uri) {
Log.v("NotificationSoundSelector","update $key to $uri")
_ringtone = uri
PreferenceManager.getDefaultSharedPreferences(context).edit().apply {
putString(key, uri.toString())
apply()
}
}

override fun onPreferenceClick(preference: Preference): Boolean {
ringtoneSelectorLauncher?.launch(RingtoneManager.TYPE_NOTIFICATION)
return true
}

override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
Log.v("NotificationSoundSelector", "onPreferenceChange: $newValue")
return true
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.github.arburk.vscp.app.settings

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.util.Log
import androidx.activity.result.contract.ActivityResultContract
import androidx.preference.PreferenceManager

class PickRingtoneContract : ActivityResultContract<Int, Uri?>() {

var sharedPrefKeyRingtone : String? = null

override fun createIntent(context: Context, input: Int) =
Intent(RingtoneManager.ACTION_RINGTONE_PICKER).apply {
putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, input)
val selectedRingtone = PreferenceManager.getDefaultSharedPreferences(context).getString(sharedPrefKeyRingtone, null)
if (!selectedRingtone.isNullOrBlank()) {
Log.v("PickRingtoneContract", "initialize to $selectedRingtone")
putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(selectedRingtone))
} else {
Log.v("PickRingtoneContract", "initialize to default")
putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true)
putExtra(
RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI,
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
)
}
}

override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
if (resultCode != Activity.RESULT_OK) {
return null
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
return intent?.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI, Uri::class.java)
}

return intent?.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI)
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@
<com.github.arburk.vscp.app.settings.NotificationSoundSelector
app:key="sound_next_round"
app:title="@string/sound_next_round"
app:persistent="true"
app:summary="Select a sound played when next round starts"/>

<com.github.arburk.vscp.app.settings.NotificationSoundSelector
app:key="sound_warning_of_next_round"
app:title="@string/sound_warning"
app:persistent="true"
app:summary="Select a sound played as warning for next round"/>

</PreferenceCategory>
Expand Down

0 comments on commit 2fbd041

Please sign in to comment.