Procházet zdrojové kódy

Now predicts journeys

Added JourneyUtil class with common functionality
master
Chris Smith před 14 roky
rodič
revize
c3cfec3738

binární
code/ContextAnalyser/dist/ContextAnalyser.apk Zobrazit soubor


+ 31
- 0
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/ContextAnalyserService.java Zobrazit soubor

@@ -32,6 +32,7 @@ import android.os.IBinder;
32 32
 import android.util.Log;
33 33
 
34 34
 import java.io.IOException;
35
+import java.util.Collection;
35 36
 import java.util.HashMap;
36 37
 import java.util.Iterator;
37 38
 import java.util.LinkedList;
@@ -44,6 +45,8 @@ import uk.co.md87.android.common.aggregator.AutoAggregator;
44 45
 import uk.co.md87.android.common.aggregator.AutoAggregatorFactory;
45 46
 import uk.co.md87.android.common.geo.LocationMonitor;
46 47
 import uk.co.md87.android.common.geo.LocationMonitorFactory;
48
+import uk.co.md87.android.contextanalyser.model.Journey;
49
+import uk.co.md87.android.contextanalyser.model.JourneyStep;
47 50
 import uk.co.md87.android.contextanalyser.model.Place;
48 51
 
49 52
 /**
@@ -205,6 +208,8 @@ public class ContextAnalyserService extends Service {
205 208
         if (location == null && lastLocation != null) {
206 209
             // We're going somewhere - record the activity
207 210
             activityLog.add(newActivity);
211
+
212
+            checkPredictions();
208 213
         }
209 214
 
210 215
         if (!newActivity.equals(lastActivity)) {
@@ -219,6 +224,32 @@ public class ContextAnalyserService extends Service {
219 224
         }
220 225
     }
221 226
 
227
+    protected void checkPredictions() {
228
+        final Collection<Journey> journeys = dataHelper.findJourneys(lastLocation);
229
+        final List<JourneyStep> mySteps = JourneyUtil.getSteps(activityLog);
230
+
231
+        int total = 0;
232
+        final Iterator<Journey> it = journeys.iterator();
233
+        while (it.hasNext()) {
234
+            final Journey journey = it.next();
235
+
236
+            if (journey.getSteps() < mySteps.size()) {
237
+                it.remove();
238
+                continue;
239
+            }
240
+
241
+            final List<JourneyStep> theirSteps = dataHelper.getSteps(journey);
242
+
243
+            if (JourneyUtil.isCompatible(mySteps, theirSteps)) {
244
+                total += journey.getNumber();
245
+            } else {
246
+                it.remove();
247
+            }
248
+        }
249
+
250
+        Log.i(getClass().getSimpleName(), "Predictions: " + journeys);
251
+    }
252
+
222 253
     @Override
223 254
     public void onDestroy() {
224 255
         super.onDestroy();

+ 4
- 28
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/DataHelper.java Zobrazit soubor

@@ -53,7 +53,7 @@ public class DataHelper {
53 53
     public static final String JOURNEYSTEPS_TABLE = "journeysteps";
54 54
 
55 55
     private static final String DATABASE_NAME = "contextapi.db";
56
-    private static final int DATABASE_VERSION = 6;
56
+    private static final int DATABASE_VERSION = 7;
57 57
 
58 58
     private static final String INSERT_LOCATION = "insert into "
59 59
       + LOCATIONS_TABLE + "(name, lat, lon) values (?, ?, ?)";
@@ -157,7 +157,7 @@ public class DataHelper {
157 157
     }
158 158
 
159 159
     public void addJourney(final Place start, final Place end, final List<String> activities) {
160
-        final List<JourneyStep> steps = getSteps(activities);
160
+        final List<JourneyStep> steps = JourneyUtil.getSteps(activities);
161 161
         final Collection<Journey> journeys = findJourneys(start, end);
162 162
 
163 163
         for (Journey journey : journeys) {
@@ -189,30 +189,6 @@ public class DataHelper {
189 189
         }
190 190
     }
191 191
 
192
-    protected static List<JourneyStep> getSteps(final List<String> activities) {
193
-        final List<JourneyStep> steps = new LinkedList<JourneyStep>();
194
-
195
-        String last = null;
196
-        int count = 0;
197
-
198
-        for (String activity : activities) {
199
-            if (activity.equals(last)) {
200
-                count++;
201
-            } else {
202
-                if (last != null) {
203
-                    steps.add(new JourneyStep(last, count));
204
-                }
205
-
206
-                count = 1;
207
-                last = activity;
208
-            }
209
-        }
210
-
211
-        steps.add(new JourneyStep(last, count));
212
-
213
-        return steps;
214
-    }
215
-
216 192
     public List<JourneyStep> getSteps(final Journey journey) {
217 193
         final Map<Long, JourneyStep> results
218 194
                 = new HashMap<Long, JourneyStep>(journey.getSteps());
@@ -290,7 +266,7 @@ public class DataHelper {
290 266
                     + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, lon REAL, lat REAL)");
291 267
             db.execSQL("CREATE TABLE " + JOURNEYS_TABLE
292 268
                     + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, start INTEGER,"
293
-                    + " end INTEGER, steps INTEGER)");
269
+                    + " end INTEGER, steps INTEGER, number INTEGER)");
294 270
             db.execSQL("CREATE TABLE " + JOURNEYSTEPS_TABLE
295 271
                     + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, activity TEXT,"
296 272
                     + " repetitions INTEGER, journey INTEGER, next INTEGER)");
@@ -304,7 +280,7 @@ public class DataHelper {
304 280
             if (oldVersion <= 2) {
305 281
                 db.execSQL("DROP TABLE " + LOCATIONS_TABLE);
306 282
                 onCreate(db);
307
-            } else if (oldVersion <= 6) {
283
+            } else if (oldVersion <= 7) {
308 284
                 db.execSQL("DROP TABLE " + LOCATIONS_TABLE);
309 285
                 db.execSQL("DROP TABLE " + JOURNEYS_TABLE);
310 286
                 db.execSQL("DROP TABLE " + JOURNEYSTEPS_TABLE);

+ 81
- 0
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/JourneyUtil.java Zobrazit soubor

@@ -0,0 +1,81 @@
1
+/*
2
+ * Copyright (c) 2009-2010 Chris Smith
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package uk.co.md87.android.contextanalyser;
24
+
25
+import java.util.LinkedList;
26
+import java.util.List;
27
+import uk.co.md87.android.contextanalyser.model.JourneyStep;
28
+
29
+/**
30
+ * Provides utility methods relating to journeys.
31
+ * 
32
+ * @author chris
33
+ */
34
+public class JourneyUtil {
35
+
36
+    public static List<JourneyStep> getSteps(final List<String> activities) {
37
+        final List<JourneyStep> steps = new LinkedList<JourneyStep>();
38
+
39
+        String last = null;
40
+        int count = 0;
41
+
42
+        for (String activity : activities) {
43
+            if (activity.equals(last)) {
44
+                count++;
45
+            } else {
46
+                if (last != null) {
47
+                    steps.add(new JourneyStep(last, count));
48
+                }
49
+
50
+                count = 1;
51
+                last = activity;
52
+            }
53
+        }
54
+
55
+        steps.add(new JourneyStep(last, count));
56
+
57
+        return steps;
58
+    }
59
+
60
+    public static boolean isCompatible(final List<JourneyStep> incomplete,
61
+            final List<JourneyStep> target) {
62
+        for (int i = 0; i < incomplete.size(); i++) {
63
+            final JourneyStep targetStep = target.get(i);
64
+            final JourneyStep incompleteStep = incomplete.get(i);
65
+
66
+            if (targetStep.getActivity().equals(incompleteStep.getActivity()) ||
67
+                    // ^ One of the activities is different
68
+                    (i < incomplete.size() - 1 && incompleteStep.getRepetitions()
69
+                    < targetStep.getRepetitions() * 0.5) ||
70
+                    // ^ Or a completed step is too short
71
+                    (incompleteStep.getRepetitions() > targetStep.getRepetitions() * 1.5)
72
+                    // ^ Or any step is too long
73
+                    ) {
74
+                return false;
75
+            }
76
+        }
77
+
78
+        return true;
79
+    }
80
+
81
+}

+ 6
- 0
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/model/Journey.java Zobrazit soubor

@@ -65,4 +65,10 @@ public class Journey {
65 65
         return number;
66 66
     }
67 67
 
68
+    @Override
69
+    public String toString() {
70
+        return "Journey{" + "id=" + id + " start=" + start + " end=" + end
71
+                + " steps=" + steps + " number=" + number + '}';
72
+    }
73
+
68 74
 }

Načítá se…
Zrušit
Uložit