IWindowManager - 通过反射调用 setOverscan

IWindowManager - call setOverscan via reflection

我是 运行 带有 android 馅饼的设备,我想在我的应用程序中以编程方式更改过扫描。因此我做了以下工作:

问题

代码 here 让我相信,IWindowManager$Stub 中应该有一个 setOverscan 方法,但没有。我是做错了什么,还是无法按照我尝试的方式做我想做的事?

我的代码显示 hasNavigationBar 有效,但 setOverscanMode 方法无效并且失败并显示 java.lang.NoSuchMethodException: android.view.IWindowManager$Stub$Proxy.setOverscan [int, int, int, int, int]

代码

object WindowTool {

    // WindowManager source code: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/IWindowManager.aidl

    fun getWindowManagerService(): Any {
        val serviceManager = Class.forName("android.os.ServiceManager")
        val serviceBinder = serviceManager.getMethod("getService", String::class.java).invoke(serviceManager, Context.WINDOW_SERVICE) as IBinder
        val stub = Class.forName("android.view.IWindowManager$Stub")
        return stub.getMethod("asInterface", IBinder::class.java).invoke(stub, serviceBinder)
    }

    fun setOverscanMode(v1: Int, v2: Int, v3: Int, v4: Int): Boolean {
        try {

            val windowManagerService = getWindowManagerService()
            for (m in windowManagerService.javaClass.methods) {
                L.d { "Method: $m" }
            }
            val setOverscan = windowManagerService.javaClass.getMethod("setOverscan", Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE)
            setOverscan.invoke(windowManagerService, 0, v1, v2, v3, v4)
            return true
        } catch (e: Exception) {
            L.e(e)
            return false
        }

    }

    fun hasNavigationBar(): Boolean? {
        try {
            val windowManagerService = getWindowManagerService()
            val hasNavigationBar = windowManagerService.javaClass.getMethod("hasNavigationBar")
            val res = hasNavigationBar.invoke(windowManagerService) as Boolean
            L.d { "hasNavigationBar: $res" }
            return res
        } catch (e: Exception) {
            L.e(e)
            return null
        }

    }
}

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?

调用 getMethod 在这种情况下不起作用。请参阅以下代码以获取和调用 setOverscan 方法:

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)

}

用法:

WindowManagerHackery.setOverscan(bottom = overscanOffset)

您将需要 android.permission.WRITE_SECURE_SETTINGS。我还会查看 https://github.com/tiann/FreeReflection 以获得对 API 28 的支持,但我不确定如果您打算在市场上发布,Google Play 是否有禁止将此库包含在 APK 中的政策.