Android 8.1 系统锁屏显示流程整理

平台:

RK3399 + Android 8.1

源码跟踪:

系统启动时显示锁屏:

|-- frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

    KeyguardServiceDelegate mKeyguardDelegate;

    /** {@inheritDoc} */
    @Override
    public void init(Context context, IWindowManager windowManager,
            WindowManagerFuncs windowManagerFuncs) {
		...
        mKeyguardDelegate = new KeyguardServiceDelegate(mContext,
                new StateCallback() {
                    @Override
                    public void onTrustedChanged() {
                        mWindowManagerFuncs.notifyKeyguardTrustedChanged();
                    }
                });
    }

	/** {@inheritDoc} */
    @Override
    public void systemBooted() {
        bindKeyguard();
        ...
    }
    private void bindKeyguard() {
        synchronized (mLock) {
            if (mKeyguardBound) {
                return;
            }
            mKeyguardBound = true;
        }
        mKeyguardDelegate.bindService(mContext);
    }

	
    /** {@inheritDoc} */
    @Override
    public void systemReady() {//系统服务启动完成.
        // In normal flow, systemReady is called before other system services are ready.
        // So it is better not to bind keyguard here.
        mKeyguardDelegate.onSystemReady();
	}

所有的锁屏相关操作由mKeyguardDelegate 代理完成:
|-- frameworks/base/services/core/java/com/android/server/policy/keyguard/KeyguardServiceDelegate.java

   public void bindService(Context context) {
       Intent intent = new Intent();
       final Resources resources = context.getApplicationContext().getResources();

       final ComponentName keyguardComponent = ComponentName.unflattenFromString(
               resources.getString(com.android.internal.R.string.config_keyguardComponent));
       intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
       intent.setComponent(keyguardComponent);

       if (!context.bindServiceAsUser(intent, mKeyguardConnection,
               Context.BIND_AUTO_CREATE, mHandler, UserHandle.SYSTEM)) {
   	...
   	}
   }

    private final ServiceConnection mKeyguardConnection = new ServiceConnection() {
       @Override
       public void onServiceConnected(ComponentName name, IBinder service) {
           if (DEBUG) Log.v(TAG, "*** Keyguard connected (yay!)");
           mKeyguardService = new KeyguardServiceWrapper(mContext,
                   IKeyguardService.Stub.asInterface(service), mCallback);
   	}
   }


   public void onSystemReady() {
       if (mKeyguardService != null) {
           mKeyguardService.onSystemReady();
       } else {
           mKeyguardState.systemIsReady = true;
       }
   }

BINDER对应的服务组件定义:
|-- frameworks/base/core/res/res/values/config.xml

    <string name="config_keyguardComponent" translatable="false">com.android.systemui/com.android.systemui.keyguard.KeyguardServicestring>

直指SystemUI:
|–frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java

	private final IKeyguardService.Stub mBinder = new IKeyguardService.Stub() {
        @Override // Binder interface
        public void onFinishedGoingToSleep(int reason, boolean cameraGestureTriggered) {
            checkPermission();
            mKeyguardViewMediator.onFinishedGoingToSleep(reason, cameraGestureTriggered);
            mKeyguardLifecyclesDispatcher.dispatch(
                    KeyguardLifecyclesDispatcher.FINISHED_GOING_TO_SLEEP);
        }
	}

        @Override // Binder interface
        public void onSystemReady() {
            Trace.beginSection("KeyguardService.mBinder#onSystemReady");
            checkPermission();
            mKeyguardViewMediator.onSystemReady();
            Trace.endSection();
        }

|-- frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java

	/**
     * Let us know that the system is ready after startup.
     */
    public void onSystemReady() {
        synchronized (this) {
            if (DEBUG) Log.d(TAG, "onSystemReady");
            mSystemReady = true;
            doKeyguardLocked(null);
            mUpdateMonitor.registerCallback(mUpdateCallback);
        }
        // Most services aren't available until the system reaches the ready state, so we
        // send it here when the device first boots.
        maybeSendUserPresentBroadcast();
    }

