Is there a direct event for keyguard lock? - android

DUPLICATE WARNING: this post IS NOT about screen on/off, this post IS about keyguard lock (those issues are not the same).
Luckily there is a direct event when the keyguard is unlocked, but what about event when the keyguard is locked? I searched Android API event list back and forth, and I simply don't see it (phrase "lock" -- none, phrase "keyguard" -- one, for off state).
I know the workaround -- wait for screen off event, check in loop the state of keyguard AND wait for screen on event, because the phone can go only in two directions starting from screen off -- either phone will be locked or the screen will be on again. Not huge amount of code, but hack anyway, so I hope I missed something in the manual and there is direct event for keyguard lock.

Related

Android ACTION_SCREEN_OFF intent meaning

In Google documentation about ACTION_SCREEN_OFF they write:
This broadcast is sent when the device becomes non-interactive which may have nothing to do with the screen turning off. To determine the actual state of the screen, use Display.getState().
So I asking when the screen becomes non-interactive and it still turned on.
When you press the power button to lock the screen (or even if the device's screen lights get off, due to inactivity), at that time it goes to a non-interactive state.
And yes, the device will be turned on.

Simulate an activity to postpone screen turn off

Each time an user touches screen, a timeout counter for turning screen off is reset.
How can I simulate an activity to reset such timeout counter programmatically, to postpone screen turning off?
I am not looking for keeping screen on permanently.
I am experiencing screen off issue when user is requested to use a fingerprint scanner and I want to reset such timeout counter each time when user's fingerprint is not recognized or any fingerprint sensor activity is recorded.
The solution you are looking for is already there in the Android documentation.
It is called Wake Lock. Keep the screen on programmatically using:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
The advantage of setting the flag programmatically in your activity is that it gives you the option of programmatically clearing the flag later and thereby allowing the screen to turn off. If you want to explicitly clear the flag and thereby allow the screen to turn off again, use clearFlags() like so:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Accepting Android volume rocker input across a range of states

I have an app that will generally be the sole app for which a device (Android 4.2 and up) is used. It needs to be able to accept input from the volume rockers in the following scenarios:
app is in foreground
app is in background
lock screen or keyguard has been engaged
screen has been switched off (ie idle mode).
(1) is easily solved by listening for KeyDown events in the Activity
(2) can be partially solved by listening for android.media.VOLUME_CHANGED_ACTION (yes I know it's not public API, but it works all devices I have managed to test it on and I can't find an alternate). But on my Nexus 5 (Android 6.0), on first press of the rocker it displays the volume dialog for a short duration and requires a second press to pass the rocker event to my app. I haven't managed to pin down for which version of Android this started occurring, but I'd like to work how how to ensure the first event is passed to my app for all Android versions.
(3) can be mitigated by using WindowManager FLAG_DISMISS_KEYGUARD and FLAG_SHOW_WHEN_LOCKED to avoid lock screen and keyguard
(4) can be partially mitigated by using WindowManager FLAG_KEEP_SCREEN_ON to ignore display timeouts when app is in foreground. But unfortunately it is a common event for the user to accidentally manually power the screen off, and even using a WAKE_LOCK doesn't allow the volume rocker events to be received via android.media.VOLUME_CHANGED_ACTION when the screen is idle.
So the questions are:
How do I ensure that the first press of the volume rocker is passed to my app, rather than being captured and consumed by the volume dialog?
How can I receive volume rocker events when the screen is idle?

Is it possible to get a device to wake up from sleep (screen dark) by detecting a touch to the screen?

I want to get an android device to wake up from sleep or however the state in which the phone gets after a certain amount of inactivity when the screen goes dark, by detecting a touch to the screen instead of clicking on any button.
In the documentation the only thing I have found is the FLAG_TOUCHABLE_WHEN_WAKING flag in WindowManager.LayoutParams and it says:
Window flag: When set, if the device is asleep when the touch screen
is pressed, you will receive this
first touch event. Usually the first
touch event is consumed by the system
since the user can not see what they
are pressing on.
I thought that meant that if the device's screen is turned off and that flag is set for an Activity then it will wake up to the touch (which is what I want it to do). Am I misunderstanding the purpose of this flag? Are there additional implementation details I'm ignoring? Is there some other way?
Am I misunderstanding the purpose of this flag?
AFAIK, yes. There is a slice of time between when the screen turns off and when the device falls asleep. During this time, if the user touches the screen someplace where the window has this flag, the screen turns on again and the inactivity timer is reset.
I can find no other use of this flag in the Android source code.
Is there some other way?
No. If the device is asleep, touch screen events are not registered.

Android WakeLock and KeyGuard

I'm just wondering if I do this correctly; I'm programming a notification app which can display a notification when the phone is sleeping
Disable keyguard lock
Aquire a wake lock
show notification
Set alarm for timeout and reenabling keyguard and release wakelock is the user dont touches the screen.
4.1 User touches the screen, and I disable the timer. Do nothing more. Done and done
4.2 User dont touch the screen, so reenable keyguard and release wakelock. Phone sleeps again
Basically I'm wondering about point 4.1 the most. cancel the pendingintent for the alarm, and do nothing more? or should the keyguard and wakelock that are set be dealt with in some way?
The trick to implement your own Keyguard-replacement appears to be the following:-
In the onCreate method, you don't disable the keyguard, but the user can interact with the screen at this point so you need to be careful about accidental touches.
getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED |
LayoutParams.FLAG_TURN_SCREEN_ON | LayoutParams.FLAG_KEEP_SCREEN_ON);
mKeyguardManager = (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock = mKeyguardManager.newKeyguardLock(TAG);
If the user performs some action to indicate they want to interact more fully, then we can disable the keyguard and move on.
mKeyguardLock.disableKeyguard();
mKeyguardManager.exitKeyguardSecurely(null);
If they don't, then since the keyguard isn't disabled you shouldn't need to do anything more, just finish your activity
Thats it, but I'm still testing it. So, I'm not 100% sure about it.
I know this question is old but the API clearly state that programatically trying to acquire a keyguard unlock is deprecated.
The correct strategy is, in the oncCreate method of your activity, to have:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Hope this helps others.
Locks are global and when one appplication acquires it it will be off until the lock is released.
You should always reenable locks. Otherwise system won't go sleep or lock from Home or any other application
EDIT: I'm not really shure how it works with keyguard :/

Categories

Resources