Skip to content

Commit 5b94d9c

Browse files
committed
MainService: Now service is stopped if interface goes down
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).
1 parent 600d9ea commit 5b94d9c

File tree

5 files changed

+161
-46
lines changed

5 files changed

+161
-46
lines changed

app/src/main/java/net/christianbeier/droidvnc_ng/ListenIfAdapter.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626

2727
import android.content.res.Resources;
2828
import android.content.Context;
29+
import android.os.Handler;
30+
import android.os.Looper;
31+
2932
import android.util.Log;
3033
import android.view.View;
3134
import android.view.ViewGroup;
@@ -43,6 +46,7 @@ private static class ViewHolder {
4346
}
4447

4548
// Data to be shown with the adapter
49+
private ArrayList<String> dataStr;
4650
private ArrayList<NetworkInterfaceTester.NetIfData> data;
4751
private int dataSize;
4852

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

5458

59+
// UI related
60+
private Handler handler;
61+
5562

5663

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

70+
this.handler = new Handler(Looper.getMainLooper());
71+
6372
nit.addOnNetworkStateChangedListener(this);
64-
this.onNetworkStateChanged(nit, null, false);
73+
this.onNetworkStateChanged(nit);
6574
}
6675

6776

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

8190

8291

92+
@Override
93+
public View getDropDownView(int position, View convertView, ViewGroup parent) {
94+
return this.handleViewRecreation(position, convertView, parent);
95+
}
96+
8397

8498
@Override
8599
public View getView(int position, View convertView, ViewGroup parent) {
100+
return this.handleViewRecreation(position, convertView, parent);
101+
}
102+
103+
104+
private View handleViewRecreation(int position, View convertView, ViewGroup parent) {
86105
if (convertView == null) { // Check if view must be recreated using the famous ViewHolder pattern
87106
convertView = this.mInflater.inflate(R.layout.spinner_row, parent, false);
88107

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

94113
ViewHolder vh = (ViewHolder)convertView.getTag();
95-
NetworkInterfaceTester.NetIfData nid = this.getItem(position);
96-
vh.txtLabel.setText(nid.toString());
114+
String label = this.dataStr.get(position);
115+
vh.txtLabel.setText(label);
97116

98117
return convertView;
99118
}
100119

101120

121+
102122
@Override
103123
public NetworkInterfaceTester.NetIfData getItem(int position) {
104124
if (0 <= position && position < this.getCount()) {
@@ -108,15 +128,35 @@ public NetworkInterfaceTester.NetIfData getItem(int position) {
108128
}
109129

110130

131+
111132
@Override
112133
public int getCount() {
113134
return this.dataSize;
114135
}
115136

116137

117138

118-
public void onNetworkStateChanged(NetworkInterfaceTester nit, NetworkInterface iface, boolean enabled) {
139+
public void onNetworkStateChanged(NetworkInterfaceTester nit) {
119140
this.data = nit.getAvailableInterfaces();
120141
this.dataSize = this.data.size();
142+
143+
this.dataStr = new ArrayList<>();
144+
for (NetworkInterfaceTester.NetIfData nid : this.data) {
145+
String actualName = " (" + nid.getName() + ")";
146+
String displayName = nid.getDisplayName();
147+
148+
if (nid.getName().equals("0.0.0.0")) {
149+
displayName = this.mContext.getResources().getString(R.string.main_activity_settings_listenif_spin_any);
150+
}
151+
152+
this.dataStr.add(displayName + actualName);
153+
}
154+
155+
// update Spinner
156+
this.handler.post(new Runnable() {
157+
public void run() {
158+
ListenIfAdapter.this.notifyDataSetChanged();
159+
}
160+
});
121161
}
122162
}

app/src/main/java/net/christianbeier/droidvnc_ng/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ protected void onCreate(Bundle savedInstanceState) {
311311
});
312312

313313

314-
NetworkInterfaceTester nit = new NetworkInterfaceTester(this);
314+
NetworkInterfaceTester nit = NetworkInterfaceTester.getInstance(this);
315315
ListenIfAdapter lsif = new ListenIfAdapter(nit, this);
316316
final Spinner listenInterfaceSpin = findViewById(R.id.settings_listening_interface);
317317
listenInterfaceSpin.setAdapter(lsif);

app/src/main/java/net/christianbeier/droidvnc_ng/MainService.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
import java.util.Objects;
6464
import java.util.concurrent.ConcurrentHashMap;
6565

66-
public class MainService extends Service {
66+
public class MainService extends Service implements NetworkInterfaceTester.OnNetworkStateChangedListener {
6767

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

190190
instance = this;
191+
NetworkInterfaceTester.getInstance(this).addOnNetworkStateChangedListener(this);
191192

192193
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
193194
/*
@@ -926,4 +927,15 @@ static Notification getCurrentNotification() {
926927
return null;
927928
}
928929
}
930+
931+
932+
933+
934+
public void onNetworkStateChanged(NetworkInterfaceTester nit) {
935+
if (isServerActive()) {
936+
if (!nit.isIfEnabled(getListenInterface())) {
937+
this.stopSelf();
938+
}
939+
}
940+
}
929941
}

0 commit comments

Comments
 (0)