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

[Android] Changes in the logic to detect Device.Idiom #11206

Merged
merged 2 commits into from
Nov 10, 2020
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
36 changes: 28 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,13 +380,33 @@ 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;
}
}
}

Device.SetIdiom(currentIdiom);

if (SdkInt >= BuildVersionCodes.JellyBeanMr1)
Device.SetFlowDirection(activity.Resources.Configuration.LayoutDirection.ToFlowDirection());

Expand All @@ -400,13 +420,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