进休眠或关屏幕:

|-- frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java


    @Override
    public void finishedGoingToSleep(int why) {
        ..
        if (mKeyguardDelegate != null) {
            mKeyguardDelegate.onFinishedGoingToSleep(why,
                    mCameraGestureTriggeredDuringGoingToSleep);
        }
        mCameraGestureTriggeredDuringGoingToSleep = false;
    }

|-- frameworks/base/services/core/java/com/android/server/policy/keyguard/KeyguardServiceDelegate.java

   public void onFinishedGoingToSleep(int why, boolean cameraGestureTriggered) {
       if (mKeyguardService != null) {
           mKeyguardService.onFinishedGoingToSleep(why, cameraGestureTriggered);
       }
       mKeyguardState.interactiveState = INTERACTIVE_STATE_SLEEP;
   }

中间跳转代码与上面相似, 此处省略

|–frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java

   public void onFinishedGoingToSleep(int why, boolean cameraGestureTriggered) {
   	...
           if (mPendingLock) {
               doKeyguardLocked(null);
   	...
   }

   /** 最终显示锁屏的关键函数
    * Enable the keyguard if the settings are appropriate.
    */
   private void doKeyguardLocked(Bundle options) {
   	...
   			//在8.1, 采用PIN等密码锁时, 设备中的存储(data)会被加密, 在关机重启后, 进入一个一级启动状态
   			//这时候, DATA中的数据是原始的, 只有核心应用在这个阶段才是可用的, 同样, 一些系统设置项, 属性系统项, DATA中的文件,
   			//如: 在正常开机时, 保存一个文件到/data/file.txt, 在这阶段, 是找不到file.txt这个文件的.
   			//这是一个解密界面(Settings中的CryptKeeper), 同样需要输入当前设备解锁密码, 用于解密data.
   			//当成功解密后(checkVoldPassword 返回true), 启动时是不需要显示锁屏的. 
               if (mLockPatternUtils.checkVoldPassword(KeyguardUpdateMonitor.getCurrentUser())) {
               if (DEBUG) Log.d(TAG, "Not showing lock screen since just decrypted");
               // Without this, settings is not enabled until the lock screen first appears
               setShowingLocked(false);
               hideLocked();
               return;
           }
   	...
       if (DEBUG) Log.d(TAG, "doKeyguard: showing the lock screen");
       showLocked(options);
   }
       
   /**
    * Send message to keyguard telling it to show itself
    * @see #handleShow
    */
   private void showLocked(Bundle options) {
       Trace.beginSection("KeyguardViewMediator#showLocked aqcuiring mShowKeyguardWakeLock");
       if (DEBUG) Log.d(TAG, "showLocked");
       // ensure we stay awake until we are finished displaying the keyguard
       mShowKeyguardWakeLock.acquire();
       Message msg = mHandler.obtainMessage(SHOW, options);
       mHandler.sendMessage(msg);
       Trace.endSection();
   }
   /**
    * Handle message sent by {@link #showLocked}.
    * @see #SHOW
    */
   private void handleShow(Bundle options) {
       Trace.beginSection("KeyguardViewMediator#handleShow");
       final int currentUser = KeyguardUpdateMonitor.getCurrentUser();
       if (mLockPatternUtils.isSecure(currentUser)) {
           mLockPatternUtils.getDevicePolicyManager().reportKeyguardSecured(currentUser);
       }
       synchronized (KeyguardViewMediator.this) {
           if (!mSystemReady) {
               if (DEBUG) Log.d(TAG, "ignoring handleShow because system is not ready.");
               return;
           } else {
               if (DEBUG) Log.d(TAG, "handleShow");
           }

           setShowingLocked(true);
           mStatusBarKeyguardViewManager.show(options);
           mHiding = false;
           mWakeAndUnlocking = false;
           resetKeyguardDonePendingLocked();
           mHideAnimationRun = false;
           adjustStatusBarLocked();
//更新最后一次操作时间, 以便系统计算进入休眠.
           userActivity();
           mShowKeyguardWakeLock.release();
       }
       mKeyguardDisplayManager.show();
       Trace.endSection();
   }

|-- frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarKeyguardViewManager.java

    protected KeyguardBouncer mBouncer;

    public void registerStatusBar(StatusBar statusBar,
            ViewGroup container,
            ScrimController scrimController,
            FingerprintUnlockController fingerprintUnlockController,
            DismissCallbackRegistry dismissCallbackRegistry) {
        ...
        mBouncer = SystemUIFactory.getInstance().createKeyguardBouncer(mContext,
                mViewMediatorCallback, mLockPatternUtils, container, dismissCallbackRegistry);
    }

|-- frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java

    public KeyguardBouncer createKeyguardBouncer(Context context, ViewMediatorCallback callback,
            LockPatternUtils lockPatternUtils,
            ViewGroup container, DismissCallbackRegistry dismissCallbackRegistry) {
        return new KeyguardBouncer(context, callback, lockPatternUtils, container,
                dismissCallbackRegistry);
    }

|-- frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBouncer.java

   public void show(boolean resetSecuritySelection) {
       final int keyguardUserId = KeyguardUpdateMonitor.getCurrentUser();
       if (keyguardUserId == UserHandle.USER_SYSTEM && UserManager.isSplitSystemUser()) {
           // In split system user mode, we never unlock system user.
           return;
       }
       mFalsingManager.onBouncerShown();
       ensureView();
       if (resetSecuritySelection) {
           // showPrimarySecurityScreen() updates the current security method. This is needed in
           // case we are already showing and the current security method changed.
           mKeyguardView.showPrimarySecurityScreen();
       }
       if (mRoot.getVisibility() == View.VISIBLE || mShowingSoon) {
           return;
       }

       final int activeUserId = ActivityManager.getCurrentUser();
       final boolean isSystemUser =
               UserManager.isSplitSystemUser() && activeUserId == UserHandle.USER_SYSTEM;
       final boolean allowDismissKeyguard = !isSystemUser && activeUserId == keyguardUserId;

       // If allowed, try to dismiss the Keyguard. If no security auth (password/pin/pattern) is
       // set, this will dismiss the whole Keyguard. Otherwise, show the bouncer.
       if (allowDismissKeyguard && mKeyguardView.dismiss(activeUserId)) {
           return;
       }

       // This condition may indicate an error on Android, so log it.
       if (!allowDismissKeyguard) {
           Slog.w(TAG, "User can't dismiss keyguard: " + activeUserId + " != " + keyguardUserId);
       }

       mShowingSoon = true;

       // Split up the work over multiple frames.
       DejankUtils.postAfterTraversal(mShowRunnable);
   }

   private final Runnable mShowRunnable = new Runnable() {
       @Override
       public void run() {
           mRoot.setVisibility(View.VISIBLE);
           mKeyguardView.onResume();
           showPromptReason(mBouncerPromptReason);
           // We might still be collapsed and the view didn't have time to layout yet or still
           // be small, let's wait on the predraw to do the animation in that case.
           if (mKeyguardView.getHeight() != 0 && mKeyguardView.getHeight() != mStatusBarHeight) {
               mKeyguardView.startAppearAnimation();
           } else {
               mKeyguardView.getViewTreeObserver().addOnPreDrawListener(
                       new ViewTreeObserver.OnPreDrawListener() {
                           @Override
                           public boolean onPreDraw() {
                               mKeyguardView.getViewTreeObserver().removeOnPreDrawListener(this);
                               mKeyguardView.startAppearAnimation();
                               return true;
                           }
                       });
               mKeyguardView.requestLayout();
           }
           mShowingSoon = false;
           mKeyguardView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
       }
   };

你可能感兴趣的:(android)