August 12th, 2021

Jetpack Window Manager alpha10 update

Craig Dunn
Principal SW Engineer

Hello Android developers!

Google recently released the latest version of the Jetpack Window Manager package, and as with previous releases, we’ve updated our docs and samples to match. This post walks through using the new API to get layout updates when your apps are running on dual-screen and foldable devices, like Microsoft Surface Duo.

UPDATE: on August 18th, 2021 Google released beta01 of the Jetpack Window Manager package. The Java sample for this post has been updated and modified from what’s shown below to ensure the WindowLayoutInfoListener is removed when appropriate.

Update to alpha10

The latest release of Window Manager contains a number of changes and bug fixes, including renaming of classes and converting to Kotlin Flow. Using the new API in Kotlin is simpler than previous versions due to the adoption of Flow to provide layout updates.

Adding the new WindowInfoRepository to an activity requires these three steps:

  1. Add the package in your build.gradle file:

    implementation 'androidx.window:window:1.0.0-alpha10'
  2. Create a var for the object reference in the activity class:

    private lateinit var windowInfoRep: WindowInfoRepository
  3. Create an instance of windowInfoRepository and then use a lifecycleScope to collect an updated WindowLayoutInfo and make changes to the user interface accordingly:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    
        windowInfoRep = windowInfoRepository()
    
        lifecycleScope.launch(Dispatchers.Main) {
              lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                      windowInfoRep.windowLayoutInfo
              .collect { newLayoutInfo ->
                  for (displayFeature : DisplayFeature in newLayoutInfo.displayFeatures) {
                      if (displayFeature is FoldingFeature) {
                          // do something with hinge/fold
                      }
                  }
              }
        }
    }
    

The Surface Duo Window Manager samples are updated to use the latest version.

Use Java and Window Manager

Because the Flow-based API is optimized for Kotlin, there is now a separate window-java package that contains helper classes for Java apps. We have created a new Java Window Manager sample that shows how to use these new helper classes.

Figure 1: Windows Manager sample in Java showing FoldingFeature properties when the app is spanned

The Java-specific API is similar to previous versions of the package, but requires an additional package and use of the new adapter class:

  1. Add the additional java package in your build.gradle file:

    implementation 'androidx.window:window:1.0.0-alpha10'
    implementation 'androidx.window:window-java:1.0.0-alpha10'
  2. Create a class field for the WindoInfoRepositoryCallbackAdapter which is provided specifically for Java implementations:

    WindowInfoRepositoryCallbackAdapter wir;
  3. Create an instance of the WindowInfoRepositoryCallbackAdapter in onCreate:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //...
        wir = new WindowInfoRepositoryCallbackAdapter(
            WindowInfoRepository.Companion.getOrCreate(
                this
            )
        );
    }
  4. Add a listener to the adapter in onStart, and handle the callbacks using the WindowLayoutInfo instances that are provided:

    @Override
    protected void onStart() {
       super.onStart();
       wir.addWindowLayoutInfoListener(runOnUiThreadExecutor(), (windowLayoutInfo -> {
            List<DisplayFeature> displayFeatures = windowLayoutInfo.getDisplayFeatures();
            displayFeatures.forEach(displayFeature -> {
                FoldingFeature foldingFeature = (FoldingFeature)displayFeature;
                if (foldingFeature != null)
                {   // only set if it's a fold, not other feature type. only works for single-fold devices.
                    // do stuff with the hinge/fold
                }
            });
        }));
    }

Feedback and resources

Check out the Surface Duo developer documentation and past blog posts for links and details on all our samples. You can find our samples on GitHub. 

If you have any questions, or would like to tell us about your apps, use the feedback forum or message us on Twitter @surfaceduodev

Finally, please join us for our dual screen developer livestream at 11am (Pacific time) each Friday – mark it in your calendar and check out the archives on YouTube.

Author

Craig Dunn
Principal SW Engineer

Craig works on the Surface Duo Developer Experience team, where he enjoys writing cross-platform code for Android using a variety of tools including the web, React Native, Flutter, Unity, and Xamarin.

0 comments

Discussion are closed.