skip to Main Content

I’m trying to apply the Jetpack WindowManager library’s WindowLayoutInfo class, which provides information about foldable displays (flat, half_opened, etc), im following the codelab for adaptative uis (url 01), in android studio, i created an instance of the emulator for a resizable device (this is experimental feature of last stable ide), but, when testing the app, the foldable states are the normal ones, checking the following function, it always returning the default posture (Normal):

I need some ideas for configuring the emulator for this feature. While unit testing is ok, the testing in the ui isnt checking the foldable state.

Thanks in advance.

Url 01: https://developer.android.com/codelabs/android-window-manager-dual-screen-foldables?hl=es-419#5


Kotlin code:

sealed interface DevicePosture {
  object NormalPosture : DevicePosture

  data class BookPosture(
    val hingePosition: Rect
  ) : DevicePosture

  data class Separating(
    val hingePosition: Rect, var orientation: FoldingFeature.Orientation
  ) : DevicePosture
}

@OptIn(ExperimentalContracts::class)
fun isBookPosture(foldFeature: FoldingFeature?): Boolean {
  contract { returns(true) implies (foldFeature != null) }
  return foldFeature?.state == FoldingFeature.State.HALF_OPENED && foldFeature.orientation == FoldingFeature.Orientation.VERTICAL
}

@OptIn(ExperimentalContracts::class)
fun isSeparating(foldFeature: FoldingFeature?): Boolean {
  contract { returns(true) implies (foldFeature != null) }
  return foldFeature?.state == FoldingFeature.State.FLAT && foldFeature.isSeparating
}

/* ... */
val devicePostureFlow = WindowInfoTracker.getOrCreate(this).windowLayoutInfo(this)
  .flowWithLifecycle(this.lifecycle)
  .map { layoutInfo ->
    val foldingFeature = layoutInfo.displayFeatures.filterIsInstance().firstOrNull()
    when {
      isBookPosture(foldingFeature) -> DevicePosture.BookPosture(foldingFeature.bounds)
      isSeparating(foldingFeature) -> DevicePosture.Separating(foldingFeature.bounds, foldingFeature.orientation)
      else -> DevicePosture.NormalPosture
    }
  }
  .stateIn(
    scope = lifecycleScope,
    started = SharingStarted.Eagerly,
    initialValue = DevicePosture.NormalPosture
  )


I’m also following this tutorial from web:
Large Screens & Foldables Tutorial for Android

2

Answers


  1. For the resizable emulator, you can uncheck the Launch in the Running Devices tool window option in Settings > Tools > Emulator.
    Emulator settings in Android Studio

    Then, you will have access to Change posture button in the sidebar.
    Change posture option in emulator sidebar

    However, it may be easier to just use a dedicated foldable emulator, like the 6.7 horizontal fold-in, 7.6 fold-in with outer display, 8 fold-out, or Pixel Fold. These will all have controls in the Extended Controls three-dot menu under Virtual sensors to change the device’s posture.
    Extended controls for foldable emulator

    Login or Signup to reply.
  2. Try Android Studio Preview Release – Giraffe. You should be able to see the change posture option in the sidebar of the emulator.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search