From 4b9164f3ae1290894a10ac3f437be135e68647dc Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Wed, 27 Dec 2023 10:22:09 -0800 Subject: [PATCH] feat: get default display via displaymanager --- .../appium/uiautomator2/core/UiAutomatorBridge.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/src/main/java/io/appium/uiautomator2/core/UiAutomatorBridge.java b/app/src/main/java/io/appium/uiautomator2/core/UiAutomatorBridge.java index 9581ed837..2d71be1f5 100644 --- a/app/src/main/java/io/appium/uiautomator2/core/UiAutomatorBridge.java +++ b/app/src/main/java/io/appium/uiautomator2/core/UiAutomatorBridge.java @@ -15,7 +15,11 @@ */ package io.appium.uiautomator2.core; +import android.app.Instrumentation; +import android.app.Service; import android.app.UiAutomation; +import android.hardware.display.DisplayManager; +import android.os.Build; import android.view.Display; import android.view.accessibility.AccessibilityNodeInfo; @@ -54,6 +58,14 @@ public UiAutomation getUiAutomation() { } public Display getDefaultDisplay() throws UiAutomator2Exception { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + Instrumentation instrumentation = (Instrumentation) invoke(getMethod(UiDevice.class, "getInstrumentation"), Device.getUiDevice()); + DisplayManager displayManager = (DisplayManager) instrumentation.getContext().getSystemService(Service.DISPLAY_SERVICE); + return displayManager.getDisplay(Display.DEFAULT_DISPLAY); + } + + // "getDefaultDisplay" called inside the UiDevice calls + // https://developer.android.com/reference/android/view/WindowManager#getDefaultDisplay() return (Display) invoke(getMethod(UiDevice.class, "getDefaultDisplay"), Device.getUiDevice()); } }