Android Developers Blog
The latest Android and Google Play news for app and game developers.
🔍
Platform Android Studio Google Play Jetpack Kotlin Docs News

03 huhtikuuta 2023

What’s new in WindowManager 1.1.0-beta01


Link copied to clipboard
Posted by Jon Eckenrode, Technical Writer, Software Engineering blog header featuring Android logos

The 1.1.0-beta01 release of Jetpack WindowManager continues the library’s steady progress toward stable release of version 1.1.0. The beta adds an assortment of new features and capabilities, which are ready for testing and early adoption today!

We need your feedback so we can make WindowManager work best for you. Add the 1.1.0-beta01 dependency to your app, follow the migration steps below (if you’re already using a previous version of the library), and let us know what you think!

Activity embedding

androidx.window.embedding

Activity embedding enables you to optimize your multi-activity apps for large screens. The 1.1.0-beta01 release augments and refactors the APIs to provide greater versatility, capability, and control in managing task window splits. We started with experimental APIs in 1.0.0 and are promoting them ultimately to stable in 1.1.0.

tl;dr

Added a manifest setting so you can inform the system your app has implemented activity embedding. Refactored SplitController to be more focused on split properties; extracted split rule APIs to RuleController and activity embedding APIs to ActivityEmbeddingController. Added the SplitAttributes class to describe embedding splits. Added the EmbeddingAspectRatio class to set a minimum ratio for applying activity embedding rules. Changed pixels units to display-independent pixels (dp). Enabled customization of split layouts. Added a tag to rules so that developers can identify and manage specific rules.

What’s new

PROPERTY_ACTIVITY_EMBEDDING_SPLITS_ENABLED

    • Added as a boolean property of the < application > tag in the app manifest. Lets you inform the system that your app has implemented activity embedding.

ActivityEmbeddingController

    • Added class for operations related to the Activity or ActivityStack classes.
    • Includes isActivityEmbedded() to replace the API in SplitController.

RuleController

    • Added class for operations related to the EmbeddingRule class and subclasses.
    • Includes the following APIs to replace APIs in SplitController:
        • addRule() — Adds a rule or updates the rule that has the same tag.
        • removeRule() — Removes a rule from the collection of registered rules.
        • setRules() — Establishes a collection of rules.
        • clearRules() — Removes all registered rules.
        • parseRules() — Parses rules from XML rule definitions.

SplitAttributes

    • Added class to define the split layout.

EmbeddingAspectRatio

    • Added class to define enum-like behavior constants related to display aspect ratio. Lets you specify when splits are enabled based on the parent window’s aspect ratio.

          See SplitRule for properties that use the constants.

What’s changed

EmbeddingRule

    • Added tag field for identification of split rules.

SplitController

    • Refactored APIs to the following modules:
        • ActivityEmbeddingController
          • Moved isActivityEmbedded() to ActivityEmbeddingController.
        • RuleController
          • Removed the following APIs and replaced their functionality with RuleController APIs:
            • clearRegisteredRules()
            • getSplitRules()
            • initialize()
            • registerRule()
            • unregisterRule()
    • Deprecated isSplitSupported() method and replaced with splitSupportStatus property to provide more detailed information about why the split feature is not available.
    • The getInstance() method now has a Context parameter.

    • Note: The getInstance() methods of ActivityEmbeddingController and RuleController also have a Context parameter.

    • Added SplitAttributes calculator functions to customize split layouts:
        • setSplitAttributesCalculator()
        • clearSplitAttributesCalculator()
        • isSplitAttributesCalculatorSupported() to check whether the SplitAttributesCalculator APIs are supported on the device.
    • Defined SplitSupportStatus nested class to provide state constants for the splitSupportStatus property. Enables you to modify app behavior based on whether activity embedding splits are supported in the current app environment.

SplitRule

    • Added defaultSplitAttributes property which defines the default layout of a split; replaces splitRatio and layoutDirection.
    • Added translation of the XML properties splitRatio and splitLayoutDirection to defaultSplitAttributes.
    • Changed minimum dimension definitions to use density-independent pixels (dp) instead of pixels.
        • Changed minWidth to minWidthDp with default value 600dp.
        • Changed minSmallestWidth to minSmallestWidthDp with default value 600dp.
        • Added minHeightDp property with default value 600dp.
    • Added maxAspectRatioInHorizontal with default value ALWAYS_ALLOW.
    • Added maxAspectRatioInPortrait with default value 1.4.
    • Defined FinishBehavior nested class to replace finish behavior constants.
    • Applied the property changes to the Builder nested class of SplitPairRule and SplitPlaceholderRule.

SplitInfo

    • Replaced getSplitRatio() with getSplitAttributes() to provide additional split-related information.

Window layout

androidx.window.layout

The window layout library lets you determine features of app display windows. With the 1.1.0-beta01 release, you can now work in contexts other than just activities.

What’s changed

WindowInfoTracker

    • Added non-activity UI context support in experimental.

WindowMetricsCalculator

    • Added non-activity UI context support in experimental.

Migration steps

Take the next step and upgrade your app from a previous alpha version. And please let us know how we can further facilitate the upgrade process.

