Android 13 keyguard 覆盖 Occluded 流程

学习笔记:

第一次接触 Occluded 是在唤起安全相机时,见Android 13 双击power键唤起安全相机。当时在这里遇到个bug:双击power键唤起安全相机,却弹出了bouncer界面,把安全相机界面遮挡了,后面就深入去看了下;看了几天都没找出问题,最后慢放复现视频,发现测试在安全相机界面有误触,根本没问题.......。

不过我们是退出锁屏还是进入锁屏,都将走这么个流程到 KeyguardController:

RootWindowContainer#ensureActivitiesVisible() -> DisplayContent#ensureActivitiesVisible() -> Task#ensureActivitiesVisible() -> ActivityTaskSupervisor#beginActivityVisibilityUpdate() -> KeyguardController#updateVisibility()

Occluded 的改变是从 system_server 进程中的 KeyguardController 开始的;

KeyguardController#updateVisibility()

// KeyguardController.java

    // 确保在完成设置所有可见性之前,根据需要更新锁定屏幕遮挡/关闭/打开屏幕状态
    void updateVisibility() {
        for (int displayNdx = mRootWindowContainer.getChildCount() - 1;
             displayNdx >= 0; displayNdx--) {
            final DisplayContent display = mRootWindowContainer.getChildAt(displayNdx);
            if (display.isRemoving() || display.isRemoved()) continue;
            final KeyguardDisplayState state = getDisplayState(display.mDisplayId);
            // 重点关注
            state.updateVisibility(this, display);
            if (state.mRequestDismissKeyguard) {
                handleDismissKeyguard(display.getDisplayId());
            }
        }
    }

state 为 KeyguardController 内部类KeyguardDisplayState 的对象;
KeyguardController#KeyguardDisplayState.updateVisibility()

// KeyguardController.java # KeyguardDisplayState.class

        void updateVisibility(KeyguardController controller, DisplayContent display) {
            final boolean lastOccluded = mOccluded;

            // 省略部分代码......
            mOccluded = false;

            final Task task = getRootTaskForControllingOccluding(display);
            final ActivityRecord top = task != null ? task.getTopNonFinishingActivity() : null;
            if (top != null) {
                // 省略部分代码......

                // 只有顶级 activity 可以控制遮挡,因为如果顶级应用不想遮挡键盘,我们就无法遮挡键盘。
                occludedByActivity = mTopOccludesActivity != null
                        || (mDismissingKeyguardActivity != null
                        && task.topRunningActivity() == mDismissingKeyguardActivity
                        && controller.canShowWhileOccluded(
                                true /* dismissKeyguard */, false /* showWhenLocked */));
                // FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD only apply for secondary display.
                if (mDisplayId != DEFAULT_DISPLAY) {
                    occludedByActivity |= display.canShowWithInsecureKeyguard()
                            && controller.canDismissKeyguard();
                }
            }

            mShowingDream = display.getDisplayPolicy().isShowingDreamLw() && (top != null
                    && top.getActivityType() == ACTIVITY_TYPE_DREAM);
            mOccluded = mShowingDream || occludedByActivity;
            mRequestDismissKeyguard = lastDismissKeyguardActivity != mDismissingKeyguardActivity
                    && !mOccluded && !mKeyguardGoingAway
                    && mDismissingKeyguardActivity != null;

            // 省略部分代码......

            if (lastOccluded != mOccluded) {
                // 重点关注,当遮挡状态改变时调用
                controller.handleOccludedChanged(mDisplayId, mTopOccludesActivity);
            } else if (!lastKeyguardGoingAway && mKeyguardGoingAway) {
                controller.handleKeyguardGoingAwayChanged(display);
            }
        }

在该方法里对变量 mOccluded 进行了更新,完成了 KeyguardController 的 occluded 属性改变。
KeyguardController# handleOccludedChanged()

