Can disableKeyguard() method dismiss secure lock also? - android

I want to ask about the disableKeyguard() method in KeyguardManager.KeyguardLock class
The documentation says :
Disable the keyguard from showing. If the keyguard is currently
showing, hide it. The keyguard will be prevented from showing again
until reenableKeyguard() is called. Note: This call has no effect
while any DevicePolicyManager is enabled that requires a password.
However this class is deprecated since API level 13
Alternate method is to use flags from Window manager
FLAG_DISMISS_KEYGUARD and/or FLAG_SHOW_WHEN_LOCKED
Now these flags work only if lock mode is Swipe
In case of secure lock like PIN or password it can mostly bring one window above
lock screen, but as soon as user moves to another window, the lock reappears.
I want to ask that is the same true for disableKeyGuard() too.
Is it only effective in case of Swipe but not for secure locks like PIN / Password / Face detection etc.

Related

What is KeyguardLock disableKeyguard() for?

I have following handed-over existing code:
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
Based on here and here, is it to prevent the lock screen to appear? However, I am still able to lock my device. Can anyone advise me what is it for?
From the docs:
public void disableKeyguard()
Disable the keyguard from showing. If the keyguard is currently
showing, hide it. The keyguard will be prevented from showing again
until reenableKeyguard() is called. A good place to call this is from
Activity.onResume() Note: This call has no effect while any
android.app.admin.DevicePolicyManager is enabled that requires a
password.
This method requires the caller to hold the permission
android.Manifest.permission.DISABLE_KEYGUARD.
So it really depends on where exactly you put this snippet exactly, and it definitely not means that you can't see your lock screen anymore. In addition, if you have some security in your lock screen, this snippet can bypass the security measure.
It is used to unlock your screen programmatically. Maybe you also noticed while testing, your lockscreen won't appear until you call reenableKeyguard() again. Although reenableKeyguard() will only work if you also called disableKeyguard() from your application.
is it to prevent the lock screen to appear? However, I am still able to lock my device.
But you do see your lockscreen?
Can anyone advise me what is it for?
Opening a notification from the lockscreen basically does this. It disables your keyguard (lockscreen) and launches the app.

FLAG_DISMISS_KEYGUARD to unlock a screen

I am using FLAG_DISMISS_KEYGUARD to disable a keygaurd. This is working only when my app is on foreground. But android documents syas that
Use FLAG_DISMISS_KEYGUARD and/or FLAG_SHOW_WHEN_LOCKED instead; this
allows you to seamlessly hide the keyguard as your application moves
in and out of the foreground and does not require that any special
permissions be requested
this is not happening in my application. Then how to use the above flag so that my application should lock the screen when my app moves in and out of the foreground?
Any one have idea?
From the Javadoc for WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD:
Window flag: when set the window will cause the keyguard to be dismissed, only if it is not a secure lock keyguard. Because such a keyguard is not needed for security, it will never re-appear if the user navigates to another window (in contrast to FLAG_SHOW_WHEN_LOCKED, which will only temporarily hide both secure and non-secure keyguards but ensure they reappear when the user moves to another UI that doesn't hide them). If the keyguard is currently active and is secure (requires an unlock pattern) than the user will still need to confirm it before seeing this window, unless FLAG_SHOW_WHEN_LOCKED has also been set.
It sounds like in your case you'd only want to use FLAG_SHOW_WHEN_LOCKED and not FLAG_DISMISS_KEYGUARD.
final Window win= getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

Unlock a screen in android using disableKeyguard

In my application I am using disableKeyguard and reenableKeyguard to Unlock and lock a screen. my application is working fine. But problem is when user unexpectedly unlocked a screen by dragging keygaurd (without using my application), the screen does't lock again with my application.
How to solve this problem?
Thanks in advace
Use FLAG_DISMISS_KEYGUARD and/or FLAG_SHOW_WHEN_LOCKED instead; this allows you to seamlessly hide the keyguard as your application moves in and out of the foreground and does not require that any special permissions be requested. Handle returned by newKeyguardLock(String) that allows you to disable / reenable the keyguard.

Using disableKeyguard and reenableKeyguard to lock and unlock

In my application I am using disableKeyguard and reenableKeyguard to lock and unlock a screen with proximity and accelerometer sensors respectively. My application working with some bugs. If user unlocks a screen by dragging (without using my app) then my application will not lock !. How to prevent this issue? How to fix this bug? plz help me.
Is there any other way to lock and unlock ?
thanks in advance.
reenableKeyguard
void reenableKeyguard() Reenable the keyguard. The keyguard will
reappear if the previous call to disableKeyguard() caused it to be
hidden. A good place to call this is from onPause() Note: This call
has no effect while any DevicePolicyManager is enabled that requires a
password.
This method requires the caller to hold the permission
DISABLE_KEYGUARD.
Your app has to be the one to call disableKeyguard() first, else you cannot lock it using reenableKeyguard()
Source: https://developer.android.com/reference/android/app/KeyguardManager.KeyguardLock.html

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