Tuesday, January 26, 2010

Android issue 6298: Unexpected KeyEvent results during sleep with activities using show when locked or dismiss keyguard flags

Submitted to the official issue log. This problem is responsible for the difficulties we are currently trying to work around to get things into beta-quality consistency.


- Steps to reproduce the problem:  I start up an activity upon receipt of screen off broadcast. The activity  adds the FLAG_SHOW_WHEN_LOCKED before inflating layout. I then call  TakeKeyEvents(true). This is supposed to allow the activity the chance to  handle key events even if it doesn't have focus. My activity does have  focus at time of creation, while the screen is off, but can lose it by  other things I do with the app. I ultimately want it to always handle key  events even if it has been pushed to the background.  - What Happened: I don't get key events. It seems I can only get events that are "wake keys"  as defined by the KeyguardViewMediator: {{{ private boolean isWakeKeyWhenKeyguardShowing(int keyCode) {         switch (keyCode) {             case KeyEvent.KEYCODE_VOLUME_UP:             case KeyEvent.KEYCODE_VOLUME_DOWN:             case KeyEvent.KEYCODE_MUTE:             case KeyEvent.KEYCODE_HEADSETHOOK:              case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:              case KeyEvent.KEYCODE_MEDIA_STOP:              case KeyEvent.KEYCODE_MEDIA_NEXT:              case KeyEvent.KEYCODE_MEDIA_PREVIOUS:              case KeyEvent.KEYCODE_MEDIA_REWIND:              case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:             case KeyEvent.KEYCODE_CAMERA:                 return false;         }         return true;     } }}}  - What you think the correct behavior should be. The window isn't able to take precedence over any key guard. Though the  display gets precedence, one does not expect that key events will still be  bound by the lockscreen. It effectively prevents your activity from  responding at all to the keys above. A related detail is the fact that the  opposite seems to happen when you use the FLAG_DISMISS_KEYGUARD. At that  point any key will cause wakeup if sleep happens while your activity is  still in progress. This behavior also seemed unexpected, as when sleep  occurs we are generally expecting the keyguard to be re-instated, but the  flag seems to be preventing that while the activity is alive.  I believe this problem affecting both flags should be considered a bug. A  fix would be appreciated, allowing these flags to be used in conjunction  with key event handling without unexpected lockscreen override of the  events. An example of what I expect to happen with either of these flags  would be if I return true on an event, that means I wanted to handle it, if  I return false then the event could next pass to the KeyguardViewMediator  to see if a wakeup should happen, etc. As it stands the event won't get  passed on in cases of sleeping while activity is alive, causing either the  unexpected unguarded wakeup, or failure to process keys that would normally  not be wakeup keys.   This happened within 2.0.1 SDK, this flag was added in API 5 and could be  extremely useful to app developers whilst there is no specific intent  support for replacing the lockscreen.

No comments:

Post a Comment