@@ -377,7 +377,7 @@ static void SetupInit(
377
377
378
378
var currentIdiom = TargetIdiom . Unsupported ;
379
379
380
- // first try UIModeManager
380
+ // First try UIModeManager
381
381
using ( var uiModeManager = UiModeManager . FromContext ( ApplicationContext ) )
382
382
{
383
383
try
@@ -391,13 +391,33 @@ static void SetupInit(
391
391
}
392
392
}
393
393
394
+ // Then try Configuration
394
395
if ( TargetIdiom . Unsupported == currentIdiom )
395
396
{
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
+ }
399
417
}
400
418
419
+ Device . SetIdiom ( currentIdiom ) ;
420
+
401
421
if ( SdkInt >= BuildVersionCodes . JellyBeanMr1 )
402
422
Device . SetFlowDirection ( activity . Resources . Configuration . LayoutDirection . ToFlowDirection ( ) ) ;
403
423
@@ -411,13 +431,13 @@ static void SetupInit(
411
431
static TargetIdiom DetectIdiom ( UiMode uiMode )
412
432
{
413
433
var returnValue = TargetIdiom . Unsupported ;
414
- if ( uiMode . HasFlag ( UiMode . TypeNormal ) )
434
+ if ( uiMode == UiMode . TypeNormal )
415
435
returnValue = TargetIdiom . Unsupported ;
416
- else if ( uiMode . HasFlag ( UiMode . TypeTelevision ) )
436
+ else if ( uiMode == UiMode . TypeTelevision )
417
437
returnValue = TargetIdiom . TV ;
418
- else if ( uiMode . HasFlag ( UiMode . TypeDesk ) )
438
+ else if ( uiMode == UiMode . TypeDesk )
419
439
returnValue = TargetIdiom . Desktop ;
420
- else if ( SdkInt >= BuildVersionCodes . KitkatWatch && uiMode . HasFlag ( UiMode . TypeWatch ) )
440
+ else if ( SdkInt >= BuildVersionCodes . KitkatWatch && uiMode == UiMode . TypeWatch )
421
441
returnValue = TargetIdiom . Watch ;
422
442
423
443
Device . SetIdiom ( returnValue ) ;
0 commit comments