/shell/src/com/google/marvin/shell/Compass.java

http://eyes-free.googlecode.com/ · Java · 94 lines · 55 code · 15 blank · 24 comment · 7 complexity · 098e7c55eb4abdb26cffe547f53cfd34 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.marvin.shell;
  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), credo@google.com (Tim Credo)
  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. Sensor compassSensor = sensorManager.getDefaultSensor(SensorManager.SENSOR_ORIENTATION);
  34. sensorManager.registerListener(mListener, compassSensor, SensorManager.SENSOR_DELAY_GAME);
  35. }
  36. private static final String[] DIRECTION_NAMES = { "north", "north north east", "north east",
  37. "east north east", "east", "east south east", "south east", "south south east", "south",
  38. "south south west", "south west", "west south west", "west", "west north west",
  39. "north west", "north north west", "north" };
  40. private SensorManager sensorManager;
  41. private float currentHeading = -1;
  42. private boolean sensorOk;
  43. /**
  44. * Handles the sensor events for changes to readings and accuracy
  45. */
  46. private final SensorEventListener mListener = new SensorEventListener() {
  47. @Override
  48. public void onSensorChanged(SensorEvent event) {
  49. // Values are yaw (heading), pitch, and roll.
  50. currentHeading = event.values[0];
  51. }
  52. @Override
  53. public void onAccuracyChanged(Sensor arg0, int arg1) {
  54. sensorOk = (arg1 == SensorManager.SENSOR_STATUS_ACCURACY_HIGH);
  55. }
  56. };
  57. public String getCurrentHeading() {
  58. if (currentHeading == -1) {
  59. return "";
  60. }
  61. if (!sensorOk) {
  62. return "Please calibrate the compass by shaking your handset.";
  63. }
  64. int index = (int) ((currentHeading * 100 + 1125) / 2250);
  65. return DIRECTION_NAMES[index];
  66. }
  67. public double getCurrentHeadingValue() {
  68. if (currentHeading == -1) {
  69. return -1;
  70. }
  71. if (!sensorOk) {
  72. return -1;
  73. }
  74. return currentHeading;
  75. }
  76. public void shutdown() {
  77. sensorManager.unregisterListener(mListener);
  78. }
  79. }