PROPERTY_ACTIVITY_EMBEDDING_SPLITS_ENABLED

    • To enable activity embedding, apps must add the property to the < application > tag in the app manifest:

      <property 
      
      android:name="android.window.PROPERTY_ACTIVITY_EMBEDDING_SPLITS_ENABLED" android:value="true">
      

      When the property is set to true, the system can optimize split behavior for the app early.

SplitInfo

    • Check if the current split is stacked: SplitInfo.splitAttributes.splitType is SplitAttributes.SplitType.ExpandContainersSplitType
    • Check the current ratio:
    • if (SplitInfo.splitAttributes.splitType is SplitAttributes.SplitType.RatioSplitType) {
        val ratio = splitInfo.splitAttributes.splitType.ratio
      } else {
        // Ratio is meaningless for other types.
      }
      

SplitController

    • SplitController.getInstance() 
           changes to:
    SplitController.getInstance(Context)
    • SplitController.initialize(Context, @ResId int) 
          changes to:
    RuleController.getInstance(Context)                                         .setRules(RuleController.parse(Context, @ResId int))
    • SplitController.getInstance().isActivityEmbedded(Activity) 
          changes to:
    ActivityEmbeddingController.getInstance(Context)                    .isActivityEmbedded(Activity)
    • SplitController.getInstance().registerRule(rule) 
          changes to:
    RuleController.getInstance(Context).addRule(rule)
    • SplitController.getInstance().unregisterRule(rule)
          changes to:
    RuleController.getInstance(Context).removeRule(rule)
    • SplitController.getInstance().clearRegisteredRules()
          changes to:
    RuleController.getInstance(Context).clearRules()
    • SplitController.getInstance().getSplitRules()
          changes to:
    RuleController.getInstance(Context).getRules()

SplitRule

    • Change minWidth to minWidthDp and minSmallestWidth to minSmallestWidthDp
    • minWidthDp and minSmallestWidthDp now use dp units instead of pixels
           Apps can use the following call:
           TypedValue.applyDimension(

  COMPLEX_UNIT_DIP,

  minWidthInPixels,

  resources.displayMetrics

)

or simply divide minWithInPixels by displayMetrics#density.

SplitPairRule.Builder

  • SplitPairRule.Builder(

  filters, minWidth, minSmallestWidth

)

             changes to:

SplitPairRule.Builder(filters)

  // Optional if minWidthInDp argument is 600.

  .setMinWidthDp(minWidthInDp)

  // Optional if minSmallestWidthInDp argument is 600.

  .setMinSmallestWidthDp(minSmallestWidthInDp)

  • setLayoutDirection(layoutDirection)and setSplitRatio(ratio)

            change to:

setDefaultSplitAttributes(SplitAttributes.Builder() 

  .setLayoutDirection(layoutDirection)

  .setSplitType(SplitAttributes.SplitType.ratio(ratio)) 

  .build()

)

  • setFinishPrimaryWithSecondary and setFinishSecondaryWithPrimary take the FinishBehavior enum-like constants.

See SplitRule migrations for details.

  • Use:
    setMaxAspectRatioInPortrait(

    EmbeddingAspectRatio.ALWAYS_ALLOW

)

            to show splits on portrait devices.

SplitPlaceholder.Builder

  • Has only filters and placeholderIntent parameters; other properties move to setters.

See SplitPairRule.Builder migrations for details.

  • setFinishPrimaryWithPlaceholder takes the FinishBehavior enum-like constants.
    See finish behavior migrations for details.

  • setLayoutDirection(layoutDirection)and setSplitRatio(ratio)

            change to

setDefaultSplitAttributes(SplitAttributes.Builder() 

  .setLayoutDirection(layoutDirection) 

  .setSplitType(SplitAttributes.SplitType.ratio(ratio))  

  .build()

)

            See layout direction migrations for details.
            Use:

setMaxAspectRatioInPortrait(

  EmbeddingAspectRatio.ALWAYS_ALLOW

)

            to show splits on portrait devices.

Finish behavior

Finish behavior constants must be migrated to FinishBehavior enum-like class constants:
  • FINISH_NEVER changes to FinishBehavior.NEVER

  • FINISH_ALWAYS changes to FinishBehavior.ALWAYS

  • FINISH_ADJACENT changes to FinishBehavior.ADJACENT

Layout direction

Layout direction must be migrated to SplitAttributes.LayoutDirection:

  • ltr changes to SplitAttributes.LayoutDirection.LEFT_TO_RIGHT

  • rtl changes to SplitAttributes.LayoutDirection.RIGHT_TO_LEFT

  • locale changes to SplitAttributes.LayoutDirection.LOCALE

  • splitRatio migrates to SplitAttributes.SplitType.ratio(splitRatio)

Get started

To get started with WindowManager, add the Google Maven repository to your app’s settings.gradle or project-level build.gradle file:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
    }
}

Then add the 1.1.0-beta01 dependency to your app’s module-level build.gradle file:
 
dependencies {
    implementation 'androidx.window:window:1.1.0-beta01'
    . . .
}

Happy coding!