Context-detection API for Android developed as a university project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContextAnalyserService.java 12KB

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