Browse Source

Add activites content provider

master
Chris Smith 14 years ago
parent
commit
fb11298115

+ 3
- 0
code/ContextAnalyser/AndroidManifest.xml View File

@@ -20,6 +20,9 @@
20 20
             android:authorities="uk.co.md87.android.contextanalyser.journeyscontentprovider"
21 21
             android:readPermission="uk.co.md87.android.contextanalyser.READ_JOURNEYS"
22 22
             android:writePermission="uk.co.md87.android.contextanalyser.WRITE_JOURNEYS"/>
23
+
24
+        <provider android:name="uk.co.md87.android.contextanalyser.ActivitiesContentProvider"
25
+            android:authorities="uk.co.md87.android.contextanalyser.activitiescontentprovider"/>
23 26
     </application>
24 27
 
25 28
     <permission-group android:description="@string/permgroupdesc"

BIN
code/ContextAnalyser/dist/ContextAnalyser.apk View File


+ 145
- 0
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/ActivitiesContentProvider.java View File

@@ -0,0 +1,145 @@
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;
24
+
25
+import android.content.ComponentName;
26
+import android.content.ContentProvider;
27
+import android.content.ContentUris;
28
+import android.content.ContentValues;
29
+import android.content.Context;
30
+import android.content.Intent;
31
+import android.content.ServiceConnection;
32
+import android.content.UriMatcher;
33
+import android.database.Cursor;
34
+import android.database.MatrixCursor;
35
+import android.net.Uri;
36
+import android.os.IBinder;
37
+import android.os.RemoteException;
38
+
39
+import uk.co.md87.android.common.model.Place;
40
+import uk.co.md87.android.contextanalyser.rpc.ContextAnalyserBinder;
41
+
42
+/**
43
+ * A content provider for activities.
44
+ * 
45
+ * @author chris
46
+ */
47
+public class ActivitiesContentProvider extends ContentProvider {
48
+
49
+    public static final String AUTHORITY
50
+            = "uk.co.md87.android.contextanalyser.activitiescontentprovider";
51
+
52
+    private static final int CODE_CURRENT = 1;
53
+    private static final UriMatcher URI_MATCHER;
54
+
55
+    static {
56
+        URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
57
+        URI_MATCHER.addURI(AUTHORITY, "current", CODE_CURRENT);
58
+    }
59
+    
60
+    private ContextAnalyserBinder service;
61
+
62
+    private final ServiceConnection connection = new ServiceConnection() {
63
+
64
+        public void onServiceConnected(ComponentName arg0, IBinder arg1) {
65
+            service = ContextAnalyserBinder.Stub.asInterface(arg1);
66
+        }
67
+
68
+        public void onServiceDisconnected(ComponentName arg0) {
69
+            service = null;
70
+            onCreate();
71
+        }
72
+    };
73
+
74
+    /** {@inheritDoc} */
75
+    @Override
76
+    public boolean onCreate() {
77
+        getContext().bindService(new Intent(getContext(),
78
+                ContextAnalyserService.class), connection, Context.BIND_AUTO_CREATE);
79
+        return true;
80
+    }
81
+
82
+    /** {@inheritDoc} */
83
+    @Override
84
+    public Cursor query(final Uri uri, final String[] projection,
85
+            final String selection, final String[] selectionArgs,
86
+            final String sortOrder) {
87
+        if (URI_MATCHER.match(uri) != CODE_CURRENT) {
88
+            throw new IllegalArgumentException("Unknown URI " + uri);
89
+        }
90
+
91
+        if (service == null) {
92
+            throw new IllegalStateException("Unable to bind to service");
93
+        }
94
+
95
+        try {
96
+            final MatrixCursor cursor = new MatrixCursor(new String[]{"activity"}, 1);
97
+            cursor.addRow(new Object[]{service.getActivity()});
98
+            return cursor;
99
+        } catch (RemoteException ex) {
100
+            throw new RuntimeException("Unable to retrieve activity", ex);
101
+        }
102
+    }
103
+
104
+    /** {@inheritDoc} */
105
+    @Override
106
+    public String getType(final Uri uri) {
107
+        if (URI_MATCHER.match(uri) != CODE_CURRENT) {
108
+            throw new IllegalArgumentException("Unknown URI " + uri);
109
+        }
110
+
111
+        return "vnd.android.cursor." + (ContentUris.parseId(uri) == -1 ? "dir" : "item")
112
+                + Place.CONTENT_TYPE;
113
+    }
114
+
115
+    /** {@inheritDoc} */
116
+    @Override
117
+    public Uri insert(final Uri uri, final ContentValues values) {
118
+        if (URI_MATCHER.match(uri) != CODE_CURRENT) {
119
+            throw new IllegalArgumentException("Unknown URI " + uri);
120
+        }
121
+
122
+        throw new UnsupportedOperationException("Cannot insert");
123
+    }
124
+
125
+    /** {@inheritDoc} */
126
+    @Override
127
+    public int delete(Uri uri, String where, String[] whereArgs) {
128
+        if (URI_MATCHER.match(uri) != CODE_CURRENT) {
129
+            throw new IllegalArgumentException("Unknown URI " + uri);
130
+        }
131
+
132
+        throw new UnsupportedOperationException("Cannot delete");
133
+    }
134
+
135
+    /** {@inheritDoc} */
136
+    @Override
137
+    public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
138
+        if (URI_MATCHER.match(uri) != CODE_CURRENT) {
139
+            throw new IllegalArgumentException("Unknown URI " + uri);
140
+        }
141
+
142
+        throw new UnsupportedOperationException("Cannot update");
143
+    }
144
+
145
+}

Loading…
Cancel
Save