欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android 设置window信息到inputflinger

程序员文章站 2022-03-11 22:52:54
在Android InputDispatch事件派发->选择目标窗口一文中我们分析了Android 根据Wms的window信息来派发事件到window对应的应用程序,今天我们来分析下Android如何更新window信息到inputflinger。当window信息发生变化的时候Wms需要更新window的信息到inputflinger, Android使用InputMonitor.java类来完成操作,InputMonitor 更新window的函数为updateInputWindowsLw。 我们在...

在Android InputDispatch事件派发->选择目标窗口一文中我们分析了Android 根据Wms的window信息来派发事件到window对应的应用程序,今天我们来分析下Android如何更新window信息到inputflinger。
当window信息发生变化的时候Wms需要更新window的信息到inputflinger, Android使用InputMonitor.java类来完成操作,InputMonitor 更新window的函数为updateInputWindowsLw。 我们在在Android InputDispatch事件派发->选择目标窗口一文中看到InputDispatcher使用InputWindowHandle代表一个Window,在Wms中由于是java代码,使用InputWindowHandle java类作为inputflinger中InputWindowHandle的对等体。

public final class InputWindowHandle {
 // Pointer to the native input window handle.
    // This field is lazily initialized via JNI.
    @SuppressWarnings("unused")
    private long ptr;

    // The input application handle.
    public final InputApplicationHandle inputApplicationHandle;

    // The window manager's window state.
    public final Object windowState;

    // The input channel associated with the window.
    public InputChannel inputChannel;

    // The window name.
    public String name;

    // Window layout params attributes.  (WindowManager.LayoutParams)
    public int layoutParamsFlags;
    public int layoutParamsType;

    // Dispatching timeout.
    public long dispatchingTimeoutNanos;

    // Window frame.
    public int frameLeft;
    public int frameTop;
    public int frameRight;
    public int frameBottom;

    // Global scaling factor applied to touch events when they are dispatched
    // to the window
    public float scaleFactor;

    // Window touchable region.
    public final Region touchableRegion = new Region();

    // Window is visible.
    public boolean visible;

    // Window can receive keys.
    public boolean canReceiveKeys;

    // Window has focus.
    public boolean hasFocus;

    // Window has wallpaper.  (window is the current wallpaper target)
    public boolean hasWallpaper;

    // Input event dispatching is paused.
    public boolean paused;

    // Window layer.
    public int layer;

    // Id of process and user that owns the window.
    public int ownerPid;
    public int ownerUid;

    // Window input features.
    public int inputFeatures;

    // Display this input is on.
    public final int displayId;

}

