Sistema de gerenciamento SystemUIOverlayWindow

O sistema de gerenciamento do SystemUIOverlayWindow oferece uma maneira de exibir e gerenciar visualizações no SystemUIOverlayWindow. No momento, essa janela é usada para visualizações incluindo o seletor de usuário em tela cheia, o painel de notificações e o bloqueio de teclado. Ela não:

  • Crie restrições sobre o que o OEM pode adicionar à janela.
  • Forçam você a adotar as abstrações descritas nesta página.

Visão geral

Você pode usar o sistema de gerenciamento do SystemUIOverlayWindow para mostrar visualizações como as aviso legal, seletor de usuários em tela cheia, câmera traseira, controles de AVAC (aquecimento, ventilação e ar-condicionado) e bloqueio de teclado. Isso fica fora do espaço do app e oferece controle sobre a ordem Z da visualização, gatilhos de revelação/ocultação e personalizações gerais, incluindo posicionamento da visualização, tamanho, transparência, e cor. Ao mesmo tempo, você não precisa se preocupar com o estado das barras de sistema ou outros Objetos da interface do sistema que precisam ser ocultados ou mostrados quando a respectiva visualização é ocultada ou mostrada.

Para aproveitar o SystemUIOverlayWindow, crie controladores de visualização para sua visualização mediadores. Os mediadores são transmitidos para o controlador de estado global da janela. Esses mediadores de visualização:

  • Coordenação entre controladores de visualização.
  • Hospeda a lógica de negócios para controladores de visualização.

Controles de visualização (coordenados por mediadores de visualização):

  • A propriedade da visualização.
  • Criar setters com que o OverlayViewsMediator pode anexar uma lógica de negócios.
  • Criar animações para revelar e ocultar as visualizações.

SystemUIOverlayWindowO gerenciador, um componente SystemUI,funciona como um ponto de entrada para inicializar e registrar mediadores com o controlador de estado global, enquanto o controlador de estado global vincula-se aos controladores de visualização de forma que os mediadores possam chamar diretamente os controladores de visualização. para mostrar e ocultar visualizações na janela.

OverlayViewController

OverlayViewController é responsável pela visualização mostrada no SystemUIOverlayWindow e controla como ela é reveladas e ocultas. Ele também permite anexar os listeners necessários para que ele possa ser vinculado à lógica de negócios.

Assinaturas de método importantes

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

O controlador OverlayPanelViewController estende OverlayViewController e fornece recursos adicionais de animação de arrastar à superclasse.

OverlayViewMediator

O OverlayViewMediator armazena a lógica de negócios que revela ou oculta várias instâncias de OverlayViewController, portanto, de uma forma que também gerencia a coordenação entre controladores de visualização.

/**
 * 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 é responsável por ser o objeto SystemUI que serve um ponto de entrada para o SystemUIOverlayWindow Sistema de gerenciamento para inicializar e registrar OverlayViewMediator instâncias com OverlayViewGlobalStateController.

Mostrar fluxo de visualização
Figura 1. SystemUIOverlayWindowManager

OverlayViewGlobalStateController

OverlayViewGlobalStateController recebe chamadas de OverlayViewController instâncias para revelar ou ocultar a si mesmas. Portanto, ela também contém o estado do que é mostrado ou oculto na SystemUIOverlayWindow.

O fluxo de visualização do programa é ilustrado abaixo:

Mostrar fluxo de visualização
Figura 2. mostre o fluxo de visualização

Ocultar fluxo de visualização

O fluxo de ocultação de visualização é ilustrado abaixo:

Ocultar fluxo de visualização
Figura 3. Ocultar fluxo de visualização

Assinaturas de método público

As assinaturas de método público são codificadas da seguinte forma:

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

Como adicionar uma visualização à SysUIOverlayWindow.

Para mais detalhes, consulte o Codelab.

Etapa 1: adicionar uma ViewStub à SysUIOverlayWindow

Adicionar ViewStub ao layout de janela.

Etapa 2: criar um OverlayViewController

Usar o novo ViewStub para criar um novo injetável OverlayViewController.

Etapa 3: OverlayViewMediator

Criar um novo injetável OverlayViewMediator ou use um já existente (pule a etapa 4) e registre listeners para ocultar ou mostrar o novo OverlayViewController.

Etapa 4: configurar o novo OverlayViewMediator

Adicione seu novo OverlayViewMediator a OverlayWindowModule e para config_carSystemUIOverlayViewsMediator

Avisos

Quando SysUIPrimaryWindow cobre a tela inteira, todos os elementos abaixo dela não registram eventos de toque. Por isso, quando a janela cobre toda a tela, mas o conteúdo dela deixar algum espaço negativo, pode optar por desfocar o espaço negativo e anexar ouvintes a esse espaço para dispensar o conteúdo na janela.