2018-01-01 09:06:37 +01:00
|
|
|
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 {
|
2018-01-08 03:32:01 +01:00
|
|
|
private static final String KEY_SELECTED_TUNNEL = "selected_tunnel";
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
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)
|
2018-01-08 03:32:01 +01:00
|
|
|
savedTunnelName = savedInstanceState.getString(KEY_SELECTED_TUNNEL);
|
2018-01-01 09:06:37 +01:00
|
|
|
else if (getIntent() != null)
|
2018-01-08 03:32:01 +01:00
|
|
|
savedTunnelName = getIntent().getStringExtra(KEY_SELECTED_TUNNEL);
|
2018-01-01 09:06:37 +01:00
|
|
|
if (savedTunnelName != null) {
|
2018-01-08 04:51:55 +01:00
|
|
|
final TunnelManager tunnelManager = Application.getComponent().getTunnelManager();
|
|
|
|
selectedTunnel = tunnelManager.getTunnels().get(savedTunnelName);
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
// 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)
|
2018-01-08 03:32:01 +01:00
|
|
|
outState.putString(KEY_SELECTED_TUNNEL, selectedTunnel.getName());
|
2018-01-01 09:06:37 +01:00
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
}
|
|
|
|
|
2018-01-06 11:15:36 +01:00
|
|
|
protected abstract void onSelectedTunnelChanged(Tunnel oldTunnel, Tunnel newTunnel);
|
2018-01-01 09:06:37 +01:00
|
|
|
|
|
|
|
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;
|
2018-01-06 11:15:36 +01:00
|
|
|
selectedTunnel = tunnel;
|
|
|
|
onSelectedTunnelChanged(oldTunnel, tunnel);
|
|
|
|
selectionChangeRegistry.notifyCallbacks(oldTunnel, 0, tunnel);
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnSelectedTunnelChangedListener {
|
|
|
|
void onSelectedTunnelChanged(Tunnel oldTunnel, Tunnel newTunnel);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final class SelectionChangeNotifier
|
|
|
|
extends NotifierCallback<OnSelectedTunnelChangedListener, Tunnel, Tunnel> {
|
|
|
|
@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<OnSelectedTunnelChangedListener, Tunnel, Tunnel> {
|
|
|
|
private SelectionChangeRegistry() {
|
|
|
|
super(new SelectionChangeNotifier());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|