Reputation: 31
Is there any way checking if you have an android lock screen password enabled on your'e device programmatically? I started checking this using DeviceAdminReceiver and DevicePolicyManager classes but those classes do not refer to a state which is: 1.I had a password for the lock screen->I removed the password and now the password is disabled.
Thanks for the help, Nir
Upvotes: 2
Views: 1972
Reputation: 1
public class ScreenLock {
private static final String SYSTEM_DIRECTORY = "/system/";
private static final String LOCK_PATTERN_FILE = "gesture.key";
private static final String LOCK_PASSWORD_FILE = "password.key";
public static boolean lockExists() {
String dataSystemDirectory = Environment.getDataDirectory().getAbsolutePath() + SYSTEM_DIRECTORY;
String sLockPatternFilename = dataSystemDirectory + LOCK_PATTERN_FILE;
String sLockPasswordFilename = dataSystemDirectory + LOCK_PASSWORD_FILE;
if (nonEmptyFileExists(sLockPatternFilename) || nonEmptyFileExists(sLockPasswordFilename)) {
return true;
}
return false;
}
private static boolean nonEmptyFileExists(String filename) {
File file = new File(filename);
return file.exists() && file.length() > 0;
}
}
Upvotes: 0
Reputation: 30825
There's no clean way of doing this. No formal API. If you're willing to risk a hack, you can try this. I cobbled it together some code I found on this site.
boolean hasPasswordOnLockScreen(){
String sLockPasswordFilename =
android.os.Environment.getDataDirectory().getAbsolutePath() +
"/system/password.key";
try {
// Check if we can read a byte from the file
RandomAccessFile raf = new RandomAccessFile(filename, "r");
raf.readByte();
raf.close();
return true;
} catch (FileNotFoundException fnfe) {
return false;
} catch (IOException ioe) {
return false;
}
}
Note that this is a hack and has the potential to not work in the future is the path and file name of the password file changes.
Upvotes: 1