Skip to content

Commit a8ee987

Browse files
author
David Gräff
committed
Forgot DeviceOberserverBase class.
Proguard-rules.txt
1 parent 6e4d5fe commit a8ee987

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

app/proguard-rules.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-assumenosideeffects class android.util.Log {
2+
public static boolean isLoggable(java.lang.String, int);
3+
public static int v(...);
4+
public static int i(...);
5+
public static int w(...);
6+
public static int d(...);
7+
public static int e(...);
8+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package oly.netpowerctrl.network;
2+
3+
import android.os.Handler;
4+
import android.util.Log;
5+
6+
import java.util.Iterator;
7+
import java.util.List;
8+
9+
import oly.netpowerctrl.R;
10+
import oly.netpowerctrl.application_state.NetpowerctrlApplication;
11+
import oly.netpowerctrl.datastructure.DeviceInfo;
12+
13+
/**
14+
* Created by david on 16.04.14.
15+
*/
16+
public abstract class DeviceObserverBase {
17+
public void setDeviceQueryResult(DeviceQueryResult target) {
18+
this.target = target;
19+
}
20+
21+
protected List<DeviceInfo> devices_to_observe;
22+
protected DeviceQueryResult target;
23+
protected Handler mainLoopHandler = new Handler(NetpowerctrlApplication.instance.getMainLooper());
24+
protected Runnable timeoutRunnable = new Runnable() {
25+
@Override
26+
public void run() {
27+
NetpowerctrlApplication.getDataController().removeUpdateDeviceState(DeviceObserverBase.this);
28+
if (target == null)
29+
return;
30+
31+
for (DeviceInfo di : devices_to_observe) {
32+
di.setNotReachable(NetpowerctrlApplication.instance.getString(R.string.error_timeout_device, ""));
33+
target.onDeviceTimeout(di);
34+
}
35+
NetpowerctrlApplication.getDataController().removeUpdateDeviceState(DeviceObserverBase.this);
36+
target.onDeviceQueryFinished(devices_to_observe);
37+
}
38+
};
39+
40+
protected abstract void doAction(DeviceInfo di);
41+
42+
protected Runnable redoRunnable = new Runnable() {
43+
@Override
44+
public void run() {
45+
Log.w("DeviceObserverBase", "redo action");
46+
for (DeviceInfo di : devices_to_observe) {
47+
doAction(di);
48+
}
49+
}
50+
};
51+
52+
/**
53+
* Return true if all devices responded and this DeviceQuery object
54+
* have to be removed.
55+
*
56+
* @param received_data The DeviceInfo object all observes should be notified of.
57+
*/
58+
public boolean notifyObservers(DeviceInfo received_data) {
59+
Iterator<DeviceInfo> it = devices_to_observe.iterator();
60+
while (it.hasNext()) {
61+
DeviceInfo device_to_observe = it.next();
62+
if (device_to_observe.equalsByUniqueID(received_data)) {
63+
it.remove();
64+
if (target != null)
65+
target.onDeviceUpdated(received_data);
66+
break;
67+
}
68+
}
69+
return checkIfDone();
70+
}
71+
72+
public boolean notifyObservers(String device_name) {
73+
Iterator<DeviceInfo> it = devices_to_observe.iterator();
74+
while (it.hasNext()) {
75+
DeviceInfo device_to_observe = it.next();
76+
boolean eq = device_name.equals(device_to_observe.DeviceName);
77+
if (eq) {
78+
it.remove();
79+
if (target != null)
80+
target.onDeviceUpdated(device_to_observe);
81+
break;
82+
}
83+
}
84+
return checkIfDone();
85+
}
86+
87+
public void addDevice(DeviceInfo device, boolean resetTimeout) {
88+
devices_to_observe.add(device);
89+
90+
if (!resetTimeout)
91+
return;
92+
mainLoopHandler.removeCallbacks(timeoutRunnable);
93+
mainLoopHandler.postDelayed(timeoutRunnable, 1500);
94+
}
95+
96+
private boolean checkIfDone() {
97+
if (devices_to_observe.isEmpty()) {
98+
mainLoopHandler.removeCallbacks(timeoutRunnable);
99+
mainLoopHandler.removeCallbacks(redoRunnable);
100+
if (target != null)
101+
target.onDeviceQueryFinished(devices_to_observe);
102+
return true;
103+
}
104+
return false;
105+
}
106+
107+
/**
108+
* Called right before this object is removed from the Application list
109+
* of DeviceQueries because the listener service has been shutdown. All
110+
* remaining device queries of this object have to timeout now.
111+
*/
112+
public void finishWithTimeouts() {
113+
mainLoopHandler.removeCallbacks(timeoutRunnable);
114+
for (DeviceInfo di : devices_to_observe) {
115+
di.setNotReachable(NetpowerctrlApplication.instance.getString(R.string.error_timeout_device, ""));
116+
//di.setUpdatedNow();
117+
if (target != null)
118+
target.onDeviceTimeout(di);
119+
}
120+
if (target != null)
121+
target.onDeviceQueryFinished(devices_to_observe);
122+
}
123+
}

0 commit comments

Comments
 (0)