SystemUIOverlayWindow 管理系统

SystemUIOverlayWindow 管理系统为您提供了一种在 SystemUIOverlayWindow 中显示和管理视图的方法。目前,此窗口用于包括全屏用户切换器、通知面板和锁屏在内的视图。此页面不会:

  • 围绕 OEM 可以向窗口中添加什么内容设定限制。
  • 强制您采用本页介绍的抽象概念。

概览

您可以使用 SystemUIOverlayWindow 管理系统显示法律声明、全屏用户切换器、后视摄像头、HVAC 控件和锁屏等视图。此窗口位于应用空间之外,使您能够控制视图的 Z 排序、显示/隐藏触发器和整体自定义(包括视图放置、大小、透明度和颜色)。同时,您不必关心系统栏或其他系统界面对象的状态,当隐藏或显示这些对象各自的视图时,需要隐藏或显示这些对象。

为了利用 SystemUIOverlayWindow,您需要为视图中介者创建视图控制器。系统会将中介者传递给窗口的全局状态控制器。这些视图中介者:

  • 在视图控制器之间进行协调。
  • 包含视图控制器的业务逻辑。

视图控制器(由视图中介者进行协调):

  • 拥有其视图。
  • 创建 setter,OverlayViewsMediator 可通过 setter 附加业务逻辑。
  • 创建其视图的显示和隐藏动画。

SystemUIOverlayWindowManager 是一个 SystemUI 组件,它充当初始化中介者并向全局状态控制器注册中介者的入口点,而全局状态控制器以一种适当的方式与视图控制器相关联,以使中介者可以直接调用视图控制器以在窗口中显示和隐藏视图。

OverlayViewController

OverlayViewController 负责在 SystemUIOverlayWindow 中显示的视图,并控制如何显示和隐藏视图。此外,它还支持附加所需的监听器,以便与业务逻辑相关联。

重要方法签名

/**
 * Owns a {@link View} that is present in SystemUIOverlayWindow.
 */
public class OverlayViewController {

    /**
     * Shows content of {@link OverlayViewController}.
     *
     * Should be used to show view externally and in particular by {@link OverlayViewMediator}.
     */
    public final void start();

    /**
     * Hides content of {@link OverlayViewController}.
     *
     * Should be used to hide view externally and in particular by {@link OverlayViewMediator}.
     */
    public final void stop();

    /**
     * Inflate layout owned by controller.
     */
    public final void inflate(ViewGroup baseLayout);

    /**
     * Called once inflate finishes.
     */
    protected void onFinishInflate();

    /**
     * Returns {@code true} if layout owned by controller has been inflated.
     */
    public final boolean isInflated();

    /**
     * Subclasses should override this method to implement reveal animations and implement logic
     * specific to when the layout owned by the controller is shown.
     *
     * Should only be overridden by Superclass but not called by any {@link OverlayViewMediator}.
     */
    protected void showInternal();

    /**
     * Subclasses should override this method to implement conceal animations and implement logic
     * specific to when the layout owned by the controller is hidden.
     *
     * Should only be overridden by Superclass but not called by any {@link OverlayViewMediator}.
     */
    protected void hideInternal();

    /**
     * Provides access to layout owned by controller.
     */
    protected final View getLayout();

    /** Returns the {@link OverlayViewGlobalStateController}. */
    protected final OverlayViewGlobalStateController getOverlayViewGlobalStateController();

    /** Returns whether the view controlled by this controller is visible. */
    public final boolean isVisible();

    /**
     * Returns the ID of the focus area that should receive focus when this view is the
     * topmost view or {@link View#NO_ID} if there is no focus area.
     */
    @IdRes
    protected int getFocusAreaViewId();

    /** Returns whether the view controlled by this controller has rotary focus. */
    protected final boolean hasRotaryFocus();

    /**
     * Sets whether this view allows rotary focus. This should be set to {@code true} for the
     * topmost layer in the overlay window and {@code false} for the others.
     */
    public void setAllowRotaryFocus(boolean allowRotaryFocus);

    /**
     * Refreshes the rotary focus in this view if we are in rotary mode. If the view already has
     * rotary focus, it leaves the focus alone. Returns {@code true} if a new view was focused.
     */
    public boolean refreshRotaryFocusIfNeeded();

