Android6.0 WMS(三) WMS窗口次序

这篇博客我们主要分析下,窗口位置排序的一些原理。


一、添加窗口的时候 调整窗口位置

上篇博客我们分析了WMS的addWindow函数,这里我们就窗口的次序问题继续分析。

  1. boolean imMayMove = true;
  2. if (type == TYPE_INPUT_METHOD) {//如果窗口类是输入法窗口
  3. win.mGivenInsetsPending = true;
  4. mInputMethodWindow = win;
  5. addInputMethodWindowToListLocked(win);//插入输入法窗口到应用窗口上层
  6. imMayMove = false;
  7. } else if (type == TYPE_INPUT_METHOD_DIALOG) {//如果窗口是输入法对话框
  8. mInputMethodDialogs.add(win);
  9. addWindowToListInOrderLocked(win, true);//插入到正常位置
  10. moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));//调整对话框位置
  11. imMayMove = false;
  12. } else {
  13. addWindowToListInOrderLocked(win, true);//插入正常位置
  14. if (type == TYPE_WALLPAPER) {
  15. mLastWallpaperTimeoutTime = 0;
  16. displayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
  17. } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
  18. displayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
  19. } else if (mWallpaperTarget != null
  20. && mWallpaperTarget.mLayer >= win.mBaseLayer) {
  21. // If there is currently a wallpaper being shown, and
  22. // the base layer of the new window is below the current
  23. // layer of the target window, then adjust the wallpaper.
  24. // This is to avoid a new window being placed between the
  25. // wallpaper and its target.
  26. displayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
  27. }
  28. }

DisplayContent类的mWindows列表按Z序保存了每个窗口,这段代码就是在根据窗口类型把窗口加入到DisplayContent合适位置。

addInputMethodWindowToListLocked方法作用就是一个输入法窗口放子啊需要显示输入法窗口的上面。

addWindowToListInOrderLocked将一个窗口插入到窗口堆栈的当前位置。


我们继续看addWindow函数,

  1. final WindowStateAnimator winAnimator = win.mWinAnimator;
  2. winAnimator.mEnterAnimationPending = true;
  3. winAnimator.mEnteringAnimation = true;
  4. if (displayContent.isDefaultDisplay) {
  5. mPolicy.getInsetHintLw(win.mAttrs, mRotation, outContentInsets, outStableInsets,//计算窗口大小
  6. outOutsets);
  7. } else {
  8. outContentInsets.setEmpty();
  9. outStableInsets.setEmpty();
  10. }
  11. if (mInTouchMode) {
  12. res |= WindowManagerGlobal.ADD_FLAG_IN_TOUCH_MODE;//加入支持触屏的标志
  13. }
  14. if (win.mAppToken == null || !win.mAppToken.clientHidden) {
  15. res |= WindowManagerGlobal.ADD_FLAG_APP_VISIBLE;//加入应用可用的标志
  16. }
  17. mInputMonitor.setUpdateInputWindowsNeededLw();//设置更新输入法窗口的标志
  18. boolean focusChanged = false;
  19. if (win.canReceiveKeys()) {//如果窗口能接受输入计算是否引起焦点变化
  20. focusChanged = updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS,
  21. false /*updateInputWindows*/);
  22. if (focusChanged) {
  23. imMayMove = false;
  24. }
  25. }
  26. if (imMayMove) {
  27. moveInputMethodWindowsIfNeededLocked(false);//调整输入法的窗口位置
  28. }
  29. assignLayersLocked(displayContent.getWindowList());//重新计算z轴的位置
  30. // Don't do layout here, the window must call
  31. // relayout to be displayed, so we'll do it there.
  32. if (focusChanged) {
  33. mInputMonitor.setInputFocusLw(mCurrentFocus, false /*updateInputWindows*/);
  34. }
  35. mInputMonitor.updateInputWindowsLw(false /*force*/);//更新输入法窗口的信息
  36. if (localLOGV || DEBUG_ADD_REMOVE) Slog.v(TAG, "addWindow: New client "
  37. + client.asBinder() + ": window=" + win + " Callers=" + Debug.getCallers(5));
  38. if (win.isVisibleOrAdding() && updateOrientationFromAppTokensLocked(false)) {
  39. reportNewConfig = true;//配置发生变化
  40. }
  41. }
  42. if (reportNewConfig) {
  43. sendNewConfiguration();//发送新的配置
  44. }
  45. Binder.restoreCallingIdentity(origId);
  46. return res;
  47. }

