Hệ thống quản lý SystemUIOverlayWindow

Hệ thống quản lý SystemUIOverlayWindow cung cấp cho bạn một cách để hiển thị và quản lý các thành phần hiển thị trong SystemUIOverlayWindow. Hiện tại, cửa sổ này được dùng cho các thành phần hiển thị, bao gồm cả trình chuyển đổi người dùng toàn màn hình, bảng thông báo và khoá phím. Trang này không:

  • Tạo các quy định hạn chế về nội dung mà nhà sản xuất thiết bị gốc (OEM) có thể thêm vào cửa sổ.
  • Buộc bạn phải sử dụng các khái niệm trừu tượng được mô tả trên trang này.

Tổng quan

Bạn có thể sử dụng Hệ thống quản lý SystemUIOverlayWindow để hiển thị các thành phần hiển thị như thông báo pháp lý, trình chuyển đổi người dùng toàn màn hình, camera lùi, các nút điều khiển HVAC và khoá phím. Cửa sổ này nằm bên ngoài không gian ứng dụng và cho phép bạn kiểm soát thứ tự Z của thành phần hiển thị, trình kích hoạt hiển thị/ẩn và các tuỳ chỉnh tổng thể, bao gồm cả vị trí, kích thước, độ trong suốt và màu sắc của thành phần hiển thị. Đồng thời, bạn không cần lo lắng về trạng thái của các thanh hệ thống hoặc các đối tượng Giao diện người dùng hệ thống khác cần ẩn hoặc hiển thị khi thành phần hiển thị tương ứng của chúng bị ẩn hoặc hiển thị.

Để tận dụng SystemUIOverlayWindow, bạn tạo trình điều khiển thành phần hiển thị cho trình dàn xếp thành phần hiển thị. Các đối tượng dàn xếp được truyền đến trình điều khiển trạng thái toàn cục của cửa sổ. Các trình dàn xếp thành phần hiển thị này:

  • Điều phối giữa các trình điều khiển thành phần hiển thị.
  • Lưu trữ logic nghiệp vụ cho trình điều khiển thành phần hiển thị.

Trình điều khiển thành phần hiển thị (do trình dàn xếp thành phần hiển thị điều phối):

  • Sở hữu chế độ xem của riêng mình.
  • Tạo phương thức setter để OverlayViewsMediator có thể đính kèm logic nghiệp vụ.
  • Tạo ảnh động hiển thị và ẩn của thành phần hiển thị.

Trình quản lý SystemUIOverlayWindow, một thành phần SystemUI, đóng vai trò là điểm truy cập để khởi chạy và đăng ký trình dàn xếp với trình điều khiển trạng thái toàn cục, trong khi trình điều khiển trạng thái toàn cục liên kết với trình điều khiển thành phần hiển thị theo cách mà trình dàn xếp có thể trực tiếp gọi trình điều khiển thành phần hiển thị để hiển thị và ẩn các thành phần hiển thị trong cửa sổ.

OverlayViewController

OverlayViewController chịu trách nhiệm về thành phần hiển thị trong SystemUIOverlayWindow và kiểm soát cách thành phần hiển thị được hiển thị và ẩn. Lớp này cũng cho phép đính kèm trình nghe bắt buộc để có thể liên kết với logic nghiệp vụ.

Chữ ký phương thức quan trọng

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

Trình điều khiển OverlayPanelViewController mở rộng OverlayViewController và cung cấp thêm các tính năng ảnh động kéo cho lớp cao cấp.

OverlayViewMediator

OverlayViewMediator chứa logic kinh doanh hiển thị hoặc ẩn nhiều thực thể OverlayViewController, vì vậy, theo một cách nào đó, lớp này cũng quản lý việc điều phối giữa các trình điều khiển thành phần hiển thị.

/**
 * 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 chịu trách nhiệm là đối tượng SystemUI phân phát điểm truy cập cho Hệ thống quản lý SystemUIOverlayWindow để khởi tạo và đăng ký các thực thể OverlayViewMediator bằng OverlayViewGlobalStateController.

Hiển thị luồng xem
Hình 1. SystemUIOverlayWindowManager

OverlayViewGlobalStateController

OverlayViewGlobalStateController nhận các lệnh gọi từ các thực thể OverlayViewController để hiển thị hoặc ẩn bản thân. Do đó, lớp này cũng lưu giữ trạng thái của nội dung hiển thị hoặc ẩn trong SystemUIOverlayWindow.

Quy trình hiển thị chế độ xem được minh hoạ dưới đây:

Hiển thị luồng xem
Hình 2. Hiển thị luồng xem

Ẩn luồng xem

Quy trình ẩn chế độ xem được minh hoạ dưới đây:

Ẩn luồng xem
Hình 3. Ẩn luồng thành phần hiển thị

Chữ ký phương thức công khai

Chữ ký phương thức công khai được mã hoá như sau:

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

Cách thêm thành phần hiển thị vào SysUIOverlayWindow

Để biết thông tin chi tiết, hãy xem Lớp học lập trình.

Bước 1: Thêm ViewStub vào SysUIOverlayWindow

Thêm ViewStub vào bố cục cửa sổ.

Bước 2: Tạo OverlayViewController

Sử dụng ViewStub mới để tạo một OverlayViewController mới có thể chèn.

Bước 3: OverlayViewMediator

Tạo một OverlayViewMediator mới có thể chèn hoặc sử dụng một OverlayViewMediator hiện có (bỏ qua Bước 4) và đăng ký trình nghe để ẩn hoặc hiển thị OverlayViewController mới.

Bước 4: Định cấu hình OverlayViewMediator mới

Thêm OverlayViewMediator mới vào OverlayWindowModule và vào config_carSystemUIOverlayViewsMediator.

Chú ý

Khi SysUIPrimaryWindow bao phủ toàn bộ màn hình, mọi phần tử trong cửa sổ sẽ không đăng ký sự kiện chạm. Do đó, khi cửa sổ bao phủ toàn bộ màn hình nhưng nội dung của cửa sổ để lại một số không gian âm, bạn có thể chọn làm mờ không gian âm và đính kèm trình nghe vào không gian đó để loại bỏ nội dung trong cửa sổ.