-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
On Android 14, the methods to access the display have been moved to DisplayControl, which is not in the core framework. Use a specific ClassLoader to access this class and its native dependencies. Fixes #3927 <#3927> Refs #3927 comment <#3927 (comment)> Refs #4446 comment <#4446 (comment)> PR #4456 <#4456> Co-authored-by: Simon Chan <[email protected]> Signed-off-by: Romain Vimont <[email protected]>
- Loading branch information
Showing
3 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
server/src/main/java/com/genymobile/scrcpy/wrappers/DisplayControl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.genymobile.scrcpy.wrappers; | ||
|
||
import com.genymobile.scrcpy.Ln; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.annotation.TargetApi; | ||
import android.os.Build; | ||
import android.os.IBinder; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
@SuppressLint({"PrivateApi", "SoonBlockedPrivateApi", "BlockedPrivateApi"}) | ||
@TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) | ||
public final class DisplayControl { | ||
|
||
private static final Class<?> CLASS; | ||
|
||
static { | ||
Class<?> displayControlClass = null; | ||
try { | ||
Class<?> classLoaderFactoryClass = Class.forName("com.android.internal.os.ClassLoaderFactory"); | ||
Method createClassLoaderMethod = classLoaderFactoryClass.getDeclaredMethod("createClassLoader", String.class, String.class, String.class, | ||
ClassLoader.class, int.class, boolean.class, String.class); | ||
ClassLoader classLoader = (ClassLoader) createClassLoaderMethod.invoke(null, "/system/framework/services.jar", null, null, | ||
ClassLoader.getSystemClassLoader(), 0, true, null); | ||
|
||
displayControlClass = classLoader.loadClass("com.android.server.display.DisplayControl"); | ||
|
||
Method loadMethod = Runtime.class.getDeclaredMethod("loadLibrary0", Class.class, String.class); | ||
loadMethod.setAccessible(true); | ||
loadMethod.invoke(Runtime.getRuntime(), displayControlClass, "android_servers"); | ||
} catch (Throwable e) { | ||
Ln.e("Could not initialize DisplayControl", e); | ||
// Do not throw an exception here, the methods will fail when they are called | ||
} | ||
CLASS = displayControlClass; | ||
} | ||
|
||
private static Method getPhysicalDisplayTokenMethod; | ||
private static Method getPhysicalDisplayIdsMethod; | ||
|
||
private DisplayControl() { | ||
// only static methods | ||
} | ||
|
||
private static Method getGetPhysicalDisplayTokenMethod() throws NoSuchMethodException { | ||
if (getPhysicalDisplayTokenMethod == null) { | ||
getPhysicalDisplayTokenMethod = CLASS.getMethod("getPhysicalDisplayToken", long.class); | ||
} | ||
return getPhysicalDisplayTokenMethod; | ||
} | ||
|
||
public static IBinder getPhysicalDisplayToken(long physicalDisplayId) { | ||
try { | ||
Method method = getGetPhysicalDisplayTokenMethod(); | ||
return (IBinder) method.invoke(null, physicalDisplayId); | ||
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { | ||
Ln.e("Could not invoke method", e); | ||
return null; | ||
} | ||
} | ||
|
||
private static Method getGetPhysicalDisplayIdsMethod() throws NoSuchMethodException { | ||
if (getPhysicalDisplayIdsMethod == null) { | ||
getPhysicalDisplayIdsMethod = CLASS.getMethod("getPhysicalDisplayIds"); | ||
} | ||
return getPhysicalDisplayIdsMethod; | ||
} | ||
|
||
public static long[] getPhysicalDisplayIds() { | ||
try { | ||
Method method = getGetPhysicalDisplayIdsMethod(); | ||
return (long[]) method.invoke(null); | ||
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { | ||
Ln.e("Could not invoke method", e); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters