Context-detection API for Android developed as a university project
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ClassifierService.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package uk.co.md87.android.activityrecorder;
  6. import android.app.Service;
  7. import android.content.ComponentName;
  8. import android.content.Intent;
  9. import android.content.ServiceConnection;
  10. import android.os.IBinder;
  11. import android.os.RemoteException;
  12. import android.util.Log;
  13. import android.widget.Toast;
  14. import java.util.Map;
  15. import uk.co.md87.android.activityrecorder.rpc.ActivityRecorderBinder;
  16. /**
  17. *
  18. * @author chris
  19. */
  20. public class ClassifierService extends Service implements Runnable {
  21. ActivityRecorderBinder service = null;
  22. String classification;
  23. private ServiceConnection connection = new ServiceConnection() {
  24. public void onServiceConnected(ComponentName arg0, IBinder arg1) {
  25. service = ActivityRecorderBinder.Stub.asInterface(arg1);
  26. try {
  27. service.submitClassification(classification);
  28. } catch (RemoteException ex) {
  29. }
  30. stopSelf();
  31. }
  32. public void onServiceDisconnected(ComponentName arg0) {
  33. Toast.makeText(ClassifierService.this, R.string.error_disconnected, Toast.LENGTH_LONG);
  34. }
  35. };
  36. private float[] data;
  37. @Override
  38. public void onStart(Intent intent, int startId) {
  39. super.onStart(intent, startId);
  40. //Log.i(getClass().getName(), "Starting classifier");
  41. data = intent.getFloatArrayExtra("data");
  42. new Thread(this).start();
  43. }
  44. @Override
  45. public void onDestroy() {
  46. super.onDestroy();
  47. unbindService(connection);
  48. }
  49. @Override
  50. public IBinder onBind(Intent arg0) {
  51. return null;
  52. }
  53. public void run() {
  54. float oddTotal = 0, evenTotal = 0;
  55. float oddMin = Float.MAX_VALUE, oddMax = Float.MIN_VALUE;
  56. float evenMin = Float.MAX_VALUE, evenMax = Float.MIN_VALUE;
  57. for (int i = 0; i < 128; i++) {
  58. evenTotal += data[i * 2];
  59. oddTotal += data[i * 2 + 1];
  60. evenMin = Math.min(evenMin, data[i * 2]);
  61. oddMin = Math.min(oddMin, data[i * 2 + 1]);
  62. evenMax = Math.max(evenMax, data[i * 2]);
  63. oddMax = Math.max(oddMax, data[i * 2 + 1]);
  64. }
  65. final float[] points = {
  66. Math.abs(evenTotal / 128),
  67. Math.abs(oddTotal / 128),
  68. evenMax - evenMin,
  69. oddMax - oddMin
  70. };
  71. float bestDistance = Float.MAX_VALUE;
  72. String bestActivity = "UNCLASSIFIED/UNKNOWN";
  73. for (Map.Entry<Float[], String> entry : RecorderService.model.entrySet()) {
  74. float distance = 0;
  75. for (int i = 0; i < points.length; i++) {
  76. distance += Math.pow(points[i] - entry.getKey()[i], 2);
  77. }
  78. if (distance < bestDistance) {
  79. bestDistance = distance;
  80. bestActivity = entry.getValue();
  81. }
  82. }
  83. classification = bestActivity;
  84. //Log.i(getClass().getName(), "Classification: " + classification);
  85. bindService(new Intent(this, RecorderService.class), connection, BIND_AUTO_CREATE);
  86. }
  87. }