diff options
author | Samuel Holland <samuel@sholland.org> | 2018-01-08 00:34:55 -0600 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2018-01-08 00:34:55 -0600 |
commit | 08cca563882ff7f743789a99f91afa2e9dbac23d (patch) | |
tree | b448da612fb98151a2fa03a086b7ed065c3f8d74 /app/src/main/java/com/wireguard/android/preference/ToolsInstallerPreference.java | |
parent | 1f30e133d6995689a89add2d0f87102cadf6938b (diff) |
ToolsInstaller: Extract to its own classes
Signed-off-by: Samuel Holland <samuel@sholland.org>
Diffstat (limited to 'app/src/main/java/com/wireguard/android/preference/ToolsInstallerPreference.java')
-rw-r--r-- | app/src/main/java/com/wireguard/android/preference/ToolsInstallerPreference.java | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/app/src/main/java/com/wireguard/android/preference/ToolsInstallerPreference.java b/app/src/main/java/com/wireguard/android/preference/ToolsInstallerPreference.java new file mode 100644 index 00000000..c9dfa7b7 --- /dev/null +++ b/app/src/main/java/com/wireguard/android/preference/ToolsInstallerPreference.java @@ -0,0 +1,91 @@ +package com.wireguard.android.preference; + +import android.content.Context; +import android.preference.Preference; +import android.system.OsConstants; +import android.util.AttributeSet; + +import com.wireguard.android.Application; +import com.wireguard.android.Application.ApplicationComponent; +import com.wireguard.android.R; +import com.wireguard.android.util.AsyncWorker; +import com.wireguard.android.util.ToolsInstaller; + +/** + * Preference implementing a button that asynchronously runs {@code ToolsInstaller} and displays the + * result as the preference summary. + */ + +public class ToolsInstallerPreference extends Preference { + private final AsyncWorker asyncWorker; + private final ToolsInstaller toolsInstaller; + private State state = State.INITIAL; + + public ToolsInstallerPreference(final Context context, final AttributeSet attrs) { + super(context, attrs); + final ApplicationComponent applicationComponent = Application.getComponent(); + asyncWorker = applicationComponent.getAsyncWorker(); + toolsInstaller = applicationComponent.getToolsInstaller(); + } + + public ToolsInstallerPreference(final Context context) { + this(context, null); + } + + private static State mapResultToState(final int scriptResult) { + if (scriptResult == OsConstants.EXIT_SUCCESS) + return State.SUCCESS; + else if (scriptResult == OsConstants.EALREADY) + return State.ALREADY; + else + return State.FAILURE; + } + + @Override + public CharSequence getSummary() { + return getContext().getString(state.messageResourceId); + } + + @Override + public CharSequence getTitle() { + return getContext().getString(getTitleRes()); + } + + @Override + public int getTitleRes() { + return R.string.tools_installer_title; + } + + @Override + protected void onClick() { + setState(State.WORKING); + asyncWorker.supplyAsync(toolsInstaller::install) + .thenApply(ToolsInstallerPreference::mapResultToState) + .thenAccept(this::setState); + } + + private void setState(final State state) { + if (this.state == state) + return; + this.state = state; + if (isEnabled() != state.shouldEnableView) + setEnabled(state.shouldEnableView); + notifyChanged(); + } + + private enum State { + ALREADY(R.string.tools_installer_already, false), + FAILURE(R.string.tools_installer_failure, true), + INITIAL(R.string.tools_installer_initial, true), + SUCCESS(R.string.tools_installer_success, false), + WORKING(R.string.tools_installer_working, false); + + private final int messageResourceId; + private final boolean shouldEnableView; + + State(final int messageResourceId, final boolean shouldEnableView) { + this.messageResourceId = messageResourceId; + this.shouldEnableView = shouldEnableView; + } + } +} |