SystemUIOverlayWindow
管理系統為您提供了一種在SystemUIOverlayWindow
中顯示和管理視圖的方法。目前,此窗口用於視圖,包括全屏用戶切換器、通知面板和鍵盤保護。本文不:
- 圍繞 OEM 可以添加到窗口的內容創建限制。
- 強制您採用本文檔中描述的抽象。
概述
您可以使用SystemUIOverlayWindow
管理系統顯示法律聲明、全屏用戶切換器、後視攝像頭、HVAC 控件和鍵盤保護等視圖。此窗口位於應用程序空間之外,可讓您控制視圖的 Z 順序、顯示/隱藏觸發器以及包括視圖放置、大小、透明度和顏色在內的整體自定義。同時,您不必關心系統欄或其他需要隱藏或顯示的系統 UI 對像在其各自的視圖被隱藏或顯示時的狀態。
要利用SystemUIOverlayWindow
,您可以為視圖中介創建視圖控制器。中介被傳遞給窗口的全局狀態控制器。這些觀點調解人:
- 視圖控制器之間的協調。
- 視圖控制器的內部業務邏輯。
視圖控制器(由視圖中介協調):
- 擁有自己的看法。
- 創建 setter,
OverlayViewsMediator
可以通過它附加業務邏輯。 - 創建他們的視圖的顯示和隱藏動畫。
SystemUIOverlayWindow
管理器是一個 SystemUI 組件,它作為一個入口點來初始化和向全局狀態控制器註冊中介,而全局狀態控制器以這樣一種方式綁定到視圖控制器,中介可以直接調用視圖控制器來顯示和隱藏視圖窗戶。
覆蓋視圖控制器
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
(s) 的業務邏輯,因此,它還管理視圖控制器之間的協調。
/** * 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
管理系統提供一個入口點,以使用OverlayViewGlobalStateController
初始化和註冊OverlayViewMediator
(s)。
OverlayViewGlobalStateController
OverlayViewGlobalStateController
接收來自OverlayViewController
(s) 的調用以顯示或隱藏自己。因此,它還保存SystemUIOverlayWindow
中顯示或隱藏的狀態。
顯示視圖流程如下圖所示:
隱藏視圖流
隱藏視圖流程如下圖所示:
公共方法簽名
公共方法簽名編碼如下:
/**
* 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
第 2 步:創建一個 OverlayViewController
使用新的ViewStub
創建一個新的可注入OverlayViewController
。
第三步:OverlayViewMediator
創建一個新的可注入OverlayViewMediator
或使用現有的(跳過第 4 步)並註冊偵聽器以隱藏或顯示新的OverlayViewController
。
第 4 步:配置新的 OverlayViewMediator
將新的OverlayViewMediator
添加到OverlayWindowModule
和config_carSystemUIOverlayViewsMediator
。
注意事項
請注意,一旦SysUIPrimaryWindow
覆蓋整個屏幕,窗口下的任何元素都不會註冊觸摸事件。因此,當窗口覆蓋整個屏幕但其內容留下一些負空間時,您可以選擇模糊負空間並將偵聽器附加到該空間以消除窗口中的內容。