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