    /**
     * Returns {@code true} if heads up notifications should be displayed over this view.
     */
    protected boolean shouldShowHUN();

    /**
     * Returns {@code true} if navigation bar insets should be displayed over this view. Has no
     * effect if {@link #shouldFocusWindow} returns {@code false}.
     */
    protected boolean shouldShowNavigationBarInsets();

    /**
     * Returns {@code true} if status bar insets should be displayed over this view. Has no
     * effect if {@link #shouldFocusWindow} returns {@code false}.
     */
    protected boolean shouldShowStatusBarInsets();

    /**
     * Returns {@code true} if this view should be hidden during the occluded state.
     */
    protected boolean shouldShowWhenOccluded();

    /**
     * Returns {@code true} if the window should be focued when this view is visible. Note that
     * returning {@code false} here means that {@link #shouldShowStatusBarInsets} and
     * {@link #shouldShowNavigationBarInsets} will have no effect.
     */
    protected boolean shouldFocusWindow();

    /**
     * Returns {@code true} if the window should use stable insets. Using stable insets means that
     * even when system bars are temporarily not visible, inset from the system bars will still be
     * applied.
     *
     * NOTE: When system bars are hidden in transient mode, insets from them will not be applied
     * even when the system bars become visible. Setting the return value to {@true} here can
     * prevent the OverlayView from overlapping with the system bars when that happens.
     */
    protected boolean shouldUseStableInsets();

    /**
     * Returns the insets types to fit to the sysui overlay window when this
     * {@link OverlayViewController} is in the foreground.
     */
    @WindowInsets.Type.InsetsType
    protected int getInsetTypesToFit();

    /**
     * Optionally returns the sides of enabled system bar insets to fit to the sysui overlay window
     * when this {@link OverlayViewController} is in the foreground.
     *
     * For example, if the bottom and left system bars are enabled and this method returns
     * WindowInsets.Side.LEFT, then the inset from the bottom system bar will be ignored.
     *
     * NOTE: By default, this method returns {@link #INVALID_INSET_SIDE}, so insets to fit are
     * defined by {@link #getInsetTypesToFit()}, and not by this method, unless it is overridden
     * by subclasses.
     *
     * NOTE: {@link #NO_INSET_SIDE} signifies no insets from any system bars will be honored. Each
     * {@link OverlayViewController} can first take this value and add sides of the system bar
     * insets to honor to it.
     *
     * NOTE: If getInsetSidesToFit is overridden to return {@link WindowInsets.Side}, it always
     * takes precedence over {@link #getInsetTypesToFit()}. That is, the return value of {@link
     * #getInsetTypesToFit()} will be ignored.
     */
    @WindowInsets.Side.InsetsSide
    protected int getInsetSidesToFit();
}

OverlayPanelViewController

OverlayPanelViewController 控制器扩展了 OverlayViewController,并为其父类提供了额外的拖动动画功能。

OverlayViewMediator

OverlayViewMediator 包含用于显示或隐藏多个 OverlayViewController 实例的业务逻辑,因此在某种程度上,它也管理视图控制器之间的协调。

/**
 * Controls when to show and hide {@link OverlayViewController}(s).
 */
public interface OverlayViewMediator {

    /**
     * Register listeners that could use ContentVisibilityAdjuster to show/hide content.
     *
     * Note that we do not unregister listeners because SystemUI components are expected to live
     * for the lifecycle of the device.
     */
    void registerListeners();

    /**
     * Allows for post-inflation callbacks and listeners to be set inside required {@link
     * OverlayViewController}(s).
     */
    void setupOverlayContentViewControllers();
}

SystemUIOverlayWindowManager

SystemUIOverlayWindowManager 负责作为 SystemUI 对象,它充当 SystemUIOverlayWindow 管理系统初始化 OverlayViewMediator 实例并向 OverlayViewGlobalStateController 注册 OverlayViewMediator 实例的入口点。

显示视图流程
图 1. SystemUIOverlayWindowManager

OverlayViewGlobalStateController

