日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 添加屏幕锁和移除锁屏密码

發布時間:2024/3/24 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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();}@Overrideprotected Intent saveAndVerifyInBackground() {Intent result = null;final int userId = mUserId;mUtils.saveLockPattern(mChosenPattern, mCurrentPattern, userId);// add for save Patternlock dataString mSavedPattern = mUtils.patternToString(mChosenPattern);SystemProperties.set("persist.android.screen.lock", mSavedPattern);// endif (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中有提供方法

/*** Deserialize a pattern.* @param string The pattern serialized with {@link #patternToString}* @return The pattern.*/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;}/*** Serialize a pattern.* @param pattern The pattern.* @return The pattern in string form.*/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();}@Overrideprotected Intent saveAndVerifyInBackground() {Intent result = null;mUtils.saveLockPassword(mChosenPassword, mCurrentPassword, mRequestedQuality,mUserId);// add for save lock passwordSystemProperties.set("persist.android.screen.lock", mChosenPassword);// endif (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) {// Sanity check. We should never get here without confirming user's existing password.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);// mLockPatternUtils.setLockScreenDisabled(true, 0); savedPassword("");}private void savedPassword(String pwd){SystemProperties.set(KEY, pwd);}private class Task extends AsyncTask<String, Void, String> {@Overrideprotected String doInBackground(String... params){String password = params[0];clearScreenLock();mLockPatternUtils.saveLockPassword(password, null, mRequestedQuality, 0);return password;}@Overrideprotected void onPostExecute(String s) {savedPassword(s);mPowerManager.goToSleep(SystemClock.uptimeMillis());}}//LOCAL_JAVA_LIBRARIES := framework //LOCAL_PRIVATE_PLATFORM_APIS := true

然后編寫aidl,提供給客戶使用

參考:https://blog.csdn.net/u012932409/article/details/120916705

總結

以上是生活随笔為你收集整理的Android 添加屏幕锁和移除锁屏密码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。