Android Focused Window的更新

 

启动App时更新inputInfo/请求焦点窗口流程:

App主线程调ViewRootImpl.java的relayoutWindow();然后调用到Wms的relayoutWindow(),窗口布局流程。焦点窗口的更新,通过WMS#updateFocusedWindowLocked()方法开始,下面从这个方法开始,看下整个更新流程。

WMS#updateFocusedWindowLocked: 其中,第一个参数mode表示更新焦点窗口时所处的阶段,共有五个参数

// 表示正常更新    
static final int UPDATE_FOCUS_NORMAL = 0;    
// 表示此次更新焦点窗口发生在window layer分配之前    
static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;    
// 表示此次更新焦点窗口发生在进行放置Surface过程中,在performSurfacePlacement()时    
static final int UPDATE_FOCUS_PLACING_SURFACES = 2;    
// 表示此次更新焦点窗口发生在进行放置Surface之前    
static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;    
// 表示此次更新焦点窗口发生在焦点窗口移除后    
static final int UPDATE_FOCUS_REMOVING_FOCUS = 4;

如在Window添加过程的addWindow()方法中,更新焦点窗口发生在分配窗口layer流程之前,因此使用UPDATE_FOCUS_WILL_ASSIGN_LAYERS作为第一个参数,表示此次更新时,还没有分配layer。 针对不同阶段,会有不同的操作。第二个参数表示是否同步更新InputWindow,一般在调用的地方会写死

RootWindowContainer#updateFocusedWindowLocked: 这里会遍历DisplayContent,并在每个DisplayContent中进行更新,然后将更新的结果返回给DisplayContent#mCurrentFocus变量,该变量表示全局的焦点窗口。同时更新mTopFocusedDisplayId变量,表示当前焦点屏(即焦点窗口所在的屏)。

DisplayContent#updateFocusedWindowLocked: 上述方法中:

1.通过findFocusedWindowIfNeeded()方法寻找焦点窗口;

2.根据焦点窗口的变化,更新Input Target窗口;

3.更新全局焦点窗口对象mCurrentFocus;

4.根据更新的不同阶段做不同处理。

5.向InputMonitor中设置焦点窗口setInputFocusLw

下面看下如何寻找到焦点窗口。

DisplayContent#findFocusedWindowIfNeeded: 当topFocusedDisplayId为INVALID_DISPLAY时,认为当前焦点display没有焦点窗口,需要寻找重新确认,所以又继续执行findFocusedWindow()方法寻找, 该方法中,会遍历所有WindowState,然后将寻找到的WindowState赋值给mTmpWindow,并返回给WMS。接下来看下这个mFindFocusedWindow函数接口对象

1.如果WindowState不能接收Input事件,则不能作为焦点窗口;

2.如果没有前台Activity,则当前WindowState作为焦点窗口返回;

3.如果前台Activity是不可获焦状态,则当前WindowState作为焦点窗口返回;

4.如果当前WindowState由ActivityRecord管理,且该WindowState不是Staring Window类型,那么当前台Activity在当前WindowState所属Activity之上时,不存在焦点窗口;

5.如果Activity当前嵌入到焦点任务中,则除非 Activity与FocusedApp位于同一TaskFragment上,否则无法获得焦点。

6.如果以上条件都不满足,则当前WindowState作为焦点窗口返回;

接下来看一下canReceiveKeys这个函数。如果一个WindowState可以接受Input事件,需要同时满足多个条件:

1. isVisibleRequestedOrAdding()方法为true,表示该WindowState可见或处于添加过程中:

2. mViewVisibility属性为View.VISIBLE,表示客户端View可见;

3. mRemoveOnExit为false,表示WindowState的退出动画不存在;

4. mAttrs.flags中不存在FLAG_NOT_FOCUSABLE标记,该标记如果设置,表示该窗口为不可获焦窗口;

5. mActivityRecord为null或者mActivityRecord可获焦;

6. shouldIgnoreInput()方法为false,表示可以接受Touch事件。

isVisibleRequestedOrAdding()方法用来判断该WindowState可见或处于添加过程中:

shouldIgnoreInput() 方法用来判断该WindowState是否能够接收touch事件。

如果遇到找不到焦点窗口的情况:比如log发现窗口已经是onresume的状态,但是焦点窗口一直未请求切换到此窗口可以查看如下这条log,主要是打印canReceiveKeys为何false的原因(那个属性不对)

canReceiveKeysReason此方法

/** Returns {@code true} if this window desires key events. */
boolean canReceiveKeys() {
    return canReceiveKeys(false /* fromUserTouch */);
}
 
