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