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