SystemUIOverlaySistema di gestione delle finestre

Il sistema di gestione SystemUIOverlayWindow fornisce un modo per visualizzare e gestire le visualizzazioni in SystemUIOverlayWindow . Attualmente, questa finestra viene utilizzata per visualizzazioni che includono il cambio utente a schermo intero, il pannello delle notifiche e lo scudo tastiera. Questa pagina non:

  • Crea restrizioni su ciò che l'OEM può aggiungere alla finestra.
  • Ti costringono ad adottare le astrazioni descritte in questa pagina.

Panoramica

È possibile utilizzare il sistema di gestione SystemUIOverlayWindow per mostrare visualizzazioni quali note legali, cambio utente a schermo intero, telecamera posteriore, controlli HVAC e blocco tastiera. Questa finestra si trova all'esterno dello spazio dell'app e ti dà il controllo sull'ordinamento Z della vista, sui trigger di rivela/nascondi e sulle personalizzazioni generali tra cui posizionamento, dimensione, trasparenza e colore della vista. Allo stesso tempo, non devi preoccuparti dello stato delle barre di sistema o di altri oggetti dell'interfaccia utente di sistema che devono essere nascosti o mostrati quando la rispettiva vista è nascosta o mostrata.

Per sfruttare SystemUIOverlayWindow , crei controller di visualizzazione per i mediatori di visualizzazione. I mediatori vengono passati al controller dello stato globale della finestra. Questi mediatori di visualizzazione:

  • Coordinare tra i controller di visualizzazione.
  • Logica aziendale interna per i controller di visualizzazione.

Visualizza controller (coordinati dai mediatori di visualizzazione):

  • Possiedi la sua visione.
  • Crea setter attraverso i quali OverlayViewsMediator può allegare la logica aziendale.
  • Crea le animazioni per rivelare e nascondere la loro vista.

SystemUIOverlayWindow Manager, un componente SystemUI, funge da punto di ingresso per inizializzare e registrare i mediatori con il controller di stato globale mentre il controller di stato globale si collega ai controller di visualizzazione in modo tale che i mediatori possano chiamare direttamente i controller di visualizzazione per mostrare e nascondere le visualizzazioni in la finestra.

OverlayViewController

OverlayViewController è responsabile della visualizzazione visualizzata in SystemUIOverlayWindow e controlla il modo in cui la visualizzazione viene rivelata e nascosta. Consente inoltre di collegare gli ascoltatori richiesti in modo che possano essere collegati alla logica aziendale.

Importanti firme del metodo

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

Il controller OverlayPanelViewController estende OverlayViewController e fornisce ulteriori funzionalità di animazione di trascinamento alla sua superclasse.

OverlayViewMediator

OverlayViewMediator ospita la logica aziendale che rivela o nasconde più istanze OverlayViewController , quindi, in un certo senso, gestisce anche il coordinamento tra i controller di visualizzazione.

/**
 * 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 è responsabile di essere l'oggetto SystemUI che funge da punto di ingresso per il sistema di gestione SystemUIOverlayWindow per inizializzare e registrare le istanze OverlayViewMediator con OverlayViewGlobalStateController .

Mostra flusso di visualizzazione
Figura 1. SystemUIOverlayWindowManager

OverlayViewGlobalStateController

OverlayViewGlobalStateController riceve chiamate dalle istanze OverlayViewController per rivelarsi o nascondersi. Pertanto, contiene anche lo stato di ciò che viene mostrato o nascosto in SystemUIOverlayWindow .

Il flusso della visualizzazione dello spettacolo è illustrato di seguito:

Mostra il flusso di visualizzazione
Figura 2. Mostra il flusso della vista

Nascondi flusso di visualizzazione

Il flusso della visualizzazione nascosta è illustrato di seguito:

Nascondi flusso di visualizzazione
Figura 3. Nascondi flusso di visualizzazione

Firme di metodi pubblici

Le firme dei metodi pubblici sono codificate come segue:

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

Come aggiungere una vista a SysUIOverlayWindow

Per i dettagli, vedere il Codelab .

Passaggio 1: aggiungi un ViewStub a SysUIOverlayWindow

Aggiungi ViewStub al layout della finestra .

Passaggio 2: crea un OverlayViewController

Utilizzare il nuovo ViewStub per creare un nuovo OverlayViewController iniettabile.

Passaggio 3: OverlayViewMediator

Crea un nuovo OverlayViewMediator iniettabile o utilizzane uno esistente (salta il passaggio 4) e registra gli ascoltatori per nascondere o mostrare il nuovo OverlayViewController .

Passaggio 4: configura il nuovo OverlayViewMediator

Aggiungi il tuo nuovo OverlayViewMediator a OverlayWindowModule e a config_carSystemUIOverlayViewsMediator .

Avvertenze

Quando SysUIPrimaryWindow copre l'intero schermo, tutti gli elementi sotto la finestra non registrano gli eventi di tocco. Pertanto, quando la finestra copre l'intero schermo ma i suoi contenuti lasciano uno spazio negativo, puoi scegliere di offuscare lo spazio negativo e associare gli ascoltatori a quello spazio per eliminare il contenuto nella finestra.