From 65e2eff2b03be29ad87dec11f4ee8403a072ae31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Sua=CC=81rez=20Ruiz?= Date: Wed, 16 Sep 2020 10:58:14 +0200 Subject: [PATCH 1/2] Changes in the logic to detect Device.Idiom on Android --- Xamarin.Forms.Platform.Android/Forms.cs | 34 +++++++++++++++++++------ 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/Xamarin.Forms.Platform.Android/Forms.cs b/Xamarin.Forms.Platform.Android/Forms.cs index db4e72648d2..563bb534faa 100644 --- a/Xamarin.Forms.Platform.Android/Forms.cs +++ b/Xamarin.Forms.Platform.Android/Forms.cs @@ -366,7 +366,7 @@ static void SetupInit( var currentIdiom = TargetIdiom.Unsupported; - // first try UIModeManager + // First try UIModeManager using (var uiModeManager = UiModeManager.FromContext(ApplicationContext)) { try @@ -380,11 +380,29 @@ static void SetupInit( } } + // Then try Configuration if (TargetIdiom.Unsupported == currentIdiom) { - // This could change as a result of a config change, so we need to check it every time - int minWidthDp = activity.Resources.Configuration.SmallestScreenWidthDp; - Device.SetIdiom(minWidthDp >= TabletCrossover ? TargetIdiom.Tablet : TargetIdiom.Phone); + var configuration = activity.Resources.Configuration; + + if (configuration != null) + { + var minWidth = configuration.SmallestScreenWidthDp; + var isWide = minWidth >= TabletCrossover; + currentIdiom = isWide ? TargetIdiom.Tablet : TargetIdiom.Phone; + } + else + { + // Start clutching at straws + var metrics = activity.Resources?.DisplayMetrics; + + if (metrics != null) + { + var minSize = Math.Min(metrics.WidthPixels, metrics.HeightPixels); + var isWide = minSize * metrics.Density >= TabletCrossover; + currentIdiom = isWide ? TargetIdiom.Tablet : TargetIdiom.Phone; + } + } } if (SdkInt >= BuildVersionCodes.JellyBeanMr1) @@ -400,13 +418,13 @@ static void SetupInit( static TargetIdiom DetectIdiom(UiMode uiMode) { var returnValue = TargetIdiom.Unsupported; - if (uiMode.HasFlag(UiMode.TypeNormal)) + if (uiMode == UiMode.TypeNormal) returnValue = TargetIdiom.Unsupported; - else if (uiMode.HasFlag(UiMode.TypeTelevision)) + else if (uiMode == UiMode.TypeTelevision) returnValue = TargetIdiom.TV; - else if (uiMode.HasFlag(UiMode.TypeDesk)) + else if (uiMode == UiMode.TypeDesk) returnValue = TargetIdiom.Desktop; - else if (SdkInt >= BuildVersionCodes.KitkatWatch && uiMode.HasFlag(UiMode.TypeWatch)) + else if (SdkInt >= BuildVersionCodes.KitkatWatch && uiMode == UiMode.TypeWatch) returnValue = TargetIdiom.Watch; Device.SetIdiom(returnValue); From 9049eb8c36ee085ce714ebd82c683b284964b448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Sua=CC=81rez=20Ruiz?= Date: Fri, 6 Nov 2020 16:19:06 +0100 Subject: [PATCH 2/2] Set the correct idiom --- Xamarin.Forms.Platform.Android/Forms.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Xamarin.Forms.Platform.Android/Forms.cs b/Xamarin.Forms.Platform.Android/Forms.cs index 563bb534faa..d355fd2bd00 100644 --- a/Xamarin.Forms.Platform.Android/Forms.cs +++ b/Xamarin.Forms.Platform.Android/Forms.cs @@ -405,6 +405,8 @@ static void SetupInit( } } + Device.SetIdiom(currentIdiom); + if (SdkInt >= BuildVersionCodes.JellyBeanMr1) Device.SetFlowDirection(activity.Resources.Configuration.LayoutDirection.ToFlowDirection());