Android13重置锁屏(2)
锁屏凭证包括图案、PIN、密码,系统开机默认无锁屏,重置锁屏的想法是在每次设置新锁屏凭证成功后,保存新锁屏凭证到文件,当再次设置时,用保存的锁屏凭证作为验证。
回到LockSettingsService的setLockCredential方法,代码最后增加代码:
@Override public boolean setLockCredential(LockscreenCredential credential, LockscreenCredential savedCredential, int userId) { ........................................ final long identity = Binder.clearCallingIdentity(); try { ............................... notifySeparateProfileChallengeChanged(userId); onPostPasswordChanged(credential, userId); scheduleGc(); //add code Settings.Global.putString(mContext.getContentResolver(),\"lockscreen_password\", byteToHexString(credential.getCredential())); //add end return true; } finally { Binder.restoreCallingIdentity(identity); } } private String byteToHexString(byte [] byteHex){ String strHex=\"\"; if(byteHex==null) return \"\"; for(int i=0;i<byteHex.length;i++){ strHex=strHex+String.format(\"%02X\", byteHex[i]); } return strHex; }
将凭证的byte数组转成HEX码,保存到Settings,通过系统设置修改的锁屏凭证也会经过这里保存。
在定制化system service里面增加一个setPassword方法:
@Override public boolean setPassword(String pwd) throws RemoteException { long id = Binder.clearCallingIdentity(); mContext.enforceCallingOrSelfPermission(Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE,\"YourService\"); mContext.enforceCallingOrSelfPermission(Manifest.permission.SET_AND_VERIFY_LOCKSCREEN_CREDENTIALS,\"YourService\"); mContext.enforceCallingOrSelfPermission(Manifest.permission.SET_INITIAL_LOCK,YourService\"); LockPatternUtils lockPatternUtils = new LockPatternUtils(mContext); LockscreenCredential newCredential; String savePwd=Settings.Global.getString(mContext.getContentResolver(),\"lockscreen_password\"); int type=lockPatternUtils.getCredentialTypeForUser(UserHandle.USER_SYSTEM); newCredential = LockscreenCredential.createPasswordOrNone(pwd); boolean iset=false; byte data[]= hexStringToByte(savePwd); switch (type){ case LockPatternUtils.CREDENTIAL_TYPE_NONE: iset=lockPatternUtils.setLockCredential(newCredential, LockscreenCredential.createNone(), UserHandle.USER_SYSTEM); break; case LockPatternUtils.CREDENTIAL_TYPE_PATTERN: iset=lockPatternUtils.setLockCredential(newCredential, LockscreenCredential.createPattern(LockPatternUtils.byteArrayToPattern(data)), UserHandle.USER_SYSTEM); break; case LockPatternUtils.CREDENTIAL_TYPE_PIN: iset=lockPatternUtils.setLockCredential(newCredential, LockscreenCredential.createPinOrNone(new String(data)), UserHandle.USER_SYSTEM); break; case LockPatternUtils.CREDENTIAL_TYPE_PASSWORD: iset=lockPatternUtils.setLockCredential(newCredential, LockscreenCredential.createPasswordOrNone(new String(data)), UserHandle.USER_SYSTEM); break; default: break; } Binder.restoreCallingIdentity(id); return iset; } private byte[] hexStringToByte(String str) { if(str==null) return null; if(str.length()%2!=0) str=\"0\"+str; int len = (str.length()/2); byte[] result = new byte[len]; char[] achar = str.toCharArray(); for (int i = 0; i < len; i++) { int pos = i * 2; result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1])); } return result; } private byte toByte(char c) { byte b = (byte) \"0123456789ABCDEF\".indexOf(c); if(b==-1) b = (byte) \"0123456789abcdef\".indexOf(c); return b; }
代码从保存的锁屏凭证获取值,并根据当前的锁屏方式,创建还原保存的锁屏作为验证,同时创建一个新的以pwd为密码的锁屏凭证,通过lockPatternUtils.setLockCredential设置。