PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://eyes-free.googlecode.com/
Java | 134 lines | 73 code | 16 blank | 45 comment | 11 complexity | a7af3aabca6ad3f8f67cd72f2d82deb4 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 */
 30class ProximitySensor {
 31    private final SensorManager mSensorManager;
 32    private final Sensor mSensor;
 33    private final boolean mIgnoreCallbackOnRegistration;
 34    private final float mFarValue;
 35
 36    private ProximityChangeListener mCallback;
 37
 38    private boolean mIgnoreNextCallback;
 39    private boolean mActive;
 40    private boolean mClose;
 41
 42    /**
 43     * Constructor for ProximitySensor
 44     * 
 45     * @param context The parent context.
 46     * @param ignoreCallbackOnRegistration Flag indicating whether or not to
 47     *            drop the first callback event after registering the listener.
 48     */
 49    public ProximitySensor(Context context, boolean ignoreCallbackOnRegistration) {
 50        mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
 51        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
 52        mIgnoreCallbackOnRegistration = ignoreCallbackOnRegistration;
 53
 54        if (mSensor != null) {
 55            mFarValue = mSensor.getMaximumRange();
 56        } else {
 57            mFarValue = 0;
 58        }
 59    }
 60
 61    public void setProximityChangeListener(ProximityChangeListener listener) {
 62        mCallback = listener;
 63    }
 64
 65    /**
 66     * Checks if something is close to the proximity sensor
 67     * 
 68     * @return {@code true} if there is something close to the proximity sensor
 69     */
 70    public boolean isClose() {
 71        return mClose;
 72    }
 73
 74    /**
 75     * @return the current state of the proximity sensor
 76     */
 77    public boolean isActive() {
 78        return mActive;
 79    }
 80
 81    /**
 82     * Stops listening for sensor events.
 83     */
 84    public void stop() {
 85        if (mSensor != null) {
 86            mSensorManager.unregisterListener(mSensorEventListener);
 87            mActive = false;
 88        }
 89    }
 90
 91    /**
 92     * Starts listening for sensor events.
 93     */
 94    public void start() {
 95        if (mSensor != null) {
 96            mSensorManager.registerListener(mSensorEventListener, mSensor,
 97                    SensorManager.SENSOR_DELAY_UI);
 98            mActive = true;
 99
100            if (mIgnoreCallbackOnRegistration) {
101                mIgnoreNextCallback = true;
102            }
103        }
104    }
105
106    private final SensorEventListener mSensorEventListener = new SensorEventListener() {
107        @Override
108        public void onAccuracyChanged(Sensor sensor, int accuracy) {
109            // Do nothing.
110        }
111
112        @Override
113        public void onSensorChanged(SensorEvent event) {
114            if (mIgnoreNextCallback) {
115                mIgnoreNextCallback = false;
116                return;
117            }
118            if (event.values[0] < mFarValue) {
119                mClose = true;
120                mCallback.onProximityChanged(mClose);
121            } else {
122                mClose = false;
123                mCallback.onProximityChanged(mClose);
124            }
125        }
126    };
127
128    /**
129     * Callback for when the proximity sensor detects a change
130     */
131    public interface ProximityChangeListener {
132        public void onProximityChanged(boolean close);
133    }
134}