@ -1,11 +1,16 @@
package com.genymobile.scrcpy.wrappers ;
package com.genymobile.scrcpy.wrappers ;
import com.genymobile.scrcpy.Ln ;
import android.annotation.SuppressLint ;
import android.annotation.SuppressLint ;
import android.graphics.Rect ;
import android.graphics.Rect ;
import android.os.Build ;
import android.os.Build ;
import android.os.IBinder ;
import android.os.IBinder ;
import android.view.Surface ;
import android.view.Surface ;
import java.lang.reflect.InvocationTargetException ;
import java.lang.reflect.Method ;
@SuppressLint ( "PrivateApi" )
@SuppressLint ( "PrivateApi" )
public final class SurfaceControl {
public final class SurfaceControl {
@ -23,6 +28,9 @@ public final class SurfaceControl {
}
}
}
}
private static Method getBuiltInDisplayMethod ;
private static Method setDisplayPowerModeMethod ;
private SurfaceControl ( ) {
private SurfaceControl ( ) {
// only static methods
// only static methods
}
}
@ -76,24 +84,56 @@ public final class SurfaceControl {
}
}
}
}
private static Method getGetBuiltInDisplayMethod ( ) {
if ( getBuiltInDisplayMethod = = null ) {
try {
// the method signature has changed in Android Q
// <https://github.com/Genymobile/scrcpy/issues/586>
if ( Build . VERSION . SDK_INT < Build . VERSION_CODES . Q ) {
getBuiltInDisplayMethod = CLASS . getMethod ( "getBuiltInDisplay" , int . class ) ;
} else {
getBuiltInDisplayMethod = CLASS . getMethod ( "getPhysicalDisplayToken" , long . class ) ;
}
} catch ( NoSuchMethodException e ) {
Ln . e ( "Could not find method" , e ) ;
}
}
return getBuiltInDisplayMethod ;
}
public static IBinder getBuiltInDisplay ( int builtInDisplayId ) {
public static IBinder getBuiltInDisplay ( int builtInDisplayId ) {
Method method = getGetBuiltInDisplayMethod ( ) ;
if ( method = = null ) {
return null ;
}
try {
try {
// the method signature has changed in Android Q
return ( IBinder ) method . invoke ( null , builtInDisplayId ) ;
// <https://github.com/Genymobile/scrcpy/issues/586>
} catch ( InvocationTargetException | IllegalAccessException e ) {
if ( Build . VERSION . SDK_INT < Build . VERSION_CODES . Q ) {
Ln . e ( "Could not invoke " + method . getName ( ) , e ) ;
return ( IBinder ) CLASS . getMethod ( "getBuiltInDisplay" , int . class ) . invoke ( null , builtInDisplayId ) ;
return null ;
}
}
private static Method getSetDisplayPowerModeMethod ( ) {
if ( setDisplayPowerModeMethod = = null ) {
try {
setDisplayPowerModeMethod = CLASS . getMethod ( "setDisplayPowerMode" , IBinder . class , int . class ) ;
} catch ( NoSuchMethodException e ) {
Ln . e ( "Could not find method" , e ) ;
}
}
return ( IBinder ) CLASS . getMethod ( "getPhysicalDisplayToken" , long . class ) . invoke ( null , builtInDisplayId ) ;
} catch ( Exception e ) {
throw new AssertionError ( e ) ;
}
}
return setDisplayPowerModeMethod ;
}
}
public static void setDisplayPowerMode ( IBinder displayToken , int mode ) {
public static void setDisplayPowerMode ( IBinder displayToken , int mode ) {
Method method = getSetDisplayPowerModeMethod ( ) ;
if ( method = = null ) {
return ;
}
try {
try {
CLASS . getMethod ( "setDisplayPowerMode" , IBinder . class , int . class ) . invoke ( null , displayToken , mode ) ;
method . invoke ( null , displayToken , mode ) ;
} catch ( Exception e ) {
} catch ( InvocationTargetException | IllegalAccess Exception e ) {
throw new AssertionError ( e ) ;
Ln . e ( "Could not invoke " + method . getName ( ) , e ) ;
}
}
}
}