PageRenderTime 16ms CodeModel.GetById 7ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/talkback_preics/src/com/google/android/marvin/talkback/ProximitySensor.java

http://eyes-free.googlecode.com/
Java | 168 lines | 85 code | 24 blank | 59 comment | 18 complexity | 8352a6a04fa82292cb72ede0c0e4f6c0 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.android.marvin.talkback;
 18
 19import android.content.Context;
 20import android.hardware.Sensor;
 21import android.hardware.SensorEvent;
 22import android.hardware.SensorEventListener;
 23import android.hardware.SensorManager;
 24
 25/**
 26 * Convenience class for working with the ProximitySensor
 27 * 
 28 * @author clchen@google.com (Charles L. Chen)
 29 */
 30
 31public class ProximitySensor {
 32
 33    /**
 34     * Callback for when the proximity sensor detects a change
 35     */
 36    public interface ProximityChangeListener {
 37        public void onProximityChanged(float proximity);
 38    }
 39    
 40    /**
 41     * State where the proximity sensor is inactive and uninitialized
 42     */
 43    public static final int STATE_STOPPED = 0;
 44    
 45    /**
 46     * State where the proximity sensor is initialized but not detecting
 47     */
 48    public static final int STATE_STANDBY = 1;
 49    
 50    /**
 51     * State where the proximity sensor is active and detecting
 52     */
 53    public static final int STATE_RUNNING = 2;
 54
 55    private SensorManager mSensorManager;
 56
 57    private Sensor mSensor;
 58
 59    private float mFarValue;
 60    
 61    private boolean mIgnoreCallbackOnRegistration;
 62    
 63    private boolean mIgnoreNextCallback;
 64    
 65    private int mCurrentState = STATE_STOPPED;
 66
 67    private ProximityChangeListener mCallback;
 68
 69    private boolean mClose;
 70
 71    SensorEventListener listener = new SensorEventListener() {
 72        @Override
 73        public void onAccuracyChanged(Sensor sensor, int accuracy) {
 74            // Ignoring this for now
 75        }
 76
 77        @Override
 78        public void onSensorChanged(SensorEvent event) {
 79            if (mIgnoreNextCallback) {
 80                mIgnoreNextCallback = false;
 81                return;
 82            }
 83            if (event.values[0] < mFarValue) {
 84                mClose = true;
 85                mCallback.onProximityChanged(0);
 86            } else {
 87                mClose = false;
 88                mCallback.onProximityChanged(1);
 89            }
 90        }
 91    };
 92
 93    /**
 94     * Constructor for ProximitySensor
 95     *
 96     * @param ctx The Context of the app using the proximity sensor
 97     * @param ignoreCallbackOnRegistration Flag indicating whether or not to
 98     *            drop the first callback event after registering the listener.
 99     * @param proximityChangeListener Callback that will be invoked when a
100     *            change is detected.
101     */
102    public ProximitySensor(Context ctx, boolean ignoreCallbackOnRegistration,
103            ProximityChangeListener proximityChangeListener) {
104        mCallback = proximityChangeListener;
105        mIgnoreCallbackOnRegistration = ignoreCallbackOnRegistration;
106        mClose = false;
107        mSensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE);
108        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
109        if (mSensor != null) {
110            mFarValue = mSensor.getMaximumRange();
111            mCurrentState = STATE_STANDBY;
112        }
113    }
114
115    /**
116     * Checks if something is close to the proximity sensor
117     *
118     * @return True if there is something close to the proximity sensor
119     */
120    public boolean isClose() {
121        return mClose;
122    }
123    
124    /**
125     * @return the current state of the proximity sensor
126     */
127    public int getState() {
128        return mCurrentState;
129    }
130                          
131    /**
132     * Shuts down the sensor, but keeps the instance around for easy resume
133     */
134    public void standby() {
135        if (mCurrentState == STATE_RUNNING) {
136            if (mSensor != null) {
137                mSensorManager.unregisterListener(listener);
138                mCurrentState = STATE_STANDBY;
139            }
140        }
141    }
142
143    /**
144     * Re-registers the listener on the sensor, resuming from standby state.
145     */
146    public void resume() {
147        if (mCurrentState == STATE_STANDBY) {
148            if (mSensor != null && mSensor != null) {
149                mSensorManager.registerListener(listener, mSensor, SensorManager.SENSOR_DELAY_UI);
150                mCurrentState = STATE_RUNNING;
151                if (mIgnoreCallbackOnRegistration) {
152                    mIgnoreNextCallback = true;
153                }
154            }
155        }
156    }
157
158    /**
159     * The app using the ProximitySensor must shut it down when it is done using it.
160     */
161    public void shutdown() {
162        if (mSensor != null) {
163            mSensorManager.unregisterListener(listener);
164            mSensor = null;
165            mCurrentState = STATE_STOPPED;
166        }
167    }
168}