Android14 WMS-窗口添加流程(一)-Client端

news2024/10/5 18:30:49

      窗口布局在onCreate方法中通过setContentView(R.layout.xxx)加载,但窗口的显示并不是在wm_on_create_called中, 而是在wm_on_resume_called后,也就是说应用onResume时此窗口是不可见的,真正可见是当此window窗口的mDrawState变化状态从NO_SURFACE -> DRAW_PENDING -> COMMIT_DRAW_PENDING  -> HAS_DRAWN-> READY_TO_SHOW,然后才会将图层置为可见状态,这个在后面会讲解到。设置为可见的log如下:

05-25 10:56:31.956  1915  1973 V WindowManager: performShow on Window{f4647f5 u0 com.android.settings/com.android.settings.homepage.SettingsHomepageActivity}: mDrawState=READY_TO_SHOW readyForDisplay=true starting=false during animation: policyVis=true parentHidden=false tok.visibleRequested=true tok.visible=true animating=false tok animating=false Callers=com.android.server.wm.WindowState.performShowLocked:4372 com.android.server.wm.WindowStateAnimator.commitFinishDrawingLocked:256 com.android.server.wm.DisplayContent.lambda$new$8:1082 com.android.server.wm.DisplayContent.$r8$lambda$NJwM1ysKPNyOazqyI2QXlp2I4yA:0 
05-25 10:56:31.962  1915  1973 V WindowManager: Showing Window{f4647f5 u0 com.android.settings/com.android.settings.homepage.SettingsHomepageActivity}: mDrawState=READY_TO_SHOW readyForDisplay=true starting=false during animation: policyVis=true parentHidden=false tok.visibleRequested=true tok.visible=true animating=true tok animating=false Callers=com.android.server.wm.WindowState.performShowLocked:4387 com.android.server.wm.WindowStateAnimator.commitFinishDrawingLocked:256 com.android.server.wm.DisplayContent.lambda$new$8:1082 com.android.server.wm.DisplayContent.$r8$lambda$NJwM1ysKPNyOazqyI2QXlp2I4yA:0


1. ActivityThread#handleResumeActivity

