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

Commit c4664eb0 authored by Catherine Liang's avatar Catherine Liang
Browse files

Fix lock workspace preview crash

The wallpaper picker app would crash when one enters the small preview,
then switches the display, for example on foldable devices, and enters
the small preview again. This is because
context.createWindowContext(...) cannot be called multiple times with
different display, or it would result in a Window Manager crash with the
message "multiple clients unsupported". In this CL, a workaround is used
to get the display size so the window context is no longer needed.

Bug: 303313708
Test: manually verified by testing the preview with different displays
Change-Id: I898fc3ca186cbefaf3cf1d653259b5acb6054ae5
parent 2a6cd0fc
Loading
Loading
Loading
Loading
+10 −8
Original line number Diff line number Diff line
@@ -29,12 +29,12 @@ import android.os.Handler
import android.os.IBinder
import android.view.Display
import android.view.Display.DEFAULT_DISPLAY
import android.view.DisplayInfo
import android.view.LayoutInflater
import android.view.SurfaceControlViewHost
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.view.WindowManager.LayoutParams.TYPE_KEYGUARD
import android.widget.FrameLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isInvisible
@@ -129,7 +129,7 @@ constructor(
        bundle.getBoolean(ClockPreviewConstants.KEY_HIDE_CLOCK, false)
    private val wallpaperColors: WallpaperColors? = bundle.getParcelable(KEY_COLORS)
    private val displayId = bundle.getInt(KEY_DISPLAY_ID, DEFAULT_DISPLAY)
    private val display: Display = displayManager.getDisplay(displayId)
    private val display: Display? = displayManager.getDisplay(displayId)

    private var host: SurfaceControlViewHost

@@ -179,7 +179,7 @@ constructor(

    fun render() {
        mainHandler.post {
            val previewContext = context.createDisplayContext(display)
            val previewContext = display?.let { context.createDisplayContext(it) } ?: context

            val rootView = FrameLayout(previewContext)

@@ -189,16 +189,18 @@ constructor(
                setUpBottomArea(rootView)
            }

            val windowContext = context.createWindowContext(display, TYPE_KEYGUARD, null)
            val windowManagerOfDisplay = windowContext.getSystemService(WindowManager::class.java)
            var displayInfo: DisplayInfo? = null
            display?.let {
                displayInfo = DisplayInfo()
                it.getDisplayInfo(displayInfo)
            }
            rootView.measure(
                View.MeasureSpec.makeMeasureSpec(
                    windowManagerOfDisplay?.currentWindowMetrics?.bounds?.width()
                        ?: windowManager.currentWindowMetrics.bounds.width(),
                    displayInfo?.logicalWidth ?: windowManager.currentWindowMetrics.bounds.width(),
                    View.MeasureSpec.EXACTLY
                ),
                View.MeasureSpec.makeMeasureSpec(
                    windowManagerOfDisplay?.currentWindowMetrics?.bounds?.height()
                    displayInfo?.logicalHeight
                        ?: windowManager.currentWindowMetrics.bounds.height(),
                    View.MeasureSpec.EXACTLY
                ),