Determine if the app is spanned

A spanned layout occurs when a single app is presented across two screens. By default, if an app is not customized for this scenario, the system will notify the app that it now occupies a greater screen width and height and the app's UI will resize itself to fit the new screen dimensions.

For more info, see How to work with the seam in Introduction to dual-screen devices.

Use this code to determine whether your app is spanned across both screens.

Important

APIs in the Surface Duo SDK interact with the Surface Duo device, and should not be called when your app is running on other devices. Before calling these APIs, you should check whether your app is running on a Surface Duo device. Use the isDeviceSurfaceDuo code snippet to perform this check.

This collection of methods can be composed to create an isAppSpanned check:

fun getCurrentRotation(activity: Activity): Int {
    return try {
        val wm = activity.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        wm.defaultDisplay.rotation
    } catch (e: IllegalStateException) { Surface.ROTATION_0 }
}
fun getHinge(activity: Activity): Rect? {
    // Hinge's coordinates of its 4 edges in different mode
    // Double Landscape Rect(0, 1350 - 1800, 1434)
    // Double Portrait  Rect(1350, 0 - 1434, 1800)
    return if (isDeviceSurfaceDuo()) {
        val displayMask = DisplayMask.fromResourcesRectApproximation(activity)
        if (displayMask != null) {
            val screensBounding = displayMask.getBoundingRectsForRotation(
                    getCurrentRotation(activity)
            )
            if (screensBounding.size == 0) {
                Rect(0, 0, 0, 0)
            } else {
                screensBounding[0]
            }
        } else { null }
    } else { null }
}
fun getWindowRect(activity: Activity): Rect {
    val windowRect = Rect()
    activity.windowManager.defaultDisplay.getRectSize(windowRect)
    return windowRect
}
fun isAppSpanned(activity: Activity): Boolean {
    val hinge = getHinge(activity)
    val windowRect = getWindowRect(activity)

    return if (hinge != null && windowRect.width() > 0 && windowRect.height() > 0) {
        // The windowRect doesn't intersect hinge
        hinge.intersect(windowRect)
    } else {
        false
    }
}

Use the getHinge method to retrieve the bounds for the location of the hinge mask.

See also