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.

RealAccelReader.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.common.accel;
  23. import android.content.Context;
  24. import android.hardware.Sensor;
  25. import android.hardware.SensorEvent;
  26. import android.hardware.SensorEventListener;
  27. import android.hardware.SensorManager;
  28. import android.os.PowerManager;
  29. /**
  30. * An accelerometer reader which reads real data from the device's
  31. * accelerometer.
  32. *
  33. * @author chris
  34. */
  35. public class RealAccelReader implements AccelReader {
  36. private final SensorEventListener accelListener = new SensorEventListener() {
  37. /** {@inheritDoc} */
  38. @Override
  39. public void onSensorChanged(final SensorEvent event) {
  40. values = new float[]{
  41. event.values[SensorManager.DATA_Y],
  42. event.values[SensorManager.DATA_Z]
  43. };
  44. }
  45. /** {@inheritDoc} */
  46. @Override
  47. public void onAccuracyChanged(final Sensor sensor, final int accuracy) {
  48. // Don't really care
  49. }
  50. };
  51. float[] values = new float[]{0, 0};
  52. private SensorManager manager;
  53. private PowerManager.WakeLock wl;
  54. public RealAccelReader(final Context context) {
  55. PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
  56. wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Activity recorder");
  57. manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
  58. wl.acquire();
  59. }
  60. public void startSampling() {
  61. manager.registerListener(accelListener,
  62. manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
  63. SensorManager.SENSOR_DELAY_FASTEST);
  64. }
  65. public void stopSampling() {
  66. manager.unregisterListener(accelListener);
  67. }
  68. public float[] getSample() {
  69. return values;
  70. }
  71. @Override
  72. protected void finalize() throws Throwable {
  73. wl.release();
  74. }
  75. }