android 13 WMS/AMS系统开发-窗口层级相关DisplayArea,WindowContainer第二节

这篇具有很好参考价值的文章主要介绍了android 13 WMS/AMS系统开发-窗口层级相关DisplayArea,WindowContainer第二节。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

接着上一节课学习,我们已经清楚的知道了层级结构应该怎么看,根据dumpsys的输出可以完美复原出层级结构树,也理解了结构树对于层级结构的控制作用。但还没有从源码部分对这个结构树进行一个分析,即分析生成这个结构树的源码部分。

1、DisplayContent中启动层级树的构建

    /**
     * Create new {@link DisplayContent} instance, add itself to the root window container and
     * initialize direct children.
     * @param display May not be null.
     * @param root {@link RootWindowContainer}
     */
    DisplayContent(Display display, RootWindowContainer root) {
        super(root.mWindowManager, "DisplayContent", FEATURE_ROOT);
        
    //ignore
    //实际调用这里surface相关图层配置,因为显示东西都需要Surfaceflinger
        configureSurfaces(pendingTransaction);
    //ignore
    }
    private void configureSurfaces(Transaction transaction) {
     //ignore

        if (mDisplayAreaPolicy == null) {
            // Setup the policy and build the display area hierarchy.
            // Build the hierarchy only after creating the surface so it is reparented correctly
            //实际调用这里进行DisplayArea构建
            mDisplayAreaPolicy = mWmService.getDisplayAreaPolicyProvider().instantiate(
                    mWmService, this /* content */, this /* root */,
                    mImeWindowsContainer);
        }
//ignore
    }

结下来调用是实现类DefaultProvider的instantiate
frameworks/base/services/core/java/com/android/server/wm/DisplayAreaPolicy.java

 public DisplayAreaPolicy instantiate(WindowManagerService wmService,
                DisplayContent content, RootDisplayArea root,
                DisplayArea.Tokens imeContainer) {
                //创建特殊的TaskDisplayArea,这里我们前面讲过它是专门来装Activity相关的容器
            final TaskDisplayArea defaultTaskDisplayArea = new TaskDisplayArea(content, wmService,
                    "DefaultTaskDisplayArea", FEATURE_DEFAULT_TASK_CONTAINER);
            final List<TaskDisplayArea> tdaList = new ArrayList<>();
            tdaList.add(defaultTaskDisplayArea);

            // Define the features that will be supported under the root of the whole logical
            // display. The policy will build the DisplayArea hierarchy based on this.
            final HierarchyBuilder rootHierarchy = new HierarchyBuilder(root);
            // Set the essential containers (even if the display doesn't support IME).
 //这里同时setImeContainer进行输入法直接容器设置           
 rootHierarchy.setImeContainer(imeContainer).setTaskDisplayAreas(tdaList);
            if (content.isTrusted()) {//主屏幕肯定进入,也是构成层级关键
                // Only trusted display can have system decorations.
                configureTrustedHierarchyBuilder(rootHierarchy, wmService, content);
            }

             return new DisplayAreaPolicyBuilder().setRootHierarchy(rootHierarchy).build(wmService);
        }

上面可以看出主要有以下部分
1、构建HierarchyBuilder对象
2、准备TaskDisplayArea
3、准备好imeContainer
4、识别屏幕情况,进入configureTrustedHierarchyBuilder
5、配置完成进入build
先看configureTrustedHierarchyBuilder

