Android4.4之Keyguard--KeyguardSecurityModel

KeyguardSecurityModel.java

/frameworks/base/packages/Keyguard/src/com/android/keyguard

一、构造方法

几乎只有一个成员变量:mLockPatternUtils,提供了修改其值的方法:setLockPatternUtils

只有一个构造方法,传入Context即可初始化。

private Context mContext;
private LockPatternUtils mLockPatternUtils;

KeyguardSecurityModel(Context context) {
    mContext = context;
    mLockPatternUtils = new LockPatternUtils(context);
}

void setLockPatternUtils(LockPatternUtils utils) {
    mLockPatternUtils = utils;
}

二、声明了重要的枚举变量SecurityMode

 public static enum SecurityMode {
    Invalid, // NULL state
    None, // No security enabled
    Pattern, // Unlock by drawing a pattern.
    Password, // Unlock by entering an alphanumeric password
    PIN, // Strictly numeric password
    Biometric, // Unlock with a biometric key (e.g. finger print or face unlock)
    Account, // Unlock by entering an account's login and password.
    AlarmBoot, // add for power-off alarm.
    SimPinPukMe1, // Unlock by entering a sim pin/puk/me for sim or gemini sim1.
    SimPinPukMe2, // Unlock by entering a sim pin/puk/me for sim or gemini sim2.
    SimPinPukMe3, // Unlock by entering a sim pin/puk/me for sim or gemini sim3.
    SimPinPukMe4, // Unlock by entering a sim pin/puk/me for sim or gemini sim4.
    Voice, // Unlock with voice password
    AntiTheft // Antitheft feature
}
Biometric包括指纹识别、人脸解锁;

SimPinPukMe1~SimPinPukMe4 应该对应的是sim卡锁,有四个是因为MTK的多卡项目;

SimPinPukMe跟PIN是不同的;

AlarmBoot应该是MTK添加的;

Voice不一定支持,有个isVoiceUnlockEnabled方法在代码中暂时没有看到哪里会调用;

AntiTheft不知道是干什么的,碰到它我们就忽略。


三、几个方法

1. SecurityMode getAlternateFor(SecurityMode mode)

/**
 * Some unlock methods can have an alternate, such as biometric unlocks (e.g. face unlock).
 * This function decides if an alternate unlock is available and returns it. Otherwise,
 * returns @param mode.
 */
SecurityMode getAlternateFor(SecurityMode mode) {
    if (!isBiometricUnlockSuppressed() && (mode == SecurityMode.Password
                    || mode == SecurityMode.PIN
                    || mode == SecurityMode.Pattern)) {
        if (isBiometricUnlockEnabled()) {
            return SecurityMode.Biometric;
        } else if (mLockPatternUtils.usingVoiceWeak()) { ///M add for voice unlock
            return SecurityMode.Voice;
        }
    }
    return mode;// no alternate, return what was given

首先,当前SecurityMode为Password、PIN或者Pattern;

其次,当前没有什么东西限制biometric unlock;

这样的话,如果支持biometric unlock且也已经安装的话,当前模式设置为Biometric;否则,如果支持语音解锁,则设置为Voice。

在KeyguardHostView.java文件中会调用这个方法。

2. public boolean isPinPukOrMeRequired(int simId)

/// M; This function checking if we need to show the SimPin lock view for this sim id.
public boolean isPinPukOrMeRequired(int simId) {
    KeyguardUpdateMonitor updateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
    if(updateMonitor != null && KeyguardUtils.isValidSimId(simId)) {
        final IccCardConstants.State simState = updateMonitor.getSimState(simId);
                  // check PIN required
        return ( (simState == IccCardConstants.State.PIN_REQUIRED 
            && !updateMonitor.getPINDismissFlag(simId, KeyguardUpdateMonitor.SimLockType.SIM_LOCK_PIN))
            // check PUK required
            || (simState == IccCardConstants.State.PUK_REQUIRED 
            && !updateMonitor.getPINDismissFlag(simId, KeyguardUpdateMonitor.SimLockType.SIM_LOCK_PUK)
            && KeyguardUtils.getRetryPukCount(simId) != 0)
            // check ME required
            || (simState == IccCardConstants.State.NETWORK_LOCKED
            && !updateMonitor.getPINDismissFlag(simId, KeyguardUpdateMonitor.SimLockType.SIM_LOCK_ME)
            && updateMonitor.getSimMeLeftRetryCount(simId) != 0)
            );
    } else {
        return false;
    }
}

KeyguardSimPinPukView.java的getNextRepollStateSimId方法中调用过这个方法。

留意KeyguardUpdateMonitor的几个方法在这里使用了:getSimState、getPINDismissFlag、getSimMeLeftRetryCount。


3.最重要的当属SecurityMode getSecurityMode()

SecurityMode getSecurityMode() {       
    SecurityMode mode = SecurityMode.None;//初始化为None
    //首先检查是否是SimPinPukMe或AlarmBoot,它们优先级高,如果是则返回结果退出
    if (isPinPukOrMeRequired(PhoneConstants.GEMINI_SIM_1)) {
        mode = SecurityMode.SimPinPukMe1;
    } else if (isPinPukOrMeRequired(PhoneConstants.GEMINI_SIM_2)) {
        mode = SecurityMode.SimPinPukMe2;
    } else if (isPinPukOrMeRequired(PhoneConstants.GEMINI_SIM_3)) {
        mode = SecurityMode.SimPinPukMe3;
    } else if (isPinPukOrMeRequired(PhoneConstants.GEMINI_SIM_4)) {
        mode = SecurityMode.SimPinPukMe4;
    } else if (PowerOffAlarmManager.isAlarmBoot()) {/// M: add for power-off alarm
        mode = SecurityMode.AlarmBoot;
    } 
	
    //如果前面已经满足条件,这里就会跳过,结束程序   
    if (mode == SecurityMode.None) {
	//判断当前是什么解锁模式,要用到DevicePolicyManager
        final int security = mLockPatternUtils.getKeyguardStoredPasswordQuality();
        switch (security) {
	     //如果是NUMERIC,而且isLockPasswordEnabled,则为PIN
            case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
                mode = mLockPatternUtils.isLockPasswordEnabled() ?
                        SecurityMode.PIN : SecurityMode.None;
                break;
	    //如果是ALPHABETIC、ALPHANUMERIC或者COMPLEX,而且isLockPasswordEnabled,则为Password
            case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
            case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
            case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
                mode = mLockPatternUtils.isLockPasswordEnabled() ?
                        SecurityMode.Password : SecurityMode.None;
                break;
	    //如果是SOMETHING或者UNSPECIFIED,而且isLockPatternEnabled:如果isPermanentlyLocked也满足,则为Account,否则才是Pattern
            case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
            case DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED:
                if (mLockPatternUtils.isLockPatternEnabled()) {
                    mode = mLockPatternUtils.isPermanentlyLocked() ?
                        SecurityMode.Account : SecurityMode.Pattern;
                }
                break;
        }
    }
    return mode;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值