public String canReceiveKeysReason(boolean fromUserTouch) {
    return "fromTouch= " + fromUserTouch
            + " isVisibleRequestedOrAdding=" + isVisibleRequestedOrAdding()
            + " mViewVisibility=" + mViewVisibility
            + " mRemoveOnExit=" + mRemoveOnExit
            + " flags=" + mAttrs.flags
            + " appWindowsAreFocusable="
            + (mActivityRecord == null || mActivityRecord.windowsAreFocusable(fromUserTouch))
            + " canReceiveTouchInput=" + canReceiveTouchInput()
            + " displayIsOnTop=" + getDisplayContent().isOnTop()
            + " displayIsTrusted=" + getDisplayContent().isTrusted()
            + " transitShouldKeepFocus=" + (mActivityRecord != null
                    && mTransitionController.shouldKeepFocus(mActivityRecord));     
}

然后我们回到mFindFocusedWindow接口对象中,其他条件就不一一说明,最终从DisplayContent中自顶向下寻找到的第一个满足条件的窗口,将作为新的焦点窗口后,接下来更新Display#mCurrentFocus变量,表示当前焦点窗口。

@frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java
relayoutWindow
    if (focusMayChange) { if (updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL, true /*updateInputWindows*/)) }
@frameworks/base/services/core/java/com/android/server/wm/RootWindowContainer.java    
        //其中,第一个参数mode表示更新焦点窗口时所处的阶段,共有五个参数:
        boolean changed = mRoot.updateFocusedWindowLocked(mode, updateInputWindows);
            // 存储了当前焦点窗口的Pid和ActivityRecord的Map映射
            mTopFocusedAppByProcess.clear();
            // Go through the children in z-order starting at the top-most
            // 遍历DisplayContent
            for (int i = mChildren.size() - 1; i >= 0; --i) {
                final DisplayContent dc = mChildren.get(i);
@frameworks/base/services/core/java/com/android/server/wm/DisplayContent.java                
                    // 针对每个DisplayContent,进行焦点窗口更新
                    changed |= dc.updateFocusedWindowLocked(mode, updateInputWindows, topFocusedDisplayId);
                        // 不要自动将焦点重新分配离开应该保持焦点的应用程序窗口。findFocusedWindow` 将始终抓取瞬态启动的应用程序,因为它位于“顶部”,会造成不匹配,所以早点出去。
                        if (mCurrentFocus != null && mTransitionController.shouldKeepFocus(mCurrentFocus)
                                // 这只是保持焦点,所以如果焦点应用程序已被显式更改(例如通过 setFocusedTask),请不要提前退出。
                                && mFocusedApp != null && mCurrentFocus.isDescendantOf(mFocusedApp) && mCurrentFocus.isVisible() && mCurrentFocus.isFocusable()) {
                            return false; }     
                        // 寻找焦点窗口                   
                        WindowState newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId); 
                            (mWmService.mPerDisplayFocusEnabled || topFocusedDisplayId == INVALID_DISPLAY) ? findFocusedWindow() : null;
                                // 遍历windowstate mFindFocusedWindow会在找到新的焦点窗口时填充 mTmpWindow。
                                forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
                                /***用于查找给定窗口的聚焦窗口的 lambda 函数. 如果找到聚焦窗口,则lambda返回true,否则返回 false。如果找到焦点窗口,它将被存储在mTmpWindow中。*/
                                    private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
                                        final ActivityRecord focusedApp = mFocusedApp;  // 当前处于前台的ActivityRecord
