Create Gong preference

This commit is contained in:
2020-05-31 14:01:52 +02:00
parent bba4e1ca31
commit e9c3bd4016
4 changed files with 54 additions and 2 deletions

View File

@@ -7,6 +7,8 @@ object PreferenceKey {
const val ENABLE_SAY_ENDPOINT = "preference_enable_say_endpoint"
const val ENABLE_WAVE_ENDPOINT = "preference_enable_wave_endpoint"
const val ENABLE_SONOS_ENDPOINT = "preference_enable_sonos_endpoint"
const val ENABLE_GONG = "preference_enable_gong"
const val GONG = "preference_gong"
const val TTS = "preference_tts"
const val INVALIDATE_SONOS_CACHE = "preference_invalidate_sonos_cache"
}

View File

@@ -15,12 +15,14 @@ import com.bartlomiejpluta.R
import com.bartlomiejpluta.ttsserver.service.foreground.ForegroundService
import com.bartlomiejpluta.ttsserver.service.state.ServiceState
class PreferencesFragment : PreferenceFragmentCompat() {
private lateinit var portPreference: IntEditTextPreference
private lateinit var sayEndpointPreference: SwitchPreference
private lateinit var waveEndpointPreference: SwitchPreference
private lateinit var sonosEndpointPreference: SwitchPreference
private lateinit var httpDebugPreference: SwitchPreference
private lateinit var enableGongPreference: SwitchPreference
private lateinit var ttsEnginePreference: Preference
private lateinit var clearSonosCachePreference: Preference
@@ -35,6 +37,7 @@ class PreferencesFragment : PreferenceFragmentCompat() {
private fun updateViewAccordingToServiceState(state: ServiceState) {
portPreference.isEnabled = state == ServiceState.STOPPED
sonosEndpointPreference.isEnabled = state == ServiceState.STOPPED
enableGongPreference.isEnabled = state == ServiceState.STOPPED
}
override fun onResume() {
@@ -60,6 +63,12 @@ class PreferencesFragment : PreferenceFragmentCompat() {
sayEndpointPreference = findPreference(PreferenceKey.ENABLE_SAY_ENDPOINT)!!
waveEndpointPreference = findPreference(PreferenceKey.ENABLE_WAVE_ENDPOINT)!!
sonosEndpointPreference = findPreference(PreferenceKey.ENABLE_SONOS_ENDPOINT)!!
enableGongPreference = findPreference(PreferenceKey.ENABLE_GONG)!!
enableGongPreference.setOnPreferenceClickListener { preference ->
openFilePicker(preference)
true
}
ttsEnginePreference = findPreference(PreferenceKey.TTS)!!
ttsEnginePreference.setOnPreferenceClickListener {
startActivity(Intent(ANDROID_TTS_SETTINGS))
@@ -67,14 +76,46 @@ class PreferencesFragment : PreferenceFragmentCompat() {
}
clearSonosCachePreference = findPreference(PreferenceKey.INVALIDATE_SONOS_CACHE)!!
clearSonosCachePreference.setOnPreferenceClickListener {
context?.cacheDir?.listFiles() ?. forEach { it.delete() }
Toast.makeText(context, getString(R.string.preference_invalidate_sonos_cache_toast), Toast.LENGTH_SHORT).show()
context?.cacheDir?.listFiles()?.forEach { it.delete() }
Toast.makeText(
context,
getString(R.string.preference_invalidate_sonos_cache_toast),
Toast.LENGTH_SHORT
).show()
true
}
updateViewAccordingToServiceState(ForegroundService.state)
}
private fun openFilePicker(preference: Preference?) {
if ((preference as SwitchPreference).isChecked) {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
.apply { type = "audio/x-wav" }
.let { Intent.createChooser(it, getString(R.string.preference_gong_picker_prompt)) }
startActivityForResult(intent, PICKFILE_RESULT_CODE)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
PICKFILE_RESULT_CODE -> updateGongPathPreference(resultCode, data)
}
}
private fun updateGongPathPreference(resultCode: Int, data: Intent?) {
enableGongPreference.isChecked = resultCode == -1
if (resultCode == -1) {
enableGongPreference.sharedPreferences?.edit()?.let { editor ->
editor.putString(PreferenceKey.GONG, data?.data?.toString())
editor.commit()
}
}
}
companion object {
private const val ANDROID_TTS_SETTINGS = "com.android.settings.TTS_SETTINGS"
private const val PICKFILE_RESULT_CODE = 1
}
}

View File

@@ -25,6 +25,9 @@
<string name="preference_enable_sonos_endpoint_title">Enable /sonos endpoint</string>
<string name="preference_http_debug_summary">Attach the stacktrace to each error HTTP response if available</string>
<string name="preference_http_debug_title">Enable HTTP debugging</string>
<string name="preference_gong_picker_prompt">Choose a gong file</string>
<string name="preference_enable_gong_summary">Play a signal gong before the announcement</string>
<string name="preference_enable_gong_title">Enable gong</string>
<string name="preference_tts_summary">Go to platform\'s TTS engine settings and adjust its parameters</string>
<string name="preference_tts_title">TTS engine settings</string>
<string name="preference_invalidate_sonos_cache_summary">Clear application cache directory from already generated Sonos TTS data for given sentences. You typically want to do it when you change the TTS engine\'s voice settings.</string>

View File

@@ -41,6 +41,12 @@
</PreferenceCategory>
<PreferenceCategory android:title="@string/preference_category_tts" app:iconSpaceReserved="false">
<SwitchPreference
android:defaultValue="false"
android:key="preference_enable_gong"
android:summary="@string/preference_enable_gong_summary"
android:title="@string/preference_enable_gong_title"
app:iconSpaceReserved="false" />
<Preference
android:key="preference_tts"
android:summary="@string/preference_tts_summary"