ระบบการจัดการ SystemUIOverlayWindow

ระบบการจัดการ SystemUIOverlayWindow ช่วยให้คุณแสดงและจัดการมุมมองใน SystemUIOverlayWindow ได้ ปัจจุบันหน้าต่างนี้ใช้สำหรับมุมมองต่างๆ ซึ่งรวมถึงตัวสลับผู้ใช้แบบเต็มหน้าจอ แผงการแจ้งเตือน และโปรแกรมรักษาความปลอดภัย หน้านี้จะไม่มีลักษณะต่อไปนี้

  • สร้างข้อจำกัดเกี่ยวกับสิ่งที่ OEM สามารถเพิ่มลงในกรอบเวลาได้
  • บังคับให้คุณใช้การแยกความคิดตามที่อธิบายไว้ในหน้านี้

ภาพรวม

คุณสามารถใช้SystemUIOverlayWindowระบบการจัดการเพื่อแสดงมุมมองต่างๆ เช่น ประกาศทางกฎหมาย ตัวสลับผู้ใช้แบบเต็มหน้าจอ กล้องมองหลัง การควบคุมระบบปรับอากาศ (HVAC) และระบบล็อก หน้าต่างนี้อยู่นอกพื้นที่แอปและให้คุณควบคุมลําดับ Z ของมุมมอง เงื่อนไขการเปิด/ปิด และการปรับแต่งโดยรวม ซึ่งรวมถึงตําแหน่ง มุมมอง ขนาด ความโปร่งใส และสี ในขณะเดียวกัน คุณก็ไม่จําเป็นต้องกังวลเกี่ยวกับสถานะของแถบระบบหรือออบเจ็กต์ UI ของระบบอื่นๆ ที่ต้องซ่อนหรือแสดงเมื่อมุมมองที่เกี่ยวข้องซ่อนหรือแสดง

หากต้องการใช้ประโยชน์จาก SystemUIOverlayWindow ให้สร้างตัวควบคุมมุมมองสําหรับสื่อกลางมุมมอง ระบบจะส่งสื่อกลางไปยังตัวควบคุมสถานะส่วนกลางของหน้าต่าง ตัวสื่อกลางการแสดงผลเหล่านี้

  • ประสานงานระหว่างตัวควบคุมมุมมอง
  • เก็บตรรกะทางธุรกิจสําหรับตัวควบคุมมุมมอง

ตัวควบคุมมุมมอง (ประสานงานโดยสื่อกลางของมุมมอง)

  • เป็นเจ้าของมุมมอง
  • สร้างตัวตั้งค่าที่ OverlayViewsMediator จะใช้แนบตรรกะทางธุรกิจได้
  • สร้างภาพเคลื่อนไหวแสดงและซ่อนมุมมอง

SystemUIOverlayWindowManager ซึ่งเป็นคอมโพเนนต์ SystemUI ทำหน้าที่เป็นจุดแรกเข้าเพื่อเริ่มต้นและลงทะเบียนสื่อกลางกับตัวควบคุมสถานะส่วนกลาง ขณะที่ตัวควบคุมสถานะส่วนกลางจะเชื่อมโยงกับตัวควบคุมมุมมองในลักษณะที่สื่อกลางสามารถเรียกตัวควบคุมมุมมองโดยตรงเพื่อแสดงและซ่อนมุมมองในหน้าต่าง

OverlayViewController

OverlayViewController เป็นผู้รับผิดชอบต่อมุมมองที่แสดงใน SystemUIOverlayWindow และควบคุมวิธีแสดงและซ่อนมุมมอง นอกจากนี้ยังช่วยให้แนบ Listener ที่จำเป็นเพื่อให้เชื่อมโยงกับตรรกะทางธุรกิจได้

ลายเซ็นเมธอดที่สําคัญ

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

ตัวควบคุม OverlayPanelViewController ขยาย OverlayViewController และให้ความสามารถเพิ่มเติมในการลากภาพเคลื่อนไหวแก่ซุปเปอร์คลาส

OverlayViewMediator

OverlayViewMediator เก็บตรรกะทางธุรกิจที่แสดงหรือซ่อนOverlayViewControllerหลายอินสแตนซ์ ดังนั้นจึงจัดการการประสานงานระหว่างตัวควบคุมมุมมองด้วย

/**
 * 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 มีหน้าที่เป็นออบเจ็กต์ SystemUI ที่ทำหน้าที่เป็นจุดแรกเข้าสำหรับSystemUIOverlayWindowระบบการจัดการเพื่อเริ่มต้นและลงทะเบียนOverlayViewMediatorอินสแตนซ์กับ OverlayViewGlobalStateController

แสดงเส้นทางการดู
รูปที่ 1 SystemUIOverlayWindowManager

OverlayViewGlobalStateController

OverlayViewGlobalStateController รับสายจากอินสแตนซ์ OverlayViewController เพื่อเปิดเผยหรือซ่อนตัว ดังนั้น SystemUIOverlayWindow จึงเก็บสถานะของสิ่งที่แสดงหรือซ่อนไว้ด้วย

ขั้นตอนการแสดงผลรายการแสดงอยู่ด้านล่าง

แสดงโฟลว์การดู
รูปที่ 2 แสดงขั้นตอนในมุมมอง

ซ่อนโฟลว์มุมมอง

ขั้นตอนในการซ่อนมุมมองแสดงอยู่ด้านล่าง

ซ่อนโฟลว์มุมมอง
รูปที่ 3 ซ่อนขั้นตอนในมุมมอง

ลายเซ็นเมธอดสาธารณะ

ลายเซ็นเมธอดสาธารณะจะเข้ารหัสดังนี้

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

วิธีเพิ่มมุมมองไปยัง SysUIOverlayWindow

โปรดดูรายละเอียดที่ Codelab

ขั้นตอนที่ 1: เพิ่ม ViewStub ลงใน SysUIOverlayWindow

เพิ่ม ViewStub ลงในเลย์เอาต์หน้าต่าง

ขั้นตอนที่ 2: สร้าง OverlayViewController

ใช้ ViewStub ใหม่เพื่อสร้างไฟล์ OverlayViewController ที่ฉีดได้

ขั้นตอนที่ 3: OverlayViewMediator

สร้างอินเจ็กต์ใหม่ OverlayViewMediator หรือใช้อินเจ็กต์ที่มีอยู่ (ข้ามขั้นตอนที่ 4) และลงทะเบียน Listeners เพื่อซ่อนหรือแสดงอินเจ็กต์ใหม่ OverlayViewController

ขั้นตอนที่ 4: กำหนดค่า OverlayViewMediator ใหม่

เพิ่ม OverlayViewMediator ใหม่ลงใน OverlayWindowModule และ config_carSystemUIOverlayViewsMediator

ข้อจำกัด

เมื่อ SysUIPrimaryWindow ครอบคลุมทั้งหน้าจอ องค์ประกอบที่อยู่ใต้หน้าต่างจะไม่บันทึกเหตุการณ์การสัมผัส ดังนั้น เมื่อหน้าต่างครอบคลุมทั้งหน้าจอ แต่เนื้อหาของหน้าต่างมีบางส่วนที่ว่างเปล่า คุณสามารถเลือกเบลอพื้นที่ว่างเปล่านั้นและแนบ Listener กับพื้นที่ว่างนั้นเพื่อปิดเนื้อหาในหน้าต่าง