3

I'm writing an Android launcher in Kotlin. It should open apps in freeform mode. I expect the following piece of code to open an app on the left half of the screen. Instead, it opens the app in the middle of the screen at half size.

The app opens in the middle of the screen

private fun launchPackage(context: Context, packageName : String) {
    val metrics = getRealDisplayMetrics(context)
    val options = ActivityOptions.makeBasic()
    options.launchBounds = leftSide(metrics)

    val launchIntent = context.packageManager.getLaunchIntentForPackage(packageName)

    context.startActivity(launchIntent, options.toBundle())
}

private fun leftSide(metrics: DisplayMetrics) : Rect {
    val left = 0
    val top = 0
    val right = metrics.widthPixels / 2
    val bottom = metrics.heightPixels

    return Rect(left, top, right, bottom)
}

private fun getRealDisplayMetrics(context: Context): DisplayMetrics {
    val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    val defaultDisplay = windowManager.defaultDisplay
    val metrics = DisplayMetrics()

    defaultDisplay.getMetrics(metrics)

    return metrics
}

Interestingly, when I maximize the app, then from the overview I set it to windowed mode again, it does occupy the area that I set with LaunchBounds, ie. the left half of the screen.

The app shown half-screen after maximizing and returning to windowed mode

Is there anyway to make this work right away?

0 Answers0