Hy,
I have a problem related to the proximity sensor. When I put the finger on it, I want to turn the screen off and when I take the finger, I want to turn the screen on. I successfully did the turning off part, but when I take the finger off the sensor, it does not seem to execute the onSensorChanged method. Here is the code for it:
public void onSensorChanged(SensorEvent event) {
float distance = event.values[0];
boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD && distance < event.sensor.getMaximumRange());
boolean isValidCallState = false;
if (callsInfo != null) {
for (SipCallSession callInfo : callsInfo) {
int state = callInfo.getCallState();
isValidCallState |= ((state == SipCallSession.CallState.CONFIRMED)
|| (state == SipCallSession.CallState.CONNECTING)
|| (state == SipCallSession.CallState.CALLING) || (state == SipCallSession.CallState.EARLY && !callInfo
.isIncoming()));
}
}
if (isValidCallState && active) {
Log.e("", "turn off");
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
getWindow().setAttributes(lp);
lockOverlay.show();
} else {
Log.e("", "turn on");
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
getWindow().setAttributes(lp);
}
Log.e("", "turn ???"); // --> does not print this when releasing the sensor
}
Any ideas? Thanks.
I found the answer to my question and managed to figure out the main flow of the proximity sensor.
First, there are 2 ways to handle the proximity sensor: 1. Make a lock on PROXIMITY_SCREEN_OFF_WAKE_LOCK. From what I read this is somehow a hidden constant used by android, that has the value 32(at least for the moment - might or might not be changed in future versions). If this lock succeeds, the proximity sensor, will behave as you are in a call.
try {
Method method = powerManager.getClass().getDeclaredMethod(
"getSupportedWakeLockFlags");
int supportedFlags = (Integer) method.invoke(powerManager);
Field f = PowerManager.class.getDeclaredField("PROXIMITY_SCREEN_OFF_WAKE_LOCK");
int proximityScreenOffWakeLock = (Integer) f.get(null);
if ((supportedFlags & proximityScreenOffWakeLock) != 0x0) {
proximityWakeLock = powerManager.newWakeLock(proximityScreenOffWakeLock,
"com.voalte.CallProximity");
proximityWakeLock.setReferenceCounted(false);
}
} catch (Exception e) {
}
Note:
Hope this is of help for somebody.