生活随笔
收集整理的這篇文章主要介紹了
Android 添加屏幕锁和移除锁屏密码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
需求:客戶app控制鎖屏和移除鎖屏密碼
MTK-8.1 Settings 中鎖屏密碼設置相關代碼
1.鎖屏圖案保存核心代碼
源碼:vendor\mediatek\proprietary\packages\apps\MtkSettings\src\com\android\settings\password\ChooseLockPattern.java
public static class SaveAndFinishWorker extends SaveChosenLockWorkerBase { private List < LockPatternView. Cell > mChosenPattern
; private String mCurrentPattern
; private boolean mLockVirgin
; public void start ( LockPatternUtils utils
, boolean credentialRequired
, boolean hasChallenge
, long challenge
, List < LockPatternView. Cell > chosenPattern
, String currentPattern
, int userId
) { prepare ( utils
, credentialRequired
, hasChallenge
, challenge
, userId
) ; mCurrentPattern
= currentPattern
; mChosenPattern
= chosenPattern
; mUserId
= userId
; mLockVirgin
= ! mUtils
. isPatternEverChosen ( mUserId
) ; start ( ) ; } @Override protected Intent saveAndVerifyInBackground ( ) { Intent result
= null ; final int userId
= mUserId
; mUtils
. saveLockPattern ( mChosenPattern
, mCurrentPattern
, userId
) ; String mSavedPattern
= mUtils
. patternToString ( mChosenPattern
) ; SystemProperties . set ( "persist.android.screen.lock" , mSavedPattern
) ; if ( mHasChallenge
) { byte [ ] token
; try { token
= mUtils
. verifyPattern ( mChosenPattern
, mChallenge
, userId
) ; } catch ( RequestThrottledException e
) { token
= null ; } if ( token
== null ) { Log . e ( TAG , "critical: no token returned for known good pattern" ) ; } result
= new Intent ( ) ; result
. putExtra ( ChooseLockSettingsHelper . EXTRA_KEY_CHALLENGE_TOKEN , token
) ; } return result
; }
保存的關鍵方法就是mUtils.saveLockPattern(); mChosenPattern 需保存的圖案密碼 mCurrentPattern 之前保存的圖案密碼,沒有則為null 圖案密碼九宮格每個點 Cell 對應一個 Row, 一個 Column,實際規則如下
(0,0) (0,1) (0,2)
(1,0) (1,1) (1,2)
(2,0) (2,1) (2,2)
最終畫線將每一個 cell 添加到集合中。 圖案密碼轉String存儲,LockPatternUtils中有提供方法
public static List < LockPatternView. Cell > stringToPattern ( String string
) { if ( string
== null ) { return null ; } List < LockPatternView. Cell > result
= Lists . newArrayList ( ) ; final byte [ ] bytes
= string
. getBytes ( ) ; for ( int i
= 0 ; i
< bytes
. length
; i
++ ) { byte b
= ( byte ) ( bytes
[ i
] - '1' ) ; result
. add ( LockPatternView. Cell . of ( b
/ 3 , b
% 3 ) ) ; } return result
; } public static String patternToString ( List < LockPatternView. Cell > pattern
) { if ( pattern
== null ) { return "" ; } final int patternSize
= pattern
. size ( ) ; byte [ ] res
= new byte [ patternSize
] ; for ( int i
= 0 ; i
< patternSize
; i
++ ) { LockPatternView. Cell cell
= pattern
. get ( i
) ; res
[ i
] = ( byte ) ( cell
. getRow ( ) * 3 + cell
. getColumn ( ) + '1' ) ; } return new String ( res
) ; }
2.PIN碼和密碼保存核心代碼
源碼:vendor\mediatek\proprietary\packages\apps\MtkSettings\src\com\android\settings\password\ChooseLockPassword.java
public static class SaveAndFinishWorker extends SaveChosenLockWorkerBase { private String mChosenPassword
; private String mCurrentPassword
; private int mRequestedQuality
; public void start ( LockPatternUtils utils
, boolean required
, boolean hasChallenge
, long challenge
, String chosenPassword
, String currentPassword
, int requestedQuality
, int userId
) { prepare ( utils
, required
, hasChallenge
, challenge
, userId
) ; mChosenPassword
= chosenPassword
; mCurrentPassword
= currentPassword
; mRequestedQuality
= requestedQuality
; mUserId
= userId
; start ( ) ; } @Override protected Intent saveAndVerifyInBackground ( ) { Intent result
= null ; mUtils
. saveLockPassword ( mChosenPassword
, mCurrentPassword
, mRequestedQuality
, mUserId
) ; SystemProperties . set ( "persist.android.screen.lock" , mChosenPassword
) ; if ( mHasChallenge
) { byte [ ] token
; try { token
= mUtils
. verifyPassword ( mChosenPassword
, mChallenge
, mUserId
) ; } catch ( RequestThrottledException e
) { token
= null ; } if ( token
== null ) { Log . e ( TAG , "critical: no token returned for known good password." ) ; } result
= new Intent ( ) ; result
. putExtra ( ChooseLockSettingsHelper . EXTRA_KEY_CHALLENGE_TOKEN , token
) ; } return result
; } }
保存的關鍵方法mUtils.saveLockPassword() mChosenPassword 要保存的密碼 mCurrentPassword 之前保存過的密碼,沒有則為null
3.移除密碼核心代碼
源碼:vendor\mediatek\proprietary\packages\apps\MtkSettings\src\com\android\settings\password\ChooseLockGeneric.java
void updateUnlockMethodAndFinish ( int quality
, boolean disabled
, boolean chooseLockSkipped
) { if ( ! mPasswordConfirmed
) { throw new IllegalStateException ( "Tried to update password without confirming it" ) ; } . . . if ( quality
== DevicePolicyManager . PASSWORD_QUALITY_UNSPECIFIED ) { mLockPatternUtils
. setSeparateProfileChallengeEnabled ( mUserId
, true , mUserPassword
) ; mChooseLockSettingsHelper
. utils ( ) . clearLock ( mUserPassword
, mUserId
) ; mChooseLockSettingsHelper
. utils ( ) . setLockScreenDisabled ( disabled
, mUserId
) ; getActivity ( ) . setResult ( Activity . RESULT_OK ) ; removeAllFingerprintForUserAndFinish ( mUserId
) ; } else { removeAllFingerprintForUserAndFinish ( mUserId
) ; } }
清除密碼 clearLock(mUserPassword, mUserId); 禁用顯示屏幕鎖定(只有在沒有設置pin/密碼/圖案下才有意義),設置為true沒有任何鎖屏顯示 setLockScreenDisabled(disabled, mUserId);
4.創建鎖屏密碼,清除鎖屏密碼
package com. android. screenlock ; import android. content. Context ;
import android. util. Log ;
import android. os. AsyncTask ;
import android. os. SystemProperties ;
import android. os. UserHandle ;
import android. os. PowerManager ;
import android. os. SystemClock ; import com. android. internal. widget. LockPatternUtils ;
import com. android. internal. widget. LockPatternView ;
import com. android. internal. widget. LockPatternView . Cell ; import java. util. ArrayList ;
import java. util. Collections ;
import java. util. List ; import android. app. admin. DevicePolicyManager ; public class ScreenLockHelper { private static final String TAG = "ScreenLockHelper" ; private final String KEY = "persist.android.screen.lock" ; private Context mContext
; private PowerManager mPowerManager
; private LockPatternUtils mLockPatternUtils
; private int mRequestedQuality
= DevicePolicyManager . PASSWORD_QUALITY_NUMERIC ; public ScreenLockHelper ( Context context
) { mContext
= context
; mLockPatternUtils
= new LockPatternUtils ( context
) ; mPowerManager
= ( PowerManager ) mContext
. getSystemService ( Context . POWER_SERVICE ) ; } public void createAndSavePasswordLock ( String password
) { new Task ( ) . execute ( password
) ; } public void clearScreenLock ( ) { String pwdValue
= SystemProperties . get ( KEY , "" ) ; mLockPatternUtils
. clearLock ( pwdValue
, 0 ) ; savedPassword ( "" ) ; } private void savedPassword ( String pwd
) { SystemProperties . set ( KEY , pwd
) ; } private class Task extends AsyncTask < String , Void , String > { @Override protected String doInBackground ( String . . . params
) { String password
= params
[ 0 ] ; clearScreenLock ( ) ; mLockPatternUtils
. saveLockPassword ( password
, null , mRequestedQuality
, 0 ) ; return password
; } @Override protected void onPostExecute ( String s
) { savedPassword ( s
) ; mPowerManager
. goToSleep ( SystemClock . uptimeMillis ( ) ) ; } }
然后編寫aidl,提供給客戶使用
參考:https://blog.csdn.net/u012932409/article/details/120916705
總結
以上是生活随笔 為你收集整理的Android 添加屏幕锁和移除锁屏密码 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。