Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Updated the logic to detect the Idiom on Android (based on Xamarin.Es…
Browse files Browse the repository at this point in the history
…sentials)
  • Loading branch information
jsuarezruiz committed Sep 16, 2020
1 parent 536ea6e commit 005dd9f
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions Xamarin.Forms.Platform.Android/Forms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ static void SetupInit(

var currentIdiom = TargetIdiom.Unsupported;

// first try UIModeManager
// First try UIModeManager
using (var uiModeManager = UiModeManager.FromContext(ApplicationContext))
{
try
Expand All @@ -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 = ApplicationContext.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 = ApplicationContext.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)
Expand All @@ -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);
Expand Down

0 comments on commit 005dd9f

Please sign in to comment.