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

Commit b00cb31

Browse files
authored
[Android] Changes in the logic to detect Device.Idiom (#11206) fixes #11166 fixes #11177
* Changes in the logic to detect Device.Idiom on Android * Set the correct idiom
1 parent ba53417 commit b00cb31

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

Xamarin.Forms.Platform.Android/Forms.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ static void SetupInit(
377377

378378
var currentIdiom = TargetIdiom.Unsupported;
379379

380-
// first try UIModeManager
380+
// First try UIModeManager
381381
using (var uiModeManager = UiModeManager.FromContext(ApplicationContext))
382382
{
383383
try
@@ -391,13 +391,33 @@ static void SetupInit(
391391
}
392392
}
393393

394+
// Then try Configuration
394395
if (TargetIdiom.Unsupported == currentIdiom)
395396
{
396-
// This could change as a result of a config change, so we need to check it every time
397-
int minWidthDp = activity.Resources.Configuration.SmallestScreenWidthDp;
398-
Device.SetIdiom(minWidthDp >= TabletCrossover ? TargetIdiom.Tablet : TargetIdiom.Phone);
397+
var configuration = activity.Resources.Configuration;
398+
399+
if (configuration != null)
400+
{
401+
var minWidth = configuration.SmallestScreenWidthDp;
402+
var isWide = minWidth >= TabletCrossover;
403+
currentIdiom = isWide ? TargetIdiom.Tablet : TargetIdiom.Phone;
404+
}
405+
else
406+
{
407+
// Start clutching at straws
408+
var metrics = activity.Resources?.DisplayMetrics;
409+
410+
if (metrics != null)
411+
{
412+
var minSize = Math.Min(metrics.WidthPixels, metrics.HeightPixels);
413+
var isWide = minSize * metrics.Density >= TabletCrossover;
414+
currentIdiom = isWide ? TargetIdiom.Tablet : TargetIdiom.Phone;
415+
}
416+
}
399417
}
400418

419+
Device.SetIdiom(currentIdiom);
420+
401421
if (SdkInt >= BuildVersionCodes.JellyBeanMr1)
402422
Device.SetFlowDirection(activity.Resources.Configuration.LayoutDirection.ToFlowDirection());
403423

@@ -411,13 +431,13 @@ static void SetupInit(
411431
static TargetIdiom DetectIdiom(UiMode uiMode)
412432
{
413433
var returnValue = TargetIdiom.Unsupported;
414-
if (uiMode.HasFlag(UiMode.TypeNormal))
434+
if (uiMode == UiMode.TypeNormal)
415435
returnValue = TargetIdiom.Unsupported;
416-
else if (uiMode.HasFlag(UiMode.TypeTelevision))
436+
else if (uiMode == UiMode.TypeTelevision)
417437
returnValue = TargetIdiom.TV;
418-
else if (uiMode.HasFlag(UiMode.TypeDesk))
438+
else if (uiMode == UiMode.TypeDesk)
419439
returnValue = TargetIdiom.Desktop;
420-
else if (SdkInt >= BuildVersionCodes.KitkatWatch && uiMode.HasFlag(UiMode.TypeWatch))
440+
else if (SdkInt >= BuildVersionCodes.KitkatWatch && uiMode == UiMode.TypeWatch)
421441
returnValue = TargetIdiom.Watch;
422442

423443
Device.SetIdiom(returnValue);

0 commit comments

Comments
 (0)