Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0788706f authored by Coco Duan's avatar Coco Duan
Browse files

Center hub UI horizontally in landscape

When mobile devices with display cutout is in landscape,
communal container has either left or right margin set from
system window insets.
For hub to be edge to edge, reset the horizontal margin in
onApplyWindowInsets listener.

Fixes: b/395706952
Test: on foldable mobile
Flag: com.android.systemui.glanceable_hub_v2
Change-Id: Ic6244ef7a9c1a64b1e0536a36513193bf8f6dbff
parent 7f8bd2fb
Loading
Loading
Loading
Loading
+27 −2
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.systemui.shade

import android.content.Context
import android.content.res.Configuration
import android.graphics.Rect
import android.os.PowerManager
import android.os.SystemClock
@@ -25,11 +26,13 @@ import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.WindowInsets
import android.widget.FrameLayout
import androidx.activity.OnBackPressedDispatcher
import androidx.activity.OnBackPressedDispatcherOwner
import androidx.activity.setViewTreeOnBackPressedDispatcherOwner
import androidx.compose.ui.platform.ComposeView
import androidx.core.view.updateMargins
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleObserver
@@ -101,7 +104,10 @@ constructor(
) : LifecycleOwner {
    private val logger = Logger(logBuffer, TAG)

    private class CommunalWrapper(context: Context) : FrameLayout(context) {
    private class CommunalWrapper(
        context: Context,
        private val communalSettingsInteractor: CommunalSettingsInteractor,
    ) : FrameLayout(context) {
        private val consumers: MutableSet<Consumer<Boolean>> = ArraySet()

        override fun requestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
@@ -121,6 +127,24 @@ constructor(
                consumers.clear()
            }
        }

        override fun onApplyWindowInsets(windowInsets: WindowInsets): WindowInsets {
            if (
                !communalSettingsInteractor.isV2FlagEnabled() ||
                    resources.configuration.orientation != Configuration.ORIENTATION_LANDSCAPE
            ) {
                return super.onApplyWindowInsets(windowInsets)
            }
            val type = WindowInsets.Type.displayCutout()
            val insets = windowInsets.getInsets(type)

            // Reset horizontal margins added by window insets, so hub can be edge to edge.
            if (insets.left > 0 || insets.right > 0) {
                val lp = layoutParams as LayoutParams
                lp.updateMargins(0, lp.topMargin, 0, lp.bottomMargin)
            }
            return WindowInsets.CONSUMED
        }
    }

    /** The container view for the hub. This will not be initialized until [initView] is called. */
@@ -443,7 +467,8 @@ constructor(
        collectFlow(containerView, keyguardInteractor.isDreaming, { isDreaming = it })
        collectFlow(containerView, communalViewModel.swipeToHubEnabled, { swipeToHubEnabled = it })

        communalContainerWrapper = CommunalWrapper(containerView.context)
        communalContainerWrapper =
            CommunalWrapper(containerView.context, communalSettingsInteractor)
        communalContainerWrapper?.addView(communalContainerView)
        logger.d("Hub container initialized")
        return communalContainerWrapper!!