如果窗口显示在缺省的显示设备,调用mPolicy的getInsetHintLw函数来获得除了状态条、导航条所占区域后的窗口大小。

接下来如果窗口能接受输入,调用updateFocusedWindowLocked来重新确定系统的焦点位置。如果焦点发生变化,则将imMayMove置为false。

新加入的窗口的位置在前面调用addWindowToListInOrderLocked的时候位置已经确定了,所以这里调用assignLayersLocked只是重新计算Z轴的位置。如果调用updateOrientationFromAppTokensLocked函数计算窗口的配置发生变化,调用sendNewConfiguration函数发送配置。


二、确定窗口的mLayer值

显示设备的水平方向,垂直方向作为X轴Y轴,我们还可以想象有一个垂直于屏幕的Z轴,Z轴的值越来越靠近屏幕。系统中所有的窗口都按次序排列在Z轴上。窗口对象WindowState的成员变量mLayer表示窗口在Z轴的值,值越小越靠近底层。

WMS作用之一就是管理各个窗口Z轴位置,确保正确显示。在所有窗口中输入法和壁纸窗口比较特殊。输入法窗口出现时,需要显示在应用窗口的前面。壁纸窗口通常在底层,但是又不是位于所有窗口的底层,而是位于当前Activity窗口的下面。

因此,当系统调整某个应用窗口的位置时,如果需要也会调整输入法和壁纸窗口,使当前Activity的窗口位于输入法窗口和壁纸窗口之间。

WindowState的成员变量mLayer的值表示窗口在Z轴的位置,这个值越小,位置越靠下。mLayer是通过计算得到,会经常变化。WindowState的另一个成员变量mBaseLayer的值是固定不变的,只和窗口类型有关。mLayer的值是根据mBaseLayer的值计算而来。

下面我们先来看看mBaseLayer的值如何而来,在WindowState的构造函数中有如下代码:

  1. if ((mAttrs.type >= FIRST_SUB_WINDOW &&
  2. mAttrs.type <= LAST_SUB_WINDOW)) {//如果是子窗口
  3. // The multiplier here is to reserve space for multiple
  4. // windows in the same type layer.
  5. mBaseLayer = mPolicy.windowTypeToLayerLw(//使用依附窗口的类型
  6. attachedWindow.mAttrs.type) * WindowManagerService.TYPE_LAYER_MULTIPLIER
  7. + WindowManagerService.TYPE_LAYER_OFFSET;
  8. mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);//计算mSubLayer
  9. ......
  10. } else {//非子窗口
  11. // The multiplier here is to reserve space for multiple
  12. // windows in the same type layer.
  13. mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
  14. * WindowManagerService.TYPE_LAYER_MULTIPLIER
  15. + WindowManagerService.TYPE_LAYER_OFFSET;
  16. mSubLayer = 0;
  17. ......
  18. }

