Stack Overflow archive
3 scoreaccepted

IWindowManager - call setOverscan via reflection

score
3
question views
1.7K
license
CC BY-SA 4.0

The code here let's me believe, that there should be a setOverscan method inside the IWindowManager$Stub but there isn't. Am I doing something wrong or is it just not possible to do what I want to do the way I try to do it?

Calling getMethod won't work in this case. See the following code to get and invoke the setOverscan method:

kotlin
object WindowManagerHackery {

    @SuppressLint("PrivateApi")
    @Throws(OverscanInvocationError::class)
    fun setOverscan(displayId: Int = Display.DEFAULT_DISPLAY, left: Int = 0, top: Int = 0, right: Int = 0, bottom: Int = 0) {
        try {
            val ServiceManager = Class.forName("android.os.ServiceManager")
            val service = ServiceManager.getMethod("getService", String::class.java)
            val binder = service.invoke(null, "window") as IBinder
            val windowManagerStub = Class.forName("android.view.IWindowManager").classes[0]
            val serviceObj = windowManagerStub.getMethod("asInterface", IBinder::class.java).invoke(null, binder)
            windowManagerStub.methods.first { it.name == "setOverscan" }
                    .invoke(serviceObj, displayId, left, top, right, bottom)
        } catch (e: Exception) {
            throw OverscanInvocationError(e)
        }
    }

    class OverscanInvocationError(e: Exception) : Exception(e)

}

Usage:

java
WindowManagerHackery.setOverscan(bottom = overscanOffset)

You will need android.permission.WRITE_SECURE_SETTINGS. I would also check out https://github.com/tiann/FreeReflection for support on API 28 but I'm not sure if Google Play has a policy against including this library in an APK if you plan on publishing on the market.

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.