Inputflinger和wms中的InputWindowHandle提供的信息是一致的。
Android 设置window信息到inputflinger
Wms把每个屏幕抽象为DisplayContent,每个屏幕上打开的窗口按照层级关系存储再WindowList的集合中,每个Window用WindowState描述状态,WindowState中的mInputWindowHandle则描述了输入相关的窗口信息。updateInputWindowsLw函数就是将窗口信息告知给inputflinger的函数。

 /* Updates the cached window information provided to the input dispatcher. */
    public void updateInputWindowsLw(boolean force) {
        if (!force && !mUpdateInputWindowsNeeded) {
            return;
        }
        mUpdateInputWindowsNeeded = false;

        if (false) Slog.d(WindowManagerService.TAG, ">>>>>> ENTERED updateInputWindowsLw");

        // Populate the input window list with information about all of the windows that
        // could potentially receive input.
        // As an optimization, we could try to prune the list of windows but this turns
        // out to be difficult because only the native code knows for sure which window
        // currently has touch focus.
        boolean disableWallpaperTouchEvents = false;

        // If there's a drag in flight, provide a pseudowindow to catch drag input
        final boolean inDrag = (mService.mDragState != null);
        if (inDrag) {
            if (WindowManagerService.DEBUG_DRAG) {
                Log.d(WindowManagerService.TAG, "Inserting drag window");
            }
            final InputWindowHandle dragWindowHandle = mService.mDragState.mDragWindowHandle;
            if (dragWindowHandle != null) {
                 // 如果当前正在拖动窗口,将拖动处理的InputHandler添加到mInputWindowHandles中
                addInputWindowHandleLw(dragWindowHandle);
            } else {
                Slog.w(WindowManagerService.TAG, "Drag is in progress but there is no "
                        + "drag window handle.");
            }
        }

        boolean addInputConsumerHandle = mService.mInputConsumer != null;

        // Add all windows on the default display.
        final int numDisplays = mService.mDisplayContents.size();
        // 遍历所有的屏幕下的所有窗口
        for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
            WindowList windows = mService.mDisplayContents.valueAt(displayNdx).getWindowList();
            for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
                final WindowState child = windows.get(winNdx);
                final InputChannel inputChannel = child.mInputChannel;
                final InputWindowHandle inputWindowHandle = child.mInputWindowHandle;
                if (inputChannel == null || inputWindowHandle == null || child.mRemoved) {
                    // Skip this window because it cannot possibly receive input.
                    // 跳过不能处理input事件的窗口
                    continue;
                }
                // 添加mInputConsumer到mInputWindowHandles列表,注意会根据层级添加到合适的位置
                if (addInputConsumerHandle
                        && inputWindowHandle.layer <= mService.mInputConsumer.mWindowHandle.layer) {
                    addInputWindowHandleLw(mService.mInputConsumer.mWindowHandle);
                    addInputConsumerHandle = false;
                }

                final int flags = child.mAttrs.flags;
                final int privateFlags = child.mAttrs.privateFlags;
                final int type = child.mAttrs.type;

                final boolean hasFocus = (child == mInputFocus);
                final boolean isVisible = child.isVisibleLw();
                if ((privateFlags
                        & WindowManager.LayoutParams.PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS)
                            != 0) {
                    // 如果上面有window进制将事件发送给墙纸窗口,则后续都不允许将事件发送给壁纸窗口
                    disableWallpaperTouchEvents = true;
                }
                final boolean hasWallpaper = (child == mService.mWallpaperTarget)
                        && (privateFlags & WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD) == 0
                        && !disableWallpaperTouchEvents;
                final boolean onDefaultDisplay = (child.getDisplayId() == Display.DEFAULT_DISPLAY);

                // If there's a drag in progress and 'child' is a potential drop target,
                // make sure it's been told about the drag
                if (inDrag && isVisible && onDefaultDisplay) {
                    mService.mDragState.sendDragStartedIfNeededLw(child);
                }
                // 添加window到mInputWindowHandles
                addInputWindowHandleLw(inputWindowHandle, child, flags, type, isVisible, hasFocus,
                        hasWallpaper);
            }
        }

        // 提交窗口信息到inputflinger
        // Send windows to native code.
        mService.mInputManager.setInputWindows(mInputWindowHandles);

        // Clear the list in preparation for the next round.
        // 清空
        clearInputWindowHandlesLw();

        if (false) Slog.d(WindowManagerService.TAG, "<<<<<<< EXITED updateInputWindowsLw");
    }

updateInputWindowsLw函数整个流程不太复杂,整个过程主要做的工作就是按照从高到低的顺序收集窗口信息到mInputWindowHandles描述的数组中,添加窗口到mInputWindowHandles使用的函数为addInputWindowHandleLw函数。当收集完所有窗口信息后,通过mService.mInputManager.setInputWindows(mInputWindowHandles)同步窗口信息给inputflinger。最后调用clearInputWindowHandlesLw()函数来清空mInputWindowHandles,方便下一轮收集。在收集window的过程中有两个特殊的window,第一个是mService.mDragState.mDragWindowHandle,用于处理window拖动的情况。另外一个是mService.mInputConsumer.mWindowHandle,处于在应用进入全屏模式下拦截事件。比如全屏播放视频的时候,触摸窗口会将导航栏显示出来,就是通过这个window来接收的输入事件,进行处理。

本文地址:https://blog.csdn.net/woai110120130/article/details/112642294

相关标签: android