[jitsi-dev] Using proximity sensor to turn the screen off on Android


#1

Hi Emil, Yana,

I've made an attempt to implement functionality which would turn on
and off the screen when proximity sensor reports near/far distance
during call(similiar to native phone app).

The problem is that there is no API for this, but only workarounds
that not always work as expected. By searching the net I've found 3
different methods:

1. Using hidden API:
http://gitorious.org/0xdroid/frameworks_base/blobs/df5ae80b1187941951123d69ec9508d2e01346ff/core/java/android/os/Power.java
I called native method setScreenState and there was ADB system output
that the screen state switched to 0 or 1, but it has no real effect.

2. Setting screen brightness to 0 and use PowerManager's locks:
http://developer.android.com/reference/android/os/PowerManager.html

First I acquired PARTIAL_WAKE_LOCK and then
WindowManager.LayoutParams params = activity.getWindow().getAttributes();
params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
activity.getWindow().setAttributes(params);

As a result the screen is only dimmed to minimal level, but it always
stays on. It doesn't matter if the lock is acquired or not.

3. The is also an option of modifying screen off timeout, but it
affects device settings. There are suggestions that SCREEN_OFF action
can be caught and the previous value could be restored:

Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, 1000);

It requires modify system settings permission.

I've tried 1 and 2 options on my phone HTC Desire Z and none of them
worked. What do you think ? Should I try to use those hacks ? In my
opinion it would be better to just dim the screen to half and disable
all buttons.

If anyone on the list knows how to solve this I would be grateful for help.

Regards,
Pawel