|
@@ -0,0 +1,127 @@
|
|
1
|
+/*
|
|
2
|
+ * To change this template, choose Tools | Templates
|
|
3
|
+ * and open the template in the editor.
|
|
4
|
+ */
|
|
5
|
+
|
|
6
|
+package uk.co.md87.android.contextanalyser;
|
|
7
|
+
|
|
8
|
+import android.content.Context;
|
|
9
|
+import android.database.Cursor;
|
|
10
|
+import android.database.sqlite.SQLiteDatabase;
|
|
11
|
+import android.database.sqlite.SQLiteOpenHelper;
|
|
12
|
+import android.database.sqlite.SQLiteStatement;
|
|
13
|
+import android.location.Location;
|
|
14
|
+
|
|
15
|
+/**
|
|
16
|
+ *
|
|
17
|
+ * @author chris
|
|
18
|
+ */
|
|
19
|
+public class DataHelper {
|
|
20
|
+
|
|
21
|
+ private static final String LOCATIONS_TABLE = "locations";
|
|
22
|
+ private static final String DATABASE_NAME = "contextapi.db";
|
|
23
|
+ private static final int DATABASE_VERSION = 1;
|
|
24
|
+
|
|
25
|
+ private static final String INSERT_LOCATION = "insert into "
|
|
26
|
+ + LOCATIONS_TABLE + "(name, lat, lon) values (?, ?, ?)";
|
|
27
|
+ private static final String LOCATION_QUERY = "lat > %1$s - 0.005 and "
|
|
28
|
+ + "lat < %1$s + 0.005 and lon > %2$s - 0.01 and lon < %2$s + 0.01";
|
|
29
|
+
|
|
30
|
+ private final SQLiteStatement insertLocationStatement;
|
|
31
|
+
|
|
32
|
+ private final Context context;
|
|
33
|
+ private SQLiteDatabase db;
|
|
34
|
+
|
|
35
|
+ public DataHelper(final Context context) {
|
|
36
|
+ this.context = context;
|
|
37
|
+
|
|
38
|
+ final OpenHelper helper = new OpenHelper(context);
|
|
39
|
+ this.db = helper.getWritableDatabase();
|
|
40
|
+ this.insertLocationStatement = db.compileStatement(INSERT_LOCATION);
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ public long addLocation(final String name, final double lat, final double lon) {
|
|
44
|
+ insertLocationStatement.bindString(1, name);
|
|
45
|
+ insertLocationStatement.bindDouble(2, lat);
|
|
46
|
+ insertLocationStatement.bindDouble(3, lon);
|
|
47
|
+ return insertLocationStatement.executeInsert();
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ public LocationResult findLocation(final double lat, final double lon) {
|
|
51
|
+ final Cursor cursor = db.query(LOCATIONS_TABLE,
|
|
52
|
+ new String[] { "id", "name", "lat", "lon" },
|
|
53
|
+ String.format(LOCATION_QUERY, lat, lon), null, null, null, null);
|
|
54
|
+
|
|
55
|
+ if (cursor.moveToFirst()) {
|
|
56
|
+ do {
|
|
57
|
+ final float[] res = new float[1];
|
|
58
|
+ Location.distanceBetween(lat, lon, cursor.getDouble(2), cursor.getDouble(3), res);
|
|
59
|
+
|
|
60
|
+ if (res[0] <= 500) {
|
|
61
|
+ return new LocationResult(cursor.getLong(0), cursor.getString(1),
|
|
62
|
+ cursor.getDouble(2), cursor.getDouble(3));
|
|
63
|
+ }
|
|
64
|
+ } while (cursor.moveToNext());
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ if (cursor != null && !cursor.isClosed()) {
|
|
68
|
+ cursor.close();
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ return null;
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ public static class LocationResult {
|
|
75
|
+
|
|
76
|
+ private final long id;
|
|
77
|
+ private final String name;
|
|
78
|
+ private final double lat, lon;
|
|
79
|
+
|
|
80
|
+ public LocationResult(long id, String name, double lat, double lon) {
|
|
81
|
+ this.id = id;
|
|
82
|
+ this.name = name;
|
|
83
|
+ this.lat = lat;
|
|
84
|
+ this.lon = lon;
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ public long getId() {
|
|
88
|
+ return id;
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ public double getLat() {
|
|
92
|
+ return lat;
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ public double getLon() {
|
|
96
|
+ return lon;
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ public String getName() {
|
|
100
|
+ return name;
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ private static class OpenHelper extends SQLiteOpenHelper {
|
|
106
|
+
|
|
107
|
+ public OpenHelper(final Context context) {
|
|
108
|
+ super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ /** {@inheritDoc} */
|
|
112
|
+ @Override
|
|
113
|
+ public void onCreate(final SQLiteDatabase db) {
|
|
114
|
+ db.execSQL("CREATE TABLE " + LOCATIONS_TABLE
|
|
115
|
+ + " (id INTEGER PRIMARY KEY, name TEXT, lon REAL, lat REAL)");
|
|
116
|
+ }
|
|
117
|
+
|
|
118
|
+ /** {@inheritDoc} */
|
|
119
|
+ @Override
|
|
120
|
+ public void onUpgrade(final SQLiteDatabase db, final int oldVersion,
|
|
121
|
+ final int newVersion) {
|
|
122
|
+ // Do nothing. Yet.
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+}
|