PageRenderTime 20ms CodeModel.GetById 14ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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