System zarządzania SystemUIOverlayWindow

System zarządzania SystemUIOverlayWindow umożliwia wyświetlanie zarządzaj widokami w SystemUIOverlayWindow. Obecnie to okno jest używane w przypadku widoków łącznie z pełnym ekranem przełącznika użytkowników, panelem powiadomień i blokadą klawiszy. Czego nie robi ta strona:

  • Określ ograniczenia dotyczące tego, co OEM może dodać do okresu.
  • Zmuszają Cię do przyjęcia abstrakcji opisanych na tej stronie.

Omówienie

Za pomocą systemu zarządzania SystemUIOverlayWindow możesz wyświetlać widoki, takie jak informacje prawne, tryb pełnoekranowy przełącznika użytkownika, tylna kamera, sterowanie ogrzewaniem, wentylacją i klimatyzacją oraz ekran blokady. To okno znajduje się poza obszarem aplikacji i pozwala kontrolować uporządkowanie widoku według osi Z, wyzwalacze wyświetlania i ukrywania oraz ogólne opcje dostosowywania, w tym umiejscowienie, rozmiar, przezroczystość i kolor widoku. Jednocześnie nie musisz się przejmować stanem pasków systemu ani innymi elementami interfejsu systemu, które muszą być ukryte lub wyświetlane, gdy odpowiednie widoki są ukryte lub wyświetlane.

Aby korzystać z funkcji SystemUIOverlayWindow, musisz utworzyć kontrolery widoku dla swoich pośredników widoku. Pośrednicy są przekazywani do globalnego kontrolera stanu okna. Te pośredniki wyświetlania:

  • Koordynowanie między kontrolerami widoków.
  • Domowa logika biznesowa kontrolerów widoków.

Kontrolery widoku (koordynowane przez mediatory widoku):

  • mieć własny widok.
  • Utwórz metody setter, za pomocą których OverlayViewsMediator może dołączać logikę biznesową.
  • tworzyć animacje wyświetlania i ukrywania widoku.

SystemUIOverlayWindowMenedżer, który jest komponentem SystemUI, służy jako punkt wejścia do inicjowania i rejestrowania mediatorów w kontrolerze stanu globalnego. Kontroler stanu globalnego łączy się z kontrolerami widoku w taki sposób, aby mediatorzy mogli bezpośrednio wywoływać kontrolery widoku w celu wyświetlania i ukrywania widoków w oknie.

Kontroler widoku nakładki

OverlayViewController odpowiada za widok wyświetlany w SystemUIOverlayWindow i kontroluje jego działanie ujawnionych i ukrytych. Umożliwia też dołączenie wymaganych odbiorców, aby można było połączyć je z logiką biznesową.

Ważne podpisy metod

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

kontroler OverlayPanelViewView

Kontroler OverlayPanelViewController rozszerza możliwości OverlayViewController i zapewnia dodatkowe możliwości przeciągania animacji do swojej klasy nadrzędnej.

OverlayViewMediator

OverlayViewMediator zawiera logikę biznesową, która wyświetla lub ukrywa wiele instancji OverlayViewController, dzięki czemu w pewnym sensie zarządza też koordynacją między kontrolerami widoku.

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

Menedżer okien nakładki SystemUI

SystemUIOverlayWindowManager odpowiada za obiekt SystemUI, który obsługuje punkt wejścia dla System zarządzania SystemUIOverlayWindow do inicjowania i rejestracji OverlayViewMediator instancji z OverlayViewGlobalStateController.

Wyświetlanie ścieżki obejrzenia
Rysunek 1. SystemUIOverlayWindowManager

OverlayViewGlobalStateController

OverlayViewGlobalStateController otrzymuje połączenia z instancjami OverlayViewController, aby się wyświetlać lub ukrywać. Dlatego też zawiera stan tego, co jest wyświetlane lub ukryte w elemencie SystemUIOverlayWindow.

Poniżej przedstawiamy przepływ wyświetlania pokazu:

Pokaż przepływ wyświetleń
Rysunek 2. Pokaż proces wyświetlania

Ukryj proces wyświetlania

Proces ukrycia widoku przedstawiono poniżej:

Ukryj proces wyświetlania
Rysunek 3. Ukryj proces wyświetlania

Publiczne podpisy metod

Podpisy metod publicznych są kodowane w ten sposób:

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

Dodawanie widoku do okna SysUIOverlayWindow

Szczegółowe informacje znajdziesz w ćwiczeniach z programowania.

Krok 1. Dodaj komponent ViewStub do SysUIOverlayWindow

Dodaj ViewStub do okna.

Krok 2. Utwórz obiekt OverlayViewController

Użyj nowego elementu ViewStub, aby utworzyć nowy element do wstrzyknięcia OverlayViewController.

Krok 3. OverlayViewMediator

Utwórz nowy element do wstrzyknięcia OverlayViewMediator lub użyj istniejącego (pomiń krok 4) i zarejestruj słuchaczy, aby ukryć lub wyświetlić nowy element OverlayViewController.

Krok 4. Skonfiguruj nowy mediator OverlayViewMediator

Dodaj nową kartę OverlayViewMediator do OverlayWindowModule i config_carSystemUIOverlayViewsMediator.

Uwagi

Gdy SysUIPrimaryWindow zajmuje cały ekran, wszystkie elementy znajdujące się pod oknem nie rejestrują zdarzeń dotyku. Dlatego, gdy okno zajmuje cały ekran, ale jego zawartość pozostawia pewną przestrzeń negatywną, możesz rozmyć tę przestrzeń i dołączyć do niej obiekty, które będą odrzucać zawartość okna.