@frameworks/base/services/core/java/com/android/server/wm/WindowState.java                                        
                                        if (!w.canReceiveKeys()) { return false; } // 如果窗口无法接收key事件,则不能作为焦点窗口,返回false
                                            canReceiveKeys(false /* fromUserTouch */);
                                                // 在瞬态启动期间,瞬态隐藏窗口不可见,或在顶部,但保持可聚焦,因此可以接收密钥。
                                                if (mActivityRecord != null && mTransitionController.shouldKeepFocus(mActivityRecord)) {  return true; }  
                                                // 可见或处于addWindow()~relayout之间
                                                final boolean canReceiveKeys = isVisibleRequestedOrAdding() 
                                                    final ActivityRecord atoken = mActivityRecord;
                                                    // Surface已经创建,说明relayout()已经执行
                                                    return (mHasSurface  || (!mRelayoutCalled && mViewVisibility == View.VISIBLE)) // relayout()未执行,且客户端View可见状态 
                                                            && isVisibleByPolicy() && !isParentWindowHidden() // 如果是子窗口,其副窗口的mHidden属性为false
                                                            && (atoken == null || atoken.isVisibleRequested()) // mActivityRecord为null,或者其mVisibleRequested为true
                                                            && !mAnimatingExit && !mDestroying;
   // 没有执行退出动画   //且Surface没有进行销毁                                           
                                                    && (mViewVisibility == View.VISIBLE)  // 客户端View可见 
                                                    && !mRemoveOnExit  // 退出动画执行完毕
                                                    && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0)  // 没有FLAG_NOT_FOCUSABLE标记
                                                    && (mActivityRecord == null || mActivityRecord.windowsAreFocusable(fromUserTouch))  // mActivityRecord为null或者其可获焦
                                                    && (mActivityRecord == null || mActivityRecord.getTask() == null || !mActivityRecord.getTask().getRootTask().shouldIgnoreInput());//可以接受touch事件
                                                        //是否支持靠背功能 是否是固定窗口下 且不是Display上的焦点根任务
                                                        if(mAtmService.mHasLeanbackFeature && inPinnedWindowingMode() && !isFocusedRootTaskOnDisplay()) { return true }  // 防止画中画根任务接收电视上的输入
                                                // 除非用户故意触摸显示器,否则不允许不受信任的虚拟显示器接收密钥。
                                                return fromUserTouch || getDisplayContent().isOnTop() || getDisplayContent().isTrusted();
                                        if (focusedApp == null) { mTmpWindow = w;  return true; }// 如果前台没有Activity,则此次WindowState将作为焦点窗口返回
                                        if (!focusedApp.windowsAreFocusable()) { mTmpWindow = w; return true; } // 如果前台Activity是不可获焦的,则此次WindowState将作为焦点窗口返回
                                        // 如果当前WindowState由ActivityRecord管理,且非StartingWindow,则当前台Activity在当前WindowState所属Activity之上时,不存在焦点窗口
                                        if (activity != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) { 
                                            if (focusedApp.compareTo(activity) > 0) { mTmpWindow = null; return true; 
                                            TaskFragment parent = activity.getTaskFragment();                                            
                                            if (parent != null && parent.isEmbedded()) {
                                                // 如果Activity当前嵌入到焦点任务中,则除非 Activity 与 FocusedApp 位于同一 TaskFragment 上,否则无法获得焦点。
                                                if (activity.getTask() == focusedApp.getTask() && activity.getTaskFragment() != focusedApp.getTaskFragment()) {  return false; } }
                                                                                                                                
                                        }
                                        // 不满足以上条件,则此次WindowState将作为焦点窗口返回
                                        mTmpWindow = w;
                        final WindowState imWindow = mInputMethodWindow;
                        if (imWindow != null) {
                            final WindowState prevTarget = mImeLayeringTarget;  // 更新IME target窗口
                            final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/); //获取新的IME Target窗口
                            if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {                                
                                assignWindowLayers(false /* setLayoutNeeded */); } //进行window layer的分配
                        // IME target窗口发生变化,重新获取一次焦点窗口
                        if (imWindowChanged) {
                            mWmService.mWindowsChanged = true;
                            setLayoutNeeded();
                            newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId); }
                        final WindowState oldFocus = mCurrentFocus;  //记录旧焦点窗口 
                        mCurrentFocus = newFocus; // 更新新焦点窗口
                        if (newFocus != null) {
                            // 这两个列表用于记录ANR状态,表示自从焦点窗口为空时添加和移除的窗口
                            mWinAddedSinceNullFocus.clear();
                            mWinRemovedSinceNullFocus.clear();
                        if (newFocus.canReceiveKeys()) { newFocus.mToken.paused = false; }  // 设置焦点窗口所在mToken.paused属性为false
                        // IME target窗口发生变化,且旧焦点窗口非输入法窗口时
                        if (imWindowChanged && oldFocus != mInputMethodWindow) {
                            // Focus of the input method window changed. Perform layout if needed.
                            if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
                                performLayout(true /*initial*/,  updateInputWindows);
                            } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
                                // Client will do the layout, but we need to assign layers for handleNewWindowLocked() below.
                                assignWindowLayers(false /* setLayoutNeeded */); 
                            }
                        // 向InputMonitor中设置焦点窗口
                        if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
                            // If we defer assigning layers, then the caller is responsible for doing this part.
                            getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);}
    
                        // 为IME窗口进行调整
                        adjustForImeIfNeeded();
                        updateKeepClearAreas();
                        
                          
                    // 更新全局焦点窗口
                    final WindowState newFocus = dc.mCurrentFocus;
                    if (newFocus != null) {
                        mTopFocusedAppByProcess.put(pidOfNewFocus, newFocus.mActivityRecord);
                        topFocusedDisplayId = dc.getDisplayId();   }
            // 更新mTopFocusedDisplayId,表示焦点屏幕
            mTopFocusedDisplayId = topFocusedDisplayId;
            mWmService.mInputManager.setFocusedDisplay(topFocusedDisplayId);
            mWmService.mPolicy.setTopFocusedDisplay(topFocusedDisplayId);
            mWmService.mAccessibilityController.setFocusedDisplay(topFocusedDisplayId);

  • 21
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薛文旺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值