SystemUIOverlayWindow 管理系統

SystemUIOverlayWindow 管理系統可讓您顯示和 管理 SystemUIOverlayWindow 中的檢視畫面。這個視窗目前用於資料檢視 包括全螢幕使用者切換器、通知面板和鍵盤鎖這個網頁不會:

  • 限制原始設備製造商 (OEM) 可新增至視窗的內容。
  • 強制您採用本頁所述的抽象化機制。

總覽

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

如要使用 SystemUIOverlayWindow,請建立檢視控制器 仲裁者。中介者會傳遞到視窗的全域狀態控制器。這些檢視中介工具:

  • 彼此協調檢視畫面控制器。
  • 檢視控制器的內部商業邏輯。

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

  • 你擁有的檢視區塊。
  • 建立可供 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

顯示檢視流程
圖 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

詳情請參閱程式碼研究室

步驟 1:將 ViewStub 新增至 SysUIOverlayWindow

新增「ViewStub」 加入 視窗版面配置

步驟 2:建立 OverlayViewController

使用新的 ViewStub 建立新的插入項目 OverlayViewController

步驟 3:OverlayViewMediator

建立新的插入項目 OverlayViewMediator 或使用現有事件監聽器 (略過步驟 4),並註冊事件監聽器以隱藏或顯示新的 OverlayViewController

步驟 4:設定新的 OverlayViewMediator

將新的 OverlayViewMediator 加入 OverlayWindowModuleconfig_carSystemUIOverlayViewsMediator

注意事項

SysUIPrimaryWindow 覆蓋整個螢幕時,視窗下的所有元素 不要註冊觸控事件因此,當視窗覆蓋整個螢幕,但包含其內容時 留下一些負面空間,您可以選擇模糊處理負片空間,將事件監聽器附加至 這樣就能關閉視窗中的內容