Jetpackウィンドウマネージャーの再訪


約1年前、私はショートピースを書いた.Androidフレームワークの不明瞭で時代遅れのクラスについて思い出させる他に.ActivityGroup , 簡単に紹介しましたJetpack WindowManager . 使用したバージョンは1.0.0 - alpha 01でした.それ以来かなりの数が変わった.だから、再訪する時間.
Jetpack WindowManagerを使用するには、モジュールレベルのビルドで実装依存性として追加する必要があります.Gradleファイル:
dependencies {
  ...
  implementation "androidx.window:window:1.0.0-beta01"
  ...
その後、新しいパッケージにアクセスできますandroidx.window.layout . これは、新しいインターフェイスとクラスが含まれます.主な相互作用点はWindowInfoRepository , インタフェース.今、あなたは待つことを考えているかもしれません.どのようにインターフェイスと対話する必要がありますか?ソースファイルにはコンパニオンオブジェクトが含まれますActivity . ドキュメントwindowInfoRepository() says :

Provide an instance of WindowInfoRepository that is associated
to the given Activity


だから今、私たちのインスタンスにアクセスする方法を知っているWindowInfoRepository , それをどうしましょうか.

ウィンドウメトリクスの取得

currentWindowMetrics aを返すFlow 消費量WindowMetrics 現在のシステム状態に従って.
The documentation 説明

The metrics describe the size of the area the window would
occupy with MATCH_PARENT width and height and any combination
of flags that would allow the window to extend behind display
cutouts.

The value of this is based on the current windowing state of the
system. For example, for activities in multi-window mode, the
metrics returned are based on the current bounds that the user
has selected for the Activity's window.

WindowMetrics パッケージにも属しますandroidx.window.layout . ITSbounds aを返すRect これはウィンドウが占める領域の境界を表します.
さあ戻りましょうWindowInfoRepository . windowLayoutInfo aを返すFlow of WindowLayoutInfo すべての利用可能な機能が含まれます.

表示機能


インターフェースDisplayFeature ディスプレイ上の物理的特徴の説明です.ドキュメントsays :

A display feature is a distinctive physical attribute located
within the display panel of the device. It can intrude into the
application window space and create a visual distortion, visual
or touch discontinuity, make some area invisible or create a
logical divider or separation in the screen space.


現在、唯一のFoldingFeature . ドキュメントsays :

A feature that describes a fold in the flexible display or a
hinge between two physical display panels.


その折り目についてはとても興味深いことがわかります.
  • occlusionType ヒンジがどのようにコンテンツを封じ込めるかを表します.NONE , FULL
  • orientationFoldingFeature 並列に走る
  • state の状態を表しますFoldingFeature : FLAT , HALF_OPENED
  • isSeparating 計算if...
  • a FoldingFeature should be thought of as splitting the window
    into multiple physical areas that can be seen by users as
    logically separate.


    これをコードで使用する方法を示します.あなたは上のアプリを見つけることができますGitHub .
    class MainActivity : AppCompatActivity() {
    
      private lateinit var binding: MainBinding
    
      override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = MainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val repo = windowInfoRepository()
        lifecycleScope.launch {
          repo.currentWindowMetrics.collect { windowMetrics ->
            output("Bounds: ${windowMetrics.bounds.toShortString()}")
          }
        }
        lifecycleScope.launch {
          repo.windowLayoutInfo.collect {
            it.displayFeatures.forEach { displayFeature ->
              (displayFeature as FoldingFeature).run {
                output("occlusionType: $occlusionType")
                output("orientation: $orientation")
                output("state: $state")
                output("isSeparating: $isSeparating")
              }
            }
          }
        }
      }
    
      private fun output(s: String) {
        lifecycleScope.launch(Dispatchers.Main) {
          binding.textview.append("$s\n")
        }
      }
    }
    

    概要


    一年前、マイクロソフトパックを使ってJetpack WindowManagerをサポートしています.

    So far Jetpack Window Manager is quite small, yet extremely
    useful. It is a great relief that Microsoft seems to be
    utilizing this, because if there is something our ecosystem
    should not face is fragmentation the the foldable area. Things
    might have been more easy if Google had not chosen to completely
    abandon the tablet market. And a foldable by Googles seems not
    to be on the horizon either.


    グーグルが遂に遂に登場rediscovered its love for large screens and foldables . 他のベンダーも折り畳みの経験を押し進めている.そして、我々はピクセル折り目さえ得るかもしれません..