/actionslib/src/com/google/android/marvin/commands/impls/Compass.java

http://eyes-free.googlecode.com/ · Java · 101 lines · 61 code · 16 blank · 24 comment · 9 complexity · 8d39f51b2a73031271ae222b7d6afdf9 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.android.marvin.commands.impls;
  17. import android.content.Context;
  18. import android.hardware.Sensor;
  19. import android.hardware.SensorEvent;
  20. import android.hardware.SensorEventListener;
  21. import android.hardware.SensorManager;
  22. /**
  23. * Compass uses the magnetic compass to track the current heading.
  24. *
  25. * @author clchen@google.com (Charles L. Chen)
  26. */
  27. public class Compass {
  28. private Context ctx;
  29. public Compass(Context context) {
  30. ctx = context;
  31. sensorOk = true;
  32. sensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE);
  33. final Sensor orientationSensor = sensorManager.getDefaultSensor(
  34. SensorManager.SENSOR_ORIENTATION);
  35. if (orientationSensor != null) {
  36. sensorManager.registerListener(
  37. mListener, orientationSensor, SensorManager.SENSOR_DELAY_GAME);
  38. }
  39. }
  40. private static final String[] DIRECTION_NAMES = {
  41. "north", "north north east", "north east", "east north east", "east",
  42. "east south east", "south east", "south south east", "south", "south south west",
  43. "south west", "west south west", "west", "west north west", "north west",
  44. "north north west", "north"
  45. };
  46. private SensorManager sensorManager;
  47. private float currentHeading = -1;
  48. private boolean sensorOk;
  49. /**
  50. * Handles the sensor events for changes to readings and accuracy
  51. */
  52. private final SensorEventListener mListener = new SensorEventListener() {
  53. @Override
  54. public void onSensorChanged(SensorEvent event) {
  55. // Values are yaw (heading), pitch, and roll.
  56. currentHeading = event.values[0];
  57. }
  58. @Override
  59. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  60. sensorOk = (accuracy == SensorManager.SENSOR_STATUS_ACCURACY_HIGH);
  61. }
  62. };
  63. public String getCurrentHeading() {
  64. if (currentHeading == -1) {
  65. return "";
  66. }
  67. if (!sensorOk) {
  68. return "Please calibrate the compass by shaking your handset.";
  69. }
  70. int index = (int) ((currentHeading * 100 + 1125) / 2250);
  71. return DIRECTION_NAMES[index];
  72. }
  73. public double getCurrentHeadingValue() {
  74. if (currentHeading == -1) {
  75. return -1;
  76. }
  77. if (!sensorOk) {
  78. return -1;
  79. }
  80. return currentHeading;
  81. }
  82. public void shutdown() {
  83. sensorManager.unregisterListener(mListener);
  84. }
  85. }