// KeyguardController.java
    private void handleOccludedChanged(int displayId, @Nullable ActivityRecord topActivity) {

        if (displayId != DEFAULT_DISPLAY) {
            updateKeyguardSleepToken(displayId);
            return;
        }
        // 重点关注
        mWindowManager.mPolicy.onKeyguardOccludedChangedLw(isDisplayOccluded(DEFAULT_DISPLAY));
        if (isKeyguardLocked(displayId)) {
            mService.deferWindowLayout();
            try {
                mRootWindowContainer.getDefaultDisplay()
                        .requestTransitionAndLegacyPrepare(
                                isDisplayOccluded(DEFAULT_DISPLAY)
                                        ? TRANSIT_KEYGUARD_OCCLUDE
                                        : TRANSIT_KEYGUARD_UNOCCLUDE, 0 /* flags */);
                updateKeyguardSleepToken(DEFAULT_DISPLAY);
                mWindowManager.executeAppTransition();
            } finally {
                mService.continueWindowLayout();
            }
        }
        dismissMultiWindowModeForTaskIfNeeded(displayId, topActivity != null
                ? topActivity.getRootTask() : null);
    }

mWindowManager为 WindowManagerService 对象,mPolicy 是 WindowManagerPolicy 接口的对象,而 WindowManagerPolicy 被 PhoneWindowManager 继承,实现了其相关方法;则上述代码将调用到:PhoneWindowManager#onKeyguardOccludedChangedLw()

// PhoneWindowManager.java
    @Override
    public void onKeyguardOccludedChangedLw(boolean occluded) {
        if (mKeyguardDelegate != null && mKeyguardDelegate.isShowing()
                && !WindowManagerService.sEnableShellTransitions) {
            mPendingKeyguardOccluded = occluded;
            mKeyguardOccludedChanged = true;
        } else {
            // 重点关注
            setKeyguardOccludedLw(occluded, false /* force */,
                    false /* transitionStarted */);
        }
    }

这里当 mKeyguardDelegate.isShowing() 为 true 时,将会绕过 setKeyguardOccludedLw(),但是在后面的过渡动画会再调用 到setKeyguardOccludedLw(),该处过渡动画流程不作介绍。

直接看 PhoneWindowManager#setKeyguardOccludedLw():

// PhoneWindowManager.java
    // 更新 Keyguard 的遮挡状态
    private boolean setKeyguardOccludedLw(boolean isOccluded, boolean force,
            boolean transitionStarted) {
        if (DEBUG_KEYGUARD) Slog.d(TAG, "setKeyguardOccluded occluded=" + isOccluded);
        mKeyguardOccludedChanged = false;
        if (isKeyguardOccluded() == isOccluded && !force) {
            return false;
        }

        final boolean showing = mKeyguardDelegate.isShowing();
        final boolean animate = showing && !isOccluded;
        // 当为keyguard((un)occlude 转换启用远程动画时,
        // KeyguardService 使用远程动画开始作为更新其遮挡状态的信号,因此我们不需要在这里通知。
        final boolean notify = !WindowManagerService.sEnableRemoteKeyguardOccludeAnimation
                || !transitionStarted;
        // 重点关注
        mKeyguardDelegate.setOccluded(isOccluded, animate, notify);
        return showing;
    }

这里改变了mKeyguardDelegate 的值,也就完成了PhoneWidnowManager 的 occluded 属性改变。
KeyguardServiceDelegate#setOccluded()

// KeyguardServiceDelegate.java
    public void setOccluded(boolean isOccluded, boolean animate, boolean notify) {
        if (mKeyguardService != null && notify) {
            if (DEBUG) Log.v(TAG, "setOccluded(" + isOccluded + ") animate=" + animate);
            // 这里最终是调到 KeyguardService 的;
            mKeyguardService.setOccluded(isOccluded, animate);
        }
        mKeyguardState.occluded = isOccluded;
    }

KeyguardService#setOccluded()

// KeyguardService.java
        @Override // Binder interface
        public void setOccluded(boolean isOccluded, boolean animate) {
            Log.d(TAG, "setOccluded(" + isOccluded + ")");
            Trace.beginSection("KeyguardService.mBinder#setOccluded");
            checkPermission();
            // 重点关注
            mKeyguardViewMediator.setOccluded(isOccluded, animate);
            Trace.endSection();
        }

