Context-detection API for Android developed as a university project
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

RecorderService.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.activityrecorder;
  23. import android.app.Service;
  24. import android.content.Intent;
  25. import android.os.Handler;
  26. import android.os.IBinder;
  27. import android.os.RemoteException;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. import java.util.Map;
  31. import uk.co.md87.android.activityrecorder.rpc.ActivityRecorderBinder;
  32. import uk.co.md87.android.activityrecorder.rpc.Classification;
  33. import uk.co.md87.android.common.Aggregator;
  34. import uk.co.md87.android.common.ModelReader;
  35. import uk.co.md87.android.common.accel.AccelReader;
  36. import uk.co.md87.android.common.accel.AccelReaderFactory;
  37. /**
  38. *
  39. * @author chris
  40. */
  41. public class RecorderService extends Service {
  42. private final ActivityRecorderBinder.Stub binder = new ActivityRecorderBinder.Stub() {
  43. public void submitClassification(String classification) throws RemoteException {
  44. //Log.i(getClass().getName(), "Received classification: " + classification);
  45. updateScores(classification);
  46. }
  47. public List<Classification> getClassifications() throws RemoteException {
  48. return classifications;
  49. }
  50. public boolean isRunning() throws RemoteException {
  51. return running;
  52. }
  53. };
  54. private final Runnable sampleRunnable = new Runnable() {
  55. public void run() {
  56. final float[] values = reader.getSample();
  57. data[nextSample * 2] = values[0];
  58. data[nextSample * 2 + 1] = values[1];
  59. if (++nextSample == 128) {
  60. float[] cache = new float[256];
  61. System.arraycopy(data, 0, cache, 0, 256);
  62. analyse(cache);
  63. reader.stopSampling();
  64. return;
  65. }
  66. handler.postDelayed(sampleRunnable, 50);
  67. }
  68. };
  69. private final Runnable registerRunnable = new Runnable() {
  70. public void run() {
  71. //Log.i(getClass().getName(), "Registering");
  72. nextSample = 0;
  73. reader.startSampling();
  74. handler.postDelayed(sampleRunnable, 50);
  75. handler.postDelayed(registerRunnable, 30000);
  76. }
  77. };
  78. final Handler handler = new Handler();
  79. float[] data = new float[256];
  80. volatile int nextSample = 0;
  81. boolean running;
  82. final Aggregator aggregator = new Aggregator();
  83. AccelReader reader;
  84. public static Map<Float[], String> model;
  85. private final List<Classification> classifications = new ArrayList<Classification>();
  86. public void analyse(float[] data) {
  87. //Log.i(getClass().getName(), "Analysing");
  88. final Intent intent = new Intent(this, ClassifierService.class);
  89. intent.putExtra("data", data);
  90. startService(intent);
  91. }
  92. @Override
  93. public IBinder onBind(Intent arg0) {
  94. return binder;
  95. }
  96. @Override
  97. public void onStart(final Intent intent, final int startId) {
  98. super.onStart(intent, startId);
  99. running = true;
  100. reader = new AccelReaderFactory().getReader(this);
  101. init();
  102. }
  103. @SuppressWarnings("unchecked")
  104. public void init() {
  105. model = ModelReader.getModel(this, R.raw.basic_model);
  106. handler.postDelayed(registerRunnable, 1000);
  107. classifications.add(new Classification("", System.currentTimeMillis()));
  108. }
  109. void updateScores(final String classification) {
  110. aggregator.addClassification(classification);
  111. final String best = aggregator.getClassification();
  112. if (!classifications.isEmpty() && best.equals(classifications
  113. .get(classifications.size() - 1).getClassification())) {
  114. classifications.get(classifications.size() - 1).updateEnd(System.currentTimeMillis());
  115. } else {
  116. classifications.add(new Classification(best, System.currentTimeMillis()));
  117. }
  118. }
  119. @Override
  120. public void onDestroy() {
  121. super.onDestroy();
  122. if (running) {
  123. running = false;
  124. handler.removeCallbacks(sampleRunnable);
  125. handler.removeCallbacks(registerRunnable);
  126. reader.stopSampling();
  127. }
  128. }
  129. }