Browse Source

Method to store journeys

master
Chris Smith 14 years ago
parent
commit
c1bf7da238

+ 1
- 1
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/ContextAnalyserService.java View File

@@ -22,7 +22,6 @@
22 22
 
23 23
 package uk.co.md87.android.contextanalyser;
24 24
 
25
-import uk.co.md87.android.contextanalyser.model.Place;
26 25
 import android.app.Service;
27 26
 import android.content.Intent;
28 27
 import android.location.Address;
@@ -46,6 +45,7 @@ import uk.co.md87.android.common.accel.AccelReaderFactory;
46 45
 import uk.co.md87.android.common.accel.Sampler;
47 46
 import uk.co.md87.android.common.geo.LocationMonitor;
48 47
 import uk.co.md87.android.common.geo.LocationMonitorFactory;
48
+import uk.co.md87.android.contextanalyser.model.Place;
49 49
 
50 50
 /**
51 51
  * Background service which monitors and aggregates various sources of

+ 110
- 5
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/DataHelper.java View File

@@ -33,9 +33,11 @@ import android.util.Log;
33 33
 import java.util.Collection;
34 34
 import java.util.HashMap;
35 35
 import java.util.LinkedList;
36
+import java.util.List;
36 37
 import java.util.Map;
37 38
 
38 39
 import uk.co.md87.android.contextanalyser.model.Journey;
40
+import uk.co.md87.android.contextanalyser.model.JourneyStep;
39 41
 import uk.co.md87.android.contextanalyser.model.Place;
40 42
 
41 43
 /**
@@ -51,19 +53,25 @@ public class DataHelper {
51 53
     public static final String JOURNEYSTEPS_TABLE = "journeysteps";
52 54
 
53 55
     private static final String DATABASE_NAME = "contextapi.db";
54
-    private static final int DATABASE_VERSION = 3;
56
+    private static final int DATABASE_VERSION = 4;
55 57
 
56 58
     private static final String INSERT_LOCATION = "insert into "
57 59
       + LOCATIONS_TABLE + "(name, lat, lon) values (?, ?, ?)";
60
+    private static final String INSERT_JOURNEY = "insert into "
61
+      + JOURNEYS_TABLE + "(start, end, steps) values (?, ?, ?)";
62
+    private static final String INSERT_JOURNEYSTEP = "insert into "
63
+      + JOURNEYSTEPS_TABLE + "(activity, reptitions, journey, next) values (?, ?, ?, ?)";
58 64
     private static final String UPDATE_LOCATION = "update "
59 65
       + LOCATIONS_TABLE + " set name = ? where _id = ?";
60 66
     private static final String UNNAMED_QUERY = "name LIKE '%.%,%.%'";
61 67
     private static final String LOCATION_QUERY = "lat > %1$s - 0.005 and "
62 68
             + "lat < %1$s + 0.005 and lon > %2$s - 0.01 and lon < %2$s + 0.01";
69
+    private static final String JOURNEY_STEPS_QUERY = "journey = %1$s";
63 70
     private static final String JOURNEY_START_QUERY = "start = %1$s";
64 71
     private static final String JOURNEY_BOTH_QUERY = JOURNEY_START_QUERY + " AND end = %1$s";
65 72
 
66
-    private final SQLiteStatement insertLocationStatement, updateLocationStatement;
73
+    private final SQLiteStatement insertLocationStatement, insertJourneyStatement,
74
+            insertJourneyStepStatement, updateLocationStatement;
67 75
 
68 76
     private SQLiteDatabase db;
69 77
 
@@ -72,6 +80,8 @@ public class DataHelper {
72 80
         this.db = helper.getWritableDatabase();
73 81
         this.insertLocationStatement = db.compileStatement(INSERT_LOCATION);
74 82
         this.updateLocationStatement = db.compileStatement(UPDATE_LOCATION);
83
+        this.insertJourneyStatement = db.compileStatement(INSERT_JOURNEY);
84
+        this.insertJourneyStepStatement = db.compileStatement(INSERT_JOURNEYSTEP);
75 85
     }
76 86
 
77 87
     public SQLiteDatabase getDatabase() {
@@ -129,13 +139,13 @@ public class DataHelper {
129 139
         }
130 140
 
131 141
         final Cursor cursor = db.query(JOURNEYS_TABLE,
132
-                new String[] { "_id", "start", "end", "steps", "first" },
142
+                new String[] { "_id", "start", "end", "steps" },
133 143
                 query, null, null, null, null);
134 144
 
135 145
         if (cursor.moveToFirst()) {
136 146
             do {
137 147
                 results.add(new Journey(cursor.getLong(0), cursor.getLong(1),
138
-                        cursor.getLong(2), cursor.getInt(3), cursor.getLong(4)));
148
+                        cursor.getLong(2), cursor.getInt(3)));
139 149
             } while (cursor.moveToNext());
140 150
         }
141 151
 
@@ -146,6 +156,96 @@ public class DataHelper {
146 156
         return results;
147 157
     }
148 158
 
159
+    public void addJourney(final Place start, final Place end, final List<String> activities) {
160
+        final List<JourneyStep> steps = getSteps(activities);
161
+        final Collection<Journey> journeys = findJourneys(start, end);
162
+
163
+        for (Journey journey : journeys) {
164
+            if (journey.getSteps() == steps.size()) {
165
+                final List<JourneyStep> theirSteps = getSteps(journey);
166
+
167
+                if (theirSteps.equals(steps)) {
168
+                    // TODO: Increment journey count/time/etc
169
+                    return;
170
+                }
171
+            }
172
+        }
173
+
174
+        insertJourneyStatement.bindLong(1, start.getId());
175
+        insertJourneyStatement.bindLong(2, end.getId());
176
+        insertJourneyStatement.bindLong(3, steps.size());
177
+        final long id = insertJourneyStatement.executeInsert();
178
+
179
+        long next = 0;
180
+        for (int i = steps.size() - 1; i >= 0; i--) {
181
+            final JourneyStep step = steps.get(i);
182
+
183
+            insertJourneyStepStatement.bindString(1, step.getActivity());
184
+            insertJourneyStepStatement.bindLong(2, step.getRepetitions());
185
+            insertJourneyStepStatement.bindLong(3, id);
186
+            insertJourneyStepStatement.bindLong(4, next);
187
+            next = insertJourneyStepStatement.executeInsert();
188
+        }
189
+    }
190
+
191
+    protected static List<JourneyStep> getSteps(final List<String> activities) {
192
+        final List<JourneyStep> steps = new LinkedList<JourneyStep>();
193
+
194
+        String last = null;
195
+        int count = 0;
196
+
197
+        for (String activity : activities) {
198
+            if (activity.equals(last)) {
199
+                count++;
200
+            } else {
201
+                if (last != null) {
202
+                    steps.add(new JourneyStep(last, count));
203
+                }
204
+
205
+                count = 1;
206
+                last = activity;
207
+            }
208
+        }
209
+
210
+        steps.add(new JourneyStep(last, count));
211
+
212
+        return steps;
213
+    }
214
+
215
+    public List<JourneyStep> getSteps(final Journey journey) {
216
+        final Map<Long, JourneyStep> results
217
+                = new HashMap<Long, JourneyStep>(journey.getSteps());
218
+
219
+        final String query = String.format(JOURNEY_STEPS_QUERY, journey.getId());
220
+
221
+        final Cursor cursor = db.query(JOURNEYSTEPS_TABLE,
222
+                new String[] { "_id", "activity", "repetitions", "next" },
223
+                query, null, null, null, null);
224
+
225
+        if (cursor.moveToFirst()) {
226
+            do {
227
+                results.put(cursor.getLong(3),
228
+                        new JourneyStep(cursor.getLong(0), cursor.getString(1),
229
+                        cursor.getInt(2), journey.getId(), cursor.getLong(3)));
230
+            } while (cursor.moveToNext());
231
+        }
232
+
233
+        if (cursor != null && !cursor.isClosed()) {
234
+            cursor.close();
235
+        }
236
+
237
+        final List<JourneyStep> ordered = new LinkedList<JourneyStep>();
238
+
239
+        long previous = 0;
240
+        while (results.containsKey(previous)) {
241
+            final JourneyStep step = results.get(previous);
242
+            ordered.add(step);
243
+            previous = step.getId();
244
+        }
245
+
246
+        return ordered;
247
+    }
248
+
149 249
     public Place findLocation(final double lat, final double lon) {
150 250
         final Cursor cursor = db.query(LOCATIONS_TABLE,
151 251
                 new String[] { Place._ID, Place.NAME, Place.LATITUDE, Place.LONGITUDE },
@@ -183,7 +283,7 @@ public class DataHelper {
183 283
                     + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, lon REAL, lat REAL)");
184 284
             db.execSQL("CREATE TABLE " + JOURNEYS_TABLE
185 285
                     + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, start INTEGER,"
186
-                    + " end INTEGER, steps INTEGER, first INTEGER)");
286
+                    + " end INTEGER, steps INTEGER)");
187 287
             db.execSQL("CREATE TABLE " + JOURNEYSTEPS_TABLE
188 288
                     + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, activity TEXT,"
189 289
                     + " repetitions INTEGER, journey INTEGER, next INTEGER)");
@@ -197,6 +297,11 @@ public class DataHelper {
197 297
             if (oldVersion <= 2) {
198 298
                 db.execSQL("DROP TABLE " + LOCATIONS_TABLE);
199 299
                 onCreate(db);
300
+            } else if (oldVersion <= 3) {
301
+                db.execSQL("DROP TABLE " + LOCATIONS_TABLE);
302
+                db.execSQL("DROP TABLE " + JOURNEYS_TABLE);
303
+                db.execSQL("DROP TABLE " + JOURNEYSTEPS_TABLE);
304
+                onCreate(db);
200 305
             }
201 306
         }
202 307
 

+ 1
- 7
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/model/Journey.java View File

@@ -34,25 +34,19 @@ public class Journey {
34 34
     private final long start;
35 35
     private final long end;
36 36
     private final int steps;
37
-    private final long first;
38 37
 
39 38
     public Journey(final long id, final long start, final long end,
40
-            final int steps, final long first) {
39
+            final int steps) {
41 40
         this.id = id;
42 41
         this.start = start;
43 42
         this.end = end;
44 43
         this.steps = steps;
45
-        this.first = first;
46 44
     }
47 45
 
48 46
     public long getEnd() {
49 47
         return end;
50 48
     }
51 49
 
52
-    public long getFirst() {
53
-        return first;
54
-    }
55
-
56 50
     public long getId() {
57 51
         return id;
58 52
     }

+ 25
- 0
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/model/JourneyStep.java View File

@@ -36,6 +36,10 @@ public class JourneyStep {
36 36
     private final long journey;
37 37
     private final long next;
38 38
 
39
+    public JourneyStep(final String activity, final int repetitions) {
40
+        this(-1, activity, repetitions, -1, -1);
41
+    }
42
+
39 43
     public JourneyStep(final long id, final String activity,
40 44
             final int repetitions, final long journey, final long next) {
41 45
         this.id = id;
@@ -65,4 +69,25 @@ public class JourneyStep {
65 69
         return repetitions;
66 70
     }
67 71
 
72
+    /** {@inheritDoc} */
73
+    @Override
74
+    public boolean equals(final Object obj) {
75
+        if (obj == null || getClass() != obj.getClass()) {
76
+            return false;
77
+        }
78
+        
79
+        final JourneyStep other = (JourneyStep) obj;
80
+        return this.activity.equals(other.getActivity())
81
+                && this.repetitions == other.getRepetitions();
82
+    }
83
+
84
+    /** {@inheritDoc} */
85
+    @Override
86
+    public int hashCode() {
87
+        int hash = 7;
88
+        hash = 37 * hash + this.activity.hashCode();
89
+        hash = 37 * hash + this.repetitions;
90
+        return hash;
91
+    }
92
+
68 93
 }

Loading…
Cancel
Save