views:

453

answers:

2

Hi.

I try to find a way to disable the PatternLock screen temporary. I don't want the lock to be disabled completely, but the user should not need to re-enter his pattern all the time.

My idea is to write a service which disables the pattern after some user activity and re-enables it after a while. (and even more)

There are apps on the market that do something like that (i.e. AutoLock or TogglePattern), so there must be a solution ;)

I know that I can prevent a lock completely by using: getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) or KeyguardLock.disableKeyguard()

But that is not what I'm after.

I saw the class "com.android.internal.widget.LockPatternUtils" in the android sources which is used by the settings activity, but this class is not (at least as far as I know) accessible for a "normal" application.

Do you have an advice?

Thanks in advanced

+2  A: 

Have you tried looking at the code for com.android.internal.widget.LockPatternUtils and doing what it does?

It has something like:

public void setLockPatternEnabled(boolean enabled) {
    setBoolean(android.provider.Settings.System.LOCK_PATTERN_ENABLED, enabled);
}

private void setBoolean(String systemSettingKey, boolean enabled) {
    android.provider.Settings.System.putInt(
                    mContentResolver,
                    systemSettingKey,
                    enabled ? 1 : 0);
}

You might be able to do something similar in your code.

Malachi
Thanks for your answer.I have no idea why I did not make this logically next step my self yesterday... I guess from time to time we all need someone who "pushes" us in the right direction. ;) All I had to do in addition to the code of your post in to initialize "mContentResolver" and of course to add the "android.permission.WRITE_SETTINGS" privilege to my manifest.Thanks again for your "push" ;)
I would strongly recommend that you don't play these tricks. In the future you will likely not be able to modify these settings (they will be moved to secure settings) because, for example, if an Exchange account has required that the user have a password then an application should not be able to bypass this requirement.
hackbod
I agree that it's probably not the best thing to do, but I'm not here to judge, I was just trying to provide a possible answer to the question.
Malachi
+2  A: 

As of 2.0 (API level 5), you can use this window flag to prevent the lock screen from being displayed while your window is shown:

http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SHOW_WHEN_LOCKED

You can also use this flag to allow a non-secure keyguard to be dismissed when your window is displayed:

http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DISMISS_KEYGUARD

Note that these do not allow you to bypass the lock screen outside of your application's environment, which is an intentional design decision.

There is also an older API that lets you hide the lock screen in a similar way to a wake lock:

http://developer.android.com/reference/android/app/KeyguardManager.html#newKeyguardLock(java.lang.String)

Use of this API is discouraged on newer platforms, because it is very easy to get wrong and cause bad behavior (the screen not locking when the user would expect it to), and basically impossible to have clean transitions between activities with unlocked states. For example, this is the API that the in-call screen originally used to hide the lock screen when it was displayed, but as of 2.0 it has switched to the new cleaner window flags. Likewise for the alarm clock etc.

hackbod
Thanks hackbod.Of course you're absolutely right that is always a bad idea to bypass provided API's. But in this case I still see no other way. What I plan to develop is a background service which temporary disabled the LockPattern and re-enables it after some time (maybe 10min). So there none of "my windows" shown... Because of this I cannot use "LayoutParams". Also the usage of the KeyguardManager from a service to prevent the pattern does not look ideal.
Why does it not look ideal? That is what the API does. It is far better than messing with the user's preference for whether to use a lock pattern; at any rate, if you do mess with the user's preference, be forewarned that your app is going to break in a future version of the platform as stronger enterprise support is introduced.
hackbod
Thanks again hackrod. As I mentioned above, I already "played" with the "KeyguardLock.disableKeyguard()", it seems it disables the lockscreen completely. But this is not what I'm looking for."exitKeyguardSecurely()" looks (without testing) also only interesting if I would like to show information to the user... But I plan a background service which should run silently in the background...Did I overlook something?