SystemUIOverlayWindow 管理系統

SystemUIOverlayWindow 管理系統提供一種方式,可在 SystemUIOverlayWindow 中顯示及管理檢視畫面。目前,這個視窗用於全螢幕使用者切換器、通知面板和螢幕鎖定畫面等檢視畫面。這個網頁不會:

  • 針對原始設備製造商可在視窗中新增的內容建立限制。
  • 強制您採用本頁所述的抽象化機制。

總覽

你可以使用「SystemUIOverlayWindow」管理系統來顯示檢視畫面,例如 法律聲明、全螢幕使用者切換器、後置鏡頭、空調控制和鍵盤鎖。這個 視窗位於應用程式空間之外,可讓您控制該檢視區塊的 Z 順序, 顯示/隱藏觸發條件和整體自訂項目,包括瀏覽刊登位置、大小、透明度、 和顏色同時,您不必擔心系統資訊列的狀態 當個別檢視區塊隱藏或顯示時,需要隱藏或顯示系統 UI 物件。

如要充分利用 SystemUIOverlayWindow,請為檢視器仲介建立檢視控制器。中介者會傳遞至視窗的全域狀態控制器。這些 ViewMediator 有以下功能:

  • 協調檢視控制器之間的作業。
  • 容納檢視控制項的商業邏輯。

檢視控制器 (由檢視中介服務協調):

  • 擁有自己的檢視畫面。
  • 建立可供 OverlayViewsMediator 附加商業邏輯的 setter。
  • 建立檢視畫面的顯示和隱藏動畫。

SystemUIOverlayWindowSystemUI 元件的管理員,做為進入 使用全域狀態控制器,同時在全域狀態控制器初始化及註冊中介者 與檢視控制器緊密連結,讓中介者能直接呼叫檢視控制器 即可顯示及隱藏視窗中的檢視畫面。

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

Show View 流程
圖 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 涵蓋整個畫面時,窗格下方的任何元素都不會註冊觸控事件。因此,當視窗覆蓋整個螢幕,但包含其內容時 留下一些負面空間,您可以選擇模糊處理負片空間,將事件監聽器附加至 這樣就能關閉視窗中的內容