Ver código fonte

Content provider for predictions

master
Chris Smith 14 anos atrás
pai
commit
f63c497dca

+ 3
- 0
code/ContextAnalyser/AndroidManifest.xml Ver arquivo

@@ -23,6 +23,9 @@
23 23
 
24 24
         <provider android:name="uk.co.md87.android.contextanalyser.ActivitiesContentProvider"
25 25
             android:authorities="uk.co.md87.android.contextanalyser.activitiescontentprovider"/>
26
+
27
+        <provider android:name="uk.co.md87.android.contextanalyser.PredictionsContentProvider"
28
+            android:authorities="uk.co.md87.android.contextanalyser.predictionscontentprovider"/>
26 29
     </application>
27 30
 
28 31
     <permission-group android:description="@string/permgroupdesc"

BIN
code/ContextAnalyser/dist/ContextAnalyser.apk Ver arquivo


+ 7
- 1
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/ContextAnalyserService.java Ver arquivo

@@ -97,8 +97,14 @@ public class ContextAnalyserService extends Service {
97 97
         public String getActivity() throws RemoteException {
98 98
             return lastActivity;
99 99
         }
100
+
101
+        public Map getPredictions() throws RemoteException {
102
+            return predictions;
103
+        }
104
+
100 105
     };
101 106
 
107
+    private final Map<Long, Integer> predictions = new HashMap<Long, Integer>();
102 108
     private final Map<String, Long> names = new HashMap<String, Long>();
103 109
     private final List<String> activityLog = new LinkedList<String>();
104 110
 
@@ -283,7 +289,7 @@ public class ContextAnalyserService extends Service {
283 289
     protected void checkPredictions() {
284 290
         final Collection<Journey> journeys = dataHelper.findJourneys(lastLocation);
285 291
         final List<JourneyStep> mySteps = JourneyUtil.getSteps(activityLog);
286
-        final Map<Long, Integer> predictions = new HashMap<Long, Integer>();
292
+        predictions.clear();
287 293
 
288 294
         int total = 0;
289 295
         int count = 0;

+ 154
- 0
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/PredictionsContentProvider.java Ver arquivo

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

+ 2
- 0
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/rpc/ContextAnalyserBinder.aidl Ver arquivo

@@ -31,4 +31,6 @@ interface ContextAnalyserBinder {
31 31
 
32 32
     String getActivity();
33 33
 
34
+    Map getPredictions();
35
+
34 36
 }

+ 29
- 0
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/rpc/ContextAnalyserBinder.java Ver arquivo

@@ -4,6 +4,7 @@
4 4
  */
5 5
 package uk.co.md87.android.contextanalyser.rpc;
6 6
 import java.lang.String;
7
+import java.util.Map;
7 8
 import android.os.RemoteException;
8 9
 import android.os.IBinder;
9 10
 import android.os.IInterface;
@@ -61,6 +62,14 @@ reply.writeNoException();
61 62
 reply.writeString(_result);
62 63
 return true;
63 64
 }
65
+case TRANSACTION_getPredictions:
66
+{
67
+data.enforceInterface(DESCRIPTOR);
68
+java.util.Map _result = this.getPredictions();
69
+reply.writeNoException();
70
+reply.writeMap(_result);
71
+return true;
72
+}
64 73
 }
65 74
 return super.onTransact(code, data, reply, flags);
66 75
 }
@@ -96,8 +105,28 @@ _data.recycle();
96 105
 }
97 106
 return _result;
98 107
 }
108
+public java.util.Map getPredictions() throws android.os.RemoteException
109
+{
110
+android.os.Parcel _data = android.os.Parcel.obtain();
111
+android.os.Parcel _reply = android.os.Parcel.obtain();
112
+java.util.Map _result;
113
+try {
114
+_data.writeInterfaceToken(DESCRIPTOR);
115
+mRemote.transact(Stub.TRANSACTION_getPredictions, _data, _reply, 0);
116
+_reply.readException();
117
+java.lang.ClassLoader cl = (java.lang.ClassLoader)this.getClass().getClassLoader();
118
+_result = _reply.readHashMap(cl);
119
+}
120
+finally {
121
+_reply.recycle();
122
+_data.recycle();
123
+}
124
+return _result;
125
+}
99 126
 }
100 127
 static final int TRANSACTION_getActivity = (IBinder.FIRST_CALL_TRANSACTION + 0);
128
+static final int TRANSACTION_getPredictions = (IBinder.FIRST_CALL_TRANSACTION + 1);
101 129
 }
102 130
 public java.lang.String getActivity() throws android.os.RemoteException;
131
+public java.util.Map getPredictions() throws android.os.RemoteException;
103 132
 }

Carregando…
Cancelar
Salvar