private void configureTrustedHierarchyBuilder(HierarchyBuilder rootHierarchy,
                WindowManagerService wmService, DisplayContent content) {
            // WindowedMagnification should be on the top so that there is only one surface
            // to be magnified.
            rootHierarchy.addFeature(new Feature.Builder(wmService.mPolicy, "WindowedMagnification",
                    FEATURE_WINDOWED_MAGNIFICATION)
                    .upTo(TYPE_ACCESSIBILITY_MAGNIFICATION_OVERLAY)
                    .except(TYPE_ACCESSIBILITY_MAGNIFICATION_OVERLAY)
                    // Make the DA dimmable so that the magnify window also mirrors the dim layer.
                    .setNewDisplayAreaSupplier(DisplayArea.Dimmable::new)
                    .build());
            if (content.isDefaultDisplay) {
                // Only default display can have cutout.
                // See LocalDisplayAdapter.LocalDisplayDevice#getDisplayDeviceInfoLocked.
                rootHierarchy.addFeature(new Feature.Builder(wmService.mPolicy, "HideDisplayCutout",
                        FEATURE_HIDE_DISPLAY_CUTOUT)
                        .all()
                        .except(TYPE_NAVIGATION_BAR, TYPE_NAVIGATION_BAR_PANEL, TYPE_STATUS_BAR,
                                TYPE_NOTIFICATION_SHADE)
                        .build())
                        .addFeature(new Feature.Builder(wmService.mPolicy, "OneHanded",
                                FEATURE_ONE_HANDED)
                                .all()
                                .except(TYPE_NAVIGATION_BAR, TYPE_NAVIGATION_BAR_PANEL,
                                        TYPE_SECURE_SYSTEM_OVERLAY)
                                .build());
            }
            rootHierarchy
                    .addFeature(new Feature.Builder(wmService.mPolicy, "FullscreenMagnification",
                            FEATURE_FULLSCREEN_MAGNIFICATION)
                            .all()
                            .except(TYPE_ACCESSIBILITY_MAGNIFICATION_OVERLAY, TYPE_INPUT_METHOD,
                                    TYPE_INPUT_METHOD_DIALOG, TYPE_MAGNIFICATION_OVERLAY,
                                    TYPE_NAVIGATION_BAR, TYPE_NAVIGATION_BAR_PANEL)
                            .build())
                    .addFeature(new Feature.Builder(wmService.mPolicy, "ImePlaceholder",
                            FEATURE_IME_PLACEHOLDER)
                            .and(TYPE_INPUT_METHOD, TYPE_INPUT_METHOD_DIALOG)
                            .build());
        }
    }

