SystemUIOverlayWindow-Verwaltungssystem

Mit dem SystemUIOverlayWindow-Verwaltungssystem können Sie Ansichten in der SystemUIOverlayWindow anzeigen und verwalten. Derzeit wird dieses Fenster für Ansichten verwendet, darunter der Vollbild-Nutzerwechsel, das Benachrichtigungsfeld und der Keyguard. Auf dieser Seite ist Folgendes nicht zulässig:

  • Legen Sie fest, was der OEM dem Fenster hinzufügen darf.
  • Sie zwingen Sie, die auf dieser Seite beschriebenen Abstraktionsebenen zu verwenden.

Übersicht

Mit dem SystemUIOverlayWindow-Verwaltungssystem können Sie Ansichten wie den Haftungsausschluss, den Nutzerwechsel im Vollbildmodus, die Rückkamera, die HLK-Steuerung und den Keyguard anzeigen. Dieses Fenster befindet sich außerhalb des App-Bereichs und bietet Ihnen die Möglichkeit, die Z-Reihenfolge der Ansicht, die Trigger zum Ein-/Ausblenden und allgemeine Anpassungen wie die Platzierung, Größe, Transparenz und Farbe der Ansicht zu steuern. Gleichzeitig müssen Sie sich nicht um den Status von Systemleisten oder anderen System-UI-Objekten kümmern, die ausgeblendet oder angezeigt werden müssen, wenn die entsprechende Ansicht ausgeblendet oder angezeigt wird.

Wenn Sie SystemUIOverlayWindow nutzen möchten, erstellen Sie View-Controller für Ihre View-Mediatoren. Die Mediatoren werden an den globalen Statuscontroller des Fensters übergeben. Diese Mediatoren für die Aufrufe:

  • Koordinieren Sie die Ansichtscontroller.
  • Geschäftslogik für View-Controller verwalten

Ansichtscontroller (von Ansichtsmediatoren koordiniert):

  • Eigene Ansicht
  • Erstellen Sie Setter, über die OverlayViewsMediator Geschäftslogik anhängen kann.
  • Erstellen Sie die Animationen zum Ein- und Ausblenden der Ansicht.

SystemUIOverlayWindowManager, eine SystemUI-Komponente, dient als Einstiegspunkt, um Mediatoren beim globalen Statuscontroller zu initialisieren und zu registrieren. Der globale Statuscontroller ist so mit den Ansichtscontrollern verknüpft, dass Mediatoren die Ansichtscontroller direkt aufrufen können, um Ansichten im Fenster ein- und auszublenden.

OverlayViewController

OverlayViewController ist für die Ansicht verantwortlich, die in SystemUIOverlayWindow angezeigt wird, und steuert, wie die Ansicht angezeigt und ausgeblendet wird. Außerdem können erforderliche Listener angehängt werden, damit sie mit der Geschäftslogik verknüpft werden können.

Wichtige Methodensignaturen

/**
 * 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

Der OverlayPanelViewController-Controller erweitert OverlayViewController und bietet seiner Superklasse zusätzliche Funktionen für die Drag-and-drop-Animation.

OverlayViewMediator

OverlayViewMediator enthält die Geschäftslogik, die mehrere OverlayViewController-Instanzen ein- oder ausblendet. So wird auch die Koordination zwischen den View-Controllern verwaltet.

/**
 * 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 ist das SystemUI-Objekt, das als Einstiegspunkt für das SystemUIOverlayWindow-Verwaltungssystem dient, um OverlayViewMediator-Instanzen bei OverlayViewGlobalStateController zu initialisieren und zu registrieren.

Aufrufabfolge einer Videoansicht
Abbildung 1 SystemUIOverlayWindowManager

OverlayViewGlobalStateController

OverlayViewGlobalStateController empfängt Anrufe von OverlayViewController-Instanzen, um sich zu offenbaren oder zu verbergen. Daher enthält es auch den Status dessen, was in SystemUIOverlayWindow angezeigt oder ausgeblendet wird.

Der Ablauf der Wiedergabeansicht wird unten veranschaulicht:

Aufrufabfolge anzeigen
Abbildung 2. Aufrufabfolge der Ansicht anzeigen

Aufrufabfolge der Ansichten ausblenden

Unten ist der Ablauf zum Ausblenden einer Ansicht dargestellt:

Aufrufabfolge der Ansichten ausblenden
Abbildung 3 Aufrufabfolge ausblenden

Signaturen öffentlicher Methoden

Die Signatur einer öffentlichen Methode wird so codiert:

/**
 * 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);
}

Ansicht zu SysUIOverlayWindow hinzufügen

Weitere Informationen finden Sie im Codelab.

Schritt 1: SysUIOverlayWindow ein ViewStub hinzufügen

Fügen Sie ViewStub zum Fensterlayout hinzu.

Schritt 2: OverlayViewController erstellen

Verwenden Sie die neue ViewStub, um eine neue injizierbare OverlayViewController zu erstellen.

Schritt 3: OverlayViewMediator

Erstellen Sie eine neue InjectableOverlayViewMediator oder verwenden Sie eine vorhandene (überspringen Sie Schritt 4) und registrieren Sie Listener, um die neueOverlayViewController auszublenden oder einzublenden.

Schritt 4: Neuen OverlayViewMediator konfigurieren

Fügen Sie den neuen OverlayViewMediator zu OverlayWindowModule und config_carSystemUIOverlayViewsMediator hinzu.

Einschränkungen

Wenn SysUIPrimaryWindow den gesamten Bildschirm bedeckt, werden für Elemente unter dem Fenster keine Touch-Ereignisse registriert. Wenn das Fenster also den gesamten Bildschirm bedeckt, aber der Inhalt etwas Negativraum lässt, können Sie den Negativraum unkenntlich machen und Listener an diesen Bereich anhängen, um Inhalte im Fenster zu schließen.