From b858284b1ea68c1a2065adab5ebc67c887155a5b Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Tue, 7 Apr 2020 02:28:15 -0600 Subject: KernelModuleDisablerPreference: do not make synchronous calls to getBackend Signed-off-by: Jason A. Donenfeld --- .../preference/KernelModuleDisablerPreference.kt | 25 +++++++++++++++------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'ui/src') diff --git a/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt b/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt index 8e834260..1479d7b6 100644 --- a/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt +++ b/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt @@ -18,11 +18,18 @@ import java9.util.concurrent.CompletableFuture import kotlin.system.exitProcess class KernelModuleDisablerPreference(context: Context, attrs: AttributeSet?) : Preference(context, attrs) { - private var state = if (Application.getBackend() is WgQuickBackend) State.ENABLED else State.DISABLED + private var state = State.UNKNOWN - override fun getSummary() = context.getString(state.summaryResourceId) + init { + isVisible = false + Application.getBackendAsync().thenAccept { backend -> + setState(if (backend is WgQuickBackend) State.ENABLED else State.DISABLED) + } + } + + override fun getSummary() = if (state == State.UNKNOWN) "" else context.getString(state.summaryResourceId) - override fun getTitle() = context.getString(state.titleResourceId) + override fun getTitle() = if (state == State.UNKNOWN) "" else context.getString(state.titleResourceId) @SuppressLint("ApplySharedPref") override fun onClick() { @@ -51,13 +58,15 @@ class KernelModuleDisablerPreference(context: Context, attrs: AttributeSet?) : P if (this.state == state) return this.state = state if (isEnabled != state.shouldEnableView) isEnabled = state.shouldEnableView + if (isVisible != state.visible) isVisible = state.visible notifyChanged() } - private enum class State(val titleResourceId: Int, val summaryResourceId: Int, val shouldEnableView: Boolean) { - ENABLED(R.string.module_disabler_enabled_title, R.string.module_disabler_enabled_summary, true), - DISABLED(R.string.module_disabler_disabled_title, R.string.module_disabler_disabled_summary, true), - ENABLING(R.string.module_disabler_disabled_title, R.string.success_application_will_restart, false), - DISABLING(R.string.module_disabler_enabled_title, R.string.success_application_will_restart, false); + private enum class State(val titleResourceId: Int, val summaryResourceId: Int, val shouldEnableView: Boolean, val visible: Boolean) { + UNKNOWN(0, 0, false, false), + ENABLED(R.string.module_disabler_enabled_title, R.string.module_disabler_enabled_summary, true, true), + DISABLED(R.string.module_disabler_disabled_title, R.string.module_disabler_disabled_summary, true, true), + ENABLING(R.string.module_disabler_disabled_title, R.string.success_application_will_restart, false, true), + DISABLING(R.string.module_disabler_enabled_title, R.string.success_application_will_restart, false, true); } } -- cgit v1.2.3