Android6.0系统指纹解锁分析总结

1.指纹解锁(application层主要涉及到Settings 和 SystemUI)

   ----指纹解锁的核心类FingerprintUnlockController,实现了KeyguardUpdateMonitorCallback

        mMode = calculateMode();
	 	//mMode mMode = MODE_NONE: 即为取出指纹解锁的模式
		public class FingerprintUnlockController extends KeyguardUpdateMonitorCallback {
		@Override
		public void onFingerprintAuthenticated(int userId) {
		    ......
		    switch (mMode) {
		        case MODE_DISMISS_BOUNCER://亮屏并且出现图案锁
		            mStatusBarKeyguardViewManager.notifyKeyguardAuthenticated(
		                    false /* strongAuth */);
		            break;
                case MODE_UNLOCK://亮屏不出现图案锁
		            if (!wasDeviceInteractive) {
		                mStatusBarKeyguardViewManager.notifyDeviceWakeUpRequested();
		            }
		            mStatusBarKeyguardViewManager.animateCollapsePanels(
		                    FINGERPRINT_COLLAPSE_SPEEDUP_FACTOR);
		            break;
		        ......
		        case MODE_WAKE_AND_UNLOCK://息屏锁屏解锁
		            mStatusBarWindowManager.setStatusBarFocusable(false);
		            mDozeScrimController.abortPulsing();
		            mKeyguardViewMediator.onWakeAndUnlocking();
		            mScrimController.setWakeAndUnlocking();
		            if (mPhoneStatusBar.getNavigationBarView() != null) {
		                mPhoneStatusBar.getNavigationBarView().setWakeAndUnlocking(true);
		            }
		            break;
                ......
		        case MODE_DISMISS_KEYGUARD://通话界面息屏解锁
		            mStatusBarWindowManager.setStatusBarFocusable(false);
		            mKeyguardViewMediator.onWakeAndUnlocking();
		            if (mPhoneStatusBar.getNavigationBarView() != null) {
		                mPhoneStatusBar.getNavigationBarView().setWakeAndUnlocking(true);
		            }
		            break;
		    }
		    ......
		}
        @Override
		public void onFinishedGoingToSleep(int why) {
		    ......
		}
		@Override
		public void onFingerprintAuthFailed() {
		    cleanup();
		}
        @Override
		public void onFingerprintError(int msgId, String errString) {
		    cleanup();

		                if(Settings.Global.getInt(mContext.getContentResolver(),"persist.fg.errorshow",0) == 0){
		        if(!mPowerManager.isScreenOn()){
		            mPowerManager.wakeUp(SystemClock.uptimeMillis());
		        }else {
		            mStatusBarKeyguardViewManager.animateCollapsePanels(
		                        FINGERPRINT_COLLAPSE_SPEEDUP_FACTOR);
		        }
		        Settings.Global.putInt(mContext.getContentResolver(),"persist.fg.errorshow",1);
		    }

		}

-----KeyguardUpdateMonitor类为解锁屏模块的监听者,它负责监听时间、sim卡、运营商信息、电池信息、电话信息等状态的变化,并通知keyguard View模块更新显示。这个类里面还有有监听指纹的方法startListeningForFingerprint, 该方法调用了FingerprintManager的authenticate()方法并且参数中传入了AuthenticationCallback对象,AuthenticationCallback类中调用了KeyguardUpdateMonitorCallback的方法,FingerprintUnlockController实现了KeyguardUpdateMonitorCallback这样就能实现指纹解锁。

//KeyguardUpdateMonitor.java
			private void startListeningForFingerprint() {
		    ......
		    //调用了FingerprintManager的authenticate()方法
		    mFpm.authenticate(null, mFingerprintCancelSignal, 0, 
		    mAuthenticationCallback, null, userId);
		    ......
		    }

	 private FingerprintManager.AuthenticationCallback mAuthenticationCallback
		        = new AuthenticationCallback() {
		    //指纹匹配失败(没超过5次)
		    @Override
		    public void onAuthenticationFailed() {
		        handleFingerprintAuthFailed();
		    };
		    //指纹匹配成功
		    @Override
		    public void onAuthenticationSucceeded(AuthenticationResult result) {
		        handleFingerprintAuthenticated(result.getUserId());
		    }
		    //指纹匹配提示
		    @Override
		    public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
		        handleFingerprintHelp(helpMsgId, helpString.toString());
		    }
             //指纹匹配错误(匹配失败次数超过5次)
		    @Override
		    public void onAuthenticationError(int errMsgId, CharSequence errString) {
		        handleFingerprintError(errMsgId, errString.toString());
		    }
		    //获得到指纹
		    @Override
		    public void onAuthenticationAcquired(int acquireInfo) {
		       if(Settings.Global.getInt(mContext.getContentResolver(),"persist.fg.errorshow",0) == 1){
		            Settings.Global.putInt(mContext.getContentResolver(),"persist.fg.errorshow",0);
		        }

		        handleFingerprintAcquired(acquireInfo);
		    }
		};
