Переглянути джерело

Add places content provider

master
Chris Smith 14 роки тому
джерело
коміт
0b2abf94ae

+ 3
- 0
code/ContextAnalyser/AndroidManifest.xml Переглянути файл

@@ -10,6 +10,9 @@
10 10
         </activity>
11 11
 
12 12
         <service android:name=".ContextAnalyserService" android:label="Context Analyser Service"/>
13
+
14
+        <provider android:name="uk.co.md87.android.contextanalyser.PlacesContentProvider"
15
+            android:authorities="uk.co.md87.android.contextanalyser.placescontentprovider" />
13 16
     </application>
14 17
 
15 18
     <permission android:name="uk.co.md87.android.contextanalyser.RECEIVE_UPDATES"

BIN
code/ContextAnalyser/dist/ContextAnalyser.apk Переглянути файл


+ 5
- 1
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/DataHelper.java Переглянути файл

@@ -40,7 +40,7 @@ import java.util.Map;
40 40
  */
41 41
 public class DataHelper {
42 42
 
43
-    private static final String LOCATIONS_TABLE = "locations";
43
+    public static final String LOCATIONS_TABLE = "locations";
44 44
     private static final String DATABASE_NAME = "contextapi.db";
45 45
     private static final int DATABASE_VERSION = 2;
46 46
 
@@ -66,6 +66,10 @@ public class DataHelper {
66 66
         this.updateLocationStatement = db.compileStatement(UPDATE_LOCATION);
67 67
     }
68 68
 
69
+    public SQLiteDatabase getDatabase() {
70
+        return db;
71
+    }
72
+
69 73
     public long addLocation(final String name, final double lat, final double lon) {
70 74
         Log.i(getClass().getSimpleName(), "Adding new place at " + lat + ", " + lon);
71 75
         insertLocationStatement.bindString(1, name);

+ 6
- 0
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/Place.java Переглянути файл

@@ -22,6 +22,8 @@
22 22
 
23 23
 package uk.co.md87.android.contextanalyser;
24 24
 
25
+import android.net.Uri;
26
+
25 27
 /**
26 28
  * A place is a named location which has some significance for the user. Most
27 29
  * places come about when the user remains stationary for a period of time.
@@ -35,6 +37,10 @@ public class Place {
35 37
     public static final String LATITUDE = "lat";
36 38
     public static final String LONGITUDE = "lon";
37 39
 
40
+    public static final Uri CONTENT_URI = Uri.parse("content://"
41
+                + PlacesContentProvider.AUTHORITY + "/places");
42
+    public static final String CONTENT_TYPE = "vnd.contextanalyser.location";
43
+
38 44
     private final long id;
39 45
     private final String name;
40 46
     private final double lat;

+ 111
- 0
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/PlacesContentProvider.java Переглянути файл

@@ -0,0 +1,111 @@
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.ContentProvider;
9
+import android.content.ContentUris;
10
+import android.content.ContentValues;
11
+import android.content.UriMatcher;
12
+import android.database.Cursor;
13
+import android.database.SQLException;
14
+import android.net.Uri;
15
+
16
+/**
17
+ * A content provider for places.
18
+ * 
19
+ * @author chris
20
+ */
21
+public class PlacesContentProvider extends ContentProvider {
22
+
23
+    public static final String AUTHORITY = "uk.co.md87.android.contextanalyser.placescontentprovider";
24
+
25
+    private static final int CODE_PLACES = 1;
26
+    private static final UriMatcher URI_MATCHER;
27
+
28
+    static {
29
+        URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
30
+        URI_MATCHER.addURI(AUTHORITY, "places", CODE_PLACES);
31
+    }
32
+
33
+    private DataHelper helper;
34
+
35
+    /** {@inheritDoc} */
36
+    @Override
37
+    public boolean onCreate() {
38
+        helper = new DataHelper(getContext());
39
+        return true;
40
+    }
41
+
42
+    /** {@inheritDoc} */
43
+    @Override
44
+    public Cursor query(final Uri uri, final String[] projection,
45
+            final String selection, final String[] selectionArgs,
46
+            final String sortOrder) {
47
+        if (URI_MATCHER.match(uri) != CODE_PLACES) {
48
+            throw new IllegalArgumentException("Unknown URI " + uri);
49
+        }
50
+
51
+        return helper.getDatabase().query(DataHelper.LOCATIONS_TABLE,
52
+                projection, selection, selectionArgs, null, null, sortOrder);
53
+    }
54
+
55
+    /** {@inheritDoc} */
56
+    @Override
57
+    public String getType(final Uri uri) {
58
+        if (URI_MATCHER.match(uri) != CODE_PLACES) {
59
+            throw new IllegalArgumentException("Unknown URI " + uri);
60
+        }
61
+
62
+        return "vnd.android.cursor." + (ContentUris.parseId(uri) == -1 ? "dir" : "item")
63
+                + Place.CONTENT_TYPE;
64
+    }
65
+
66
+    /** {@inheritDoc} */
67
+    @Override
68
+    public Uri insert(final Uri uri, final ContentValues values) {
69
+        if (URI_MATCHER.match(uri) != CODE_PLACES) {
70
+            throw new IllegalArgumentException("Unknown URI " + uri);
71
+        }
72
+
73
+        long rowId = helper.getDatabase().insert(DataHelper.LOCATIONS_TABLE, Place.NAME, values);
74
+
75
+        if (rowId > 0) {
76
+            final Uri placeUri = ContentUris.withAppendedId(Place.CONTENT_URI, rowId);
77
+            getContext().getContentResolver().notifyChange(placeUri, null);
78
+            return placeUri;
79
+        }
80
+
81
+        throw new SQLException("Failed to insert row into " + uri);
82
+    }
83
+
84
+    /** {@inheritDoc} */
85
+    @Override
86
+    public int delete(Uri uri, String where, String[] whereArgs) {
87
+        if (URI_MATCHER.match(uri) != CODE_PLACES) {
88
+            throw new IllegalArgumentException("Unknown URI " + uri);
89
+        }
90
+
91
+        final int count = helper.getDatabase().delete(DataHelper.LOCATIONS_TABLE, where, whereArgs);
92
+
93
+        getContext().getContentResolver().notifyChange(uri, null);
94
+        return count;
95
+    }
96
+
97
+    /** {@inheritDoc} */
98
+    @Override
99
+    public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
100
+        if (URI_MATCHER.match(uri) != CODE_PLACES) {
101
+            throw new IllegalArgumentException("Unknown URI " + uri);
102
+        }
103
+
104
+        final int count = helper.getDatabase().update(DataHelper.LOCATIONS_TABLE,
105
+                values, where, whereArgs);
106
+
107
+        getContext().getContentResolver().notifyChange(uri, null);
108
+        return count;
109
+    }
110
+
111
+}

Завантаження…
Відмінити
Зберегти