PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 16package com.google.android.marvin.commands.impls;
 17
 18import android.content.Context;
 19import android.hardware.Sensor;
 20import android.hardware.SensorEvent;
 21import android.hardware.SensorEventListener;
 22import android.hardware.SensorManager;
 23
 24/**
 25 * Compass uses the magnetic compass to track the current heading.
 26 * 
 27 * @author clchen@google.com (Charles L. Chen)
 28 */
 29
 30public class Compass {
 31    private Context ctx;
 32
 33    public Compass(Context context) {
 34        ctx = context;
 35        sensorOk = true;
 36        sensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE);
 37
 38        final Sensor orientationSensor = sensorManager.getDefaultSensor(
 39                SensorManager.SENSOR_ORIENTATION);
 40
 41        if (orientationSensor != null) {
 42            sensorManager.registerListener(
 43                    mListener, orientationSensor, SensorManager.SENSOR_DELAY_GAME);
 44        }
 45    }
 46
 47    private static final String[] DIRECTION_NAMES = {
 48            "north", "north north east", "north east", "east north east", "east",
 49            "east south east", "south east", "south south east", "south", "south south west",
 50            "south west", "west south west", "west", "west north west", "north west",
 51            "north north west", "north"
 52    };
 53
 54    private SensorManager sensorManager;
 55
 56    private float currentHeading = -1;
 57
 58    private boolean sensorOk;
 59
 60    /**
 61     * Handles the sensor events for changes to readings and accuracy
 62     */
 63    private final SensorEventListener mListener = new SensorEventListener() {
 64        @Override
 65        public void onSensorChanged(SensorEvent event) {
 66            // Values are yaw (heading), pitch, and roll.
 67            currentHeading = event.values[0];
 68        }
 69
 70        @Override
 71        public void onAccuracyChanged(Sensor sensor, int accuracy) {
 72            sensorOk = (accuracy == SensorManager.SENSOR_STATUS_ACCURACY_HIGH);
 73        }
 74    };
 75
 76    public String getCurrentHeading() {
 77        if (currentHeading == -1) {
 78            return "";
 79        }
 80        if (!sensorOk) {
 81            return "Please calibrate the compass by shaking your handset.";
 82        }
 83        int index = (int) ((currentHeading * 100 + 1125) / 2250);
 84        return DIRECTION_NAMES[index];
 85    }
 86
 87    public double getCurrentHeadingValue() {
 88        if (currentHeading == -1) {
 89            return -1;
 90        }
 91        if (!sensorOk) {
 92            return -1;
 93        }
 94        return currentHeading;
 95    }
 96
 97    public void shutdown() {
 98        sensorManager.unregisterListener(mListener);
 99    }
100
101}