From d73fb42418566a45e4bd3fc0e425bb2b382c38c0 Mon Sep 17 00:00:00 2001 From: James Montemagno Date: Tue, 18 Feb 2020 18:02:29 -0800 Subject: [PATCH] Cache device type and name (#1087) Co-authored-by: Jonathan Dick --- .../DeviceInfo/DeviceInfo.uwp.cs | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/Xamarin.Essentials/DeviceInfo/DeviceInfo.uwp.cs b/Xamarin.Essentials/DeviceInfo/DeviceInfo.uwp.cs index 369dda8f3..503d42278 100644 --- a/Xamarin.Essentials/DeviceInfo/DeviceInfo.uwp.cs +++ b/Xamarin.Essentials/DeviceInfo/DeviceInfo.uwp.cs @@ -10,11 +10,21 @@ public static partial class DeviceInfo { static readonly EasClientDeviceInformation deviceInfo; static DeviceIdiom currentIdiom; + static DeviceType currentType = DeviceType.Unknown; + static string systemProductName; static DeviceInfo() { deviceInfo = new EasClientDeviceInformation(); currentIdiom = DeviceIdiom.Unknown; + try + { + systemProductName = deviceInfo.SystemProductName; + } + catch (Exception ex) + { + Debug.WriteLine($"Unable to get system product name. {ex.Message}"); + } } static string GetModel() => deviceInfo.SystemProductName; @@ -77,12 +87,23 @@ static DeviceIdiom GetIdiom() static DeviceType GetDeviceType() { - var isVirtual = deviceInfo.SystemProductName.Contains("Virtual") || deviceInfo.SystemProductName == "HMV domU"; + if (currentType != DeviceType.Unknown) + return currentType; - if (isVirtual) - return DeviceType.Virtual; + try + { + if (string.IsNullOrWhiteSpace(systemProductName)) + systemProductName = deviceInfo.SystemProductName; - return DeviceType.Physical; + var isVirtual = systemProductName.Contains("Virtual") || systemProductName == "HMV domU"; + + currentType = isVirtual ? DeviceType.Virtual : DeviceType.Physical; + } + catch (Exception ex) + { + Debug.WriteLine($"Unable to get device type. {ex.Message}"); + } + return currentType; } } }