Activity启动时一开始都是置为不可见INVISIBLE的,然后才置为可见VISIBLE。
ActivityThread.java - OpenGrok cross reference for /frameworks/base/core/java/android/app/ActivityThread.java

    @Override
    public void handleResumeActivity(ActivityClientRecord r, boolean finalStateRequest,
            boolean isForward, boolean shouldSendCompatFakeFocus, String reason) {
        // If we are getting ready to gc after going to the background, well
        // we are back active so skip it.
        unscheduleGcIdler();
        mSomeActivitiesChanged = true;

        // TODO Push resumeArgs into the activity for consideration
        // skip below steps for double-resume and r.mFinish = true case.
//这个是activity resume很重要的一步,会调用到activity本身的onResume方法,后面会做详细解释
        if (!performResumeActivity(r, finalStateRequest, reason)) {
            return;
        }
//如果mActivitiesToBeDestroyed集合包含此acitivity,则不往下执行resume操作。
//mActivitiesToBeDestroyed是即将 要销毁的activity集合
        if (mActivitiesToBeDestroyed.containsKey(r.token)) {
            // Although the activity is resumed, it is going to be destroyed. So the following
            // UI operations are unnecessary and also prevents exception because its token may
            // be gone that window manager cannot recognize it. All necessary cleanup actions
            // performed below will be done while handling destruction.
            return;
        }

        final Activity a = r.activity;

        if (localLOGV) {
            Slog.v(TAG, "Resume " + r + " started activity: " + a.mStartedActivity
                    + ", hideForNow: " + r.hideForNow + ", finished: " + a.mFinished);
        }
...
//如果这个window没有被add进window manager,并且这个activity没有执行finish操作,或者启动其他activity,则首先要将此window添加进window manager中。
        boolean willBeVisible = !a.mStartedActivity;
        if (!willBeVisible) {
            willBeVisible = ActivityClient.getInstance().willActivityBeVisible(
                    a.getActivityToken());
        }
//这里代表这是首次启动这个activity
//如果window为空,并且不要执行finish,并且申请可见,则进入如下逻辑
        if (r.window == null && !a.mFinished && willBeVisible) {
            r.window = r.activity.getWindow();
            View decor = r.window.getDecorView();
//先将根布局设置为不可见
            decor.setVisibility(View.INVISIBLE);
//获取当前window manager对象
            ViewManager wm = a.getWindowManager();
//获取当前window属性
            WindowManager.LayoutParams l = r.window.getAttributes();
            a.mDecor = decor;
//将window type类型设置为普通APP类型
            l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
            l.softInputMode |= forwardBit;
            if (r.mPreserveWindow) {
                a.mWindowAdded = true;
                r.mPreserveWindow = false;
                // Normally the ViewRoot sets up callbacks with the Activity
                // in addView->ViewRootImpl#setView. If we are instead reusing
                // the decor view we have to notify the view root that the
                // callbacks may have changed.
//获取ViewRootImpl
                ViewRootImpl impl = decor.getViewRootImpl();
                if (impl != null) {
                    impl.notifyChildRebuilt();
                }
            }
            if (a.mVisibleFromClient) {
                if (!a.mWindowAdded) {
                    a.mWindowAdded = true;
//如果此window没有被add进window manager过,则将此根view(decor)add进window manager中
                    wm.addView(decor, l);
                } else {
                    // The activity will get a callback for this {@link LayoutParams} change
                    // earlier. However, at that time the decor will not be set (this is set
                    // in this method), so no action will be taken. This call ensures the
                    // callback occurs with the decor set.
                    a.onWindowAttributesChanged(l);
                }
            }

            // If the window has already been added, but during resume
            // we started another activity, then don't yet make the
            // window visible.
        } else if (!willBeVisible) {//如果这个activity已经被add进window manager过了,并且在resume期间又起了其他activity,那么我们就不会将这个window设置为可见
            if (localLOGV) Slog.v(TAG, "Launch " + r + " mStartedActivity set");
//设置立即hide的flag为true
            r.hideForNow = true;
        }

        // Get rid of anything left hanging around.
        cleanUpPendingRemoveWindows(r, false /* force */);

        // The window is now visible if it has been added, we are not
        // simply finishing, and we are not starting another activity.
//这里代表这个activity已经被添加进window manager了,并非首次启动
        if (!r.activity.mFinished && willBeVisible && r.activity.mDecor != null && !r.hideForNow) {
            if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward=" + isForward);
//获取ViewRootImpl
            ViewRootImpl impl = r.window.getDecorView().getViewRootImpl();
//获取窗口属性
            WindowManager.LayoutParams l = impl != null
                    ? impl.mWindowAttributes : r.window.getAttributes();
            if ((l.softInputMode
                    & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
                    != forwardBit) {
                l.softInputMode = (l.softInputMode
                        & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
                        | forwardBit;
                if (r.activity.mVisibleFromClient) {
                    ViewManager wm = a.getWindowManager();
                    View decor = r.window.getDecorView();
                    wm.updateViewLayout(decor, l);
                }
            }

            r.activity.mVisibleFromServer = true;
            mNumVisibleActivities++;
            if (r.activity.mVisibleFromClient) {
                r.activity.makeVisible();
            }
...
    }

 这块大概的流程图如上所示,在执行handleResumeActivity的时候,会先去执行activity的onResume方法,然后再将当前的window add进window manager。

这块有个地方拓展下,就是窗口类型,普通apk启动的窗口类型都是TYPE_BASE_APPLICATION

            l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;

Android系统三大窗口类型

Android系统中共有三大窗口类型,分别是Application类型, 子窗口类型,以及系统窗口类型:

窗口类型Vlaue含义
以下窗口类型为普通APPLICAT窗口类型,范围1-99
FIRST_APPLICATION_WINDOW 1App的初始值
TYPE_BASE_APPLICATION1所有App的基础值
TYPE_APPLICATION2普通应用程序窗口
TYPE_APPLICATION_STARTING3starting窗口
TYPE_DRAWN_APPLICATION4等待绘制完成的窗口
LAST_APPLICATION_WINDOW 99App的最大值
以下窗口类型为子窗口类型,范围1000-1999
FIRST_SUB_WINDOW 1000子窗口的初始值
TYPE_APPLICATION_PANELFIRST_SUB_WINDOW 应用程序窗口顶部的面板。这些窗口出现在它们的附属窗口的顶部。
TYPE_APPLICATION_MEDIAFIRST_SUB_WINDOW +1media子窗口
TYPE_APPLICATION_SUB_PANELFIRST_SUB_WINDOW +2子窗口之上的子窗口

TYPE_APPLICATION_ATTACHED

_DIALOG

FIRST_SUB_WINDOW +3Dialog子窗口

TYPE_APPLICATION_MEDIA

_OVERLAY

FIRST_SUB_WINDOW +4media子窗口之上的子窗口

TYPE_APPLICATION_ABOVE_

SUB_PANEL

FIRST_SUB_WINDOW +5在SUB_PANEL之上的子窗口
LAST_SUB_WINDOW 1999子窗口的最大值
以下为系统窗口类型,范围2000-2999
FIRST_SYSTEM_WINDOW2000系统窗口的初始值
TYPE_STATUS_BARFIRST_SYSTEM_WINDOW系统状态栏窗口
TYPE_SEARCH_BARFIRST_SYSTEM_WINDOW+1搜索条窗口
TYPE_PHONEFIRST_SYSTEM_WINDOW+2通话窗口,位于状态栏之上
TYPE_SYSTEM_ALERTFIRST_SYSTEM_WINDOW+3Alert窗口,如电量不足提示,显示在APP之上窗口
TYPE_KEYGUARDFIRST_SYSTEM_WINDOW+4锁屏窗口
TYPE_TOASTFIRST_SYSTEM_WINDOW+5短暂的提示框窗口
TYPE_SYSTEM_OVERLAYFIRST_SYSTEM_WINDOW+6系统覆盖窗口,不能接受input焦点,否则会与屏保发生冲突
TYPE_PRIORITY_PHONEFIRST_SYSTEM_WINDOW+7电话优先窗口,如屏保状态下显示来电窗口
TYPE_SYSTEM_DIALOGFIRST_SYSTEM_WINDOW+8系统Dialog窗口
TYPE_KEYGUARD_DIALOGFIRST_SYSTEM_WINDOW+9keyguard Dialog窗口
TYPE_SYSTEM_ERRORFIRST_SYSTEM_WINDOW+10系统报错窗口
TYPE_INPUT_METHODFIRST_SYSTEM_WINDOW+11输入法窗口
TYPE_INPUT_METHOD_DIALOGFIRST_SYSTEM_WINDOW+12输入法Dialog窗口
TYPE_WALLPAPERFIRST_SYSTEM_WINDOW+13壁纸窗口
TYPE_STATUS_BAR_PANELFIRST_SYSTEM_WINDOW+14从状态栏滑出的面板在多用户系统中显示在所有用户的窗口上。

TYPE_SECURE_SYSTEM_OVERLAY

FIRST_SYSTEM_WINDOW+15@hide
TYPE_DRAGFIRST_SYSTEM_WINDOW+16@hide拖拽窗口

TYPE_STATUS_BAR_SUB_PANEL

FIRST_SYSTEM_WINDOW+17@hide,status bar之上的子窗口
TYPE_POINTERFIRST_SYSTEM_WINDOW+18@hide
TYPE_NAVIGATION_BARFIRST_SYSTEM_WINDOW+19@hide导航栏窗口
TYPE_VOLUME_OVERLAYFIRST_SYSTEM_WINDOW+20@hide系统音量条
TYPE_BOOT_PROGRESSFIRST_SYSTEM_WINDOW+21@hide启动时的进度条窗口
TYPE_INPUT_CONSUMERFIRST_SYSTEM_WINDOW+22@hide消耗input事件的窗口
TYPE_NAVIGATION_BAR_PANELFIRST_SYSTEM_WINDOW+24@hide
TYPE_DISPLAY_OVERLAYFIRST_SYSTEM_WINDOW+26@hide用于模拟第二个Display显示屏
TYPE_MAGNIFICATION_OVERLAYFIRST_SYSTEM_WINDOW+27@hide
TYPE_PRIVATE_PRESENTATIONFIRST_SYSTEM_WINDOW+30
TYPE_VOICE_INTERACTIONFIRST_SYSTEM_WINDOW+31@hide
TYPE_ACCESSIBILITY_OVERLAYFIRST_SYSTEM_WINDOW+32

TYPE_VOICE_INTERACTION_STARTING

FIRST_SYSTEM_WINDOW+33@hide
TYPE_DOCK_DIVIDERFIRST_SYSTEM_WINDOW+34@hide
TYPE_QS_DIALOGFIRST_SYSTEM_WINDOW+35@hide
TYPE_SCREENSHOTFIRST_SYSTEM_WINDOW+36@hide在锁屏之上,该层保留截图动画,区域选择和UI。
TYPE_PRESENTATIONFIRST_SYSTEM_WINDOW+37@hide用于在外部显示器上显示的窗口(多个屏幕情况下)
TYPE_APPLICATION_OVERLAYFIRST_SYSTEM_WINDOW+38

TYPE_ACCESSIBILITY_MAGNIFICATION

_OVERLAY

FIRST_SYSTEM_WINDOW+39@hide
TYPE_NOTIFICATION_SHADEFIRST_SYSTEM_WINDOW+40@hide
TYPE_STATUS_BAR_ADDITIONALFIRST_SYSTEM_WINDOW+41@hide
LAST_SYSTEM_WINDOW2999系统窗口的最大值

 1.1. ActivityThread#performResumeActivity

    @VisibleForTesting
    public boolean performResumeActivity(ActivityClientRecord r, boolean finalStateRequest,
            String reason) {
        if (localLOGV) {
            Slog.v(TAG, "Performing resume of " + r + " finished=" + r.activity.mFinished);
        }
//如果activity将要finish,则直接return掉,不执行resume操作了
        if (r.activity.mFinished) {
            return false;
        }
//如果activity已经on_resume了,则不继续往下执行,return
        if (r.getLifecycleState() == ON_RESUME) {
            if (!finalStateRequest) {
                final RuntimeException e = new IllegalStateException(
                        "Trying to resume activity which is already resumed");
...
            }
            return false;
        }
...
//执行activity的onResume操作
            r.activity.performResume(r.startsNotResumed, reason);

            r.state = null;
            r.persistentState = null;
//设置activity状态为on_resume
            r.setState(ON_RESUME);
//将会打印出event log---wm_on_top_resumed_gained_called相关信息
            reportTopResumedActivityChanged(r, r.isTopResumedActivity, "topWhenResuming");
        } catch (Exception e) {
            if (!mInstrumentation.onException(r.activity, e)) {
                throw new RuntimeException("Unable to resume activity "
                        + r.intent.getComponent().toShortString() + ": " + e.toString(), e);
            }
        }
        return true;
    }

本小节介绍了activity启动过程中其他的知识,下文再继续接着讲怎么添加窗口流程的,下面接着1.1中的addView方法。

2. WindowManager#addView

wm的获取方法如下,可以看到最后是获得到了一个WindowManager对象,

    private WindowManager mWindowManager;
    public WindowManager getWindowManager() {
        return mWindowManager;
    }

那就来到了WindowManager中的addView方法

查看WindowManager中没有addView方法,但是ViewManager中有,那我们再看下谁实现了WindowManager呢

public interface WindowManager extends ViewManager {
-------------------------------------------------------------
public interface ViewManager
{
    public void addView(View view, ViewGroup.LayoutParams params);
    public void updateViewLayout(View view, ViewGroup.LayoutParams params);
    public void removeView(View view);
}

可找到是WindowManagerImpl中实现此addView方法,因为WindowManagerImpl继承了WindowManager:

/*
 * @see WindowManager
 * @see WindowManagerGlobal
 * @hide
 */
public final class WindowManagerImpl implements WindowManager {
...
    @Override
    public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
        applyTokens(params);
        mGlobal.addView(view, params, mContext.getDisplayNoVerify(), mParentWindow,
                mContext.getUserId());
    }

此方法又调用了 WindowManagerGlobal中的addView方法,并将view,窗口属性,要显示的屏幕,父window,以及user id传入

WindowManagerGlobal.java - OpenGrok cross reference for /frameworks/base/core/java/android/view/WindowManagerGlobal.java

    public void addView(View view, ViewGroup.LayoutParams params,
            Display display, Window parentWindow, int userId) {
        if (view == null) {
            throw new IllegalArgumentException("view must not be null");
        }
        if (display == null) {
            throw new IllegalArgumentException("display must not be null");
        }
        if (!(params instanceof WindowManager.LayoutParams)) {
            throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
        }
//获取窗口布局属性
        final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
        if (parentWindow != null) {
//如果父窗口不为空,则调整当前父窗口下所有子窗口布局的属性
            parentWindow.adjustLayoutParamsForSubWindow(wparams);
        } else {
            // If there's no parent, then hardware acceleration for this view is
            // set from the application's hardware acceleration setting.
            final Context context = view.getContext();
            if (context != null
                    && (context.getApplicationInfo().flags
                            & ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
                wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
            }
        }

        ViewRootImpl root;
        View panelParentView = null;

        synchronized (mLock) {
            // Start watching for system property changes.
            if (mSystemPropertyUpdater == null) {
                mSystemPropertyUpdater = new Runnable() {
                    @Override public void run() {
                        synchronized (mLock) {
                            for (int i = mRoots.size() - 1; i >= 0; --i) {
                                mRoots.get(i).loadSystemProperties();
                            }
                        }
                    }
                };
                SystemProperties.addChangeCallback(mSystemPropertyUpdater);
            }

            int index = findViewLocked(view, false);
            if (index >= 0) {
                if (mDyingViews.contains(view)) {
                    // Don't wait for MSG_DIE to make it's way through root's queue.
                    mRoots.get(index).doDie();
                } else {
                    throw new IllegalStateException("View " + view
                            + " has already been added to the window manager.");
                }
                // The previous removeView() had not completed executing. Now it has.
            }

            // If this is a panel window, then find the window it is being
            // attached to for future reference.
//如果是子窗口
            if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
                    wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
                final int count = mViews.size();
                for (int i = 0; i < count; i++) {
                    if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
                        panelParentView = mViews.get(i);
                    }
                }
            }

            IWindowSession windowlessSession = null;
            // If there is a parent set, but we can't find it, it may be coming
            // from a SurfaceControlViewHost hierarchy.
//如果token不为空并且不是子窗口
            if (wparams.token != null && panelParentView == null) {
                for (int i = 0; i < mWindowlessRoots.size(); i++) {
                    ViewRootImpl maybeParent = mWindowlessRoots.get(i);
                    if (maybeParent.getWindowToken() == wparams.token) {
//获取WindowSession
                        windowlessSession = maybeParent.getWindowSession();
                        break;
                    }
                }
            }

            if (windowlessSession == null) {
//如果windowlessSession为空
                root = new ViewRootImpl(view.getContext(), display);
            } else {
//如果windowlessSession不为空,则在此处传入windowlessSession
                root = new ViewRootImpl(view.getContext(), display,
                        windowlessSession, new WindowlessWindowLayout());
            }
//将窗口布局属性设置进view
            view.setLayoutParams(wparams);
//将此view添加进mViews
            mViews.add(view);
//将此root添加进MRoots
            mRoots.add(root);
//将此窗口布局属性添加进mParams
            mParams.add(wparams);

            // do this last because it fires off messages to start doing things
            try {
//将view,窗口属性和子窗口,user id 设置进ViewRootImpl
                root.setView(view, wparams, panelParentView, userId);
            } catch (RuntimeException e) {
                final int viewIndex = (index >= 0) ? index : (mViews.size() - 1);
                // BadTokenException or InvalidDisplayException, clean up.
                if (viewIndex >= 0) {
                    removeViewLocked(viewIndex, true);
                }
                throw e;
            }
        }
    }

3. ViewRootImpl#setView

然后就从WindowManagerGlobal走到了ViewRootImpl中

http://aospxref.com/android-14.0.0_r2/xref/frameworks/base/core/java/android/view/ViewRootImpl.java#1214

    final IWindowSession mWindowSession;

    public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
        setView(view, attrs, panelParentView, UserHandle.myUserId());
    }

    public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView, int userId) {
        synchronized (this) {
//这个mView和上一个方法中的可不一样,当窗口detach后,这个mView会被置为空
            if (mView == null) {
                mView = view;
...

                mWindowAttributes.copyFrom(attrs)
...
                // Schedule the first layout -before- adding to the window
                // manager, to make sure we do the relayout before receiving
                // any other events from the system.
//首先需要layout
                requestLayout();
...
//这个是很重要的一步
                    res = mWindowSession.addToDisplayAsUser(mWindow, mWindowAttributes,
                            getHostVisibility(), mDisplay.getDisplayId(), userId,
                            mInsetsController.getRequestedVisibleTypes(), inputChannel, mTempInsets, mTempControls, attachedFrame, compatScale);
...
                mPendingAlwaysConsumeSystemBars = mAttachInfo.mAlwaysConsumeSystemBars;
                mInsetsController.onStateChanged(mTempInsets);
                mInsetsController.onControlsChanged(mTempControls.get());
                final InsetsState state = mInsetsController.getState();
                final Rect displayCutoutSafe = mTempRect;
                state.getDisplayCutoutSafe(displayCutoutSafe);
                final WindowConfiguration winConfig = getCompatWindowConfiguration();
                mWindowLayout.computeFrames(mWindowAttributes, state,
                        displayCutoutSafe, winConfig.getBounds(), winConfig.getWindowingMode(),
                        UNSPECIFIED_LENGTH, UNSPECIFIED_LENGTH,
                        mInsetsController.getRequestedVisibleTypes(), 1f /* compactScale */, mTmpFrames);
                setFrame(mTmpFrames.frame, true /* withinRelayout */);
...
    }

接着往下走,就会讲到WindowSession.addToDisplayAsUser方法

4. WindowSession.addToDisplayAsUser

这里是涉及到AIDL通信,因为mWindowSession是final IWindowSession mWindowSession;

用到的AIDL文件如下:

frameworks/base/core/java/android/view/IWindowSession.aidl

http://aospxref.com/android-14.0.0_r2/xref/frameworks/base/core/java/android/view/IWindowSession.aidl

 在Client端,WindowlessWindowManager实现了IWindowSession

public class WindowlessWindowManager implements IWindowSession {
    /**
     * IWindowSession implementation. Currently this class doesn't need to support for multi-user.
     */
    @Override
    public int addToDisplayAsUser(IWindow window, WindowManager.LayoutParams attrs,
            int viewVisibility, int displayId, int userId, @InsetsType int requestedVisibleTypes,
            InputChannel outInputChannel, InsetsState outInsetsState,
            InsetsSourceControl.Array outActiveControls, Rect outAttachedFrame,
            float[] outSizeCompatScale) {
        return addToDisplay(window, attrs, viewVisibility, displayId, requestedVisibleTypes,
                outInputChannel, outInsetsState, outActiveControls, outAttachedFrame,
                outSizeCompatScale);
    }

    @Override
    public int addToDisplay(IWindow window, WindowManager.LayoutParams attrs,
            int viewVisibility, int displayId, @InsetsType int requestedVisibleTypes,
            InputChannel outInputChannel, InsetsState outInsetsState,
            InsetsSourceControl.Array outActiveControls, Rect outAttachedFrame,
            float[] outSizeCompatScale) {
// setParent方法作用如下
// Set a parent surface for our new SurfaceControl.
// 给我们的new SurfaceControl设置父surface
// Child surfaces are constrained to the onscreen region of their parent.
//子surfaces被限制在它们的父surface之上
// Furthermore they stack relatively in Z order, and inherit the transformation of the parent.
//此外这些stack按照Z值排序,并继承父级的transformation 

//window leash 图层创建
        final SurfaceControl leash = new SurfaceControl.Builder(mSurfaceSession)
                .setName(attrs.getTitle().toString() + "Leash")
                .setCallsite("WindowlessWindowManager.addToDisplay")
                .setParent(getParentSurface(window, attrs))
                .build();
//window图层创建
        final SurfaceControl sc = new SurfaceControl.Builder(mSurfaceSession)
                .setFormat(attrs.format)
                .setBLASTLayer()
                .setName(attrs.getTitle().toString())
                .setCallsite("WindowlessWindowManager.addToDisplay")
                .setHidden(false)
                .setParent(leash)
                .build();
//在当前文件的最上面有State的定义,主要就是将入参赋值给State的全局变量
        final State state = new State(sc, attrs, displayId, window, leash, /* frame= */ new Rect());
        synchronized (this) {
            State parentState = mStateForWindow.get(attrs.token);
            if (parentState != null) {
                state.mAttachedFrame = parentState.mFrame;
            }

            // Give the first window the mFocusGrantToken since that's the token the host can use
            // to give focus to the embedded.
            if (mStateForWindow.isEmpty()) {
                state.mFocusGrantToken = mFocusGrantToken;
            } else {
                state.mFocusGrantToken = new Binder();
            }

            mStateForWindow.put(window.asBinder(), state);
        }

        if (state.mAttachedFrame == null) {
            outAttachedFrame.set(0, 0, -1, -1);
        } else {
            outAttachedFrame.set(state.mAttachedFrame);
        }
        outSizeCompatScale[0] = 1f;

        if (((attrs.inputFeatures &
                WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0)) {
            try {
                if (mRealWm instanceof IWindowSession.Stub) {
                    mRealWm.grantInputChannel(displayId,
                            new SurfaceControl(sc, "WindowlessWindowManager.addToDisplay"),
                            window, mHostInputToken, attrs.flags, attrs.privateFlags,
                            attrs.inputFeatures, attrs.type,
                            attrs.token, state.mFocusGrantToken, attrs.getTitle().toString(), outInputChannel);
                } else {
                    mRealWm.grantInputChannel(displayId, sc, window, mHostInputToken, attrs.flags, attrs.privateFlags, attrs.inputFeatures, attrs.type, attrs.token,
                  state.mFocusGrantToken, attrs.getTitle().toString(), outInputChannel);
                }
                state.mInputChannelToken =
                        outInputChannel != null ? outInputChannel.getToken() : null;
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to grant input to surface: ", e);
            }
        }

        final int res = WindowManagerGlobal.ADD_OKAY | WindowManagerGlobal.ADD_FLAG_APP_VISIBLE |
                        WindowManagerGlobal.ADD_FLAG_USE_BLAST;

        // Include whether the window is in touch mode.
        return isInTouchModeInternal(displayId) ? res | WindowManagerGlobal.ADD_FLAG_IN_TOUCH_MODE
                : res;
    }

这里我们来看一下mRealWm的赋值过程

public class WindowlessWindowManager implements IWindowSession {
    ...
//全局变量声明
    private final IWindowSession mRealWm;
    public WindowlessWindowManager(Configuration c, SurfaceControl rootSurface,
            IBinder hostInputToken) {
        mRootSurface = rootSurface;
        mConfiguration = new Configuration(c);
//赋值
        mRealWm = WindowManagerGlobal.getWindowSession();
        mHostInputToken = hostInputToken;
    }

然后就走到了WindowManagerGlobal这里去获取WindowSession

WindowManagerGlobal.java - OpenGrok cross reference for /frameworks/base/core/java/android/view/WindowManagerGlobal.java

    @UnsupportedAppUsage
    public static IWindowSession getWindowSession() {
        synchronized (WindowManagerGlobal.class) {
            if (sWindowSession == null) {
                try {
// Emulate the legacy behavior.  The global instance of InputMethodManager
// was instantiated here.
// TODO(b/116157766): Remove this hack after cleaning up @UnsupportedAppUsage
                    InputMethodManager.ensureDefaultInstanceForDefaultDisplayIfNecessary();
//第一步
                    IWindowManager windowManager = getWindowManagerService();
//第二步
                    sWindowSession = windowManager.openSession(
                            new IWindowSessionCallback.Stub() {
                                @Override
                                public void onAnimatorScaleChanged(float scale) {
                                    ValueAnimator.setDurationScale(scale);
                                }
                            });
                } catch (RemoteException e) {
                    throw e.rethrowFromSystemServer();
                }
            }
            return sWindowSession;
        }
    }

 在第一步中,通过getWindowManagerService方法获取了server端binder对象,即获取了WMS服务, 看代码是单例模式,可以看出来这个sWindowSession是一个单例,也就是一个应用的所有ViewRootImpl的WindowSession都是同一个,也就是一个应用只有一个WindowSession,用于与WMS进行通信。

    @UnsupportedAppUsage
    public static IWindowManager getWindowManagerService() {
        synchronized (WindowManagerGlobal.class) {
            if (sWindowManagerService == null) {
//获取server端binder对象,即获取WMS服务
                sWindowManagerService = IWindowManager.Stub.asInterface(
                        ServiceManager.getService("window"));
...
            return sWindowManagerService;
        }
    }

在第二步中,则调用到了Server端服务WMS中的方法,这里也是单例模式的写法,sWindowManagerService 在Client端也只有一个对象。

WindowManagerService.java - OpenGrok cross reference for /frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java

    @Override
    public IWindowSession openSession(IWindowSessionCallback callback) {
        return new Session(this, callback);
    }

然后在WMS中new了一个Session,Session实例化相关如下 

Session.java - OpenGrok cross reference for /frameworks/base/services/core/java/com/android/server/wm/Session.java

class Session extends IWindowSession.Stub implements IBinder.DeathRecipient {
    final WindowManagerService mService;
    final IWindowSessionCallback mCallback;
    final int mUid;
    final int mPid;
  ...
    public Session(WindowManagerService service, IWindowSessionCallback callback) {
        mService = service;
        mCallback = callback;
        mUid = Binder.getCallingUid();
        mPid = Binder.getCallingPid();
...

来看看WMS,其实也是继承了 IWindowSession.Stub

/** {@hide} */
public class WindowManagerService extends IWindowManager.Stub implements Watchdog.Monitor, WindowManagerPolicy.WindowManagerFuncs {

从上我们总结出:

一个应用只有一个WindowSession, 用于与WMS进行通信。

5. Session#grantInputChannel

接着讲上文中的 grantInputChannel方法

Session.java - OpenGrok cross reference for /frameworks/base/services/core/java/com/android/server/wm/Session.java

    @Override
    public void grantInputChannel(int displayId, SurfaceControl surface,
            IWindow window, IBinder hostInputToken, int flags, int privateFlags, int type, int inputFeatures, IBinder windowToken, IBinder focusGrantToken, String inputHandleName,
            InputChannel outInputChannel) {
...
//这里会调用Server端的grantInputChannel方法
            mService.grantInputChannel(this, mUid, mPid, displayId, surface, window, hostInputToken, flags, mCanAddInternalSystemWindow ? privateFlags : 0,
                    type, inputFeatures, windowToken, focusGrantToken, inputHandleName,
                    outInputChannel);
...
    }

下面是一个关于以上流程的setting主页面启动流程

05-25 12:58:21.791  1909  2175 I wm_task_created: 441
05-25 12:58:21.795  1909  2175 I wm_task_moved: [441,441,0,1,2]
05-25 12:58:21.795  1909  2175 I wm_task_to_front: [0,441,0]
05-25 12:58:21.795  1909  2175 I wm_create_task: [0,441,441,0]
05-25 12:58:21.795  1909  2175 I wm_create_activity: [0,232997572,441,com.android.settings/.homepage.SettingsHomepageActivity,android.settings.SETTINGS,NULL,NULL,335544320]
05-25 12:58:21.795  1909  2175 I wm_task_moved: [441,441,0,1,2]
05-25 12:58:22.265  1909  3494 I wm_restart_activity: [0,232997572,441,com.android.settings/.homepage.SettingsHomepageActivity]
05-25 12:58:22.267  1909  3494 I wm_set_resumed_activity: [0,com.android.settings/.homepage.SettingsHomepageActivity,minimalResumeActivityLocked - onActivityStateChanged]
05-25 12:58:22.363  2518  2518 I wm_on_create_called: [232997572,com.android.settings.homepage.SettingsHomepageActivity,performCreate,66]
05-25 12:58:22.457  2518  2518 I wm_on_start_called: [232997572,com.android.settings.homepage.SettingsHomepageActivity,handleStartActivity,92]
05-25 12:58:22.460  2518  2518 V ActivityThread: Performing resume of ActivityRecord{a4a98fc token=android.os.BinderProxy@c7155ef {com.android.settings/com.android.settings.homepage.SettingsHomepageActivity}} finished=false
05-25 12:58:22.460  2518  2518 I wm_on_resume_called: [232997572,com.android.settings.homepage.SettingsHomepageActivity,RESUME_ACTIVITY,0]
05-25 12:58:22.525  2518  2518 V ActivityThread: Resume ActivityRecord{a4a98fc token=android.os.BinderProxy@c7155ef {com.android.settings/com.android.settings.homepage.SettingsHomepageActivity}} started activity: false, hideForNow: false, finished: false
05-25 12:58:22.545  1909  2175 V WindowManager: Attaching Window{a90e6ab u0 com.android.settings/com.android.settings.homepage.SettingsHomepageActivity} token=ActivityRecord{de342c4 u0 com.android.settings/.homepage.SettingsHomepageActivity t441}
05-25 12:58:22.552  2518  2518 V ActivityThread: Resuming ActivityRecord{a4a98fc token=android.os.BinderProxy@c7155ef {com.android.settings/com.android.settings.homepage.SettingsHomepageActivity}} with isForward=true
05-25 12:58:22.552  2518  2518 V ActivityThread: Scheduling idle handler for ActivityRecord{a4a98fc token=android.os.BinderProxy@c7155ef {com.android.settings/com.android.settings.homepage.SettingsHomepageActivity}}
05-25 12:58:22.553  2518  2518 I wm_on_top_resumed_gained_called: [232997572,com.android.settings.homepage.SettingsHomepageActivity,topStateChangedWhenResumed]
05-25 12:58:22.769  1909  1971 I wm_activity_launch_time: [0,232997572,com.android.settings/.homepage.SettingsHomepageActivity,970]

6. WindowManagerService#grantInputChannel

这里就到了Server端,以上都是Client端。

由于本篇文章已经过长,故Server端的另起一篇文章分析。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1711454.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

JVS物联网、逻辑引擎、智能BI(重构优化)5.21功能新增说明

项目介绍 JVS是企业级数字化服务构建的基础脚手架&#xff0c;主要解决企业信息化项目交付难、实施效率低、开发成本高的问题&#xff0c;采用微服务配置化的方式&#xff0c;提供了 低代码数据分析物联网的核心能力产品&#xff0c;并构建了协同办公、企业常用的管理工具等&am…

AI播客下载:Acquired podcast每个公司都有一个故事

"Acquired Podcast" 是一档专注于深度解析科技行业和企业发展历程的播客节目&#xff0c;由Ben Gilbert和David Rosenthal主持。其口号是&#xff1a;Every company has a story.《Acquired》每一集都围绕一个特定的主题或公司进行讨论。它以独特的视角和深入的分析&…

数字孪生技术助力智慧园区建设

随着城市化进程的加速和科技创新的推动&#xff0c;城市面临着诸多挑战和机遇。如何提升城市的竞争力和可持续性&#xff0c;是一个亟待解决的问题。在这个背景下&#xff0c;智慧园区作为一种新型的城市发展模式&#xff0c;引起了越来越多的关注和探索。 什么是智慧园区&…

科技查新是什么?一文了解!

本文主要解答 1、什么是科技查新&#xff1f; 2、科技查新有哪些作用&#xff1f; 3、科技查新一般应用于什么地方&#xff1f; 4、在哪能出具正规查新报告&#xff1f; 5、科技查新流程是怎样的&#xff1f; 带着这些问题阅读这篇文章相信一定会有收获&#xff01;干活内…

Jetson Orin Nano v6.0 + tensorflow2.15.0+nv24.05 GPU版本安装

Jetson Orin Nano v6.0 tensorflow2.15.0nv24.05 GPU版本安装 1. 源由2. 步骤2.1 Step1&#xff1a;系统安装2.2 Step2: nvidia-jetpack安装2.3 Step3&#xff1a;jtop安装2.4 Step4&#xff1a;h5py安装2.5 Step5&#xff1a;tensorflow安装2.6 Step6&#xff1a;jupyterlab安…

Axios的使用简单说明

axios 请求方式和参数 axios 可以发送 ajax 请求&#xff0c;不同的方法可以发送不同的请求: axios.get&#xff1a;发送get请求 axios.post&#xff1a;发送post请求 axios.put&#xff1a;发送put请求 axios.delete&#xff1a;发送delete请求 无论哪种方法&#xff0c;第一…

计算机考研|408开始的晚,如何规划复习?

408开始的比较晚不用怕 只需要6个月的复习时间&#xff0c;按照我的复习计划&#xff0c;你至少可以考100这个分数 我的计划是对于基础差的考生的&#xff0c;大佬们就可以绕道了&#xff0c;对于基础差的考生来说&#xff0c;408复习的难点其实在于学好数据结构和计算机组成…

如何使用OCR批量提取短剧和电视剧内的字幕?

为什么使用光字符识别 (OCR) 提取视频字幕&#xff1f; 视频字幕提取是将视频中的字幕转换为 SRT 格式文本的过程。传统方法是使用自动语音识别 (ASR) 技术&#xff0c;但对于某些类型的视频&#xff08;例如短剧、电视剧、电影、访谈等节目&#xff09;&#xff0c;由于这类型…

代码随想录——合并二叉树(Leetcode617)

题目链接 层序遍历 /*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val val; }* TreeNode(int val, TreeNode left, TreeNode right) …

C++青少年简明教程:for循环语句

C青少年简明教程&#xff1a;for循环语句 C的for循环语句是一种迭代控制语句&#xff0c;用于重复执行一段代码。 语法格式&#xff1a; for(表达式1&#xff1b;表达式2&#xff1b;表达式3) 循环体 for循环语句执行流程图&#xff1a; 不太好理解&#xff0c;请看下图&am…

如何在生产环境中以非 Root 用户启动 Kafka

目录 如何在生产环境中以非 Root 用户启动 Kafka1. 创建 Kafka 用户2. 设置目录权限3. 配置 systemd 服务文件4. 启动和启用 Kafka 服务5. 验证 Kafka 服务经验总结 为了在生产环境中以非 root 用户&#xff08;如 kafka 用户&#xff09;启动 Kafka&#xff0c;您需要确保 Ka…

Unity射击游戏开发教程:(28)敌人被摧毁时掉落的能量提升

在这篇文章中,我将介绍如何在敌人被摧毁时产生能量提升。 首先,有一个生成管理器,负责生成敌人和能量提升。我正在对其进行转换,以便当敌人被摧毁时,有可能会掉落能量。本文将仅介绍当敌人被摧毁时掉落的能量道具。我将介绍为电源添加一个平衡的生成系统。 Spawn Manager…

Linux 一键部署alfresco 6

alfresco 前言 Alfresco是一个流行的企业级开源内容管理系统和协作平台。它提供了丰富的功能,包括文档管理、记录管理、协作工具、工作流管理、搜索和版本控制等。Alfresco还具有灵活的部署选项,可以作为本地部署的软件或云服务来使用。 该平台可以帮助组织管理和存储各种类…

仿真51单片机程序(下载安装+Proteus)

我是看的这个大佬的:http://t.csdnimg.cn/Z07SZ 大佬写的很详细了,我就不献丑了. 贴上俩个运行成功的截图,有碰到问题的欢迎交流.

咖啡看书休闲时光404错误页面源码

源码介绍 咖啡看书休闲时光404错误页面源码&#xff0c;源码由HTMLCSSJS组成&#xff0c;记事本打开源码文件可以进行内容文字之类的修改&#xff0c;双击html文件可以本地运行效果&#xff0c;也可以上传到服务器里面&#xff0c;重定向这个界面 源码效果 源码下载 咖啡看书…

环境土壤物理模型HYDRUS1D/2D/3D建模方法与案例教程

原文链接&#xff1a;环境土壤物理模型HYDRUS1D/2D/3D建模方法与案例教程https://mp.weixin.qq.com/s?__bizMzUzNTczMDMxMg&mid2247605540&idx6&sn22a128de401e146d21c9f2487d589a3b&chksmfa821cc3cdf595d54e46be8247a67eda290349039c85b8e8542aaf34509dae0bb…

27快28了,想转行JAVA或者大数据,还来得及吗?

转行到JAVA或者大数据领域&#xff0c;27岁快28岁的年龄完全来得及。我这里有一套编程入门教程&#xff0c;不仅包含了详细的视频讲解&#xff0c;项目实战。如果你渴望学习编程&#xff0c;不妨点个关注&#xff0c;给个评论222&#xff0c;私信22&#xff0c;我在后台发给你。…

【机器学习300问】102、什么是混淆矩阵?

一、混淆矩阵的定义 混淆矩阵是一种用于评估分类模型性能的评估指标。当模型对数据进行预测并将数据分配到预定义的类别时&#xff0c;混淆矩阵提供了一种直观的方式来总结这些预测与数据实际类别之间的对应关系。具体来说&#xff0c;它是一个表格。 二、分类模型性能评估一级…

Redhat9 LAMP安全配置方案及测试

目录 数据库主机 安装Mariadb数据库服务 设置mariadb开机自动启动 Php主机 部署Apache服务器 设置apache服务开机自启 安装php 安装 phpMyAdmin 打开测试机 更新软件包列表&#xff1a; 首先&#xff0c;确保你的软件包列表是最新的。打开终端并输入以下命令&#xf…

js使用链表实现音乐播放器(新增,下一首播放,置顶,删除)

什么是链表 链表是一种线性数据结构&#xff0c;与数组类似&#xff0c;它用于存储一系列元素。不过&#xff0c;与数组在内存中连续存储元素不同&#xff0c;链表中的元素&#xff08;称为节点&#xff09;在内存中可以是非连续存放的。每个节点包含两部分&#xff1a;一部分…