From c0f3c080b63eb70c0941c300e461fc2ea1246cec Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 22 Jun 2023 00:49:11 +0200 Subject: [PATCH] Register DisplayFoldListener only for Android 10+ This listener does not exist on Android < 10, and it makes scrcpy fail. --- .../java/com/genymobile/scrcpy/Device.java | 34 ++++++++++--------- .../scrcpy/wrappers/WindowManager.java | 2 ++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/Device.java b/server/src/main/java/com/genymobile/scrcpy/Device.java index a3b6a270..f817a3ce 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Device.java +++ b/server/src/main/java/com/genymobile/scrcpy/Device.java @@ -99,25 +99,27 @@ public final class Device { } }, displayId); - ServiceManager.getWindowManager().registerDisplayFoldListener(new IDisplayFoldListener.Stub() { - @Override - public void onDisplayFoldChanged(int displayId, boolean folded) { - synchronized (Device.this) { - DisplayInfo displayInfo = ServiceManager.getDisplayManager().getDisplayInfo(displayId); - if (displayInfo == null) { - Ln.e("Display " + displayId + " not found\n" + LogUtils.buildDisplayListMessage()); - return; - } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + ServiceManager.getWindowManager().registerDisplayFoldListener(new IDisplayFoldListener.Stub() { + @Override + public void onDisplayFoldChanged(int displayId, boolean folded) { + synchronized (Device.this) { + DisplayInfo displayInfo = ServiceManager.getDisplayManager().getDisplayInfo(displayId); + if (displayInfo == null) { + Ln.e("Display " + displayId + " not found\n" + LogUtils.buildDisplayListMessage()); + return; + } - screenInfo = ScreenInfo.computeScreenInfo(displayInfo.getRotation(), displayInfo.getSize(), options.getCrop(), - options.getMaxSize(), options.getLockVideoOrientation()); - // notify - if (foldListener != null) { - foldListener.onFoldChanged(displayId, folded); + screenInfo = ScreenInfo.computeScreenInfo(displayInfo.getRotation(), displayInfo.getSize(), options.getCrop(), + options.getMaxSize(), options.getLockVideoOrientation()); + // notify + if (foldListener != null) { + foldListener.onFoldChanged(displayId, folded); + } } } - } - }); + }); + } if (options.getControl() && options.getClipboardAutosync()) { // If control and autosync are enabled, synchronize Android clipboard to the computer automatically diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/WindowManager.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/WindowManager.java index d9fd9825..dde26e82 100644 --- a/server/src/main/java/com/genymobile/scrcpy/wrappers/WindowManager.java +++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/WindowManager.java @@ -2,6 +2,7 @@ package com.genymobile.scrcpy.wrappers; import com.genymobile.scrcpy.Ln; +import android.annotation.TargetApi; import android.os.IInterface; import android.view.IRotationWatcher; import android.view.IDisplayFoldListener; @@ -110,6 +111,7 @@ public final class WindowManager { } } + @TargetApi(29) public void registerDisplayFoldListener(IDisplayFoldListener foldListener) { try { Class cls = manager.getClass();