KeyguardViewMediator#setOccluded()

// KeyguardViewMediator.java
    /**
     * 当键盘被另一个窗口遮挡时通知我们
     */
    public void setOccluded(boolean isOccluded, boolean animate) {
        Log.d(TAG, "setOccluded(" + isOccluded + ")");

        Trace.beginSection("KeyguardViewMediator#setOccluded");
        if (DEBUG) Log.d(TAG, "setOccluded " + isOccluded);
        mInteractionJankMonitor.cancel(CUJ_LOCKSCREEN_TRANSITION_FROM_AOD);
        mHandler.removeMessages(SET_OCCLUDED);
        Message msg = mHandler.obtainMessage(SET_OCCLUDED, isOccluded ? 1 : 0, animate ? 1 : 0);
        // 重点关注
        mHandler.sendMessage(msg);
        Trace.endSection();
    }

上述代码通过 Handler 发送了一条消息,调用到 KeyguardViewMediator#handleSetOccluded()

// KeyguardViewMediator.java

    private void handleSetOccluded(boolean isOccluded, boolean animate) {
        Trace.beginSection("KeyguardViewMediator#handleSetOccluded");
        Log.d(TAG, "handleSetOccluded(" + isOccluded + ")");
        synchronized (KeyguardViewMediator.this) {
            if (mHiding && isOccluded) {
                // We're in the process of going away but WindowManager wants to show a
                // SHOW_WHEN_LOCKED activity instead.
                // TODO(bc-unlock): Migrate to remote animation.
                startKeyguardExitAnimation(0, 0);
            }

            if (mOccluded != isOccluded) {
                mOccluded = isOccluded;
                mUpdateMonitor.setKeyguardOccluded(isOccluded);
                // 重点关注
                mKeyguardViewControllerLazy.get().setOccluded(isOccluded, animate
                        && mDeviceInteractive);
                adjustStatusBarLocked();
            }
        }
        Trace.endSection();
    }

这里完成了 mOccluded 值的改变,并把值传到 StatusBarKeyguardViewManager。
KeyguardViewController 被 StatusBarKeyguardViewManager 继承,实现了其相关方法。则继续看:
StatusBarKeyguardViewManager #setOccluded():

// StatusBarKeyguardViewManager.java
    @Override
    public void setOccluded(boolean occluded, boolean animate) {
        final boolean isOccluding = !mOccluded && occluded;
        final boolean isUnOccluding = mOccluded && !occluded;
        setOccludedAndUpdateStates(occluded);

        if (mShowing && isOccluding) {
            SysUiStatsLog.write(SysUiStatsLog.KEYGUARD_STATE_CHANGED,
                    SysUiStatsLog.KEYGUARD_STATE_CHANGED__STATE__OCCLUDED);
            if (mCentralSurfaces.isInLaunchTransition()) {
                final Runnable endRunnable = new Runnable() {
                    @Override
                    public void run() {
                        ///M: [ALPS01807921]
                        ///   mOccluded may be changed before the runnable is executed.
                        if (mOccluded) {
                            mNotificationShadeWindowController.setKeyguardOccluded(mOccluded);
                            reset(true /* hideBouncerWhenShowing */);
                        } else {
                            Log.d(TAG, "setOccluded.run() - mOccluded was set to false") ;
                        }
                    }
                };
                
                mCentralSurfaces.fadeKeyguardAfterLaunchTransition(
                        null /* beforeFading */,
                        endRunnable,
                        endRunnable);
                return;
            }
         // 省略部分代码......
    }

至此,完成了从系统到SystemUI Occluded 值改变。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,313评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,369评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,916评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,333评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,425评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,481评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,491评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,268评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,719评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,004评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,179评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,832评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,510评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,153评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,402评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,045评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,071评论 2 352

推荐阅读更多精彩内容