From b6ed17884fc550361e793fab45cd08fce49e6ce5 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Mon, 8 Jan 2018 03:38:44 -0600 Subject: ToggleSwitch: Improve reliability It's not great, but it's better than it was. Signed-off-by: Samuel Holland --- .../com/wireguard/android/model/TunnelManager.java | 7 +++--- .../com/wireguard/android/widget/ToggleSwitch.java | 25 +++++----------------- 2 files changed, 9 insertions(+), 23 deletions(-) (limited to 'app/src/main/java/com') diff --git a/app/src/main/java/com/wireguard/android/model/TunnelManager.java b/app/src/main/java/com/wireguard/android/model/TunnelManager.java index feb60124..816d6708 100644 --- a/app/src/main/java/com/wireguard/android/model/TunnelManager.java +++ b/app/src/main/java/com/wireguard/android/model/TunnelManager.java @@ -237,9 +237,10 @@ public final class TunnelManager extends BaseObservable { CompletionStage setTunnelState(final Tunnel tunnel, final State state) { final CompletionStage completion = asyncWorker.supplyAsync(() -> backend.setState(tunnel, state)); - completion.thenAccept(tunnel::onStateChanged); - completion.thenAccept(newState -> { - if (newState == State.UP) + completion.whenComplete((newState, e) -> { + // Ensure onStateChanged is always called (failure or not), and with the correct state. + tunnel.onStateChanged(e == null ? newState : tunnel.getState()); + if (e == null && newState == State.UP) setLastUsedTunnel(tunnel); }); return completion; diff --git a/app/src/main/java/com/wireguard/android/widget/ToggleSwitch.java b/app/src/main/java/com/wireguard/android/widget/ToggleSwitch.java index 1b3dee71..a8e4e263 100644 --- a/app/src/main/java/com/wireguard/android/widget/ToggleSwitch.java +++ b/app/src/main/java/com/wireguard/android/widget/ToggleSwitch.java @@ -22,25 +22,15 @@ import android.util.AttributeSet; import android.widget.Switch; public class ToggleSwitch extends Switch { - private boolean hasPendingStateChange; private boolean isRestoringState; private OnBeforeCheckedChangeListener listener; - public ToggleSwitch(final Context context) { - super(context); - } - public ToggleSwitch(final Context context, final AttributeSet attrs) { super(context, attrs); } - public ToggleSwitch(final Context context, final AttributeSet attrs, final int defStyleAttr) { - super(context, attrs, defStyleAttr); - } - - public ToggleSwitch(final Context context, final AttributeSet attrs, final int defStyleAttr, - final int defStyleRes) { - super(context, attrs, defStyleAttr, defStyleRes); + public ToggleSwitch(final Context context) { + this(context, null); } @Override @@ -48,28 +38,23 @@ public class ToggleSwitch extends Switch { isRestoringState = true; super.onRestoreInstanceState(state); isRestoringState = false; - } @Override public void setChecked(final boolean checked) { + if (checked == isChecked()) + return; if (isRestoringState || listener == null) { super.setChecked(checked); return; } - if (hasPendingStateChange) - return; - hasPendingStateChange = true; setEnabled(false); listener.onBeforeCheckedChanged(this, checked); } public void setCheckedInternal(final boolean checked) { - if (hasPendingStateChange) { - setEnabled(true); - hasPendingStateChange = false; - } super.setChecked(checked); + setEnabled(true); } public void setOnBeforeCheckedChangeListener(final OnBeforeCheckedChangeListener listener) { -- cgit v1.2.3