private final ToBooleanFunction mFindFocusedWindow = w -> {
// 当前处于前台的ActivityRecord
final ActivityRecord focusedApp = mFocusedApp;
// 如果窗口无法接收key事件,则不能作为焦点窗口,返回false
if (!w.canReceiveKeys()) {
return false;
}
final ActivityRecord activity = w.mActivityRecord;
// 如果前台没有Activity,则此次WindowState将作为焦点窗口返回
if (focusedApp == null) {
mTmpWindow = w;
return true;
}
// 如果前台Activity是不可获焦的,则此次WindowState将作为焦点窗口返回
if (!focusedApp.windowsAreFocusable()) {
mTmpWindow = w;
return true;
}
// 如果当前WindowState由ActivityRecord管理,且非StartingWindow,则当
if (activity != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
if (focusedApp.compareTo(activity) > 0) {
mTmpWindow = null;
return true;
}
}
// 不满足以上条件,则此次WindowState将作为焦点窗口返回
mTmpWindow = w;
return true;
};
该方法中,将依次根据如下条件获得焦点窗口:
这里接着看下第一个条件,判断窗口能否可以接受Input事件,由WindowState#canReceiveKeys()方法进行判断:
// frameworks/base/services/core/java/com/android/server/wm/WindowState.java
public boolean canReceiveKeys(boolean fromUserTouch) {
// 可见或处于addWindow()~relayout之间
final boolean canReceiveKeys = isVisibleOrAdding()
// 客户端View可见
&& (mViewVisibility == View.VISIBLE)
// 退出动画执行完毕
&& !mRemoveOnExit
// 没有FLAG_NOT_FOCUSABLE标记
&& ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0)
// mActivityRecord为null或者其可获焦
&& (mActivityRecord == null || mActivityRecord.windowsAreFocusable(fromUserTouch))
// 可以接受touch事件
&& !cantReceiveTouchInput();
if (!canReceiveKeys) {
return false;
}
return fromUserTouch || getDisplayContent().isOnTop()
|| getDisplayContent().isTrusted();
}
如果一个WindowState可以接受Input事件,需要同时满足多个条件:
isVisibleOrAdding()方法用来判断该WindowState可见或处于添加过程中:
// frameworks/base/services/core/java/com/android/server/wm/WindowState.java
boolean isVisibleOrAdding() {
final ActivityRecord atoken = mActivityRecord;
// Surface已经创建,说明relayout()已经执行
return (mHasSurface
// relayout()未执行,且客户端View可见状态
|| (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
// 表示该WindowState由policy控制可见性的flag全部设置
&& isVisibleByPolicy()
// 如果是子窗口,其副窗口的mHidden属性为false
&& !isParentWindowHidden()
// mActivityRecord为null,或者其mVisibleRequested为true
&& (atoken == null || atoken.mVisibleRequested)
// 没有执行退出动画
&& !mAnimatingExit
// Surface没有进行销毁
&& !mDestroying;
}
如果以上条件都满足,则返回true。
cantReceiveTouchInput()
方法用来判断该WindowState是否能够接收touch事件:
// frameworks/base/services/core/java/com/android/server/wm/WindowState.java
boolean cantReceiveTouchInput() {
// 如果mActivityRecord为null,或mActivityRecord所在Task为null,返回false
if (mActivityRecord == null || mActivityRecord.getTask() == null) {
return false;
}
// 所在Task是否忽略输入事件
return mActivityRecord.getTask().getStack().shouldIgnoreInput()
// mActivityRecord.mVisibleRequested属性为false
|| !mActivityRecord.mVisibleRequested
// 最近任务动画在消费Input事件
|| isRecentsAnimationConsumingAppInput();
}
回到mFindFocusedWindow
接口对象中,其他条件就不一一说明,最终从DisplayContent中自顶向下寻找到的第一个满足条件的窗口,将作为新的焦点窗口后,接下来更新Display#mCurrentFocus
变量,表示当前焦点窗口。
InputWindow的更新,是通过InputMonitor#updateInputWindowsLw()
方法来进行。在焦点窗口的 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】 更新过程以及窗口的添加过程中,都会通过InputMonitor#setInputFocusLw()
方法将新的焦点窗口同步给InputMonitor,并开始更新Input Windows。下面从这个方法开始看下Input windows的更新过程。
该方法中,会将新的焦点窗口同步给InputMonitor:
// frameworks/base/services/core/java/com/android/server/wm/InputMonitor.java
public void setInputFocusLw(WindowState newWindow, boolean updateInputWindows) {
if (newWindow != mInputFocus) {
if (newWindow != null && newWindow.canReceiveKeys()) {
newWindow.mToken.paused = false;
}
// 更新当前焦点窗口
mInputFocus = newWindow;
setUpdateInputWindowsNeededLw();
// 更新所有inputwindow
if (updateInputWindows) {
updateInputWindowsLw(false /force/);
}
}
}
之后将执行updateInputWindowsLw()
方法来完成所有InputWindows的更新,该方法的执行时机非常多。
在该方法中,只有当强制更新或者mUpdateInputWindowsNeeded
值为true时,才会进行InputWindows的更新:
// frameworks/base/services/core/java/com/android/server/wm/InputMonitor.java
void updateInputWindowsLw(boolean force) {
if (!force && !mUpdateInputWindowsNeeded) {
return;
}
scheduleUpdateInputWindows();
}
mUpdateInputWindowsNeeded
变量表示是否需要更新InputWindows,当需要更新时,会通过setUpdateInputWindowsNeededLw()将该变量值设置为true:
// frameworks/base/services/core/java/com/android/server/wm/InputMonitor.java
void setUpdateInputWindowsNeededLw() {
mUpdateInputWindowsNeeded = true;
}
之后将执行scheduleUpdateInputWindows()
方法,在这个方法中,会post一个Runnable对象,在mHandler所在的android.anim线程中执行更新流程:
// frameworks/base/services/core/java/com/android/server/wm/InputMonitor.java
private class UpdateInputWindows implements Runnable {
@Override
public void run() {
synchronized (mService.mGlobalLock) {
// 重置
mUpdateInputWindowsPending = false;
mUpdateInputWindowsNeeded = false;
// 是否正在拖拽
final boolean inDrag = mService.mDragDropController.dragDropActiveLocked();
mUpdateInputForAllWindowsConsumer.updateInputWindows(inDrag);
}
}
}
接下来执行mUpdateInputForAllWindowsConsumer.updateInputWindows()
方法。
在该方法中,首先会确认是否存在几类特殊的InputConsumer。InputConsumer用于读取事件,每个窗口对应的客户端都会通过InputConsumer来读取和消费事件,一般情况下,ViewRootImpl在添加窗口过程中,会在注册InputEventReceiver时自动创建InputConsumer对象。此处的四类特殊InputConsumer则是对一些系统UI显式地进行了创建:
// frameworks/base/services/core/java/com/android/server/wm/InputMonitor.java
private void updateInputWindows(boolean inDrag) {
// 显式创建的特殊InputConsumer对象
// 用于处理Nav相关input事件
mNavInputConsumer = getInputConsumer(INPUT_CONSUMER_NAVIGATION);
// 用于处理Pip相关input事件
mPipInputConsumer = getInputConsumer(INPUT_CONSUMER_PIP);
// 用于处理壁纸相关input事件
mWallpaperInputConsumer = getInputConsumer(INPUT_CONSUMER_WALLPAPER);
// 用于处理最近任务相关input事件
mRecentsAnimationInputConsumer = getInputConsumer(INPUT_CONSUMER_RECENTS_ANIMATION);
mAddRecentsAnimationInputConsumerHandle = mRecentsAnimationInputConsumer != null;
mTmpRect.setEmpty();
mDisableWallpaperTouchEvents = false;
mInDrag = inDrag;
mWallpaperController = mDisplayContent.mWallpaperController;
// 重置mInputTransaction
resetInputConsumers(mInputTransaction);
// 遍历
mDisplayContent.forAllWindows(this,
true /* traverseTopToBottom */);
// 将mInputTransaction合并到mPendingTransaction上进行提交
if (!mUpdateInputWindowsImmediately) {
mDisplayContent.getPendingTransaction().merge(mInputTransaction);
mDisplayContent.scheduleAnimation();
}
}
然后,将发起所有WindowState的遍历,mUpdateInputForAllWindowsConsumer本身是一个Consumer接口对象,因此会回调accept()
方法对每个WindowState进行处理:
// frameworks/base/services/core/java/com/android/server/wm/InputMonitor.java
@Override
public void accept(WindowState w) {
// 获取WindowState的InputChannel对象
final InputChannel inputChannel = w.mInputChannel;
// 获取WindowState的InputWindowHandle对象
final InputWindowHandle inputWindowHandle = w.mInputWindowHandle;
// 最近任务是否存在
final RecentsAnimationController recentsAnimationController =
mService.getRecentsAnimationController();
final boolean shouldApplyRecentsInputConsumer = recentsAnimationController != null
&& recentsAnimationController.shouldApplyInputConsumer(w.mActivityRecord);
final int type = w.mAttrs.type;
…
final int flags = w.mAttrs.flags;
final int privateFlags = w.mAttrs.privateFlags;
// 是否是焦点窗口
final boolean hasFocus = w.isFocused();
// mRecentsAnimationInputConsumer处理最近任务相关input事件
if (mAddRecentsAnimationInputConsumerHandle && shouldApplyRecentsInputConsumer) {
if (recentsAnimationController.updateInputConsumerForApp(
mRecentsAnimationInputConsumer.mWindowHandle, hasFocus)) {
mRecentsAnimationInputConsumer.show(mInputTransaction, w);
mAddRecentsAnimationInputConsumerHandle = false;
}
}
// 处理处于PIP模式时的input事件
if (w.inPinnedWindowingMode()) {
}
// mNavInputConsumer处理Nav相关input事件
if (mAddNavInputConsumerHandle) {
mNavInputConsumer.show(mInputTransaction, w);
mAddNavInputConsumerHandle = false;
}
// mWallpaperInputConsumer处理壁纸input事件
if (mAddWallpaperInputConsumerHandle) {
if (w.mAttrs.type == TYPE_WALLPAPER && w.isVisibleLw()) {
mWallpaperInputConsumer.show(mInputTransaction, w);
mAddWallpaperInputConsumerHandle = false;
}
}
// 是否壁纸不接收input事件
if ((privateFlags & PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS) != 0) {
mDisableWallpaperTouchEvents = true;
}
final boolean hasWallpaper = mWallpaperController.isWallpaperTarget(w)
&& !mService.mPolicy.isKeyguardShowing()
&& !mDisableWallpaperTouchEvents;
// 是否处于拖拽过程中
if (mInDrag && isVisible && w.getDisplayContent().isDefaultDisplay) {
mService.mDragDropController.sendDragStartedIfNeededLocked(w);
}
// 填充InputWindowHandle
populateInputWindowHandle(
inputWindowHandle, w, flags, type, isVisible, hasFocus, hasWallpaper);
// 提交inputWindowHandle
if (w.mWinAnimator.hasSurface()) {
mInputTransaction.setInputWindowInfo(
w.mWinAnimator.mSurfaceController.getClientViewRootSurface(),
inputWindowHandle);
}
}
以上方法中:
InputWindowHandle代表了WindowState,传给了InputDispatcher中用于派发事件,当InputDispatcher中进行事件的派发时,将以InputWindowHandle确定需要派发给哪些窗口。下面来看下对inputWindowHandle对象的填充。
在该方法中,会将WindowState的部分属性填充给inputWindowHandle:
//frameworks/base/services/core/java/com/android/server/wm/InputMonitor.java
void populateInputWindowHandle(final InputWindowHandle inputWindowHandle,
final WindowState child, int flags, final int type, final boolean isVisible,
final boolean hasFocus, final boolean hasWallpaper) {
inputWindowHandle.name = child.toString();
inputWindowHandle.inputApplicationHandle = child.mActivityRecord != null
? child.mActivityRecord.getInputApplicationHandle(false /* update */) : null;
flags = child.getSurfaceTouchableRegion(inputWindowHandle, flags);
inputWindowHandle.layoutParamsFlags = flags;
inputWindowHandle.layoutParamsType = type;
inputWindowHandle.dispatchingTimeoutNanos = child.getInputDispatchingTimeoutNanos();
inputWindowHandle.visible = isVisible;
inputWindowHandle.canReceiveKeys = child.canReceiveKeys();
inputWindowHandle.hasFocus = hasFocus;
inputWindowHandle.hasWallpaper = hasWallpaper;
inputWindowHandle.paused = child.mActivityRecord != null ? child.mActivityRecord.paused : false;
inputWindowHandle.ownerPid = child.mSession.mPid;
inputWindowHandle.ownerUid = child.mSession.mUid;
inputWindowHandle.inputFeatures = child.mAttrs.inputFeatures;
inputWindowHandle.displayId = child.getDisplayId();
final Rect frame = child.getFrameLw();
inputWindowHandle.frameLeft = frame.left;
inputWindowHandle.frameTop = frame.top;
inputWindowHandle.frameRight = frame.right;
inputWindowHandle.frameBottom = frame.bottom;
inputWindowHandle.surfaceInset = child.getAttrs().surfaceInsets.left;
if (child.getTask() != null && child.getTask().isOrganized()) {
inputWindowHandle.replaceTouchableRegionWithCrop(null /* Use this surfaces crop */);
}
if (child.mGlobalScale != 1) {
inputWindowHandle.scaleFactor = 1.0f/child.mGlobalScale;
} else {
inputWindowHandle.scaleFactor = 1;
}
}
填充完毕InputWindowHandle后,会将InputWindowHandle设置给mInputTransaction对象:
// frameworks/base/core/java/android/view/SurfaceControl.java
public Transaction setInputWindowInfo(SurfaceControl sc, InputWindowHandle handle) {
checkPreconditions(sc);
nativeSetInputWindowInfo(mNativeObject, sc.mNativeObject, handle);
return this;
}
当遍历完成后,回到updateInputWindows()
方法的最后,将mInputTransaction对象合并到了`DisplayContent#mPendingTransaction``统一进行事务提交,最终通过SurfaceFlinger设置给了InputDispatcher:
// frameworks/native/services/inputflinger/dispatcher/InputDispatcher.cpp