瀏覽代碼

Add journey and journey step classes

Reorganise into a model package
Add methods for finding journeys
master
Chris Smith 14 年之前
父節點
當前提交
279bc79cb6

+ 1
- 0
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/ContextAnalyserService.java 查看文件

@@ -22,6 +22,7 @@
22 22
 
23 23
 package uk.co.md87.android.contextanalyser;
24 24
 
25
+import uk.co.md87.android.contextanalyser.model.Place;
25 26
 import android.app.Service;
26 27
 import android.content.Intent;
27 28
 import android.location.Address;

+ 41
- 1
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/DataHelper.java 查看文件

@@ -30,9 +30,14 @@ import android.database.sqlite.SQLiteStatement;
30 30
 import android.location.Location;
31 31
 import android.util.Log;
32 32
 
33
+import java.util.Collection;
33 34
 import java.util.HashMap;
35
+import java.util.LinkedList;
34 36
 import java.util.Map;
35 37
 
38
+import uk.co.md87.android.contextanalyser.model.Journey;
39
+import uk.co.md87.android.contextanalyser.model.Place;
40
+
36 41
 /**
37 42
  * Facilitates accessing the SQLite database used for storing places and
38 43
  * journeys.
@@ -55,6 +60,8 @@ public class DataHelper {
55 60
     private static final String UNNAMED_QUERY = "name LIKE '%.%,%.%'";
56 61
     private static final String LOCATION_QUERY = "lat > %1$s - 0.005 and "
57 62
             + "lat < %1$s + 0.005 and lon > %2$s - 0.01 and lon < %2$s + 0.01";
63
+    private static final String JOURNEY_START_QUERY = "start = %1$s";
64
+    private static final String JOURNEY_BOTH_QUERY = JOURNEY_START_QUERY + " AND end = %1$s";
58 65
 
59 66
     private final SQLiteStatement insertLocationStatement, updateLocationStatement;
60 67
 
@@ -106,6 +113,39 @@ public class DataHelper {
106 113
         return results;
107 114
     }
108 115
 
116
+    public Collection<Journey> findJourneys(final Place start) {
117
+        return findJourneys(start, null);
118
+    }
119
+
120
+    public Collection<Journey> findJourneys(final Place start,
121
+            final Place end) {
122
+        final Collection<Journey> results = new LinkedList<Journey>();
123
+
124
+        final String query;
125
+        if (end == null) {
126
+            query = String.format(JOURNEY_START_QUERY, start.getId());
127
+        } else {
128
+            query = String.format(JOURNEY_BOTH_QUERY, start.getId(), end.getId());
129
+        }
130
+
131
+        final Cursor cursor = db.query(JOURNEYS_TABLE,
132
+                new String[] { "_id", "start", "end", "steps", "first" },
133
+                query, null, null, null, null);
134
+
135
+        if (cursor.moveToFirst()) {
136
+            do {
137
+                results.add(new Journey(cursor.getLong(0), cursor.getLong(1),
138
+                        cursor.getLong(2), cursor.getInt(3), cursor.getLong(4)));
139
+            } while (cursor.moveToNext());
140
+        }
141
+
142
+        if (cursor != null && !cursor.isClosed()) {
143
+            cursor.close();
144
+        }
145
+
146
+        return results;
147
+    }
148
+
109 149
     public Place findLocation(final double lat, final double lon) {
110 150
         final Cursor cursor = db.query(LOCATIONS_TABLE,
111 151
                 new String[] { Place._ID, Place.NAME, Place.LATITUDE, Place.LONGITUDE },
@@ -146,7 +186,7 @@ public class DataHelper {
146 186
                     + " end INTEGER, steps INTEGER, first INTEGER)");
147 187
             db.execSQL("CREATE TABLE " + JOURNEYSTEPS_TABLE
148 188
                     + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, activity TEXT,"
149
-                    + " repetitions INTEGER, next INTEGER)");
189
+                    + " repetitions INTEGER, journey INTEGER, next INTEGER)");
150 190
         }
151 191
 
152 192
         /** {@inheritDoc} */

+ 2
- 0
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/PlacesContentProvider.java 查看文件

@@ -13,6 +13,8 @@ import android.database.Cursor;
13 13
 import android.database.SQLException;
14 14
 import android.net.Uri;
15 15
 
16
+import uk.co.md87.android.contextanalyser.model.Place;
17
+
16 18
 /**
17 19
  * A content provider for places.
18 20
  * 

+ 68
- 0
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/model/Journey.java 查看文件

@@ -0,0 +1,68 @@
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.model;
24
+
25
+/**
26
+ * A journey is a sequence of actions which were observed as the user travelled
27
+ * between one known {@link Place} and another.
28
+ *
29
+ * @author chris
30
+ */
31
+public class Journey {
32
+
33
+    private final long id;
34
+    private final long start;
35
+    private final long end;
36
+    private final int steps;
37
+    private final long first;
38
+
39
+    public Journey(final long id, final long start, final long end,
40
+            final int steps, final long first) {
41
+        this.id = id;
42
+        this.start = start;
43
+        this.end = end;
44
+        this.steps = steps;
45
+        this.first = first;
46
+    }
47
+
48
+    public long getEnd() {
49
+        return end;
50
+    }
51
+
52
+    public long getFirst() {
53
+        return first;
54
+    }
55
+
56
+    public long getId() {
57
+        return id;
58
+    }
59
+
60
+    public long getStart() {
61
+        return start;
62
+    }
63
+
64
+    public int getSteps() {
65
+        return steps;
66
+    }
67
+
68
+}

+ 68
- 0
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/model/JourneyStep.java 查看文件

@@ -0,0 +1,68 @@
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.model;
24
+
25
+/**
26
+ * A journey step is a single activity which occurred one or more times in a
27
+ * {@link Journey}. The activity may be repeated any number of times in a row.
28
+ *
29
+ * @author chris
30
+ */
31
+public class JourneyStep {
32
+
33
+    private final long id;
34
+    private final String activity;
35
+    private final int repetitions;
36
+    private final long journey;
37
+    private final long next;
38
+
39
+    public JourneyStep(final long id, final String activity,
40
+            final int repetitions, final long journey, final long next) {
41
+        this.id = id;
42
+        this.activity = activity;
43
+        this.repetitions = repetitions;
44
+        this.journey = journey;
45
+        this.next = next;
46
+    }
47
+
48
+    public String getActivity() {
49
+        return activity;
50
+    }
51
+
52
+    public long getId() {
53
+        return id;
54
+    }
55
+
56
+    public long getJourney() {
57
+        return journey;
58
+    }
59
+
60
+    public long getNext() {
61
+        return next;
62
+    }
63
+
64
+    public int getRepetitions() {
65
+        return repetitions;
66
+    }
67
+
68
+}

code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/Place.java → code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/model/Place.java 查看文件

@@ -20,10 +20,12 @@
20 20
  * SOFTWARE.
21 21
  */
22 22
 
23
-package uk.co.md87.android.contextanalyser;
23
+package uk.co.md87.android.contextanalyser.model;
24 24
 
25 25
 import android.net.Uri;
26 26
 
27
+import uk.co.md87.android.contextanalyser.PlacesContentProvider;
28
+
27 29
 /**
28 30
  * A place is a named location which has some significance for the user. Most
29 31
  * places come about when the user remains stationary for a period of time.

Loading…
取消
儲存