Skip to content

Commit

Permalink
MainService: Now service is stopped if interface goes down
Browse files Browse the repository at this point in the history
I was capable of implementing an all-Java solution.
For some reason though, I was not capable of making it work with VPNs (needs more testing, I think).
  • Loading branch information
elluisian committed Sep 1, 2024
1 parent 600d9ea commit 6a318b9
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

import android.content.res.Resources;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -43,6 +46,7 @@ private static class ViewHolder {
}

// Data to be shown with the adapter
private ArrayList<String> dataStr;
private ArrayList<NetworkInterfaceTester.NetIfData> data;
private int dataSize;

Expand All @@ -52,6 +56,9 @@ private static class ViewHolder {
private LayoutInflater mInflater;


// UI related
private Handler handler;



public ListenIfAdapter(NetworkInterfaceTester nit, Context context) {
Expand All @@ -60,8 +67,10 @@ public ListenIfAdapter(NetworkInterfaceTester nit, Context context) {
this.mContext = context;
this.mInflater = LayoutInflater.from(this.mContext);

this.handler = new Handler(Looper.getMainLooper());

nit.addOnNetworkStateChangedListener(this);
this.onNetworkStateChanged(nit, null, false);
this.onNetworkStateChanged(nit, null, 0, false);
}


Expand All @@ -80,9 +89,19 @@ public int getItemPositionByIfName(String ifName) {



@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return this.handleViewRecreation(position, convertView, parent);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
return this.handleViewRecreation(position, convertView, parent);
}


private View handleViewRecreation(int position, View convertView, ViewGroup parent) {
if (convertView == null) { // Check if view must be recreated using the famous ViewHolder pattern
convertView = this.mInflater.inflate(R.layout.spinner_row, parent, false);

Expand All @@ -92,13 +111,14 @@ public View getView(int position, View convertView, ViewGroup parent) {
}

ViewHolder vh = (ViewHolder)convertView.getTag();
NetworkInterfaceTester.NetIfData nid = this.getItem(position);
vh.txtLabel.setText(nid.toString());
String label = this.dataStr.get(position);
vh.txtLabel.setText(label);

return convertView;
}



@Override
public NetworkInterfaceTester.NetIfData getItem(int position) {
if (0 <= position && position < this.getCount()) {
Expand All @@ -108,15 +128,35 @@ public NetworkInterfaceTester.NetIfData getItem(int position) {
}



@Override
public int getCount() {
return this.dataSize;
}



public void onNetworkStateChanged(NetworkInterfaceTester nit, NetworkInterface iface, boolean enabled) {
public void onNetworkStateChanged(NetworkInterfaceTester nit) {
this.data = nit.getAvailableInterfaces();
this.dataSize = this.data.size();

this.dataStr = new ArrayList<>();
for (NetworkInterfaceTester.NetIfData nid : this.data) {
String actualName = " (" + nid.getName() + ")";
String displayName = nid.getDisplayName();

if (nid.getName().equals("0.0.0.0")) {
displayName = this.mContext.getResources().getString(R.string.main_activity_settings_listenif_spin_any);
}

this.dataStr.add(displayName + actualName);
}

// update Spinner
this.handler.post(new Runnable() {
public void run() {
ListenIfAdapter.this.notifyDataSetChanged();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ protected void onCreate(Bundle savedInstanceState) {
});


NetworkInterfaceTester nit = new NetworkInterfaceTester(this);
NetworkInterfaceTester nit = NetworkInterfaceTester.getInstance(this);
ListenIfAdapter lsif = new ListenIfAdapter(nit, this);
final Spinner listenInterfaceSpin = findViewById(R.id.settings_listening_interface);
listenInterfaceSpin.setAdapter(lsif);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class MainService extends Service {
public class MainService extends Service implements NetworkInterfaceTester.OnNetworkStateChangedListener {

private static final String TAG = "MainService";
static final int NOTIFICATION_ID = 11;
Expand Down Expand Up @@ -188,6 +188,7 @@ public void onCreate() {
Log.d(TAG, "onCreate");

instance = this;
NetworkInterfaceTester.getInstance(this).addOnNetworkStateChangedListener(this);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/*
Expand Down Expand Up @@ -926,4 +927,15 @@ static Notification getCurrentNotification() {
return null;
}
}




public void onNetworkStateChanged(NetworkInterfaceTester nit) {
if (isServerActive()) {
if (!nit.isIfEnabled(getListenInterface())) {
this.stopSelf();
}
}
}
}
Loading

0 comments on commit 6a318b9

Please sign in to comment.