luiisca / floating-views

Kotlin library for effortlessly creating customizable floating UI elements in Android apps.
https://central.sonatype.com/artifact/io.github.luiisca/floating.views
MIT License
8 stars 1 forks source link

research: look for an android api to display floating windows #1

Closed luiisca closed 5 months ago

linear[bot] commented 5 months ago

LUI-369 research: look for an android api to display floating windows

luiisca commented 5 months ago

before displaying anything over other apps I need to ensure the user explicitely grants the app with the required permission as per [android docs](https://developer.android.com/reference/android/provider/Settings#canDrawOverlays(android.content.Context).

So asked gpt for a code snippet as well as reviewed [this example repo](https://developer.android.com/reference/android/provider/Settings#canDrawOverlays(android.content.Context).

    private fun checkPermission() {
        if (!Settings.canDrawOverlays(this)) {
            val intent = Intent(
                Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:$packageName")
            )
            startActivity(intent)
        }
    }
luiisca commented 5 months ago

apparentely I need to use something called a service, a long running background operation. Created a simple service class and started it using startService from a material themed LargeFloatingActionButton. Services have a lifecycle just like activities, onCreate (executed on the first call to startService), onStartComand (every time startService is called), onDestroy and onBind

luiisca commented 5 months ago

inside a service I need to get the WindowManager instance associated with a display, to do that:

  1. Get display's UI context with createWindowContext
  2. call getSystemService on that contex

according to [the docs](https://developer.android.com/reference/android/view/WindowManager?hl=en) is better to use Presentation. gotta check it out