然后我们回到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);