From 000349dbef070a49efc09b22dbdaa4279078e03c Mon Sep 17 00:00:00 2001 From: Wenxi Zeng Date: Fri, 12 May 2023 14:46:14 -0700 Subject: [PATCH] fix WindowManager crash on android 11+ (#832) --- .../com/segment/analytics/AnalyticsContext.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/analytics/src/main/java/com/segment/analytics/AnalyticsContext.java b/analytics/src/main/java/com/segment/analytics/AnalyticsContext.java index 598caeda5..527d4b950 100644 --- a/analytics/src/main/java/com/segment/analytics/AnalyticsContext.java +++ b/analytics/src/main/java/com/segment/analytics/AnalyticsContext.java @@ -340,10 +340,17 @@ public AnalyticsContext putReferrer(Referrer referrer) { */ void putScreen(Context context) { Map screen = createMap(); - WindowManager manager = getSystemService(context, Context.WINDOW_SERVICE); - Display display = manager.getDefaultDisplay(); DisplayMetrics displayMetrics = new DisplayMetrics(); - display.getMetrics(displayMetrics); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + context.getDisplay().getRealMetrics(displayMetrics); + } + else { + WindowManager manager = getSystemService(context, Context.WINDOW_SERVICE); + Display display = manager.getDefaultDisplay(); + display.getMetrics(displayMetrics); + } + screen.put(SCREEN_DENSITY_KEY, displayMetrics.density); screen.put(SCREEN_HEIGHT_KEY, displayMetrics.heightPixels); screen.put(SCREEN_WIDTH_KEY, displayMetrics.widthPixels);