如果窗口类型是子窗口,则使用它所依附的窗口类型来计算mBaseLayer,否则使用窗口类型来计算mBaseLayer。计算的方法是先调用mPolicy.windowTypeToLayerLw方法将窗口的类型转化成一个基数,然后再乘以TYPE_LAYER_MULTIPLIER(10000),最后加上TYPE_LAYER_OFFSET(1000),我们先来看看windowTypeToLayerLw函数是如果根据类型返回一个基数的。

  1. public int windowTypeToLayerLw(int type) {
  2. if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {//应用窗口
  3. return 2;
  4. }
  5. switch (type) {
  6. case TYPE_PRIVATE_PRESENTATION:
  7. return 2;
  8. case TYPE_WALLPAPER:
  9. // wallpaper is at the bottom, though the window manager may move it.
  10. return 2;
  11. case TYPE_PHONE:
  12. return 3;
  13. case TYPE_SEARCH_BAR:
  14. case TYPE_VOICE_INTERACTION_STARTING:
  15. return 4;
  16. case TYPE_VOICE_INTERACTION:
  17. // voice interaction layer is almost immediately above apps.
  18. return 5;
  19. case TYPE_INPUT_CONSUMER:
  20. return 6;
  21. case TYPE_SYSTEM_DIALOG:
  22. return 7;
  23. case TYPE_TOAST:
  24. // toasts and the plugged-in battery thing
  25. return 8;
  26. case TYPE_PRIORITY_PHONE:
  27. // SIM errors and unlock. Not sure if this really should be in a high layer.
  28. return 9;
  29. case TYPE_DREAM:
  30. // used for Dreams (screensavers with TYPE_DREAM windows)
  31. return 10;
  32. case TYPE_SYSTEM_ALERT:
  33. // like the ANR / app crashed dialogs
  34. return 11;
  35. case TYPE_INPUT_METHOD:
  36. // on-screen keyboards and other such input method user interfaces go here.
  37. return 12;
  38. case TYPE_INPUT_METHOD_DIALOG:
  39. // on-screen keyboards and other such input method user interfaces go here.
  40. return 13;
  41. case TYPE_KEYGUARD_SCRIM:
  42. // the safety window that shows behind keyguard while keyguard is starting
  43. return 14;
  44. case TYPE_STATUS_BAR_SUB_PANEL:
  45. return 15;
  46. case TYPE_STATUS_BAR:
  47. return 16;
  48. case TYPE_STATUS_BAR_PANEL:
  49. return 17;
  50. case TYPE_KEYGUARD_DIALOG:
  51. return 18;
  52. case TYPE_VOLUME_OVERLAY:
  53. // the on-screen volume indicator and controller shown when the user
  54. // changes the device volume
  55. return 19;
  56. case TYPE_SYSTEM_OVERLAY:
  57. // the on-screen volume indicator and controller shown when the user
  58. // changes the device volume
  59. return 20;
  60. case TYPE_NAVIGATION_BAR:
  61. // the navigation bar, if available, shows atop most things
  62. return 21;
  63. case TYPE_NAVIGATION_BAR_PANEL:
  64. // some panels (e.g. search) need to show on top of the navigation bar
  65. return 22;
  66. case TYPE_SYSTEM_ERROR:
  67. // system-level error dialogs
  68. return 23;
  69. case TYPE_MAGNIFICATION_OVERLAY:
  70. // used to highlight the magnified portion of a display
  71. return 24;
  72. case TYPE_DISPLAY_OVERLAY:
  73. // used to simulate secondary display devices
  74. return 25;
  75. case TYPE_DRAG:
  76. // the drag layer: input for drag-and-drop is associated with this window,
  77. // which sits above all other focusable windows
  78. return 26;
  79. case TYPE_ACCESSIBILITY_OVERLAY:
  80. // overlay put by accessibility services to intercept user interaction
  81. return 27;
  82. case TYPE_SECURE_SYSTEM_OVERLAY:
  83. return 28;
  84. case TYPE_BOOT_PROGRESS:
  85. return 29;
  86. case TYPE_POINTER:
  87. // the (mouse) pointer layer
  88. return 30;
  89. }
  90. Log.e(TAG, "Unknown window type: " + type);
  91. return 2;
  92. }

这个方法很简单就是根据类型返回一个基数。

WindowState中的成员变量mSubLayer只有在窗口是子窗口的时候才有作用,它表示在窗口和父窗口之间的相对位置。代码如下

  1. public int subWindowTypeToLayerLw(int type) {
  2. switch (type) {
  3. case TYPE_APPLICATION_PANEL:
  4. case TYPE_APPLICATION_ATTACHED_DIALOG:
  5. return APPLICATION_PANEL_SUBLAYER;//等于1
  6. case TYPE_APPLICATION_MEDIA:
  7. return APPLICATION_MEDIA_SUBLAYER;//等于-2
  8. case TYPE_APPLICATION_MEDIA_OVERLAY:
  9. return APPLICATION_MEDIA_OVERLAY_SUBLAYER;//等于-1
  10. case TYPE_APPLICATION_SUB_PANEL:
  11. return APPLICATION_SUB_PANEL_SUBLAYER;//等于2
  12. case TYPE_APPLICATION_ABOVE_SUB_PANEL:
  13. return APPLICATION_ABOVE_SUB_PANEL_SUBLAYER;//等于3
  14. }
  15. Log.e(TAG, "Unknown sub-window type: " + type);
  16. return 0;
  17. }