private void handleFingerprintAuthenticated(int authUserId) {
		    ......
		    onFingerprintAuthenticated(userId);
		    ......

		private void onFingerprintAuthenticated(int userId) {
		    mUserFingerprintAuthenticated.put(userId, true);
		    ......
		    for (int i = 0; i < mCallbacks.size(); i++) {
		        KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
		        if (cb != null) {
		            //这里会调用FingerprintUnLockController中的onFingerprintAuthenticated方法处理解锁
		            cb.onFingerprintAuthenticated(userId);
		        }
		    }
		    .....
		}

------FingerprintManager的authenticate()方法,此方法继续调用了FingerprintServiceWarpper的authenticate方法(FingerprintManager与FingerprintService直接通过aidl来通信)

public void authenticate(@Nullable CryptoObject crypto, @Nullable 
				......
				mService.authenticate(mToken, sessionId, userId, mServiceReceiver, 
				            flags,mContext.getOpPackageName());
				......
			}
			private final class FingerprintServiceWrapper extends IFingerprintService.Stub {
				@Override // Binder call
				public void authenticate(final IBinder token, final long opId, 
				final int groupId,final IFingerprintServiceReceiver receiver,
				final int flags,final String opPackageName) {
				    startAuthentication(token, opId, callingUserId, groupId, 
				    receiver, flags, restricted, opPackageName);
			}
			startAuthentication方法会调用AuthenticationClient的start方法,AuthenticationClient
			是为给定的客户端跟踪指纹认证状态。
        public abstract class AuthenticationClient extends ClientMonitor {

				public AuthenticationClient(Context context, long halDeviceId, IBinder token,
				IFingerprintServiceReceiver receiver, int targetUserId,
				int groupId, long opId,boolean restricted, String owner) {
				    super(context, halDeviceId, token, receiver, targetUserId, 
				    groupId, restricted, owner);
				}
				//指纹库处理结果调用
				@Override
				public boolean onAuthenticated(int fingerId, int groupId) {
				    ......
				    boolean authenticated = fingerId != 0;
				    if (!authenticated) {
				        //认证失败
				        receiver.onAuthenticationFailed(getHalDeviceId());
				    } else {
				        //认证成功
				        receiver.onAuthenticationSucceeded(getHalDeviceId(), fp, getTargetUserId());
				    }
         if (!authenticated) {
				        if (inLockoutMode) {
				            //认证错误
				            receiver.onError(getHalDeviceId(),
				                     FingerprintManager.FINGERPRINT_ERROR_LOCKOUT);
				          }
				    }
				    ......
				}
				/**
				 * Start authentication
				 */
                @Override
				public int start() {
				    IFingerprintDaemon daemon = getFingerprintDaemon();
				    ......
				    final int result = daemon.authenticate(mOpId, getGroupId());
				    ...... 
				    return 0; // success
				}
			//start方法会调用fingerprintd,调用底层的指纹库,底层库返回结果后会调用onAuthenticated来反馈结果给receiver,在往上层反馈。这就是指纹的识别流程。

 

你可能感兴趣的:(Android)