前言
NavigationBar 和 StatusBar 都属于 SystemBar,也叫做 decor,就是说给 App 装饰的意思。一般的 window 的布局是在 PhoneWindowManager 的 layoutWindowLw() 方法中,而 SystemBar 是在 beginLayoutLw() 方法中布局。
当前最上层的 Activity 可以修改 SystemBar 的 visibility,可以调用 View#setSystemUiVisibility() 方法,系统也有一些针对 SystemBar visibility 的策略。最终的 visibility 保存在 PhoneWindowManager 中的 mLastSystemUiFlags 变量中。
一、简单认识DisplayFrames
在分析NaivgationBar和StatusBar对应的窗口布局前,需要我们先来简单认识Android中的DisplayFrames对象。
frameworks/base/services/core/java/com/android/server/wm/DisplayFrames.java
public class DisplayFrames {
//物理屏幕相关的设备id
public final int mDisplayId;
/**
* The current size of the screen; really; extends into the overscan area of the screen and
* doesn't account for any system elements like the status bar.
*/
//当前的屏幕大小,包括过扫描区域。过扫描区域在输出到 TV 时会用到,
// 对于移动设备来说 mOverscan 大小就是物理设备的大小 (0,0)-(dw,dh)。
public final Rect mOverscan = new Rect();
/**
* The current visible size of the screen; really; (ir)regardless of whether the status bar can
* be hidden but not extending into the overscan area.
*/
//当前可见的屏幕大小,其实就是 (0,0)-(dw,dh)。
public final Rect mUnrestricted = new Rect();
/** Like mOverscan*, but allowed to move into the overscan region where appropriate. */
//是应用可显示的区域,包含 StatusBar 的区域,不包含 NavigationBar 区域。
public final Rect mRestrictedOverscan = new Rect();
/**
* The current size of the screen; these may be different than (0,0)-(dw,dh) if the status bar
* can't be hidden; in that case it effectively carves out that area of the display from all
* other windows.
*/
//一般情况下 mRestrictedOverscan 与 mRestricted 相同
public final Rect mRestricted = new Rect();
/**
* During layout, the current screen borders accounting for any currently visible system UI
* elements.
*/
//是布局过程中,当前画面的边界,包含 Translucent(半透明)区域。一般情况下 NavigationBar 区域不是 Translucent,而 StatusBar 是 Translucent。
public final Rect mSystem = new Rect();
/** For applications requesting stable content insets, these are them. */
//是应用窗口的显示区域,不包含 StatusBar 和 NavigationBar。
public final Rect mStable = new Rect();
/**
* For applications requesting stable content insets but have also set the fullscreen window
* flag, these are the stable dimensions without the status bar.
*/
//是当Activity设置Fullscreen flag 时候的窗口显示区域,这时 StatusBar 会隐藏。
public final Rect mStableFullscreen = new Rect();
/**
* During layout, the current screen borders with all outer decoration (status bar, input method
* dock) accounted for.
*/
// 是布局的时候除去外部装饰的窗口(例如 StatusBar 和输入法窗口)。
public final Rect mCurrent = new Rect();
/**
* During layout, the frame in which content should be displayed to the user, accounting for all
* screen decoration except for any space they deem as available for other content. This is
* usually the same as mCurrent*, but may be larger if the screen decor has supplied content
* insets.
*/
// 是当前应该给用户显示的窗口,通常与 mCurrent 相同。当装饰窗口提供内容插入的时候,有可能比 mCurrent 更大。
public final Rect mContent = new Rect();
/**
* During layout, the frame in which voice content should be displayed to the user, accounting
* for all screen decoration except for any space they deem as available for other content.
*/
//mVoiceContent 通常与 mContent 相同。
public final Rect mVoiceContent = new Rect();
/** During layout, the current screen borders along which input method windows are placed. */
//mDock 是输入法布局时的边界。
public final Rect mDock = new Rect();
/** The display cutout used for layout (after rotation) */
//用于刘海屏布局的剪刀工具,Android 9.0 新加入的。
@NonNull public WmDisplayCutout mDisplayCutout = WmDisplayCutout.NO_CUTOUT;
/** The cutout as supplied by display info */
//用于刘海屏布局的剪刀工具,Android 9.0 新加入的。
@NonNull public WmDisplayCutout mDisplayInfoCutout = WmDisplayCutout.NO_CUTOUT;
/**
* During layout, the frame that is display-cutout safe, i.e. that does not intersect with it.
*/
//是在刘海屏上可以安全显示的区域,即这个区域与刘海区域没有交集。
public final Rect mDisplayCutoutSafe = new Rect();
private final Rect mDisplayInfoOverscan = new Rect();
private final Rect mRotatedDisplayInfoOverscan = new Rect();
public int mDisplayWidth;//物理屏幕宽度
public int mDisplayHeight;//物理屏幕高度
public int mRotation;//屏幕旋转角度
public DisplayFrames(int displayId, DisplayInfo info, WmDisplayCutout displayCutout) {
mDisplayId = displayId;
onDisplayInfoUpdated(info, displayCutout);
}
public void onDisplayInfoUpdated(DisplayInfo info, WmDisplayCutout displayCutout) {
mDisplayWidth = info.logicalWidth;
mDisplayHeight = info.logicalHeight;
mRotation = info.rotation;
mDisplayInfoOverscan.set(
info.overscanLeft, info.overscanTop, info.overscanRight, info.overscanBottom);
mDisplayInfoCutout = displayCutout != null ? displayCutout : WmDisplayCutout.NO_CUTOUT;
}
public void onBeginLayout() {
switch (mRotation) {
case ROTATION_90:
mRotatedDisplayInfoOverscan.left = mDisplayInfoOverscan.top;
mRotatedDisplayInfoOverscan.top = mDisplayInfoOverscan.right;
mRotatedDisplayInfoOverscan.right = mDisplayInfoOverscan.bottom;
mRotatedDisplayInfoOverscan.bottom = mDisplayInfoOverscan.left;
break;
case ROTATION_180:
mRotatedDisplayInfoOverscan.left = mDisplayInfoOverscan.right;
mRotatedDisplayInfoOverscan.top = mDisplayInfoOverscan.bottom;
mRotatedDisplayInfoOverscan.right = mDisplayInfoOverscan.left;
mRotatedDisplayInfoOverscan.bottom = mDisplayInfoOverscan.top;