Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android)!: Fix AnalyticsInfo.DeviceForm returning mobile instead of tablet #12058

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions src/Uno.UWP/System/Profile/AnalyticsInfo.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved
}

// If nothing is returned, we cannot determine the family.
Expand Down Expand Up @@ -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;
}
}