|
@@ -0,0 +1,154 @@
|
|
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 java.util.Map;
|
|
40
|
+
|
|
41
|
+import uk.co.md87.android.contextanalyser.rpc.ContextAnalyserBinder;
|
|
42
|
+
|
|
43
|
+/**
|
|
44
|
+ * A content provider for predictions.
|
|
45
|
+ *
|
|
46
|
+ * @author chris
|
|
47
|
+ */
|
|
48
|
+public class PredictionsContentProvider extends ContentProvider {
|
|
49
|
+
|
|
50
|
+ public static final String AUTHORITY
|
|
51
|
+ = "uk.co.md87.android.contextanalyser.predictionscontentprovider";
|
|
52
|
+
|
|
53
|
+ private static final int CODE_DESTINATION = 1;
|
|
54
|
+ private static final UriMatcher URI_MATCHER;
|
|
55
|
+
|
|
56
|
+ static {
|
|
57
|
+ URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
|
|
58
|
+ URI_MATCHER.addURI(AUTHORITY, "destination", CODE_DESTINATION);
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ private ContextAnalyserBinder service;
|
|
62
|
+
|
|
63
|
+ private final ServiceConnection connection = new ServiceConnection() {
|
|
64
|
+
|
|
65
|
+ public void onServiceConnected(ComponentName arg0, IBinder arg1) {
|
|
66
|
+ service = ContextAnalyserBinder.Stub.asInterface(arg1);
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ public void onServiceDisconnected(ComponentName arg0) {
|
|
70
|
+ service = null;
|
|
71
|
+ onCreate();
|
|
72
|
+ }
|
|
73
|
+ };
|
|
74
|
+
|
|
75
|
+ /** {@inheritDoc} */
|
|
76
|
+ @Override
|
|
77
|
+ public boolean onCreate() {
|
|
78
|
+ getContext().bindService(new Intent(getContext(),
|
|
79
|
+ ContextAnalyserService.class), connection, Context.BIND_AUTO_CREATE);
|
|
80
|
+ return true;
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ /** {@inheritDoc} */
|
|
84
|
+ @Override
|
|
85
|
+ public Cursor query(final Uri uri, final String[] projection,
|
|
86
|
+ final String selection, final String[] selectionArgs,
|
|
87
|
+ final String sortOrder) {
|
|
88
|
+ if (URI_MATCHER.match(uri) != CODE_DESTINATION) {
|
|
89
|
+ throw new IllegalArgumentException("Unknown URI " + uri);
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ if (service == null) {
|
|
93
|
+ throw new IllegalStateException("Unable to bind to service");
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ try {
|
|
97
|
+ @SuppressWarnings("unchecked")
|
|
98
|
+ Map<Long, Integer> predictions = service.getPredictions();
|
|
99
|
+ final MatrixCursor cursor = new MatrixCursor(new String[]{"_ID", "place", "count"},
|
|
100
|
+ predictions.size());
|
|
101
|
+
|
|
102
|
+ int i = 0;
|
|
103
|
+ for (Map.Entry<Long, Integer> prediction : predictions.entrySet()) {
|
|
104
|
+ cursor.addRow(new Object[]{i++, prediction.getKey(), prediction.getValue()});
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ return cursor;
|
|
108
|
+ } catch (RemoteException ex) {
|
|
109
|
+ throw new RuntimeException("Unable to retrieve activity", ex);
|
|
110
|
+ }
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ /** {@inheritDoc} */
|
|
114
|
+ @Override
|
|
115
|
+ public String getType(final Uri uri) {
|
|
116
|
+ if (URI_MATCHER.match(uri) != CODE_DESTINATION) {
|
|
117
|
+ throw new IllegalArgumentException("Unknown URI " + uri);
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ return "vnd.android.cursor." + (ContentUris.parseId(uri) == -1 ? "dir" : "item")
|
|
121
|
+ + "vnd.contextanalyser.prediction";
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ /** {@inheritDoc} */
|
|
125
|
+ @Override
|
|
126
|
+ public Uri insert(final Uri uri, final ContentValues values) {
|
|
127
|
+ if (URI_MATCHER.match(uri) != CODE_DESTINATION) {
|
|
128
|
+ throw new IllegalArgumentException("Unknown URI " + uri);
|
|
129
|
+ }
|
|
130
|
+
|
|
131
|
+ throw new UnsupportedOperationException("Cannot insert");
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ /** {@inheritDoc} */
|
|
135
|
+ @Override
|
|
136
|
+ public int delete(Uri uri, String where, String[] whereArgs) {
|
|
137
|
+ if (URI_MATCHER.match(uri) != CODE_DESTINATION) {
|
|
138
|
+ throw new IllegalArgumentException("Unknown URI " + uri);
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ throw new UnsupportedOperationException("Cannot delete");
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ /** {@inheritDoc} */
|
|
145
|
+ @Override
|
|
146
|
+ public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
|
|
147
|
+ if (URI_MATCHER.match(uri) != CODE_DESTINATION) {
|
|
148
|
+ throw new IllegalArgumentException("Unknown URI " + uri);
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ throw new UnsupportedOperationException("Cannot update");
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+}
|