Skip to content
Snippets Groups Projects
Commit fd4f48c6 authored by Adrien Béraud's avatar Adrien Béraud Committed by Maxime Callet
Browse files

tv: cleanup about fragment, handle null state

Change-Id: Idc48663ab3e6cb5f5453eebcda5234caa053f067
parent 07912f11
No related branches found
No related tags found
No related merge requests found
...@@ -27,54 +27,25 @@ import androidx.leanback.preference.LeanbackPreferenceFragmentCompat ...@@ -27,54 +27,25 @@ import androidx.leanback.preference.LeanbackPreferenceFragmentCompat
import androidx.preference.Preference import androidx.preference.Preference
import cx.ring.BuildConfig import cx.ring.BuildConfig
import cx.ring.R import cx.ring.R
import net.jami.model.ConfigKey
class TVAboutFragment : LeanbackPreferenceFragmentCompat() { class TVAboutFragment : LeanbackPreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle, rootKey: String) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.tv_about_pref, rootKey) setPreferencesFromResource(R.xml.tv_about_pref, rootKey)
findPreference<Preference>("About.version")?.title = version findPreference<Preference>("About.version")?.title = getString(R.string.app_release, BuildConfig.VERSION_NAME)
findPreference<Preference>("About.license")?.title = license findPreference<Preference>("About.license")?.title = getString(R.string.license)
findPreference<Preference>("About.rights")?.title = rights findPreference<Preference>("About.rights")?.title = getString(R.string.copyright)
findPreference<Preference>("About.credits")?.title = credits findPreference<Preference>("About.credits")?.title = credits
} }
override fun onPreferenceTreeClick(preference: Preference): Boolean {
if (preference.key == ConfigKey.ACCOUNT_AUTOANSWER.key()) {
} else if (preference.key == ConfigKey.ACCOUNT_ISRENDEZVOUS.key()) {
}
return super.onPreferenceTreeClick(preference)
}
private val version: CharSequence
get() {
val version = SpannableString(requireContext().resources.getString(R.string.version_section))
version.setSpan(UnderlineSpan(), 0, version.length, 0)
return requireContext().resources.getString(R.string.app_release, BuildConfig.VERSION_NAME)
}
private val license: CharSequence
get() {
val licence = SpannableString(requireContext().resources.getString(R.string.section_license))
licence.setSpan(UnderlineSpan(), 0, licence.length, 0)
return requireContext().resources.getString(R.string.license)
}
private val rights: CharSequence
get() {
val licence = SpannableString(requireContext().resources.getString(R.string.copyright_section))
licence.setSpan(UnderlineSpan(), 0, licence.length, 0)
return requireContext().resources.getString(R.string.copyright)
}
private val credits: CharSequence private val credits: CharSequence
get() { get() {
val developedby = SpannableString(requireContext().resources.getString(R.string.developed_by)) val developedby = SpannableString(getText(R.string.developed_by))
developedby.setSpan(UnderlineSpan(), 0, developedby.length, 0) developedby.setSpan(UnderlineSpan(), 0, developedby.length, 0)
val developed: CharSequence = val developed = getString(R.string.credits_developer).replace("\n", "<br/>")
requireContext().resources.getString(R.string.credits_developer).replace("\n".toRegex(), "<br/>")
return Html.fromHtml("<b><u>$developedby</u></b><br/>$developed") return Html.fromHtml("<b><u>$developedby</u></b><br/>$developed")
} }
companion object { companion object {
fun newInstance(): TVAboutFragment { fun newInstance() = TVAboutFragment()
return TVAboutFragment()
}
} }
} }
\ No newline at end of file
...@@ -43,9 +43,10 @@ class TVSettingsFragment : LeanbackSettingsFragmentCompat() { ...@@ -43,9 +43,10 @@ class TVSettingsFragment : LeanbackSettingsFragmentCompat() {
override fun onPreferenceStartFragment(preferenceFragment: PreferenceFragmentCompat, preference: Preference): Boolean { override fun onPreferenceStartFragment(preferenceFragment: PreferenceFragmentCompat, preference: Preference): Boolean {
val args = preference.extras val args = preference.extras
val f = childFragmentManager.fragmentFactory.instantiate(requireContext().classLoader, preference.fragment) val f = childFragmentManager.fragmentFactory.instantiate(requireContext().classLoader, preference.fragment).apply {
f.arguments = args arguments = args
f.setTargetFragment(preferenceFragment, 0) setTargetFragment(preferenceFragment, 0)
}
if (f is PreferenceFragmentCompat if (f is PreferenceFragmentCompat
|| f is PreferenceDialogFragmentCompat || f is PreferenceDialogFragmentCompat
) { ) {
...@@ -57,18 +58,16 @@ class TVSettingsFragment : LeanbackSettingsFragmentCompat() { ...@@ -57,18 +58,16 @@ class TVSettingsFragment : LeanbackSettingsFragmentCompat() {
} }
override fun onPreferenceStartScreen(caller: PreferenceFragmentCompat, pref: PreferenceScreen): Boolean { override fun onPreferenceStartScreen(caller: PreferenceFragmentCompat, pref: PreferenceScreen): Boolean {
val prefsFragment: Fragment = PrefsFragment.newInstance() startPreferenceFragment(PrefsFragment.newInstance().apply {
val args = Bundle() arguments = Bundle().apply {
args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, pref.key) putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, pref.key)
prefsFragment.arguments = args }
startPreferenceFragment(prefsFragment) })
return true return true
} }
@AndroidEntryPoint @AndroidEntryPoint
class PrefsFragment : JamiPreferenceFragment<GeneralAccountPresenter>(), GeneralAccountView { class PrefsFragment : JamiPreferenceFragment<GeneralAccountPresenter>(), GeneralAccountView {
private var autoAnswer = false
private var rendezvousMode = false
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
presenter.init() presenter.init()
...@@ -77,13 +76,8 @@ class TVSettingsFragment : LeanbackSettingsFragmentCompat() { ...@@ -77,13 +76,8 @@ class TVSettingsFragment : LeanbackSettingsFragmentCompat() {
override fun addJamiPreferences(accountId: String) {} override fun addJamiPreferences(accountId: String) {}
override fun addSipPreferences() {} override fun addSipPreferences() {}
override fun accountChanged(account: Account) { override fun accountChanged(account: Account) {
// load information from account to ui findPreference<SwitchPreference>(ConfigKey.ACCOUNT_AUTOANSWER.key())?.isChecked = account.config.getBool(ConfigKey.ACCOUNT_AUTOANSWER)
autoAnswer = account.config.getBool(ConfigKey.ACCOUNT_AUTOANSWER) findPreference<SwitchPreference>(ConfigKey.ACCOUNT_ISRENDEZVOUS.key())?.isChecked = account.config.getBool(ConfigKey.ACCOUNT_ISRENDEZVOUS)
rendezvousMode = account.config.getBool(ConfigKey.ACCOUNT_ISRENDEZVOUS)
val pref = findPreference<SwitchPreference>(ConfigKey.ACCOUNT_AUTOANSWER.key())
if (pref != null) pref.isChecked = autoAnswer
val prefRdv = findPreference<SwitchPreference>(ConfigKey.ACCOUNT_ISRENDEZVOUS.key())
if (prefRdv != null) prefRdv.isChecked = rendezvousMode
} }
override fun finish() { override fun finish() {
...@@ -94,17 +88,17 @@ class TVSettingsFragment : LeanbackSettingsFragmentCompat() { ...@@ -94,17 +88,17 @@ class TVSettingsFragment : LeanbackSettingsFragmentCompat() {
val videoResolutionsNames = resources.getStringArray(R.array.video_resolutionStrings) val videoResolutionsNames = resources.getStringArray(R.array.video_resolutionStrings)
val videoResolutionsValues = val videoResolutionsValues =
filterResolutions(resources.getStringArray(R.array.video_resolutions), currentResolution, maxResolution) filterResolutions(resources.getStringArray(R.array.video_resolutions), currentResolution, maxResolution)
val lpVideoResolution = findPreference<ListPreference>(SharedPreferencesServiceImpl.PREF_RESOLUTION) findPreference<ListPreference>(SharedPreferencesServiceImpl.PREF_RESOLUTION)?.apply {
if (lpVideoResolution != null) { entries = videoResolutionsNames.copyOfRange(0, videoResolutionsValues.size)
lpVideoResolution.entries = videoResolutionsNames.copyOfRange(0, videoResolutionsValues.size) entryValues = videoResolutionsValues
lpVideoResolution.entryValues = videoResolutionsValues
} }
} }
override fun onCreatePreferences(bundle: Bundle?, rootKey: String?) { override fun onCreatePreferences(bundle: Bundle?, rootKey: String?) {
val pm = preferenceManager preferenceManager?.apply {
pm.sharedPreferencesMode = Context.MODE_PRIVATE sharedPreferencesMode = Context.MODE_PRIVATE
pm.sharedPreferencesName = SharedPreferencesServiceImpl.PREFS_VIDEO sharedPreferencesName = SharedPreferencesServiceImpl.PREFS_VIDEO
}
setPreferencesFromResource(R.xml.tv_account_general_pref, rootKey) setPreferencesFromResource(R.xml.tv_account_general_pref, rootKey)
} }
...@@ -117,30 +111,21 @@ class TVSettingsFragment : LeanbackSettingsFragmentCompat() { ...@@ -117,30 +111,21 @@ class TVSettingsFragment : LeanbackSettingsFragmentCompat() {
if (currentResolution > maxResolution.second) return videoResolutionsValues if (currentResolution > maxResolution.second) return videoResolutionsValues
val resolutions = ArrayList<String>() val resolutions = ArrayList<String>()
for (videoResolutionsValue in videoResolutionsValues) { for (videoResolutionsValue in videoResolutionsValues) {
val resolutionValueInt = videoResolutionsValue.toInt() if (videoResolutionsValue.toInt() <= maxResolution.second)
if (resolutionValueInt <= maxResolution.second) {
resolutions.add(videoResolutionsValue) resolutions.add(videoResolutionsValue)
}
} }
return resolutions.toTypedArray() return resolutions.toTypedArray()
} }
override fun onPreferenceTreeClick(preference: Preference): Boolean { override fun onPreferenceTreeClick(preference: Preference): Boolean {
if (preference.key == "Account.about") { val key = ConfigKey.fromString(preference.key)
} else if (preference.key == ConfigKey.ACCOUNT_AUTOANSWER.key()) { if (key != null && key.isTwoState)
presenter.twoStatePreferenceChanged(ConfigKey.ACCOUNT_AUTOANSWER, !autoAnswer) presenter.twoStatePreferenceChanged(key, (preference as SwitchPreference).isChecked)
autoAnswer = !autoAnswer
} else if (preference.key == ConfigKey.ACCOUNT_ISRENDEZVOUS.key()) {
presenter.twoStatePreferenceChanged(ConfigKey.ACCOUNT_ISRENDEZVOUS, !rendezvousMode)
rendezvousMode = !rendezvousMode
}
return super.onPreferenceTreeClick(preference) return super.onPreferenceTreeClick(preference)
} }
companion object { companion object {
fun newInstance(): PrefsFragment { fun newInstance(): PrefsFragment = PrefsFragment()
return PrefsFragment()
}
} }
} }
} }
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/about"> android:title="@string/about"
android:persistent="false">
<PreferenceCategory android:title="@string/tv_about_version"> <PreferenceCategory android:title="@string/tv_about_version">
<Preference <Preference
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment