Skip to content

Commit bace9c9

Browse files
author
David Graeff
committed
WolPlugin: Do not show entries if wol address is not in range (network host part differs).
Remove also unconfigured credentials and executables if a rescan is issued, not only connections. SimpleUDP uses utf8 instead of latin1 codec. AddToConfigured: Also update executables if a new/edited credential is saved.
1 parent a97b70c commit bace9c9

File tree

10 files changed

+132
-130
lines changed

10 files changed

+132
-130
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ android {
1616
defaultConfig {
1717
minSdkVersion 17
1818
targetSdkVersion 22
19-
versionCode 122
20-
versionName "8.2"
19+
versionCode 123
20+
versionName "8.3"
2121
applicationId appID
2222
testApplicationId "oly.netpowerctrl.tests"
2323
testInstrumentationRunner "android.test.InstrumentationTestRunner"

app/src/main/java/oly/netpowerctrl/data/DataService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ public void clearDataStorage() {
334334
public void addToConfiguredDevices(Credentials credentials) {
335335
credentials.setConfigured(true);
336336
this.credentials.put(credentials);
337-
connections.save(credentials.deviceUID);
338-
executables.save(credentials.deviceUID);
337+
ReachabilityStates r = connections.save(credentials.deviceUID);
338+
executables.notifyReachability(credentials.deviceUID, r, true);
339339
}
340340

341341
public void showNotificationForNextRefresh(boolean notificationAfterNextRefresh) {
@@ -381,6 +381,7 @@ public void onObserverJobFinished(DevicesObserver devicesObserver) {
381381
public void detectDevices() {
382382
if (observersStartStopRefresh.isRefreshing()) return;
383383
observersStartStopRefresh.onRefreshStateChanged(true);
384+
credentials.clearNotConfigured();
384385
deviceQuery.addDeviceObserver(new DevicesObserver(new DevicesObserver.onDevicesObserverFinished() {
385386
@Override
386387
public void onObserverJobFinished(DevicesObserver devicesObserver) {
@@ -389,7 +390,6 @@ public void onObserverJobFinished(DevicesObserver devicesObserver) {
389390
observersStartStopRefresh.onRefreshStateChanged(false);
390391
}
391392
}));
392-
connections.clearNotConfigured();
393393
discoverExtensions();
394394
for (AbstractBasePlugin abstractBasePlugin : plugins) {
395395
abstractBasePlugin.requestData();
@@ -400,7 +400,7 @@ public void onObserverJobFinished(DevicesObserver devicesObserver) {
400400
* This is issued by the network observer if no network connection is active (no wlan, no mobile network)
401401
*/
402402
public void makeAllOffline() {
403-
connections.clearNotConfigured();
403+
credentials.clearNotConfigured();
404404
connections.applyStateToAll(ReachabilityStates.NotReachable);
405405
}
406406

app/src/main/java/oly/netpowerctrl/devices/CredentialsCollection.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.lang.ref.WeakReference;
88
import java.util.ArrayList;
9+
import java.util.Iterator;
910
import java.util.List;
1011

1112
import oly.netpowerctrl.data.AbstractBasePlugin;
@@ -65,8 +66,8 @@ public void put(Credentials credentials) {
6566
}
6667

6768
public void remove(Credentials credentials) {
68-
items.remove(credentials.getUid());
6969
storage.remove(credentials);
70+
items.remove(credentials.getUid());
7071
notifyObservers(credentials, ObserverUpdateActions.RemoveAction);
7172
}
7273

@@ -110,4 +111,18 @@ public int countConfigured() {
110111
return i;
111112
}
112113

114+
/**
115+
* Clear all credentials where the object is in the non-configured state.
116+
*/
117+
public void clearNotConfigured() {
118+
for (Iterator<Credentials> iterator = items.values().iterator(); iterator.hasNext(); ) {
119+
Credentials credentials = iterator.next();
120+
if (!credentials.isConfigured()) {
121+
dataService.connections.remove(credentials);
122+
dataService.executables.remove(credentials);
123+
iterator.remove();
124+
notifyObservers(credentials, ObserverUpdateActions.RemoveAction);
125+
}
126+
}
127+
}
113128
}

app/src/main/java/oly/netpowerctrl/executables/ExecutableCollection.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,16 @@ public void removeOrphaned() {
131131
* Executables contain a member function for reachability we need to notify about reachability changes.
132132
*
133133
* @param deviceUID The device unique id
134+
* @param save
134135
*/
135-
public void notifyReachability(String deviceUID, ReachabilityStates r) {
136+
public void notifyReachability(String deviceUID, ReachabilityStates r, boolean save) {
136137
Log.w(TAG, "Reachability: " + r.name() + " " + deviceUID);
137138
for (Executable executable : items.values()) {
138-
if (executable.needCredentials() &&
139-
executable.deviceUID.equals(deviceUID) && executable.updateCachedReachability(r))
139+
if (!executable.deviceUID.equals(deviceUID)) continue;
140+
if (executable.needCredentials() && executable.updateCachedReachability(r))
140141
notifyObservers(executable, ObserverUpdateActions.UpdateReachableAction);
142+
if (save)
143+
storage.save(executable);
141144
}
142145
}
143146

@@ -171,18 +174,6 @@ public List<Executable> filterExecutables(Credentials credentials) {
171174
return list;
172175
}
173176

174-
/**
175-
* Save all connections that belong to the same device ID as the given credential object.
176-
* This will not issue updated notifications even if the objects have changed since last save.
177-
*/
178-
public void save(String deviceUID) {
179-
for (Executable executable : items.values()) {
180-
if (!executable.deviceUID.equals(deviceUID)) continue;
181-
executable.setHasChanged();
182-
put(executable);
183-
}
184-
}
185-
186177
public interface PredicateExecutable {
187178
boolean accept(Executable e);
188179
}

app/src/main/java/oly/netpowerctrl/ioconnection/IOConnectionsCollection.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private void putInternal(DeviceIOConnections deviceIOConnections, IOConnection i
107107
updateActions = ObserverUpdateActions.UpdateReachableAction;
108108
// Make executables aware of the change
109109
if (reachabilityChanged)
110-
dataService.executables.notifyReachability(ioConnection.deviceUID, deviceIOConnections.reachableState());
110+
dataService.executables.notifyReachability(ioConnection.deviceUID, deviceIOConnections.reachableState(), false);
111111
}
112112

113113
if (ioConnection.hasChanged()) {
@@ -149,16 +149,17 @@ public void run() {
149149
* Save all connections that belong to the same device ID as the given credential object.
150150
* This will not issue updated notifications even if the objects have changed since last save.
151151
*/
152-
public void save(String deviceUID) {
152+
public ReachabilityStates save(String deviceUID) {
153153
DeviceIOConnections deviceIOConnections = openDevice(deviceUID);
154-
if (deviceIOConnections == null) return;
154+
if (deviceIOConnections == null) return ReachabilityStates.NotReachable;
155155

156156
for (Iterator<IOConnection> iterator = deviceIOConnections.iterator(); iterator.hasNext(); ) {
157157
IOConnection ioConnection = iterator.next();
158158
if (ioConnection.deviceUID.equals(deviceIOConnections.deviceUID)) {
159159
storage.save(ioConnection);
160160
}
161161
}
162+
return deviceIOConnections.reachableState();
162163
}
163164

164165
public void remove(IOConnection ioConnection) {
@@ -201,21 +202,6 @@ public void remove(Credentials credentials) {
201202
items.remove(credentials.deviceUID);
202203
}
203204

204-
/**
205-
* Clwar all connections where the credentials object is in the non-configured state.
206-
* Connections are removed from disk, too if applicable.
207-
*/
208-
public void clearNotConfigured() {
209-
for (DeviceIOConnections deviceIOConnections : items.values())
210-
for (Iterator<IOConnection> iterator = deviceIOConnections.iterator(); iterator.hasNext(); ) {
211-
IOConnection ioConnection = iterator.next();
212-
if (!ioConnection.credentials.isConfigured()) {
213-
iterator.remove();
214-
notifyObservers(ioConnection, ObserverUpdateActions.RemoveAction);
215-
}
216-
}
217-
}
218-
219205
@Nullable
220206
public DeviceIOConnections openDevice(String deviceUID) {
221207
return items.get(deviceUID);

app/src/main/java/oly/netpowerctrl/network/NetworkChangedBroadcastReceiver.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public void onReceive(Context context, Intent intent) {
4242

4343
Handler h = App.getMainThreadHandler();
4444
h.removeMessages(GuiThreadHandler.SERVICE_DELAYED_CHECK_REACHABILITY);
45+
// Argument1: 0 if no network at all, !=0 if another network
4546
h.sendMessageDelayed(h.obtainMessage(GuiThreadHandler.SERVICE_DELAYED_CHECK_REACHABILITY, newNetInfo, 0), 2000);
4647
}
4748

app/src/main/java/oly/netpowerctrl/network/UDPSend.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* For sending udp packets
2525
*/
2626
public class UDPSend extends Thread {
27-
private static UDPSend udpSend = new UDPSend();
27+
private static UDPSend udpSend = null;
2828
private LinkedBlockingQueue<UDPCommand> q = new LinkedBlockingQueue<>();
2929

3030
private UDPSend() {
@@ -33,6 +33,8 @@ private UDPSend() {
3333
}
3434

3535
public static void killSendThread() {
36+
if (udpSend == null || udpSend.q == null) return;
37+
Log.w("UDPSEND", "killSendThread");
3638
UDPCommand command = new UDPCommand();
3739
udpSend.q.add(command);
3840
}
@@ -43,6 +45,7 @@ public static void sendBroadcast(Set<Integer> ports, byte[] message) {
4345
command.messages.add(message);
4446
command.errorID = UDPErrors.INQUERY_BROADCAST_REQUEST;
4547
command.ports = ports;
48+
if (udpSend == null) udpSend = new UDPSend();
4649
udpSend.q.add(command);
4750
}
4851

@@ -52,6 +55,7 @@ public static void sendMessage(@NonNull IOConnectionUDP ioConnection, byte[] mes
5255
command.errorID = UDPErrors.INQUERY_REQUEST;
5356
command.host = ioConnection.getDestinationHost();
5457
command.destPort = ioConnection.getDestinationPort();
58+
if (udpSend == null) udpSend = new UDPSend();
5559
udpSend.q.add(command);
5660
}
5761

@@ -141,14 +145,16 @@ public void run() {
141145
j = q.take();
142146
if (j.messages.isEmpty()) return;
143147
} catch (InterruptedException e) {
144-
return;
148+
break;
145149
}
146150

147151
if (j.ports != null)
148152
sendBroadcast(datagramSocket, j);
149153
else
150154
send(datagramSocket, j);
151155
}
156+
157+
udpSend = null;
152158
}
153159

154160
private static class UDPCommand {

0 commit comments

Comments
 (0)