理解了mBaseLayer和mSubLayer后,我们再来看看mLayer是如何计算出来的,是通过assignLayersLocked方法:

  1. private final void assignLayersLocked(WindowList windows) {
  2. int N = windows.size();
  3. int curBaseLayer = 0;
  4. int curLayer = 0;
  5. int i;
  6. boolean anyLayerChanged = false;
  7. for (i=0; i<N; i++) {
  8. final WindowState w = windows.get(i);
  9. final WindowStateAnimator winAnimator = w.mWinAnimator;
  10. boolean layerChanged = false;
  11. int oldLayer = w.mLayer;
  12. if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
  13. || (i > 0 && w.mIsWallpaper)) {//如果窗口的mBaseLayer和前一个相同、或者是输入法和壁纸窗口
  14. curLayer += WINDOW_LAYER_MULTIPLIER;
  15. w.mLayer = curLayer;
  16. } else {
  17. curBaseLayer = curLayer = w.mBaseLayer;
  18. w.mLayer = curLayer;
  19. }
  20. if (w.mLayer != oldLayer) {//层级发生改变
  21. layerChanged = true;
  22. anyLayerChanged = true;
  23. }
  24. final AppWindowToken wtoken = w.mAppToken;
  25. oldLayer = winAnimator.mAnimLayer;//后面都是调整mAnimLayerd的值
  26. if (w.mTargetAppToken != null) {
  27. winAnimator.mAnimLayer =
  28. w.mLayer + w.mTargetAppToken.mAppAnimator.animLayerAdjustment;
  29. } else if (wtoken != null) {
  30. winAnimator.mAnimLayer =
  31. w.mLayer + wtoken.mAppAnimator.animLayerAdjustment;
  32. } else {
  33. winAnimator.mAnimLayer = w.mLayer;
  34. }
  35. if (w.mIsImWindow) {
  36. winAnimator.mAnimLayer += mInputMethodAnimLayerAdjustment;
  37. } else if (w.mIsWallpaper) {
  38. winAnimator.mAnimLayer += mWallpaperAnimLayerAdjustment;
  39. }
  40. if (winAnimator.mAnimLayer != oldLayer) {
  41. layerChanged = true;
  42. anyLayerChanged = true;
  43. }
  44. final TaskStack stack = w.getStack();
  45. if (layerChanged && stack != null && stack.isDimming(winAnimator)) {
  46. // Force an animation pass just to update the mDimLayer layer.
  47. scheduleAnimationLocked();
  48. }
  49. }
  50. if (mAccessibilityController != null && anyLayerChanged
  51. && windows.get(windows.size() - 1).getDisplayId() == Display.DEFAULT_DISPLAY) {
  52. mAccessibilityController.onWindowLayersChangedLocked();
  53. }
  54. }

在调用assignLayersLocked函数之前,WindowList中的窗口其实已经排好序了,前面调用的函数addWindowToListInOrderLocked就是插入窗口到合适的位置,assignLayersLocked函数并不会改变窗口的位置,只是根据窗口的位置计算mLayer的值。

调整方法是从最底层的窗口开始,具有相同的mBaseLayer的值作为一组,每组窗口的mLayer的值从mBaseLayer的值开始,依次加上WINDOW_LAYER_MULTIPLIER(等于5),这样做的目的是在同层的窗口中每隔一个窗口就留下4个空位,方便下次插入新窗口。

这个方法还对输入法和壁纸窗口做了特殊处理。这两类窗口和它插入位置前面的窗口处于一个层级,而不是根据他们的mBaseLayer值计算。(就是前面说的当输入法和壁纸出现是在当前Activity的窗口之间的)。

三、插入窗口的位置

在addWindow函数中我们会调用addAppWindowToListLocked来确定窗口的位置,现在我们来看下这个函数。

  1. private void addWindowToListInOrderLocked(final WindowState win, boolean addToToken) {
  2. if (win.mAttachedWindow == null) {//非子窗口
  3. final WindowToken token = win.mToken;
  4. int tokenWindowsPos = 0;
  5. if (token.appWindowToken != null) {//应用窗口的顶层窗口
  6. tokenWindowsPos = addAppWindowToListLocked(win);
  7. } else {
  8. addFreeWindowToListLocked(win);//系统窗口
  9. }
  10. if (addToToken) {
  11. if (DEBUG_ADD_REMOVE) Slog.v(TAG, "Adding " + win + " to " + token);
  12. token.windows.add(tokenWindowsPos, win);
  13. }
  14. } else {
  15. addAttachedWindowToListLocked(win, addToToken);//添加子窗口
  16. }
  17. if (win.mAppToken != null && addToToken) {
  18. win.mAppToken.allAppWindows.add(win);
  19. }
  20. }

