add default tls option

This commit is contained in:
Jonathan Fisher 2020-11-10 10:57:12 +00:00
parent 3e0c9e9728
commit 5c97768bec
1 changed files with 25 additions and 10 deletions

View File

@ -24,8 +24,12 @@ class SettingsFragment: PreferenceFragmentCompat(), Preference.OnPreferenceChang
tlsCategory.title = "TLS Config" tlsCategory.title = "TLS Config"
screen.addPreference(tlsCategory) screen.addPreference(tlsCategory)
//todo - need to add the default "TLS" option here, val tlsDefaultPreference = SwitchPreferenceCompat(context)
// otherwise once set in this screen user can't get that default back tlsDefaultPreference.key = "tls_Default"
tlsDefaultPreference.title = "TLS Default"
tlsDefaultPreference.onPreferenceChangeListener = this
tlsCategory.addPreference(tlsDefaultPreference)
val sslContext = SSLContext.getInstance("TLS") val sslContext = SSLContext.getInstance("TLS")
sslContext.init(null, null, SecureRandom()) sslContext.init(null, null, SecureRandom())
val factory: SSLSocketFactory = sslContext.socketFactory val factory: SSLSocketFactory = sslContext.socketFactory
@ -45,19 +49,30 @@ class SettingsFragment: PreferenceFragmentCompat(), Preference.OnPreferenceChang
override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean { override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean {
if(preference is SwitchPreferenceCompat && newValue is Boolean && newValue == true) { if(preference is SwitchPreferenceCompat && newValue is Boolean && newValue == true) {
preference.key?.let { key -> preference.key?.let { key ->
if (key.startsWith("tls_")) { when {
protocols.forEach { protocol -> key.startsWith("tls_") -> {
val tlsSwitchKey = "tls_${protocol.toLowerCase(Locale.getDefault())}" if(key != "tls_Default"){
if (tlsSwitchKey != key) { val default = preferenceScreen.findPreference<SwitchPreferenceCompat>("tls_Default")
val otherTLSSwitch = default?.isChecked = false
preferenceScreen.findPreference<SwitchPreferenceCompat>(tlsSwitchKey) }
otherTLSSwitch?.isChecked = false protocols.forEach { protocol ->
val tlsSwitchKey = "tls_${protocol.toLowerCase(Locale.getDefault())}"
if (tlsSwitchKey != key) {
val otherTLSSwitch = preferenceScreen.findPreference<SwitchPreferenceCompat>(tlsSwitchKey)
otherTLSSwitch?.isChecked = false
}
} }
} }
} }
} }
setTLSProtocol(preference.title.toString()) val prefTitle = preference.title.toString()
if(prefTitle.toLowerCase().contains("default")){
setTLSProtocol("TLS")
}else{
setTLSProtocol(prefTitle)
}
return true return true
} }