TunnelManager: new intents
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
@@ -6,12 +6,9 @@
|
||||
|
||||
package com.wireguard.android.backend;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
import com.wireguard.android.Application;
|
||||
import com.wireguard.android.model.Tunnel;
|
||||
import com.wireguard.android.model.Tunnel.State;
|
||||
import com.wireguard.android.model.Tunnel.Statistics;
|
||||
@@ -119,12 +116,4 @@ public final class WgQuickBackend implements Backend {
|
||||
if (result != 0)
|
||||
throw new Exception("Unable to configure tunnel (wg-quick returned " + result + ')');
|
||||
}
|
||||
|
||||
public static final class WgQuickChangeReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(final Context context, final Intent intent) {
|
||||
Log.d(TAG, "Refreshing tunnel states");
|
||||
Application.getComponent().getTunnelManager().refreshTunnelStates();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,15 @@
|
||||
|
||||
package com.wireguard.android.model;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.databinding.BaseObservable;
|
||||
import android.databinding.Bindable;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.wireguard.android.Application;
|
||||
import com.wireguard.android.Application.ApplicationScope;
|
||||
import com.wireguard.android.BR;
|
||||
import com.wireguard.android.backend.Backend;
|
||||
@@ -264,4 +268,37 @@ public final class TunnelManager extends BaseObservable {
|
||||
saveState();
|
||||
});
|
||||
}
|
||||
|
||||
public static final class IntentReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(final Context context, final Intent intent) {
|
||||
final TunnelManager manager = Application.getComponent().getTunnelManager();
|
||||
if (intent == null)
|
||||
return;
|
||||
final String action = intent.getAction();
|
||||
if (action == null)
|
||||
return;
|
||||
|
||||
if ("com.wireguard.android.action.REFRESH_TUNNEL_STATES".equals(action)) {
|
||||
manager.refreshTunnelStates();
|
||||
return;
|
||||
}
|
||||
|
||||
final State state;
|
||||
if ("com.wireguard.android.action.SET_TUNNEL_UP".equals(action))
|
||||
state = State.UP;
|
||||
else if ("com.wireguard.android.action.SET_TUNNEL_DOWN".equals(action))
|
||||
state = State.DOWN;
|
||||
else
|
||||
return;
|
||||
|
||||
final String tunnelName = intent.getStringExtra("tunnel");
|
||||
if (tunnelName == null)
|
||||
return;
|
||||
final Tunnel tunnel = manager.getTunnels().get(tunnelName);
|
||||
if (tunnel == null)
|
||||
return;
|
||||
manager.setTunnelState(tunnel, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user