OverlayViewGlobalStateController 会接收来自 OverlayViewController 实例的调用以显示或隐藏自身。因此,它也保存在 SystemUIOverlayWindow 中显示或隐藏的内容的状态。

显示视图流程如下图所示:

显示视图流程
图 2. 显示视图流程

隐藏视图流程

隐藏视图流程如下图所示:

隐藏视图流程
图 3. 隐藏视图流程

公共方法签名

公共方法签名编码如下:

​/**
 * This controller is responsible for the following:
 * <p><ul>
 * <li>Holds the global state for SystemUIOverlayWindow.
 * <li>Allows {@link SystemUIOverlayWindowManager} to register {@link OverlayViewMediator}(s).
 * <li>Enables {@link OverlayViewController)(s) to reveal/conceal themselves while respecting the
 * global state of SystemUIOverlayWindow.
 * </ul>
 */
@SysUISingleton
public class OverlayViewGlobalStateController {
    /**
     * Register {@link OverlayViewMediator} to use in SystemUIOverlayWindow.
     */
    public void registerMediator(OverlayViewMediator overlayViewMediator);

    /**
     * Show content in Overlay Window using {@link OverlayPanelViewController}.
     *
     * This calls {@link OverlayViewGlobalStateController#showView(OverlayViewController, Runnable)}
     * where the runnable is nullified since the actual showing of the panel is handled by the
     * controller itself.
     */
    public void showView(OverlayPanelViewController panelViewController);

    /**
     * Show content in Overlay Window using {@link OverlayViewController}.
     */
    public void showView(OverlayViewController viewController, @Nullable Runnable show);

    /**
     * Hide content in Overlay Window using {@link OverlayPanelViewController}.
     *
     * This calls {@link OverlayViewGlobalStateController#hideView(OverlayViewController, Runnable)}
     * where the runnable is nullified since the actual hiding of the panel is handled by the
     * controller itself.
     */
    public void hideView(OverlayPanelViewController panelViewController);

    /**
     * Hide content in Overlay Window using {@link OverlayViewController}.
     */
    public void hideView(OverlayViewController viewController, @Nullable Runnable hide);

    /** Returns {@code true} is the window is visible. */
    public boolean isWindowVisible();

    /**
     * Sets the {@link android.view.WindowManager.LayoutParams#FLAG_ALT_FOCUSABLE_IM} flag of the
     * sysui overlay window.
     */
    public void setWindowNeedsInput(boolean needsInput);

    /** Returns {@code true} if the window is focusable. */
    public boolean isWindowFocusable();

    /** Sets the focusable flag of the sysui overlawy window. */
    public void setWindowFocusable(boolean focusable);

    /** Inflates the view controlled by the given view controller. */
    public void inflateView(OverlayViewController viewController);

    /**
     * Return {@code true} if OverlayWindow is in a state where HUNs should be displayed above it.
     */
    public boolean shouldShowHUN();

    /**
     * Set the OverlayViewWindow to be in occluded or unoccluded state. When OverlayViewWindow is
     * occluded, all views mounted to it that are not configured to be shown during occlusion will
     * be hidden.
     */
    public void setOccluded(boolean occluded);
}

如何将视图添加到 SysUIOverlayWindow

如需了解详情,请参阅 Codelab

第 1 步:将 ViewStub 添加到 SysUIOverlayWindow

ViewStub 添加到窗口布局

第 2 步:创建 OverlayViewController

使用新的 ViewStub 创建新的可注入 OverlayViewController

第 3 步:创建 OverlayViewMediator

创建新的可注入 OverlayViewMediator 或使用现有的 OverlayViewMediator(跳过第 4 步),并注册监听器以隐藏或显示新的 OverlayViewController

第 4 步:配置新的 OverlayViewMediator

将新的 OverlayViewMediator 添加到 OverlayWindowModuleconfig_carSystemUIOverlayViewsMediator

注意事项

SysUIPrimaryWindow 覆盖整个屏幕时,窗口下的任何元素都不会注册触摸事件。因此,当窗口覆盖整个屏幕但其内容留下一些负空间时,您可以选择对负空间进行模糊处理,并将监听器附加到这部分空间,以关闭窗口中的相应内容。