上面这个函数根据窗口的类型,应用顶层窗口,系统窗口,子窗口。

我们现在分别对这三类窗口的处理方法进行解析,先来看插入Activity顶层窗口的addAppWindowToListLocked

1.插入Activity顶层方法的addAppWindowToListLocked方法

addAppWindowToListLocked方法先判断系统中是否存在和待插入的窗口是否有相同的Token,如果有代表它不是Activity的第一个窗口,因此再判断这个窗口的类型是不是TYPE_BASE_APPLICATION,如果是这类窗口需要放在所有和它相同Token的窗口下面,否则在判断这个应用的启动窗口是否位于最前面(说明正在启动),如果是放在启动窗口的下面。如果不是下面两种情况,则寻找同一应用中位置最高的窗口,然后插在它上面,这表示加入的窗口将覆盖在前面的窗口之上。

下面是部分代码,

  1. private int addAppWindowToListLocked(final WindowState win) {
  2. final IWindow client = win.mClient;
  3. final WindowToken token = win.mToken;
  4. final DisplayContent displayContent = win.getDisplayContent();
  5. if (displayContent == null) {
  6. // It doesn't matter this display is going away.
  7. return 0;
  8. }
  9. final WindowList windows = win.getWindowList();
  10. final int N = windows.size();
  11. WindowList tokenWindowList = getTokenWindowsOnDisplay(token, displayContent);
  12. int tokenWindowsPos = 0;
  13. int windowListPos = tokenWindowList.size();
  14. if (!tokenWindowList.isEmpty()) {//如果有,说明它不是第一个窗口
  15. // If this application has existing windows, we
  16. // simply place the new window on top of them... but
  17. // keep the starting window on top.
  18. if (win.mAttrs.type == TYPE_BASE_APPLICATION) {//放在和它相同Token的窗口下面
  19. // Base windows go behind everything else.
  20. WindowState lowestWindow = tokenWindowList.get(0);//第一个0,代表最底层的window
  21. placeWindowBefore(lowestWindow, win);//放在这个window前面
  22. tokenWindowsPos = indexOfWinInWindowList(lowestWindow, token.windows);
  23. } else {
  24. AppWindowToken atoken = win.mAppToken;
  25. WindowState lastWindow = tokenWindowList.get(windowListPos - 1);
  26. if (atoken != null && lastWindow == atoken.startingWindow) {
  27. placeWindowBefore(lastWindow, win);
  28. tokenWindowsPos = indexOfWinInWindowList(lastWindow, token.windows);
  29. } else {
  30. int newIdx = findIdxBasedOnAppTokens(win);//寻找同一token位置最前面的window
  31. //there is a window above this one associated with the same
  32. //apptoken note that the window could be a floating window
  33. //that was created later or a window at the top of the list of
  34. //windows associated with this token.
  35. windows.add(newIdx + 1, win);//插在它前面
  36. if (newIdx < 0) {
  37. // No window from token found on win's display.
  38. tokenWindowsPos = 0;
  39. } else {
  40. tokenWindowsPos = indexOfWinInWindowList(
  41. windows.get(newIdx), token.windows) + 1;
  42. }
  43. mWindowsChanged = true;
  44. }
  45. }
  46. return tokenWindowsPos;
  47. }

我们再来看几个函数

placeWindowBefore函数就是插入到windows这个位置前

  1. private void placeWindowBefore(WindowState pos, WindowState window) {
  2. final WindowList windows = pos.getWindowList();
  3. int i = windows.indexOf(pos);
  4. if (i < 0) {
  5. Slog.w(TAG, "placeWindowBefore: Unable to find " + pos + " in " + windows);
  6. i = 0;
  7. }
  8. windows.add(i, window);
  9. mWindowsChanged = true;
  10. }

findIdxBasedOnAppTokens函数就是寻找相同token的最前面的window,所以要注意遍历循环的时候是从window的size最大的时候反过来遍历的。

  1. private int findIdxBasedOnAppTokens(WindowState win) {
  2. WindowList windows = win.getWindowList();
  3. for(int j = windows.size() - 1; j >= 0; j--) {
  4. WindowState wentry = windows.get(j);
  5. if(wentry.mAppToken == win.mAppToken) {
  6. return j;
  7. }
  8. }
  9. return -1;
  10. }


继续看这个函数,如果系统中不存在和窗口具有相同token的窗口(说明Activity刚启动,第一个窗口还没有创建完成),这时就会遍历系统所有的task以及task中包含的AppWindowToken,找到窗口的位置,再在task中排在本窗口前面的窗口中,找出离自己最近的,并且APPWindowToken的窗口列表不为NULL的窗口,插入到它的最后一个子窗口后面。如果前面的窗口的列表也都为NULL,则寻找排在本窗口后面的第一个包含有窗口对象的APPWindowToken,把本窗口插在前面。

  1. WindowState pos = null;
  2. final ArrayList<Task> tasks = displayContent.getTasks();
  3. int taskNdx;
  4. int tokenNdx = -1;
  5. for (taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
  6. AppTokenList tokens = tasks.get(taskNdx).mAppTokens;
  7. for (tokenNdx = tokens.size() - 1; tokenNdx >= 0; --tokenNdx) {
  8. final AppWindowToken t = tokens.get(tokenNdx);
  9. if (t == token) {
  10. --tokenNdx;
  11. if (tokenNdx < 0) {
  12. --taskNdx;
  13. if (taskNdx >= 0) {
  14. tokenNdx = tasks.get(taskNdx).mAppTokens.size() - 1;
  15. }
  16. }
  17. break;
  18. }
  19. // We haven't reached the token yet; if this token
  20. // is not going to the bottom and has windows on this display, we can
  21. // use it as an anchor for when we do reach the token.
  22. tokenWindowList = getTokenWindowsOnDisplay(t, displayContent);
  23. if (!t.sendingToBottom && tokenWindowList.size() > 0) {
  24. pos = tokenWindowList.get(0);
  25. }
  26. }
  27. if (tokenNdx >= 0) {
  28. // early exit
  29. break;
  30. }
  31. }
  32. // We now know the index into the apps. If we found
  33. // an app window above, that gives us the position; else
  34. // we need to look some more.
  35. if (pos != null) {
  36. // Move behind any windows attached to this one.
  37. WindowToken atoken = mTokenMap.get(pos.mClient.asBinder());
  38. if (atoken != null) {
  39. tokenWindowList =
  40. getTokenWindowsOnDisplay(atoken, displayContent);
  41. final int NC = tokenWindowList.size();
  42. if (NC > 0) {
  43. WindowState bottom = tokenWindowList.get(0);
  44. if (bottom.mSubLayer < 0) {
  45. pos = bottom;
  46. }
  47. }
  48. }
  49. placeWindowBefore(pos, win);
  50. return tokenWindowsPos;
  51. }
  52. // Continue looking down until we find the first
  53. // token that has windows on this display.
  54. for ( ; taskNdx >= 0; --taskNdx) {
  55. AppTokenList tokens = tasks.get(taskNdx).mAppTokens;
  56. for ( ; tokenNdx >= 0; --tokenNdx) {
  57. final AppWindowToken t = tokens.get(tokenNdx);
  58. tokenWindowList = getTokenWindowsOnDisplay(t, displayContent);
  59. final int NW = tokenWindowList.size();
  60. if (NW > 0) {
  61. pos = tokenWindowList.get(NW-1);
  62. break;
  63. }
  64. }
  65. if (tokenNdx >= 0) {
  66. // found
  67. break;
  68. }
  69. }
  70. if (pos != null) {
  71. // Move in front of any windows attached to this
  72. // one.
  73. WindowToken atoken = mTokenMap.get(pos.mClient.asBinder());
  74. if (atoken != null) {
  75. final int NC = atoken.windows.size();
  76. if (NC > 0) {
  77. WindowState top = atoken.windows.get(NC-1);
  78. if (top.mSubLayer >= 0) {
  79. pos = top;
  80. }
  81. }
  82. }
  83. placeWindowAfter(pos, win);
  84. return tokenWindowsPos;
  85. }
  86. ......

如果前面窗口的APPWindowToken的窗口列表也为空,则重新遍历整个窗口,然后根据mBaseLayer的值来确定窗口的位置。

  1. final int myLayer = win.mBaseLayer;
  2. int i;
  3. for (i = N - 1; i >= 0; --i) {
  4. WindowState w = windows.get(i);
  5. if (w.mBaseLayer <= myLayer) {
  6. break;
  7. }
  8. }
  9. if (DEBUG_FOCUS_LIGHT || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG,
  10. "Based on layer: Adding window " + win + " at " + (i + 1) + " of " + N);
  11. windows.add(i + 1, win);
  12. mWindowsChanged = true;
  13. return tokenWindowsPos;


2. 插入子窗口的addAttachedWindowToListLocked函数

addAttachedWindowToListLocked方法,如果mSubLayer大于0的子窗口,按mSubLayer的值大小插入到有相同WindowToken的子窗口的合适位置中,如果mSubLayer相同,插入已插入窗口的下层位置,如果mSubLayer小于0,如果还存在mSubLayer小于0,并且大于等于该窗口的mSubLayer的值的子窗口,则插入到该子窗口之下,否则插入到子窗口所依附的窗口下面。

  1. private void addAttachedWindowToListLocked(final WindowState win, boolean addToToken) {
  2. final WindowToken token = win.mToken;
  3. final DisplayContent displayContent = win.getDisplayContent();
  4. if (displayContent == null) {
  5. return;
  6. }
  7. final WindowState attached = win.mAttachedWindow;
  8. WindowList tokenWindowList = getTokenWindowsOnDisplay(token, displayContent);
  9. // Figure out this window's ordering relative to the window
  10. // it is attached to.
  11. final int NA = tokenWindowList.size();
  12. final int sublayer = win.mSubLayer;
  13. int largestSublayer = Integer.MIN_VALUE;
  14. WindowState windowWithLargestSublayer = null;
  15. int i;
  16. for (i = 0; i < NA; i++) {
  17. WindowState w = tokenWindowList.get(i);
  18. final int wSublayer = w.mSubLayer;
  19. if (wSublayer >= largestSublayer) {
  20. largestSublayer = wSublayer;
  21. windowWithLargestSublayer = w;
  22. }
  23. if (sublayer < 0) {
  24. // For negative sublayers, we go below all windows
  25. // in the same sublayer.
  26. if (wSublayer >= sublayer) {
  27. if (addToToken) {
  28. if (DEBUG_ADD_REMOVE) Slog.v(TAG, "Adding " + win + " to " + token);
  29. token.windows.add(i, win);
  30. }
  31. placeWindowBefore(wSublayer >= 0 ? attached : w, win);
  32. break;
  33. }
  34. } else {
  35. // For positive sublayers, we go above all windows
  36. // in the same sublayer.
  37. if (wSublayer > sublayer) {
  38. if (addToToken) {
  39. if (DEBUG_ADD_REMOVE) Slog.v(TAG, "Adding " + win + " to " + token);
  40. token.windows.add(i, win);
  41. }
  42. placeWindowBefore(w, win);
  43. break;
  44. }
  45. }
  46. }
  47. if (i >= NA) {
  48. if (addToToken) {
  49. if (DEBUG_ADD_REMOVE) Slog.v(TAG, "Adding " + win + " to " + token);
  50. token.windows.add(win);
  51. }
  52. if (sublayer < 0) {
  53. placeWindowBefore(attached, win);
  54. } else {
  55. placeWindowAfter(largestSublayer >= 0
  56. ? windowWithLargestSublayer
  57. : attached,
  58. win);
  59. }
  60. }
  61. }


3.插入系统窗口的addFreeWindowToListLocked

addFreeWindowToListLocked方法简单,只是遍历同一显示设备上的Windows,比较mBaseLayer值的大小,插入合适位置。

  1. private void addFreeWindowToListLocked(final WindowState win) {
  2. final WindowList windows = win.getWindowList();
  3. // Figure out where window should go, based on layer.
  4. final int myLayer = win.mBaseLayer;
  5. int i;
  6. for (i = windows.size() - 1; i >= 0; i--) {
  7. if (windows.get(i).mBaseLayer <= myLayer) {
  8. break;
  9. }
  10. }
  11. i++;
  12. if (DEBUG_FOCUS_LIGHT || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG,
  13. "Free window: Adding window " + win + " at " + i + " of " + windows.size());
  14. windows.add(i, win);
  15. mWindowsChanged = true;
  16. }



这篇博客我们分析了window插入到什么位置,以及mLayer的计算。但是具体里面有很多变量,stack task windows等,不是很熟悉,下篇博客我们主要分析这。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章