|
@@ -22,12 +22,12 @@ import java.io.IOException;
|
22
|
22
|
import java.io.InputStream;
|
23
|
23
|
import java.io.ObjectInputStream;
|
24
|
24
|
import java.util.ArrayList;
|
25
|
|
-import java.util.HashMap;
|
26
|
25
|
import java.util.List;
|
27
|
26
|
import java.util.Map;
|
28
|
27
|
|
29
|
28
|
import uk.co.md87.android.activityrecorder.rpc.ActivityRecorderBinder;
|
30
|
29
|
import uk.co.md87.android.activityrecorder.rpc.Classification;
|
|
30
|
+import uk.co.md87.android.common.Aggregator;
|
31
|
31
|
|
32
|
32
|
/**
|
33
|
33
|
*
|
|
@@ -35,47 +35,7 @@ import uk.co.md87.android.activityrecorder.rpc.Classification;
|
35
|
35
|
*/
|
36
|
36
|
public class RecorderService extends Service {
|
37
|
37
|
|
38
|
|
- static final double DELTA = 0.25;
|
39
|
|
- static final double THRESHOLD = 0.5;
|
40
|
|
-
|
41
|
|
- private final HashMap<String, HashMap<String, Double>> scores
|
42
|
|
- = new HashMap<String, HashMap<String, Double>>() {{
|
43
|
|
- put("", new HashMap<String, Double>() {{
|
44
|
|
- put("null", 0.5d);
|
45
|
|
- put("CLASSIFIED", 0.5d);
|
46
|
|
- }});
|
47
|
|
-
|
48
|
|
- put("CLASSIFIED", new HashMap<String, Double>() {{
|
49
|
|
- put("null", 0.2d);
|
50
|
|
- put("DANCING", 0.2d);
|
51
|
|
- put("WALKING", 0.2d);
|
52
|
|
- put("VEHICLE", 0.2d);
|
53
|
|
- put("IDLE", 0.2d);
|
54
|
|
- }});
|
55
|
|
-
|
56
|
|
- put("CLASSIFIED/WALKING", new HashMap<String, Double>() {{
|
57
|
|
- put("null", 0.5d);
|
58
|
|
- put("STAIRS", 0.5d);
|
59
|
|
- }});
|
60
|
|
-
|
61
|
|
- put("CLASSIFIED/VEHICLE", new HashMap<String, Double>() {{
|
62
|
|
- put("null", 0.333d);
|
63
|
|
- put("CAR", 0.333d);
|
64
|
|
- put("BUS", 0.333d);
|
65
|
|
- }});
|
66
|
|
-
|
67
|
|
- put("CLASSIFIED/IDLE", new HashMap<String, Double>() {{
|
68
|
|
- put("null", 0.333d);
|
69
|
|
- put("STANDING", 0.333d);
|
70
|
|
- put("SITTING", 0.333d);
|
71
|
|
- }});
|
72
|
|
-
|
73
|
|
- put("CLASSIFIED/WALKING/STAIRS", new HashMap<String, Double>() {{
|
74
|
|
- put("null", 0.333d);
|
75
|
|
- put("UP", 0.333d);
|
76
|
|
- put("DOWN", 0.333d);
|
77
|
|
- }});
|
78
|
|
- }};
|
|
38
|
+ private final Aggregator aggregator = new Aggregator();
|
79
|
39
|
|
80
|
40
|
private final ActivityRecorderBinder.Stub binder = new ActivityRecorderBinder.Stub() {
|
81
|
41
|
|
|
@@ -230,26 +190,9 @@ public class RecorderService extends Service {
|
230
|
190
|
}
|
231
|
191
|
|
232
|
192
|
void updateScores(final String classification) {
|
233
|
|
- String path = "";
|
|
193
|
+ aggregator.addClassification(classification);
|
234
|
194
|
|
235
|
|
- for (String part : classification.split("/")) {
|
236
|
|
- if (!scores.containsKey(path)) {
|
237
|
|
- throw new RuntimeException("Path not found: " + path
|
238
|
|
- + " (classification: " + classification + ")");
|
239
|
|
- }
|
240
|
|
-
|
241
|
|
- updateScores(scores.get(path), part);
|
242
|
|
- path = path + (path.length() == 0 ? "" : "/") + part;
|
243
|
|
- }
|
244
|
|
-
|
245
|
|
- if (scores.containsKey(path)) {
|
246
|
|
- // This classification has children which we're not using
|
247
|
|
- // e.g. we've received CLASSIFIED/WALKING, but we're not walking
|
248
|
|
- // up or down stairs
|
249
|
|
- updateScores(scores.get(path), "null");
|
250
|
|
- }
|
251
|
|
-
|
252
|
|
- final String best = getClassification();
|
|
195
|
+ final String best = aggregator.getClassification();
|
253
|
196
|
|
254
|
197
|
if (!classifications.isEmpty() && best.equals(classifications
|
255
|
198
|
.get(classifications.size() - 1).getClassification())) {
|
|
@@ -259,39 +202,6 @@ public class RecorderService extends Service {
|
259
|
202
|
}
|
260
|
203
|
}
|
261
|
204
|
|
262
|
|
- String getClassification() {
|
263
|
|
- String path = "";
|
264
|
|
-
|
265
|
|
- do {
|
266
|
|
- final Map<String, Double> map = scores.get(path);
|
267
|
|
- double best = THRESHOLD;
|
268
|
|
- String bestPath = "null";
|
269
|
|
-
|
270
|
|
- for (Map.Entry<String, Double> entry : map.entrySet()) {
|
271
|
|
- if (entry.getValue() >= best) {
|
272
|
|
- best = entry.getValue();
|
273
|
|
- bestPath = entry.getKey();
|
274
|
|
- }
|
275
|
|
- }
|
276
|
|
-
|
277
|
|
- path = path + (path.length() == 0 ? "" : "/") + bestPath;
|
278
|
|
- } while (scores.containsKey(path));
|
279
|
|
-
|
280
|
|
- return path.replaceAll("(^CLASSIFIED)?/?null$", "");
|
281
|
|
- }
|
282
|
|
-
|
283
|
|
- void updateScores(final Map<String, Double> map, final String target) {
|
284
|
|
- for (Map.Entry<String, Double> entry : map.entrySet()) {
|
285
|
|
- //Log.d(getClass().getName(), "Score for " + entry.getKey() + " was: " + entry.getValue());
|
286
|
|
- entry.setValue(entry.getValue() * (1 - DELTA));
|
287
|
|
-
|
288
|
|
- if (entry.getKey().equals(target)) {
|
289
|
|
- entry.setValue(entry.getValue() + DELTA);
|
290
|
|
- }
|
291
|
|
- //Log.d(getClass().getName(), "Score for " + entry.getKey() + " is now: " + entry.getValue());
|
292
|
|
- }
|
293
|
|
- }
|
294
|
|
-
|
295
|
205
|
@Override
|
296
|
206
|
public void onDestroy() {
|
297
|
207
|
super.onDestroy();
|