看到这个大家是否觉得这些名字和dumpsys的很熟悉,对的其实就是这里设置的这些Feature名字
先来看Feature类:

    /**
     * A feature that requires {@link DisplayArea DisplayArea(s)}.
     */
    static class Feature {
        private final String mName;
        private final int mId;
        private final boolean[] mWindowLayers;
        private final NewDisplayAreaSupplier mNewDisplayAreaSupplier;

        private Feature(String name, int id, boolean[] windowLayers,
                NewDisplayAreaSupplier newDisplayAreaSupplier) {
            mName = name;
            mId = id;
            mWindowLayers = windowLayers;
            mNewDisplayAreaSupplier = newDisplayAreaSupplier;
        }
           //注意这里排除36层
     Feature build() {
                if (mExcludeRoundedCorner) {
                    // Always put the rounded corner layer to the top most layer.
                    mLayers[mPolicy.getMaxWindowLayer()] = false;
                }
                return new Feature(mName, mId, mLayers.clone(), mNewDisplayAreaSupplier);
            }

首先Feature代表的是DisplayArea的一个特征,可以根据Feature来对不同的DisplayArea进行划分。
mName:这个Feature的名字,如上面的“WindowedMagnification”,“HideDisplayCutout”之类的,后续DisplayArea层级结构建立起来后,每个DisplayArea的名字用的就是当前DisplayArea对应的那个Feature的名字。
mId:Feature的ID,如上面的FEATURE_WINDOWED_MAGNIFICATION和FEATURE_HIDE_DISPLAY_CUTOUT,虽说是Feature的ID,因为Feature又是DisplayArea的特征
mWindowLayers:代表了这个DisplayArea可以包含哪些层级对应的窗口,后续会分析到。
看看其中一个Feature:

   rootHierarchy.addFeature(new Feature.Builder(wmService.mPolicy, "WindowedMagnification",
                    FEATURE_WINDOWED_MAGNIFICATION)
                    .upTo(TYPE_ACCESSIBILITY_MAGNIFICATION_OVERLAY)
                    .except(TYPE_ACCESSIBILITY_MAGNIFICATION_OVERLAY)
                    // Make the DA dimmable so that the magnify window also mirrors the dim layer.
                    .setNewDisplayAreaSupplier(DisplayArea.Dimmable::new)
                    .build());
//typeInclusive代表一个windowType,一般可以通过windowType获取对应的windowLayer,获取方法layerFromType,upTo代表逻辑就是把层级范围到typeInclusive
  Builder upTo(int typeInclusive) {
                final int max = layerFromType(typeInclusive, false);
                for (int i = 0; i < max; i++) {
                    mLayers[i] = true;
                }
                set(typeInclusive, true);
                return this;
            }
  /**
             * Set that the feature does not apply to the given window types.
             */
             //简单说就是把人家types排除
            Builder except(int... types) {
                for (int i = 0; i < types.length; i++) {
                    int type = types[i];
                    set(type, false);
                }
                return this;
            }
            //留下types
             Builder and(int... types) {
                for (int i = 0; i < types.length; i++) {
                    int type = types[i];
                    set(type, true);
                }
                return this;
            }
             private int layerFromType(int type, boolean internalWindows) {
                return mPolicy.getWindowLayerFromTypeLw(type, internalWindows);
            }

这里调用了getWindowLayerFromTypeLw来实现窗口类型到层级数的转化:

 default int getWindowLayerFromTypeLw(int type, boolean canAddInternalSystemWindow,
            boolean roundedCornerOverlay) {
        // Always put the rounded corner layer to the top most.
        if (roundedCornerOverlay && canAddInternalSystemWindow) {
            return getMaxWindowLayer();
        }
        if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {
            return APPLICATION_LAYER;
        }

        switch (type) {
            case TYPE_WALLPAPER:
                // wallpaper is at the bottom, though the window manager may move it.
                return  1;
            case TYPE_PRESENTATION:
            case TYPE_PRIVATE_PRESENTATION:
            case TYPE_DOCK_DIVIDER:
            case TYPE_QS_DIALOG:
            case TYPE_PHONE:
                return  3;
            case TYPE_SEARCH_BAR:
                return  4;
            case TYPE_INPUT_CONSUMER:
                return  5;
            case TYPE_SYSTEM_DIALOG:
                return  6;
            case TYPE_TOAST:
                // toasts and the plugged-in battery thing
                return  7;
            case TYPE_PRIORITY_PHONE:
                // SIM errors and unlock.  Not sure if this really should be in a high layer.
                return  8;
            case TYPE_SYSTEM_ALERT:
                // like the ANR / app crashed dialogs
                // Type is deprecated for non-system apps. For system apps, this type should be
                // in a higher layer than TYPE_APPLICATION_OVERLAY.
                return  canAddInternalSystemWindow ? 12 : 9;
            case TYPE_APPLICATION_OVERLAY:
                return  11;
            case TYPE_INPUT_METHOD:
                // on-screen keyboards and other such input method user interfaces go here.
                return  13;
            case TYPE_INPUT_METHOD_DIALOG:
                // on-screen keyboards and other such input method user interfaces go here.
                return  14;
            case TYPE_STATUS_BAR:
                return  15;
            case TYPE_STATUS_BAR_ADDITIONAL:
                return  16;
            case TYPE_NOTIFICATION_SHADE:
                return  17;
            case TYPE_STATUS_BAR_SUB_PANEL:
                return  18;
            case TYPE_KEYGUARD_DIALOG:
                return  19;
            case TYPE_VOICE_INTERACTION_STARTING:
                return  20;
            case TYPE_VOICE_INTERACTION:
                // voice interaction layer should show above the lock screen.
                return  21;
            case TYPE_VOLUME_OVERLAY:
                // the on-screen volume indicator and controller shown when the user
                // changes the device volume
                return  22;
            case TYPE_SYSTEM_OVERLAY:
                // the on-screen volume indicator and controller shown when the user
                // changes the device volume
                return  canAddInternalSystemWindow ? 23 : 10;
            case TYPE_NAVIGATION_BAR:
                // the navigation bar, if available, shows atop most things
                return  24;
            case TYPE_NAVIGATION_BAR_PANEL:
                // some panels (e.g. search) need to show on top of the navigation bar
                return  25;
            case TYPE_SCREENSHOT:
                // screenshot selection layer shouldn't go above system error, but it should cover
                // navigation bars at the very least.
                return  26;
            case TYPE_SYSTEM_ERROR:
                // system-level error dialogs
                return  canAddInternalSystemWindow ? 27 : 9;
            case TYPE_MAGNIFICATION_OVERLAY:
                // used to highlight the magnified portion of a display
                return  28;
            case TYPE_DISPLAY_OVERLAY:
                // used to simulate secondary display devices
                return  29;
            case TYPE_DRAG:
                // the drag layer: input for drag-and-drop is associated with this window,
                // which sits above all other focusable windows
                return  30;
            case TYPE_ACCESSIBILITY_OVERLAY:
                // overlay put by accessibility services to intercept user interaction
                return  31;
            case TYPE_ACCESSIBILITY_MAGNIFICATION_OVERLAY:
                return 32;
            case TYPE_SECURE_SYSTEM_OVERLAY:
                return  33;
            case TYPE_BOOT_PROGRESS:
                return  34;
            case TYPE_POINTER:
                // the (mouse) pointer layer
                return  35;
            default:
                Slog.e("WindowManager", "Unknown window type: " + type);
                return 3;
        }
    }
 

看到上面是不是看到和我们熟悉的窗口类型,如TYPE_WALLPAPER,TYPE_NAVIGATION_BAR等,其实他们都是有固定的一个层级的。即windowType的值并不是真正层级数目,都是需要通过这个方法进行转化才是真正层级数

那么我们再回到addFeature部分,通过以上的层级获取及相关upTo方法后我们可以得出各个Feature的一个层级情况

Feature名字 层级情况
WindowedMagnification 0-31
HideDisplayCutout 0-14 16 18-23 26-35
OneHanded 0-23 26-32 34-35
FullscreenMagnification 0-12 15-23 26-27 29-31 33-35
ImePlaceholder 13-14

也就是每个Feature对应层级已经清楚了
再接下来就要进入正式的树构建了
frameworks/base/services/core/java/com/android/server/wm/DisplayAreaPolicyBuilder.java

Result build(WindowManagerService wmService) {
        validate();

        // Attach DA group roots to screen hierarchy before adding windows to group hierarchies.
        mRootHierarchyBuilder.build(mDisplayAreaGroupHierarchyBuilders);//关键的进行build
       //ignore
        return new Result(wmService, mRootHierarchyBuilder.mRoot, displayAreaGroupRoots,
                mSelectRootForWindowFunc, mSelectTaskDisplayAreaFunc);
    }

来看看build方法:

 /**
         * Builds the {@link DisplayArea} hierarchy below root. And adds the roots of those
         * {@link HierarchyBuilder} as children.
         */
        private void build(@Nullable List<HierarchyBuilder> displayAreaGroupHierarchyBuilders) {
//ignore
            PendingArea[] areaForLayer = new PendingArea[maxWindowLayerCount];
            //搞默认的PendingArea作为root部分
            final PendingArea root = new PendingArea(null, 0, null); 
            //给areaForLayer填满都是默认new PendingArea(null, 0, null); 
            Arrays.fill(areaForLayer, root);
		       //创建features相关的树
            // Create DisplayAreas to cover all defined features.
            final int size = mFeatures.size();
            for (int i = 0; i < size; i++) {
                // Traverse the features with the order they are defined, so that the early defined
                // feature will be on the top in the hierarchy.
                final Feature feature = mFeatures.get(i);
                PendingArea featureArea = null;
                for (int layer = 0; layer < maxWindowLayerCount; layer++) {
                    if (feature.mWindowLayers[layer]) {
                        // This feature will be applied to this window layer.
                        //
                        // We need to find a DisplayArea for it:
                        // We can reuse the existing one if it was created for this feature for the
                        // previous layer AND the last feature that applied to the previous layer is
                        // the same as the feature that applied to the current layer (so they are ok
                        // to share the same parent DisplayArea).
                        if (featureArea == null || featureArea.mParent != areaForLayer[layer]) {
                            // No suitable DisplayArea:
                            // Create a new one under the previous area (as parent) for this layer.
                            featureArea = new PendingArea(feature, layer, areaForLayer[layer]);
                            areaForLayer[layer].mChildren.add(featureArea);
                        }
                        areaForLayer[layer] = featureArea;
                    } else {
                        // This feature won't be applied to this window layer. If it needs to be
                        // applied to the next layer, we will need to create a new DisplayArea for
                        // that.
                        featureArea = null;
                    }
                }
            }
					//创建叶子相关
            // Create Tokens as leaf for every layer.
            PendingArea leafArea = null;
            int leafType = LEAF_TYPE_TOKENS;
            for (int layer = 0; layer < maxWindowLayerCount; layer++) {
                int type = typeOfLayer(policy, layer);
                // Check whether we can reuse the same Tokens with the previous layer. This happens
                // if the previous layer is the same type as the current layer AND there is no
                // feature that applies to only one of them.
                if (leafArea == null || leafArea.mParent != areaForLayer[layer]
                        || type != leafType) {
                    // Create a new Tokens for this layer.
                    leafArea = new PendingArea(null /* feature */, layer, areaForLayer[layer]);
                    areaForLayer[layer].mChildren.add(leafArea);
                    leafType = type;
                    if (leafType == LEAF_TYPE_TASK_CONTAINERS) {
                        // We use the passed in TaskDisplayAreas for task container type of layer.
                        // Skip creating Tokens even if there is no TDA.
                        addTaskDisplayAreasToApplicationLayer(areaForLayer[layer]);
                        addDisplayAreaGroupsToApplicationLayer(areaForLayer[layer],
                                displayAreaGroupHierarchyBuilders);
                        leafArea.mSkipTokens = true;
                    } else if (leafType == LEAF_TYPE_IME_CONTAINERS) {
                        // We use the passed in ImeContainer for ime container type of layer.
                        // Skip creating Tokens even if there is no ime container.
                        leafArea.mExisting = mImeContainer;
                        leafArea.mSkipTokens = true;
                    }
                }
                leafArea.mMaxLayer = layer;
            }
            root.computeMaxLayer();//会计算出每个节点最大layer值

            // We built a tree of PendingAreas above with all the necessary info to represent the
            // hierarchy, now create and attach real DisplayAreas to the root.
            root.instantiateChildren(mRoot, displayAreaForLayer, 0, featureAreas);//这里会对把PendingArea生成DisplayArea

            // Notify the root that we have finished attaching all the DisplayAreas. Cache all the
            // feature related collections there for fast access.
            mRoot.onHierarchyBuilt(mFeatures, displayAreaForLayer, featureAreas);
        }

这里主要分两个部分,逻辑稍微难理解:

1、根据上面的几个Feature的配置来构造

 // Create DisplayAreas to cover all defined features.
            final int size = mFeatures.size();
            for (int i = 0; i < size; i++) {//针对5个Feature进行遍历,按照add先后顺序,意味最先add在最顶层
                // Traverse the features with the order they are defined, so that the early defined
                // feature will be on the top in the hierarchy.
                final Feature feature = mFeatures.get(i);
                PendingArea featureArea = null;
                for (int layer = 0; layer < maxWindowLayerCount; layer++) {
                    if (feature.mWindowLayers[layer]) {
                        // This feature will be applied to this window layer.
                        //
                        // We need to find a DisplayArea for it:
                        // We can reuse the existing one if it was created for this feature for the
                        // previous layer AND the last feature that applied to the previous layer is
                        // the same as the feature that applied to the current layer (so they are ok
                        // to share the same parent DisplayArea).
                        //条件1:如果featureArea为空,一般每个Feature第一次进入都为null
                        //条件2:如果featureArea不为空,featureArea的父节点不一样,即如果兄弟层级featureArea的父节点是同一个那就不需要新创建
                        if (featureArea == null || featureArea.mParent != areaForLayer[layer]) {
                            // No suitable DisplayArea:
                            // Create a new one under the previous area (as parent) for this layer.
                            featureArea = new PendingArea(feature, layer, areaForLayer[layer]);//以areaForLayer[layer]为父节点创建一个新的节点
                            areaForLayer[layer].mChildren.add(featureArea);//老容器节点添加新节点
                        }
                        areaForLayer[layer] = featureArea;//更新当前layer的容器节点变成新的PendingArea
                    } else {
                        // This feature won't be applied to this window layer. If it needs to be
                        // applied to the next layer, we will need to create a new DisplayArea for
                        // that.
                        //如果这一层不支持显示,那么就把featureArea设置为null
                        featureArea = null;
                    }
                }
            }

那么我们来一个个Feature进行分析进行树图绘制:

Feature名字 层级情况
WindowedMagnification 0-31
第一个Feature从0层就需要创建PeadingArea,父节点是Root:0:0,一直到31都是共用一个Parent
android 13 WMS/AMS系统开发-窗口层级相关DisplayArea,WindowContainer第二节,wms,framework教程,系统源码,android,android studio,java

(注意这里其实还没有最大层级,因为还没有对最大层级进行计算,这个要到最后才会结算,这里为了方便写的)
HideDisplayCutout 0-14 16 18-23 26-35
到达第二个Feature开始从0时候肯定新建一个PendingArea,他的0-31层,父节点就会变成WindowedMagnification:0:31
android 13 WMS/AMS系统开发-窗口层级相关DisplayArea,WindowContainer第二节,wms,framework教程,系统源码,android,android studio,java
因为32-35层上一个Feature并没有覆盖
OneHanded 0-23 26-32 34-35
android 13 WMS/AMS系统开发-窗口层级相关DisplayArea,WindowContainer第二节,wms,framework教程,系统源码,android,android studio,java

FullscreenMagnification 0-12 15-23 26-27 29-31 33-35
android 13 WMS/AMS系统开发-窗口层级相关DisplayArea,WindowContainer第二节,wms,framework教程,系统源码,android,android studio,java

ImePlaceholder 13-14
android 13 WMS/AMS系统开发-窗口层级相关DisplayArea,WindowContainer第二节,wms,framework教程,系统源码,android,android studio,java

2、Feature构造完成也要对应的一些特殊的进行配置,除了TaskDisplayArea和ImeContainer特殊外,其他的给每个后面都给个Tokens

这个部分较为简单一些,基于上面已经根据Feature构建的树统一需要加上一个Tokens节点,但是要除去TaskDisplayArea和ImeContainer的两个部分

 // Create Tokens as leaf for every layer.
            PendingArea leafArea = null;
            int leafType = LEAF_TYPE_TOKENS;//统一初始中为LEAF_TYPE_TOKENS
            for (int layer = 0; layer < maxWindowLayerCount; layer++) {//遍历36层
                int type = typeOfLayer(policy, layer);//获取每层的type是什么
                // Check whether we can reuse the same Tokens with the previous layer. This happens
                // if the previous layer is the same type as the current layer AND there is no
                // feature that applies to only one of them.
                //条件1针对leafArea空,或者leafArea本身不和这个layer共用一个父节点
                //条件2针对属于TYPE_INPUT_METHOD  APPLICATION_LAYER特殊处理
                if (leafArea == null || leafArea.mParent != areaForLayer[layer]
                        || type != leafType) {
                    // Create a new Tokens for this layer.
                    //以下代码相应想给每一层都挂载一个PendingArea
                    leafArea = new PendingArea(null /* feature */, layer, areaForLayer[layer]);//创建新的PendingArea,并挂载到areaForLayer[layer]下面
                    areaForLayer[layer].mChildren.add(leafArea);
                    leafType = type;
                    if (leafType == LEAF_TYPE_TASK_CONTAINERS) {
                    //APPLICATION_LAYER单独处理,就是前面设置过的TaskDisplayArea
                        // We use the passed in TaskDisplayAreas for task container type of layer.
                        // Skip creating Tokens even if there is no TDA.
                        addTaskDisplayAreasToApplicationLayer(areaForLayer[layer]);//设置本身已经设置过的TaskDisplayArea单独处理
                        addDisplayAreaGroupsToApplicationLayer(areaForLayer[layer],
                                displayAreaGroupHierarchyBuilders);
                        leafArea.mSkipTokens = true;//设置跳过
                    } else if (leafType == LEAF_TYPE_IME_CONTAINERS) {
                        // We use the passed in ImeContainer for ime container type of layer.
                        // Skip creating Tokens even if there is no ime container.
                        leafArea.mExisting = mImeContainer;//设置本身已经设置过的mImeContainer
                        leafArea.mSkipTokens = true;//设置跳过
                    }
                }
                leafArea.mMaxLayer = layer;
            }
            //根据层级获取type,这里只对特殊的APPLICATION_LAYER和TYPE_INPUT_METHOD有特殊,其他都是LEAF_TYPE_TOKENS
            private static int typeOfLayer(WindowManagerPolicy policy, int layer) {
              if (layer == APPLICATION_LAYER) {
                  return LEAF_TYPE_TASK_CONTAINERS;
              } else if (layer == policy.getWindowLayerFromTypeLw(TYPE_INPUT_METHOD)
                      || layer == policy.getWindowLayerFromTypeLw(TYPE_INPUT_METHOD_DIALOG)) {
                  return LEAF_TYPE_IME_CONTAINERS;
              } else {
                  return LEAF_TYPE_TOKENS;
              }
          }
           /** Adds all {@link TaskDisplayArea} to the application layer. */
          private void addTaskDisplayAreasToApplicationLayer(PendingArea parentPendingArea) {
              final int count = mTaskDisplayAreas.size();//这里一般等于1
              for (int i = 0; i < count; i++) {
                  PendingArea leafArea =
                          new PendingArea(null /* feature */, APPLICATION_LAYER, parentPendingArea);//创建新的APPLICATION_LAYER的PendingArea,并挂载到parentPendingArea下面
                  leafArea.mExisting = mTaskDisplayAreas.get(i);//和前面IME一样把容器设置进去
                  leafArea.mMaxLayer = APPLICATION_LAYER;//设置最大Layer
                  parentPendingArea.mChildren.add(leafArea);//添加为父节点的孩子
              }
          }
      }
  

