|
@@ -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
|
|