2018-01-01 09:06:37 +01:00
|
|
|
package com.wireguard.android.activity;
|
|
|
|
|
2018-02-18 22:16:26 +01:00
|
|
|
import android.content.Intent;
|
2018-01-01 09:06:37 +01:00
|
|
|
import android.databinding.CallbackRegistry;
|
|
|
|
import android.databinding.CallbackRegistry.NotifierCallback;
|
|
|
|
import android.os.Bundle;
|
2018-04-27 18:59:27 +02:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
|
|
|
import com.wireguard.android.Application;
|
2018-02-18 22:16:26 +01:00
|
|
|
import com.wireguard.android.backend.GoBackend;
|
2018-01-01 09:06:37 +01:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2018-04-27 18:59:27 +02:00
|
|
|
public abstract class BaseActivity extends AppCompatActivity {
|
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
|
|
|
}
|
2018-02-18 22:16:26 +01:00
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
// The selected tunnel must be set before the superclass method recreates fragments.
|
|
|
|
super.onCreate(savedInstanceState);
|
2018-02-18 22:16:26 +01:00
|
|
|
|
|
|
|
if (Application.getComponent().getBackendType() == GoBackend.class) {
|
2018-04-30 18:37:52 +02:00
|
|
|
final Intent intent = GoBackend.VpnService.prepare(this);
|
2018-02-18 22:16:26 +01:00
|
|
|
if (intent != null) {
|
|
|
|
startActivityForResult(intent, 0);
|
|
|
|
}
|
|
|
|
}
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|