From 609194fae2332e6f2ccd7a4464bfa492ad661a6f Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Mon, 1 Jan 2018 02:06:37 -0600 Subject: Serviceless rewrite, part 1 Signed-off-by: Samuel Holland --- .../wireguard/android/activity/BaseActivity.java | 94 ++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 app/src/main/java/com/wireguard/android/activity/BaseActivity.java (limited to 'app/src/main/java/com/wireguard/android/activity/BaseActivity.java') diff --git a/app/src/main/java/com/wireguard/android/activity/BaseActivity.java b/app/src/main/java/com/wireguard/android/activity/BaseActivity.java new file mode 100644 index 00000000..4bd3407e --- /dev/null +++ b/app/src/main/java/com/wireguard/android/activity/BaseActivity.java @@ -0,0 +1,94 @@ +package com.wireguard.android.activity; + +import android.app.Activity; +import android.databinding.CallbackRegistry; +import android.databinding.CallbackRegistry.NotifierCallback; +import android.os.Bundle; + +import com.wireguard.android.Application; +import com.wireguard.android.model.Tunnel; +import com.wireguard.android.model.TunnelManager; + +import java.util.Objects; + +/** + * Base class for activities that need to remember the currently-selected tunnel. + */ + +public abstract class BaseActivity extends Activity { + private static final String TAG = BaseActivity.class.getSimpleName(); + + private final SelectionChangeRegistry selectionChangeRegistry = new SelectionChangeRegistry(); + private Tunnel selectedTunnel; + + public void addOnSelectedTunnelChangedListener( + final OnSelectedTunnelChangedListener listener) { + selectionChangeRegistry.add(listener); + } + + public Tunnel getSelectedTunnel() { + return selectedTunnel; + } + + @Override + protected void onCreate(final Bundle savedInstanceState) { + // Restore the saved tunnel if there is one; otherwise grab it from the arguments. + String savedTunnelName = null; + if (savedInstanceState != null) + savedTunnelName = savedInstanceState.getString(TunnelManager.KEY_SELECTED_TUNNEL); + else if (getIntent() != null) + savedTunnelName = getIntent().getStringExtra(TunnelManager.KEY_SELECTED_TUNNEL); + if (savedTunnelName != null) { + final TunnelManager manager = Application.getComponent().getTunnelManager(); + selectedTunnel = manager.getTunnels().get(savedTunnelName); + } + // The selected tunnel must be set before the superclass method recreates fragments. + super.onCreate(savedInstanceState); + } + + @Override + protected void onSaveInstanceState(final Bundle outState) { + if (selectedTunnel != null) + outState.putString(TunnelManager.KEY_SELECTED_TUNNEL, selectedTunnel.getName()); + super.onSaveInstanceState(outState); + } + + protected abstract Tunnel onSelectedTunnelChanged(Tunnel oldTunnel, Tunnel newTunnel); + + public void removeOnSelectedTunnelChangedListener( + final OnSelectedTunnelChangedListener listener) { + selectionChangeRegistry.remove(listener); + } + + public void setSelectedTunnel(final Tunnel tunnel) { + final Tunnel oldTunnel = selectedTunnel; + if (Objects.equals(oldTunnel, tunnel)) + return; + // Give the activity a chance to override the tunnel change. + selectedTunnel = onSelectedTunnelChanged(oldTunnel, tunnel); + if (Objects.equals(oldTunnel, selectedTunnel)) + return; + selectionChangeRegistry.notifyCallbacks(oldTunnel, 0, selectedTunnel); + } + + public interface OnSelectedTunnelChangedListener { + void onSelectedTunnelChanged(Tunnel oldTunnel, Tunnel newTunnel); + } + + private static final class SelectionChangeNotifier + extends NotifierCallback { + @Override + public void onNotifyCallback(final OnSelectedTunnelChangedListener listener, + final Tunnel oldTunnel, final int ignored, + final Tunnel newTunnel) { + listener.onSelectedTunnelChanged(oldTunnel, newTunnel); + } + } + + private static final class SelectionChangeRegistry + extends CallbackRegistry { + private SelectionChangeRegistry() { + super(new SelectionChangeNotifier()); + } + } +} -- cgit v1.2.3