I'm currently checking if there is a screen lock password with:
DevicePolicyManager.getgetPasswordMinimumLength() != 0
so if I see that the length is 0 I conclude there is no screen lock password and I prompt user to set one with DevicePolicyManager.ACTION_SET_NEW_PASSWORD.
I wonder though if it's a correct method of checking whether a screen lock password has been set?
Use DevicePolicyManager.isActivePasswordSufficient to check whether your password policy is sufficient instead of getPasswordMinimumlength
Related
I developing a DeviceManager sample app,and i use resetPassword() method to reset screen lock password.
using some long length password,just like "123456"、"abcdef",this method result tells succeeded,and working good on screen lock.
using some short length password,"11" or "ab",the method result still succeeded,but not working on screen lock.
and strangely,when i input wrong password,some screen text show that is wrong password,then input correct password,it doesn't show any text,just no response.
for this problem,i have restored factory defaults three times。
In my device i already set a password of device, now i install my app which is managed by device policy manager. now when i call this method
int currentPolicy = devicePolicyManager.getPasswordQuality(demoDeviceAdmin);
if(currentPolicy==262144)passwordType="Alphabetic";
else if(currentPolicy==327680)passwordType="Alphanumeric";
else if(currentPolicy==131072)passwordType="Numeric";
//if(currentPolicy==196608)passwordType="PASSWORD_QUALITY_NUMERIC_COMPLEX";
else if(currentPolicy==393216)passwordType="Complex";
else if(currentPolicy==196608)passwordType="Pattern";
else if(currentPolicy==0)passwordType="None";
it gives me password type none. Now if i set password through device policy manager in my application like this
Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Then now if again get password quality it gives me correct value.
in First time i think device policy manager don't have or store old password type.
So my question is how to get password quality before setting password from my application through device policy manager.
Thank You
getPasswordQuality doesn't report the quality on the current password, it only reports the policy setting of lowest allowed password quality.
Basically getPasswordQuality returns the value set using the setPasswordQuality of the admin or the aggregate value if several admins are active.
The admin can check if the current password is good enough by calling DevicePolicyManager.isActivePasswordSufficient().
Keyguard reports back the current password status to DevicePolicyManager using the hidden method setActivePasswordState.
To my knowledge, this info is not available to a third party admin app.
If an incorrect password is entered, keyguard calls DevicePolicyManager.reportFailedPasswordAttempt which initiates a factory reset if too many failed attempts have been made.
Hope this helps.
/Marek Pola (Employee of Sony Mobile)
For devices before Android 6 (exclusive), you can get the unlocking method type by calling a hidden function getActivePasswordQuality() in class LockPatternUtils (you can call the function via Java reflection). But for Android 6 and above, I have not figured out how to get the unlocking method type.
/**
* Determine authentication method type (DAS, PIN, Password or Biometric)
*/
private String getAuthenticationMethodType(){
String LOCK_PATTERN_UTILS="com.android.internal.widget.LockPatternUtils";
String ACTIVE_PASSWORD_QUALITY="getActivePasswordQuality";
int lockProtectionLevel=0;
try{
Class <?> lockPatternUtilsClass=Class.forName(LOCK_PATTERN_UTILS);
Object lockPatternUtils=lockPatternUtilsClass.getConstructor(Context.class).newInstance(this);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
// Have not figured out how to get the unlocking method type for Android 6 and above devices. The same method will not work on Android 6. See detailed explanation below.
}else {
Method method = lockPatternUtilsClass.getMethod(ACTIVE_PASSWORD_QUALITY);
lockProtectionLevel=Integer.valueOf(String.valueOf(method.invoke(lockPatternUtils,null)));
}
}catch (Exception e){
e.printStackTrace();
}
switch (lockProtectionLevel){
case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
return "PIN";
case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
return "Password";
case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
return "DAS";
case DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK:
return "Biometric";
default:return "None";
}
}
For Android 6, after all my tests so far, I found that if we still want to get the unlocking method with function getActivePasswordQuality(int userId), the app must have permission android.permission.ACCESS_KEYGUARD_SECURE_STORAGE in order to read lockscreen.password_type, and get the unlocking method. However, the permission ACCESS_KEYGUARD_SECURE_STORAGE is a signature permission, which means we need to sign the app with the same key that the manufacturers use to sign the system (or to say the permission), so as to grant the signature permission to the app successfully.
Hope this helps.
I need to set Screen Lock Setting to "None" in the android system.
I am deploying an app that is being sent out to users on pre-configured tablets.
This pre-configuration is done manually by the tablet supplier.
The settings to pre-configure is to set the Screen lock to None when the app is installing.
I don't think this is possible, the user has to do it them self.
Considering all apps (With a similar requirment) I have used so far send me to the "Select lock screen" screen & ask me to disable it manually.
I could be wrong tho!
Try this:
try{
Class lockPatternUtilsCls = Class.forName("com.android.internal.widget.LockPatternUtils");
Constructor lockPatternUtilsConstructor =
lockPatternUtilsCls.getConstructor(new Class[]{Context.class});
Object lockPatternUtils = lockPatternUtilsConstructor.newInstance(getContext());
Method setLockScreenDisabled = lockPatternUtils.getClass().getMethod("setLockScreenDisabled", boolean.class ,int.class);
setLockScreenDisabled.invoke(lockPatternUtils, new Object[]{true,0});
}
catch(Exception e){
Log.e(this.toString(),e.toString());
}
if you want change lock to swipe:
setLockScreenDisabled.invoke(lockPatternUtils, new Object[]{false,0});
but you need to change your app to system uid:
android:sharedUserId="android.uid.system"
I know Screen Lock inclue None, Swipe,Pattern,Password and PIN.
I have read the docment How can i set up screen lock with a password programmatically?
I don't know what kind of lock type the following code apply to.
I have test the following code using different phones, it seems that sometimes the code apply to Password lock, and sometimes the code apply to PIN lock, I don't know why?
DevicePolicyManager devicePolicyManager =(DevicePolicyManager)getApplicationContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName demoDeviceAdmin =new ComponentName(this, MyAdmin.class);
devicePolicyManager.setPasswordQuality(demoDeviceAdmin,DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
devicePolicyManager.setPasswordMinimumLength(demoDeviceAdmin, 5);
boolean result = devicePolicyManager.resetPassword("123456", DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
Toast.makeText(this, "button_lock_password_device..."+result, Toast.LENGTH_LONG).show();
Depending on the value you pass as the first parameter in devicePolicyManager.resetPassword(), the Lock will either ask for PIN or Password.
// Screen Lock will act as PIN.
devicePolicyManager.resetPassword("123456", DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
// Screen Lock will act as Password.
devicePolicyManager.resetPassword("123abc", DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
So for alphanumeric = password
numbers = pin
Hope it helps!!!
I had caught phone boot event.
On boot complete event I am writing following code
KeyguardManager mKeyguardManager = (KeyguardManager) mContext.getSystemService(KEYGUARD_SERVICE);
KeyguardLock mLock = mKeyguardManager.newKeyguardLock("MyApp");
mLock.disableKeyguard();
but what happing I can able to see lock and after that screen is getting unlocked. But requirement is that lock should not be visible at all after booting.
My guess is that I need to make modification in framework somewhere in setting file.
But I don't know where to modify.
but what happing I can able to see lock and after that screen is getting unlocked
You did not lock the screen. Hence, you cannot unlock it. disableKeyguard() is only used to reverse the effects of reenableKeyguard().
My guess is that I need to make modification in framework somewhere in setting file.
If by "setting file" you mean "Java, or possibly C/C++, source code", then yes that is probably the case.
But I don't know where to modify.
StackOverflow is not a great resource for assistance with firmware modifications, sorry.
I have did it by commenting following code in KeyguardViewMediator
private void showLocked() {
/* if (DEBUG) Log.d(TAG, "showLocked");
Message msg = mHandler.obtainMessage(SHOW);
mHandler.sendMessage(msg);*/
}