Check screen on for current displayId

Since Android 14, the "screen on" state can be checked per-display.

Refs <956f4084df%5E!/#F17>
PR #5442 <https://github.com/Genymobile/scrcpy/pull/5442>
This commit is contained in:
Romain Vimont 2024-11-03 12:33:33 +01:00
parent 1270997f6b
commit 790ea5e58c
4 changed files with 19 additions and 8 deletions

View File

@ -137,7 +137,7 @@ public final class CleanUp {
} }
} }
if (Device.isScreenOn() && displayId != Device.DISPLAY_ID_NONE) { if (displayId != Device.DISPLAY_ID_NONE && Device.isScreenOn(displayId)) {
if (powerOffScreen) { if (powerOffScreen) {
Ln.i("Power off screen"); Ln.i("Power off screen");
Device.powerOffScreen(displayId); Device.powerOffScreen(displayId);

View File

@ -166,7 +166,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
private void control() throws IOException { private void control() throws IOException {
// on start, power on the device // on start, power on the device
if (powerOn && displayId == 0 && !Device.isScreenOn()) { if (powerOn && displayId == 0 && !Device.isScreenOn(displayId)) {
Device.pressReleaseKeycode(KeyEvent.KEYCODE_POWER, displayId, Device.INJECT_MODE_ASYNC); Device.pressReleaseKeycode(KeyEvent.KEYCODE_POWER, displayId, Device.INJECT_MODE_ASYNC);
// dirty hack // dirty hack
@ -490,7 +490,7 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
} }
private boolean pressBackOrTurnScreenOn(int action) { private boolean pressBackOrTurnScreenOn(int action) {
if (Device.isScreenOn()) { if (displayId == Device.DISPLAY_ID_NONE || Device.isScreenOn(displayId)) {
return injectKeyEvent(action, KeyEvent.KEYCODE_BACK, 0, 0, Device.INJECT_MODE_ASYNC); return injectKeyEvent(action, KeyEvent.KEYCODE_BACK, 0, 0, Device.INJECT_MODE_ASYNC);
} }

View File

@ -82,8 +82,9 @@ public final class Device {
&& injectKeyEvent(KeyEvent.ACTION_UP, keyCode, 0, 0, displayId, injectMode); && injectKeyEvent(KeyEvent.ACTION_UP, keyCode, 0, 0, displayId, injectMode);
} }
public static boolean isScreenOn() { public static boolean isScreenOn(int displayId) {
return ServiceManager.getPowerManager().isScreenOn(); assert displayId != DISPLAY_ID_NONE;
return ServiceManager.getPowerManager().isScreenOn(displayId);
} }
public static void expandNotificationPanel() { public static void expandNotificationPanel() {
@ -181,7 +182,7 @@ public final class Device {
public static boolean powerOffScreen(int displayId) { public static boolean powerOffScreen(int displayId) {
assert displayId != DISPLAY_ID_NONE; assert displayId != DISPLAY_ID_NONE;
if (!isScreenOn()) { if (!isScreenOn(displayId)) {
return true; return true;
} }
return pressReleaseKeycode(KeyEvent.KEYCODE_POWER, displayId, Device.INJECT_MODE_ASYNC); return pressReleaseKeycode(KeyEvent.KEYCODE_POWER, displayId, Device.INJECT_MODE_ASYNC);

View File

@ -1,7 +1,9 @@
package com.genymobile.scrcpy.wrappers; package com.genymobile.scrcpy.wrappers;
import com.genymobile.scrcpy.AndroidVersions;
import com.genymobile.scrcpy.util.Ln; import com.genymobile.scrcpy.util.Ln;
import android.os.Build;
import android.os.IInterface; import android.os.IInterface;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -21,14 +23,22 @@ public final class PowerManager {
private Method getIsScreenOnMethod() throws NoSuchMethodException { private Method getIsScreenOnMethod() throws NoSuchMethodException {
if (isScreenOnMethod == null) { if (isScreenOnMethod == null) {
if (Build.VERSION.SDK_INT >= AndroidVersions.API_34_ANDROID_14) {
isScreenOnMethod = manager.getClass().getMethod("isDisplayInteractive", int.class);
} else {
isScreenOnMethod = manager.getClass().getMethod("isInteractive"); isScreenOnMethod = manager.getClass().getMethod("isInteractive");
} }
}
return isScreenOnMethod; return isScreenOnMethod;
} }
public boolean isScreenOn() { public boolean isScreenOn(int displayId) {
try { try {
Method method = getIsScreenOnMethod(); Method method = getIsScreenOnMethod();
if (Build.VERSION.SDK_INT >= AndroidVersions.API_34_ANDROID_14) {
return (boolean) method.invoke(manager, displayId);
}
return (boolean) method.invoke(manager); return (boolean) method.invoke(manager);
} catch (ReflectiveOperationException e) { } catch (ReflectiveOperationException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);