From 10aa0a61d09b5b61d13897759c8a8cf9b8bf1619 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Mon, 24 Apr 2023 20:27:40 +0200 Subject: [PATCH] fix(android)!: Fix AnalyticsInfo.DeviceForm returning mobile instead of tablet --- .../System/Profile/AnalyticsInfo.Android.cs | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/Uno.UWP/System/Profile/AnalyticsInfo.Android.cs b/src/Uno.UWP/System/Profile/AnalyticsInfo.Android.cs index ca46afa6d0df..3d39b2b0edeb 100644 --- a/src/Uno.UWP/System/Profile/AnalyticsInfo.Android.cs +++ b/src/Uno.UWP/System/Profile/AnalyticsInfo.Android.cs @@ -43,14 +43,14 @@ private static UnoDeviceForm GetDeviceForm() return UnoDeviceForm.Desktop; } - if (SafeFormCheck(IsPhone)) + if (SafeFormCheck(IsTablet)) { - return UnoDeviceForm.Mobile; + return UnoDeviceForm.Tablet; } - if (SafeFormCheck(IsTablet)) + if (SafeFormCheck(IsPhone)) { - return UnoDeviceForm.Tablet; + return UnoDeviceForm.Mobile; } // If nothing is returned, we cannot determine the family. @@ -101,12 +101,28 @@ private static bool IsPhone() => private static bool IsTablet() { - var displayMetrics = Application.Context.Resources.DisplayMetrics; - var screenWidth = displayMetrics.WidthPixels / displayMetrics.Xdpi; - var screenHeight = displayMetrics.HeightPixels / displayMetrics.Ydpi; - var size = Math.Sqrt(Math.Pow(screenWidth, 2) + - Math.Pow(screenHeight, 2)); - //assume tablets have at least 6-inch diagonal - return size >= 6; + // https://github.com/dotnet/maui/blob/1bbe79de61f241217f88207b1272179ff66e6733/src/Essentials/src/DeviceInfo/DeviceInfo.android.cs#L59-L75 + const int tabletCrossover = 600; + + if (Application.Context.Resources is not { } resources) + { + return false; + } + + var configuration = resources.Configuration; + if (configuration != null) + { + return configuration.SmallestScreenWidthDp >= tabletCrossover; + } + + // start clutching at straws + using var metrics = resources.DisplayMetrics; + if (metrics != null) + { + var minSize = Math.Min(metrics.WidthPixels, metrics.HeightPixels); + return minSize * metrics.Density >= tabletCrossover; + } + + return false; } }