PluginPreference.kt 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /******************************************************************************
  2. * *
  3. * Copyright (C) 2021 by nekohasekai <[email protected]> *
  4. * Copyright (C) 2021 by Max Lv <[email protected]> *
  5. * Copyright (C) 2021 by Mygod Studio <[email protected]> *
  6. * *
  7. * This program is free software: you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation, either version 3 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  19. * *
  20. ******************************************************************************/
  21. package com.github.shadowsocks.preference
  22. import android.content.Context
  23. import android.graphics.drawable.Drawable
  24. import android.util.AttributeSet
  25. import androidx.preference.ListPreference
  26. import com.github.shadowsocks.plugin.PluginList
  27. import com.github.shadowsocks.plugin.PluginManager
  28. import io.nekohasekai.sagernet.R
  29. class PluginPreference(context: Context, attrs: AttributeSet? = null) : ListPreference(context, attrs) {
  30. companion object FallbackProvider : SummaryProvider<PluginPreference> {
  31. override fun provideSummary(preference: PluginPreference) =
  32. preference.selectedEntry?.label ?: preference.unknownValueSummary.format(preference.value)
  33. }
  34. lateinit var plugins: PluginList
  35. val selectedEntry get() = plugins.lookup[value]
  36. private val entryIcon: Drawable? get() = selectedEntry?.icon
  37. private val unknownValueSummary = context.getString(R.string.plugin_unknown)
  38. private var listener: OnPreferenceChangeListener? = null
  39. override fun getOnPreferenceChangeListener(): OnPreferenceChangeListener? = listener
  40. override fun setOnPreferenceChangeListener(listener: OnPreferenceChangeListener?) {
  41. this.listener = listener
  42. }
  43. init {
  44. super.setOnPreferenceChangeListener { preference, newValue ->
  45. val listener = listener
  46. if (listener == null || listener.onPreferenceChange(preference, newValue)) {
  47. value = newValue.toString()
  48. icon = entryIcon
  49. true
  50. } else false
  51. }
  52. }
  53. fun init() {
  54. plugins = PluginManager.fetchPlugins()
  55. entryValues = plugins.lookup.map { it.key }.toTypedArray()
  56. icon = entryIcon
  57. summaryProvider = FallbackProvider
  58. }
  59. override fun onSetInitialValue(defaultValue: Any?) {
  60. super.onSetInitialValue(defaultValue)
  61. init()
  62. }
  63. }