上面代码执行完成后基本Pending树就构造完成,但这个时候还是PendingArea还不是真正DisplayArea,而且当前根节点只是个Root:0:0,根本不是我们DisplayContent,继续看build还剩下以下几步

 root.computeMaxLayer();//会计算出每个节点最大layer值

 // We built a tree of PendingAreas above with all the necessary info to represent the
 // hierarchy, now create and attach real DisplayAreas to the root.
 root.instantiateChildren(mRoot, displayAreaForLayer, 0, featureAreas);//这里会对把PendingArea生成DisplayArea,这里的参数mRoot就是我们的DisplayContent,root是Root:0:0这个PendingArea

这里重点看看 root.instantiateChildren(mRoot, displayAreaForLayer, 0, featureAreas);

  void instantiateChildren(DisplayArea<DisplayArea> parent, DisplayArea.Tokens[] areaForLayer,
912                  int level, Map<Feature, List<DisplayArea<WindowContainer>>> areas) {
913              mChildren.sort(Comparator.comparingInt(pendingArea -> pendingArea.mMinLayer));
914              for (int i = 0; i < mChildren.size(); i++) {
915                  final PendingArea child = mChildren.get(i);
916                  final DisplayArea area = child.createArea(parent, areaForLayer);
                     //这里可能create出来为null情况,比如前面说的ImeContainer,和DefaultTaskDisplayArea
917                  if (area == null) {
918                      // TaskDisplayArea and ImeContainer can be set at different hierarchy, so it can
919                      // be null.
920                      continue;
921                  }
922                  parent.addChild(area, WindowContainer.POSITION_TOP);//把返回的area设置为Child
923                  if (child.mFeature != null) {
924                      areas.get(child.mFeature).add(area);//同时也放入一下Feature集合
925                  }
926                  child.instantiateChildren(area, areaForLayer, level + 1, areas);//迭代子节点
927              }
928          }
929  
930          @Nullable
931          private DisplayArea createArea(DisplayArea<DisplayArea> parent,
932                  DisplayArea.Tokens[] areaForLayer) {
933              if (mExisting != null) {//Ime和DefaultTaskDisplayArea可以进入,针对已经存在mExisting直接使用它返回
934                  if (mExisting.asTokens() != null) {//判断是否属于Tokens类型,其实就是ImeContainer
935                      // Store the WindowToken container for layers
936                      fillAreaForLayers(mExisting.asTokens(), areaForLayer);//给对应覆盖的层级都需要赋值为mExisting
937                  }
938                  return mExisting;//返回之前设置的
939              }
940              if (mSkipTokens) {//如果被设置了跳过的直接返回null
941                  return null;
942              }
943              DisplayArea.Type type;
944              if (mMinLayer > APPLICATION_LAYER) {
945                  type = DisplayArea.Type.ABOVE_TASKS;
946              } else if (mMaxLayer < APPLICATION_LAYER) {
947                  type = DisplayArea.Type.BELOW_TASKS;
948              } else {
949                  type = DisplayArea.Type.ANY;
950              }
951              if (mFeature == null) {
952                  final DisplayArea.Tokens leaf = new DisplayArea.Tokens(parent.mWmService, type,
953                          "Leaf:" + mMinLayer + ":" + mMaxLayer);//构造对应Tokens
954                  fillAreaForLayers(leaf, areaForLayer);//给对应覆盖的层级都需要赋值为leaf
955                  return leaf;
956              } else {
957                  return mFeature.mNewDisplayAreaSupplier.create(parent.mWmService, type,
958                          mFeature.mName + ":" + mMinLayer + ":" + mMaxLayer, mFeature.mId);//这里一般是构造对应DisplayArea
959              }
960          }

