I'm running a device with android pie and want to programmatically change the overscan from within my app. Therefore I've done following:
WRITE_SECURE_SETTINGS
via adbProblem
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?
My code shows that hasNavigationBar
is working, but the setOverscanMode
method isn't and fails with java.lang.NoSuchMethodException: android.view.IWindowManager$Stub$Proxy.setOverscan [int, int, int, int, int]
Code
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?
Calling getMethod
won't work in this case. See the following code to get and invoke the setOverscan
method:
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:
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.