WindowManagerService

Window

public class Activity extends ContextThemeWrapper implements ...... {

    private Window mWindow;
    private WindowManager mWindowManager;

    final void attach(Context context, ActivityThread aThread,
            Instrumentation instr, IBinder token, int ident,
            Application application, Intent intent, ActivityInfo info,
            CharSequence title, Activity parent, String id,
            NonConfigurationInstances lastNonConfigurationInstances,
            Configuration config, String referrer, IVoiceInteractor voiceInteractor,
            Window window, ActivityConfigCallback activityConfigCallback) {
        attachBaseContext(context);

        mFragments.attachHost(null /*parent*/);

        // 代碼 1
        mWindow = new PhoneWindow(this, window, activityConfigCallback);
        mWindow.setWindowControllerCallback(this);
        // 代碼 2
        mWindow.setCallback(this);
        mWindow.setOnWindowDismissedCallback(this);
       ···
        // 代碼 3
        mWindow.setWindowManager(
                (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
                mToken, mComponent.flattenToString(),
                (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
        if (mParent != null) {
            mWindow.setContainer(mParent.getWindow());
        }
        mWindowManager = mWindow.getWindowManager();
    }

}

1、創(chuàng)建PhoneWindow
2、設(shè)置Window的callback,用戶的觸摸 & 鍵盤等輸入事件就是通過此接口回調(diào)到 Activity 中的,Window#Callback 回調(diào)源碼如下
3、獲取WindowManager,WindowManager是ViewManager的子接口他的實現(xiàn)類是WindowManagerImpl,所有的方法都會轉(zhuǎn)交給WindowManagerGlobal,這個類是進(jìn)程單例的類,管理素有的Window,包含成員變量

ArrayList<View> mViews:存儲所有 Window 所對應(yīng)的 View
ArrayList<ViewRootImpl>:存儲所有 Window 所對應(yīng)的 ViewRootImpl
ArrayList<WindowManager.LayoutParams>:存儲所有 Window 所對應(yīng)的 WindowManager.LayoutParams
ArraySet<View>:存儲所有即將被移除的 View

Session

ViewRootImpl 和 WMS 之間的通信就是通過 Session 對象完成的。
Session 類繼承自 IWindowSession.Stub,每一個應(yīng)用進(jìn)程都有一個唯一的 Session 對象與 WMS 通信

WindowState

每一個應(yīng)用的Window在WMS都對應(yīng)著一個WindowState

class WindowState extends WindowContainer<WindowState> implements WindowManagerPolicy.WindowState {
    final WindowManagerService mService;
    final WindowManagerPolicy mPolicy;
    final Context mContext;
    final Session mSession;
    final IWindow mClient;


    WindowState(WindowManagerService service, Session s, IWindow c, WindowToken token,
           WindowState parentWindow, int appOp, int seq, WindowManager.LayoutParams a,
           int viewVisibility, int ownerId, boolean ownerCanAddInternalSystemWindow) {
        mService = service;
        mSession = s;
        mClient = c;
        mAppOp = appOp;
        mToken = token;
        mAppToken = mToken.asAppWindowToken();

        ......
    }

    void attach() {
        if (localLOGV) Slog.v(TAG, "Attaching " + this + " token=" + mToken);
        mSession.windowAddedLocked(mAttrs.packageName);
    }
    
    ......
}

WindowToken

簡單的理解,WindowToken 有兩個作用:

1、在 WMS 中,一個 WindowToken 就代表著一個應(yīng)用組件,應(yīng)用組件包括:Activity、InputMethod 等。在 WMS 中,會將屬于同一 WindowToken 的做統(tǒng)一處理,比如在對窗口進(jìn)行 ZOrder 排序時,會將屬于統(tǒng)一 WindowToken 的排在一起。
2、WindowToken 也具有令牌的作用。應(yīng)用組件在創(chuàng)建 Window 時都需要提供一個有效的 WindowToken 以表明自己的身份,并且窗口的類型必須與所持有的 WindowToken 類型保持一致。如果是系統(tǒng)類型的窗口,可以不用提供 WindowToken,WMS 會自動為該系統(tǒng)窗口隱式的創(chuàng)建 WindowToken,但是要求應(yīng)用必須具有創(chuàng)建該系統(tǒng)類型窗口的權(quán)限

AppWindowToken 是 WindowToken 的子類,與 WindowToken 不同的是,AppWindowToken 只可以用于 Activity 中的 Window 的 WindowToken。

參考
初步理解 Window 體系
Window與WMS通信過程
深入理解 WindowManagerService
有關(guān)Window的愛恨情仇!
Android源碼-深入理解Window和WindowManager
WindowManagerService架構(gòu)剖析之a(chǎn)ddWindow流程
Android窗口管理分析(2):WindowManagerService窗口管理之Window添加流程
WindowManagerService架構(gòu)剖析之窗口分組與分層
深入理解WindowManagerService
[深入理解Android卷一全文-第八章]深入理解Surface系統(tǒng)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容