对于代码都给予了详细注释,这里就只做个总结:
1、根据窗口层级37层,每一层进行遍历,挂载一个新的叶子TOKENS节点,规则和前面Feature一样,如果同一个父亲则不需要新生成
2、针对TYPE_INPUT_METHOD APPLICATION_LAYER需要进行特殊处理

那么综上其实可以得出以下图(红色的Leaf就是新添加的,还有ImeContainer和DefaultTaskDisplayArea):

android 13 WMS/AMS系统开发-窗口层级相关DisplayArea,WindowContainer第二节,wms,framework教程,系统源码,android,android studio,java文章来源地址https://www.toymoban.com/news/detail-618989.html

到了这里,关于android 13 WMS/AMS系统开发-窗口层级相关DisplayArea,WindowContainer第二节的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android Framework最难模块WMS实战作业-手机车机系统开发必备

    0-整体介绍 1-window-container.mp4 窗口层级树实战启动篇 2-displayarea-feature.mp4 窗口层级树源码分析相关 3-displayarea-draw-feature.mp4 窗口层级树绘制实战1 4-displayarea-draw-leaf.mp4 窗口层级树绘制实战2 5-displayarea-draw-leaf-2.mp4 窗口层级树绘制实战3 6-displayarea-surfacelayer.mp4 窗口层级树相关sur

    2024年02月12日
    浏览(50)
  • Android窗口层级(Window Type)分析

    Android的窗口Window分为三种类型: 应用Window,比如 Activity 、 Dialog ;子Window,比如 PopupWindow ;系统Window,比如 Toast 、系统状态栏、导航栏等等。 应用Window的Z-Ordered最低,就是在系统中的显示层级最低,然后到子Window,层级最高的是系统Window。层级高的Window会覆盖层级低的W

    2024年02月09日
    浏览(41)
  • android framework实战开发之WINDOWING_MODE_FREEFORM自由窗口相关

    hi,粉丝朋友们! 今天开始就进入正式的自由窗口的相关的内容讲解,blog只是一些知识点的记录,更多的干货,还请看马哥的视频,及视频配套资料。 b站免费视频教程讲解: https://www.bilibili.com/video/BV1wj411o7A9/ aosp默认并没有公开自由窗口模式,如果需要体验自由窗口模式必须

    2024年02月06日
    浏览(41)
  • 盘点3个.Net开发的WMS仓库管理系统

    更多开源项目请查看:一个专注推荐.Net开源项目的榜单 仓库管理系统在企业中,重要性越来越高,不仅可以提高效率,还能降低企业的压力,企业通过协调和优化资源使用和物料流动,能极大程度地提升了管理效率,节约了成本。 特别是最近这两年,WMS仓库管理系统特别受

    2024年02月01日
    浏览(57)
  • android多屏触摸相关的详解方案-安卓framework开发手机车载车机系统开发课程

    直播免费视频课程地址:https://www.bilibili.com/video/BV1hN4y1R7t2/ 在做双屏相关需求开发过程中,经常会有对两个屏幕都要求可以正确触摸的场景。但是目前我们模拟器默认创建的双屏其实是没有办法进行触摸的 静态修改方案 使用命令查看display2即副屏的信息情况 adb shell dumpsys d

    2024年02月11日
    浏览(52)
  • 基于 ThinkPHP 5.1(稳定版本) 开发wms 进销存系统源码

    基于ThinkPHP 5.1(LTS版本)开发的WMS进销存系统源码 管理员账号密码:admin 一、项目简介 这个系统是一个基于ThinkPHP框架的WMS进销存系统。 二、实现功能 控制台 – 权限管理(用户管理、角色管理、节点管理) – 订单管理(采购订单、销售订单) – 仓库管理(采购入库、销

    2024年02月14日
    浏览(39)
  • android相机、相册相关(android13 适配)13又又又又又又搞事,让第三方难以生存,踩坑

    前几天公司开发的新项目要上线,结果...... 由于项目中有用户更换头像功能,应用市场经过检测android13无法更换头像,也没有崩溃也没有反应,所以App就被无情退回,相当无奈,那就查问题,手头有没有android13测试机,怎么办,怎么办,怎么办...那就想办法,还好vivo有云测

    2024年02月06日
    浏览(70)
  • Android 13 网络 Adb相关流程深入分析研究

    通过代码分析发现Android13 上对 网络adb 进行了限制! Android13原生代码要求:必现连接上某个wifi,才能进行adb ,并且切换wifi或者关闭wifi都是会停止adb。 并且Android13 上 wifi adb 端口号每次是变化的,这个也是很不方便的! 如果要做成Android11 或者之前一样,设备连接WiFi/有线网

    2024年02月09日
    浏览(54)
  • Android 13(T) - binder阅读(3)- binder相关的类

    原先准备在binder阅读(3)中记录ServiceManager的使用,但是写着写着发现,如果不了解和binder相关的类,那么阅读起来将会由很多困惑,所以就先来记录binder相关的类了。记录完发现特别凌乱…先就这样吧。 从 binder阅读(1)一开始的图中可以看到, getService 获得的是一个han

    2024年02月11日
    浏览(37)
  • 概念Android AMS

    Android的Activity Manager Service(AMS)是负责管理整个系统中Activity生命周期、任务栈、应用启动等核心功能的系统服务。它通过系统级别的进程间通信(IPC)机制与应用程序组件交互,确保应用程序的行为符合Android的多任务处理逻辑和用户的预期。AMS是Android系统架构中的一个关

    2024年04月25日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包