Context-detection API for Android developed as a university project
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ContextAnalyserService.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. package uk.co.md87.android.contextanalyser;
  23. import android.app.Service;
  24. import android.content.Intent;
  25. import android.content.SharedPreferences;
  26. import android.location.Address;
  27. import android.location.Geocoder;
  28. import android.location.Location;
  29. import android.os.Handler;
  30. import android.os.IBinder;
  31. import android.os.RemoteException;
  32. import android.util.Log;
  33. import com.flurry.android.FlurryAgent;
  34. import java.io.IOException;
  35. import java.util.Collection;
  36. import java.util.HashMap;
  37. import java.util.Iterator;
  38. import java.util.LinkedList;
  39. import java.util.List;
  40. import java.util.Map;
  41. import uk.co.md87.android.common.ExceptionHandler;
  42. import uk.co.md87.android.common.ModelReader;
  43. import uk.co.md87.android.common.accel.AccelReaderFactory;
  44. import uk.co.md87.android.common.aggregator.AutoAggregator;
  45. import uk.co.md87.android.common.aggregator.AutoAggregatorFactory;
  46. import uk.co.md87.android.common.geo.LocationMonitor;
  47. import uk.co.md87.android.common.geo.LocationMonitorFactory;
  48. import uk.co.md87.android.common.model.Journey;
  49. import uk.co.md87.android.common.model.JourneyStep;
  50. import uk.co.md87.android.common.model.Place;
  51. import uk.co.md87.android.contextanalyser.rpc.ContextAnalyserBinder;
  52. /**
  53. * Background service which monitors and aggregates various sources of
  54. * contextual information, including current activity and place. Changes in
  55. * activity or other context cause the service to emit a broadcast Intent.
  56. *
  57. * @author chris
  58. */
  59. public class ContextAnalyserService extends Service
  60. implements SharedPreferences.OnSharedPreferenceChangeListener {
  61. public static final String ACTIVITY_CHANGED_INTENT
  62. = "uk.co.md87.android.contextanalyser.ACTIVITY_CHANGED";
  63. public static final String CONTEXT_CHANGED_INTENT
  64. = "uk.co.md87.android.contextanalyser.CONTEXT_CHANGED";
  65. public static final String PREDICTION_AVAILABLE_INTENT
  66. = "uk.co.md87.android.contextanalyser.PREDICTION_AVAILABLE";
  67. public static final int LOCATION_REPEATS = 2;
  68. public static final int CONTEXT_PLACE = 1;
  69. public static final boolean DEBUG = true;
  70. private static final int POLLING_DELAY = 6000;
  71. private final Runnable scheduleRunnable = new Runnable() {
  72. public void run() {
  73. poll();
  74. }
  75. };
  76. private final Runnable analyseRunnable = new Runnable() {
  77. public void run() {
  78. analyse();
  79. }
  80. };
  81. private final ContextAnalyserBinder.Stub binder = new ContextAnalyserBinder.Stub() {
  82. public String getActivity() throws RemoteException {
  83. return lastActivity;
  84. }
  85. public Map getPredictions() throws RemoteException {
  86. return predictions;
  87. }
  88. };
  89. private final Map<Long, Integer> predictions = new HashMap<Long, Integer>();
  90. private final Map<String, Long> names = new HashMap<String, Long>();
  91. private final List<String> activityLog = new LinkedList<String>();
  92. private double lat = 0, lon = 0;
  93. private int locationCount = 0;
  94. private long locationStart;
  95. private Place location, lastLocation;
  96. private Geocoder geocoder;
  97. private String lastActivity = "";
  98. private AutoAggregator aggregator;
  99. private LocationMonitor locationMonitor;
  100. private DataHelper dataHelper;
  101. private Handler handler = new Handler();
  102. @Override
  103. public void onStart(Intent intent, int startId) {
  104. super.onStart(intent, startId);
  105. Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
  106. locationMonitor = new LocationMonitorFactory().getMonitor(this);
  107. aggregator = new AutoAggregatorFactory().getAutoAggregator(this, handler,
  108. new AccelReaderFactory().getReader(this),
  109. ModelReader.getModel(this, R.raw.basic_model).entrySet(), analyseRunnable);
  110. dataHelper = new DataHelper(this);
  111. geocoder = new Geocoder(this);
  112. names.putAll(dataHelper.getUnnamedLocations());
  113. handler.postDelayed(scheduleRunnable, POLLING_DELAY);
  114. FlurryAgent.onStartSession(this, "MKB8YES3C6CFB86PXYXK");
  115. Log.i("ContextAnalyser", "Starting...");
  116. SharedPreferences prefs = getSharedPreferences("contextanalyser", MODE_WORLD_READABLE);
  117. prefs.registerOnSharedPreferenceChangeListener(this);
  118. }
  119. public void poll() {
  120. handler.postDelayed(scheduleRunnable, POLLING_DELAY);
  121. if (DEBUG) { Log.v(getClass().getSimpleName(), "Polling..."); }
  122. pollLocation();
  123. pollGeolocation();
  124. aggregator.start();
  125. }
  126. protected void pollLocation() {
  127. final double newLat = locationMonitor.getLat();
  128. final double newLon = locationMonitor.getLon();
  129. final float[] distance = new float[1];
  130. Location.distanceBetween(newLat, newLon, lat, lon, distance);
  131. if ((lat == 0 && lon == 0) || distance[0] > 500) {
  132. // New location
  133. if (location != null) {
  134. handleLocationEnd(location);
  135. }
  136. lat = newLat;
  137. lon = newLon;
  138. locationCount = 1;
  139. updateLastLocation(false);
  140. } else {
  141. // Existing location
  142. if (++locationCount > LOCATION_REPEATS && location == null) {
  143. // But we don't know it yet - add it!
  144. final String name = lat + "," + lon;
  145. final long id = dataHelper.addLocation(name, lat, lon);
  146. names.put(name, id);
  147. updateLastLocation(true);
  148. }
  149. }
  150. }
  151. protected void pollGeolocation() {
  152. final Iterator<Map.Entry<String, Long>> it = names.entrySet().iterator();
  153. while (it.hasNext()) {
  154. final Map.Entry<String, Long> tuple = it.next();
  155. if (DEBUG) { Log.v(getClass().getSimpleName(), "Attempting to geocode " + tuple.getKey()); }
  156. final String[] parts = tuple.getKey().split(",", 2);
  157. try {
  158. final List<Address> addresses = geocoder.getFromLocation(
  159. Double.parseDouble(parts[0]), Double.parseDouble(parts[1]), 1);
  160. if (addresses != null && !addresses.isEmpty()) {
  161. // We found a nice address
  162. dataHelper.updateLocation(tuple.getValue(),
  163. addresses.get(0).getAddressLine(0));
  164. it.remove();
  165. }
  166. } catch (IOException ex) {
  167. // Do nothing
  168. }
  169. }
  170. }
  171. protected void handleLocationEnd(final Place place) {
  172. dataHelper.recordVisit(place, locationStart, System.currentTimeMillis());
  173. }
  174. protected void updateLastLocation(final boolean added) {
  175. location = dataHelper.findLocation(lat, lon);
  176. if (location != null) {
  177. if ((lastLocation == null || !lastLocation.equals(location))) {
  178. if (DEBUG) {
  179. Log.i(getClass().getSimpleName(), "New location, broadcasting: " + location);
  180. }
  181. locationStart = System.currentTimeMillis();
  182. if (lastLocation != null) {
  183. if (added) {
  184. // The place was newly added, so for the final N
  185. // elements, the user has been at the place.
  186. for (int i = 0; i < Math.min(LOCATION_REPEATS + 1, activityLog.size()); i++) {
  187. activityLog.remove(activityLog.size() - 1);
  188. }
  189. }
  190. if (!activityLog.isEmpty()) {
  191. if (DEBUG) {
  192. Log.i(getClass().getSimpleName(), "Activity log to here: " + activityLog);
  193. }
  194. dataHelper.addJourney(lastLocation, location, activityLog);
  195. }
  196. }
  197. final Intent intent = new Intent(CONTEXT_CHANGED_INTENT);
  198. intent.putExtra("type", CONTEXT_PLACE);
  199. intent.putExtra("old", lastLocation == null ? -1 : lastLocation.getId());
  200. intent.putExtra("new", location.getId());
  201. sendBroadcast(intent, Manifest.permission.RECEIVE_UPDATES);
  202. FlurryAgent.onEvent("broadcast_context_location");
  203. }
  204. activityLog.clear();
  205. lastLocation = location;
  206. }
  207. }
  208. protected void analyse() {
  209. final String newActivity = aggregator.getClassification();
  210. if (DEBUG) {
  211. Log.v(getClass().getSimpleName(), "Aggregator says: " + newActivity);
  212. }
  213. if (location == null && lastLocation != null) {
  214. // We're going somewhere - record the activity
  215. activityLog.add(newActivity);
  216. checkPredictions();
  217. }
  218. if (!newActivity.equals(lastActivity)) {
  219. if (DEBUG) {
  220. Log.i(getClass().getSimpleName(), "Broadcasting activity change");
  221. }
  222. final Intent intent = new Intent(ACTIVITY_CHANGED_INTENT);
  223. intent.putExtra("old", lastActivity);
  224. intent.putExtra("new", newActivity);
  225. sendBroadcast(intent, Manifest.permission.RECEIVE_UPDATES);
  226. FlurryAgent.onEvent("broadcast_activity");
  227. lastActivity = newActivity;
  228. }
  229. }
  230. protected void checkPredictions() {
  231. final Collection<Journey> journeys = dataHelper.findJourneys(lastLocation);
  232. final List<JourneyStep> mySteps = JourneyUtil.getSteps(activityLog);
  233. predictions.clear();
  234. int total = 0;
  235. int count = 0;
  236. int best = 0;
  237. long bestTarget = -1;
  238. final Iterator<Journey> it = journeys.iterator();
  239. while (it.hasNext()) {
  240. final Journey journey = it.next();
  241. if (journey.getSteps() < mySteps.size()) {
  242. it.remove();
  243. continue;
  244. }
  245. final List<JourneyStep> theirSteps = dataHelper.getSteps(journey);
  246. if (JourneyUtil.isCompatible(mySteps, theirSteps)) {
  247. total += journey.getNumber();
  248. count++;
  249. int last = predictions.containsKey(journey.getEnd())
  250. ? predictions.get(journey.getEnd()) : 0;
  251. last += journey.getNumber();
  252. if (last > best) {
  253. best = last;
  254. bestTarget = journey.getEnd();
  255. }
  256. predictions.put(journey.getEnd(), last);
  257. } else {
  258. if (DEBUG) {
  259. Log.d(getClass().getSimpleName(), "Journey " + journey + " incompatible");
  260. Log.d(getClass().getSimpleName(), "Their steps: " + theirSteps);
  261. Log.d(getClass().getSimpleName(), "My steps: " + mySteps);
  262. }
  263. it.remove();
  264. }
  265. }
  266. if (DEBUG) {
  267. Log.i(getClass().getSimpleName(), "Predictions: " + journeys);
  268. }
  269. final Intent intent = new Intent(PREDICTION_AVAILABLE_INTENT);
  270. intent.putExtra("count", count);
  271. intent.putExtra("best_target", bestTarget);
  272. intent.putExtra("best_probability", (float) best / total);
  273. sendBroadcast(intent, Manifest.permission.RECEIVE_UPDATES);
  274. FlurryAgent.onEvent("broadcast_prediction");
  275. }
  276. @Override
  277. public void onDestroy() {
  278. super.onDestroy();
  279. dataHelper.close();
  280. handler.removeCallbacks(scheduleRunnable);
  281. FlurryAgent.onEndSession(this);
  282. }
  283. @Override
  284. public IBinder onBind(Intent arg0) {
  285. return binder;
  286. }
  287. public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
  288. if (!prefs.getBoolean("run", true)) {
  289. Log.i("ContextAnalyser", "Stopping...");
  290. stopSelf();
  291. prefs.unregisterOnSharedPreferenceChangeListener(this);
  292. }
  293. }
  294. }