How launch 3rd-party app on dedicated area of screen in Android 9?

Viewed 171

I would like my Android app to launch another app on a dedicated area of the screen, i.e. set the screen bounds within which the other app will be displayed. Below is a screenshot with an example of how this could look, where I have launched Google Maps overlaid on my app:

Before launching 3rd party app 3rd party app launched on dedicated area

This is achieved by the following code:

final String packageName = "advanced.scientific.calculator.calc991.plus";
    new Handler().postDelayed(() -> {

        final int[] location = new int[2];
        containerLayout.getLocationOnScreen(location);
        final Rect appBounds = new Rect(location[0], location[1],
                location[0] + containerLayout.getWidth(),
                location[1] + containerLayout.getHeight());

        final ActivityOptions launchOpts = ActivityOptions.makeBasic();
        launchOpts.setLaunchBounds(appBounds);

        final Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

        startActivity(intent, launchOpts.toBundle());
    }, 5000);

The above result would be satisfactory, however this is run on a Samsung Galaxy S10 device running Android 10. Now, I need to be able to achieve a similar result using Android 9. I have looked into the picture-in-picture and freeform modes, but I have not managed to use the former for this purpose, and the latter unfortunately only seems to be available from Android 10 and up. I am open to any solution for this, including creating a launcher app, etc. I am also looking to maximize compatibility with third-party apps.

What are the possible ways to achieve this on Android 9?

0 Answers
Related