From 673a3c0205005858eaf27610ea5564312876b5a9 Mon Sep 17 00:00:00 2001 From: Vincent Date: Fri, 20 Mar 2020 22:47:57 +0100 Subject: [PATCH] Change SystemInformation from static to regular class (#3168) * change system information to instance class * add singleton property * seal class --- Microsoft.Toolkit.Uwp.SampleApp/App.xaml.cs | 2 +- .../SystemInformationCode.bind | 42 ++++---- .../SystemInformationPage.xaml.cs | 47 +++++---- .../Extensions/Markup/OnDevice.cs | 21 ++-- .../Helpers/SystemInformation.cs | 97 ++++++++++--------- 5 files changed, 101 insertions(+), 108 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/App.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/App.xaml.cs index 69d8bcea8bb..ff8bb4709d2 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/App.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/App.xaml.cs @@ -76,7 +76,7 @@ protected override async void OnLaunched(LaunchActivatedEventArgs e) await RunAppInitialization(e?.Arguments); } - SystemInformation.TrackAppUse(e); + SystemInformation.Instance.TrackAppUse(e); } /// diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/SystemInformation/SystemInformationCode.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/SystemInformation/SystemInformationCode.bind index f90f32577ef..7fc9a563dcb 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/SystemInformation/SystemInformationCode.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/SystemInformation/SystemInformationCode.bind @@ -1,59 +1,59 @@ // To get application's name: - public string ApplicationName => SystemInformation.ApplicationName; + public string ApplicationName => SystemInformation.Instance.ApplicationName; // To get application's version: - public string ApplicationVersion => $"{SystemInformation.ApplicationVersion.Major}.{SystemInformation.ApplicationVersion.Minor}.{SystemInformation.ApplicationVersion.Build}.{SystemInformation.ApplicationVersion.Revision}"; + public string ApplicationVersion => $"{SystemInformation.Instance.ApplicationVersion.Major}.{SystemInformation.Instance.ApplicationVersion.Minor}.{SystemInformation.Instance.ApplicationVersion.Build}.{SystemInformation.Instance.ApplicationVersion.Revision}"; // To get the most preferred language by the user: - public CultureInfo Culture => SystemInformation.Culture; + public CultureInfo Culture => SystemInformation.Instance.Culture; - // To get operating syste, - public string OperatingSystem => SystemInformation.OperatingSystem; + // To get operating system + public string OperatingSystem => SystemInformation.Instance.OperatingSystem; // To get used processor architecture - public ProcessorArchitecture OperatingSystemArchitecture => SystemInformation.OperatingSystemArchitecture; + public ProcessorArchitecture OperatingSystemArchitecture => SystemInformation.Instance.OperatingSystemArchitecture; // To get operating system version - public OSVersion OperatingSystemVersion => SystemInformation.OperatingSystemVersion; + public OSVersion OperatingSystemVersion => SystemInformation.Instance.OperatingSystemVersion; // To get device family - public string DeviceFamily => SystemInformation.DeviceFamily; + public string DeviceFamily => SystemInformation.Instance.DeviceFamily; // To get device model - public string DeviceModel => SystemInformation.DeviceModel; + public string DeviceModel => SystemInformation.Instance.DeviceModel; // To get device manufacturer - public string DeviceManufacturer => SystemInformation.DeviceManufacturer; + public string DeviceManufacturer => SystemInformation.Instance.DeviceManufacturer; // To get available memory in MB - public float AvailableMemory => SystemInformation.AvailableMemory; + public float AvailableMemory => SystemInformation.Instance.AvailableMemory; // To get if the app is being used for the first time since it was installed. - public bool IsFirstUse => SystemInformation.IsFirstRun; + public bool IsFirstUse => SystemInformation.Instance.IsFirstRun; // To get if the app is being used for the first time since being upgraded from an older version. - public bool IsAppUpdated => SystemInformation.IsAppUpdated; + public bool IsAppUpdated => SystemInformation.Instance.IsAppUpdated; // To get the first version installed - public PackageVersion FirstVersionInstalled => SystemInformation.FirstVersionInstalled; + public PackageVersion FirstVersionInstalled => SystemInformation.Instance.FirstVersionInstalled; // To get the first time the app was launched - public DateTime FirstUseTime => SystemInformation.FirstUseTime; + public DateTime FirstUseTime => SystemInformation.Instance.FirstUseTime; // To get the time the app was launched. - public DateTime LaunchTime => SystemInformation.LaunchTime; + public DateTime LaunchTime => SystemInformation.Instance.LaunchTime; // To get the time the app was previously launched, not including this instance. - public DateTime LastLaunchTime => SystemInformation.LastLaunchTime; + public DateTime LastLaunchTime => SystemInformation.Instance.LastLaunchTime; // To get the time the launch count was reset, not including this instance - public string LastResetTime => SystemInformation.LastResetTime.ToString(Culture.DateTimeFormat); + public string LastResetTime => SystemInformation.Instance.LastResetTime.ToString(Culture.DateTimeFormat); // To get the number of times the app has been launched sicne the last reset. - public long LaunchCount => SystemInformation.LaunchCount; + public long LaunchCount => SystemInformation.Instance.LaunchCount; // To get the number of times the app has been launched. - public long TotalLaunchCount => SystemInformation.TotalLaunchCount; + public long TotalLaunchCount => SystemInformation.Instance.TotalLaunchCount; // To get how long the app has been running - public TimeSpan AppUptime => SystemInformation.AppUptime; + public TimeSpan AppUptime => SystemInformation.Instance.AppUptime; diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/SystemInformation/SystemInformationPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/SystemInformation/SystemInformationPage.xaml.cs index 59d86463202..f6c68407e71 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/SystemInformation/SystemInformationPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/SystemInformation/SystemInformationPage.xaml.cs @@ -2,14 +2,11 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.ComponentModel; using System.Globalization; -using System.Runtime.CompilerServices; using Microsoft.Toolkit.Uwp.Helpers; using Windows.System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Navigation; namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages { @@ -19,52 +16,52 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages public sealed partial class SystemInformationPage : Page { // To get application's name: - public string ApplicationName => SystemInformation.ApplicationName; + public string ApplicationName => SystemInformation.Instance.ApplicationName; // To get application's version: - public string ApplicationVersion => SystemInformation.ApplicationVersion.ToFormattedString(); + public string ApplicationVersion => SystemInformation.Instance.ApplicationVersion.ToFormattedString(); // To get the most preferred language by the user: - public CultureInfo Culture => SystemInformation.Culture; + public CultureInfo Culture => SystemInformation.Instance.Culture; - // To get operating syste, - public string OperatingSystem => SystemInformation.OperatingSystem; + // To get operating system + public string OperatingSystem => SystemInformation.Instance.OperatingSystem; // To get used processor architecture - public ProcessorArchitecture OperatingSystemArchitecture => SystemInformation.OperatingSystemArchitecture; + public ProcessorArchitecture OperatingSystemArchitecture => SystemInformation.Instance.OperatingSystemArchitecture; // To get operating system version - public OSVersion OperatingSystemVersion => SystemInformation.OperatingSystemVersion; + public OSVersion OperatingSystemVersion => SystemInformation.Instance.OperatingSystemVersion; // To get device family - public string DeviceFamily => SystemInformation.DeviceFamily; + public string DeviceFamily => SystemInformation.Instance.DeviceFamily; // To get device model - public string DeviceModel => SystemInformation.DeviceModel; + public string DeviceModel => SystemInformation.Instance.DeviceModel; // To get device manufacturer - public string DeviceManufacturer => SystemInformation.DeviceManufacturer; + public string DeviceManufacturer => SystemInformation.Instance.DeviceManufacturer; // To get available memory in MB - public float AvailableMemory => SystemInformation.AvailableMemory; + public float AvailableMemory => SystemInformation.Instance.AvailableMemory; // To get if the app is being used for the first time since it was installed. - public string IsFirstUse => SystemInformation.IsFirstRun.ToString(); + public string IsFirstUse => SystemInformation.Instance.IsFirstRun.ToString(); // To get if the app is being used for the first time since being upgraded from an older version. - public string IsAppUpdated => SystemInformation.IsAppUpdated.ToString(); + public string IsAppUpdated => SystemInformation.Instance.IsAppUpdated.ToString(); // To get the first version installed - public string FirstVersionInstalled => SystemInformation.FirstVersionInstalled.ToFormattedString(); + public string FirstVersionInstalled => SystemInformation.Instance.FirstVersionInstalled.ToFormattedString(); // To get the first time the app was launched - public string FirstUseTime => SystemInformation.FirstUseTime.ToString(Culture.DateTimeFormat); + public string FirstUseTime => SystemInformation.Instance.FirstUseTime.ToString(Culture.DateTimeFormat); // To get the time the app was launched - public string LaunchTime => SystemInformation.LaunchTime.ToString(Culture.DateTimeFormat); + public string LaunchTime => SystemInformation.Instance.LaunchTime.ToString(Culture.DateTimeFormat); // To get the time the app was previously launched, not including this instance - public string LastLaunchTime => SystemInformation.LastLaunchTime.ToString(Culture.DateTimeFormat); + public string LastLaunchTime => SystemInformation.Instance.LastLaunchTime.ToString(Culture.DateTimeFormat); // To get the time the launch count was reset, not including this instance public string LastResetTime @@ -87,10 +84,10 @@ public long LaunchCount DependencyProperty.Register(nameof(LaunchCount), typeof(long), typeof(SystemInformationPage), new PropertyMetadata(0)); // To get the number of times the app has been launched. - public long TotalLaunchCount => SystemInformation.TotalLaunchCount; + public long TotalLaunchCount => SystemInformation.Instance.TotalLaunchCount; // To get how long the app has been running - public string AppUptime => SystemInformation.AppUptime.ToString(); + public string AppUptime => SystemInformation.Instance.AppUptime.ToString(); public SystemInformationPage() { @@ -100,15 +97,15 @@ public SystemInformationPage() SampleController.Current.RegisterNewCommand("Reset launch count", (sender, args) => { - SystemInformation.ResetLaunchCount(); + SystemInformation.Instance.ResetLaunchCount(); RefreshProperties(); }); } private void RefreshProperties() { - LaunchCount = SystemInformation.LaunchCount; - LastResetTime = SystemInformation.LastResetTime.ToString(Culture.DateTimeFormat); + LaunchCount = SystemInformation.Instance.LaunchCount; + LastResetTime = SystemInformation.Instance.LastResetTime.ToString(Culture.DateTimeFormat); } } } \ No newline at end of file diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/OnDevice.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/OnDevice.cs index 21fdf480344..6cf96c4a532 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/OnDevice.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/Markup/OnDevice.cs @@ -2,8 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; -using Windows.ApplicationModel; +using Windows.System.Profile; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Markup; @@ -22,6 +21,11 @@ namespace Microsoft.Toolkit.Uwp.UI.Extensions.Markup [Bindable] public class OnDevice : MarkupExtension { + /// + /// Gets the current device family. + /// + private string DeviceFamily { get; } = AnalyticsInfo.VersionInfo.DeviceFamily; + /// /// Gets or sets the default value for this property /// @@ -58,18 +62,7 @@ public class OnDevice : MarkupExtension /// The object value to set on the property where the extension is applied. protected override object ProvideValue() { - string deviceFamily = null; - if (DesignMode.DesignMode2Enabled) - { - // TODO: detect DeviceFamily in XAML Designer (device dropdown) - // deviceFamily = ??? - } - else - { - deviceFamily = Uwp.Helpers.SystemInformation.DeviceFamily; - } - - switch (deviceFamily) + switch (DeviceFamily) { case "Windows.Desktop": return this.Desktop ?? this.Default; diff --git a/Microsoft.Toolkit.Uwp/Helpers/SystemInformation.cs b/Microsoft.Toolkit.Uwp/Helpers/SystemInformation.cs index dd9399b1465..978aa9a3ef1 100644 --- a/Microsoft.Toolkit.Uwp/Helpers/SystemInformation.cs +++ b/Microsoft.Toolkit.Uwp/Helpers/SystemInformation.cs @@ -18,25 +18,40 @@ namespace Microsoft.Toolkit.Uwp.Helpers /// /// This class provides info about the app and the system. /// - public static class SystemInformation + public sealed class SystemInformation { - private static readonly LocalObjectStorageHelper _localObjectStorageHelper = new LocalObjectStorageHelper(); - private static DateTime _sessionStart; + private readonly LocalObjectStorageHelper _localObjectStorageHelper = new LocalObjectStorageHelper(); + private DateTime _sessionStart; + + /// + /// Launches the store app so the user can leave a review. + /// + /// A representing the asynchronous operation. + /// This method needs to be called from your UI thread. + public static async Task LaunchStoreForReviewAsync() + { + await Launcher.LaunchUriAsync(new Uri(string.Format("ms-windows-store://review/?PFN={0}", Package.Current.Id.FamilyName))); + } + + /// + /// Gets the unique instance of . + /// + public static SystemInformation Instance { get; } = new SystemInformation(); /// /// Gets the application's name. /// - public static string ApplicationName { get; } + public string ApplicationName { get; } /// /// Gets the application's version. /// - public static PackageVersion ApplicationVersion { get; } + public PackageVersion ApplicationVersion { get; } /// /// Gets the user's most preferred culture. /// - public static CultureInfo Culture { get; } + public CultureInfo Culture { get; } /// /// Gets the device's family. @@ -53,98 +68,98 @@ public static class SystemInformation /// /// Prepare your code for other values. /// - public static string DeviceFamily { get; } + public string DeviceFamily { get; } /// /// Gets the operating system's name. /// - public static string OperatingSystem { get; } + public string OperatingSystem { get; } /// /// Gets the operating system's version. /// - public static OSVersion OperatingSystemVersion { get; } + public OSVersion OperatingSystemVersion { get; } /// /// Gets the processor architecture. /// - public static ProcessorArchitecture OperatingSystemArchitecture { get; } + public ProcessorArchitecture OperatingSystemArchitecture { get; } /// /// Gets the available memory. /// - public static float AvailableMemory => (float)MemoryManager.AppMemoryUsageLimit / 1024 / 1024; + public float AvailableMemory => (float)MemoryManager.AppMemoryUsageLimit / 1024 / 1024; /// /// Gets the device's model. /// Will be empty if the model couldn't be determined (For example: when running in a virtual machine). /// - public static string DeviceModel { get; } + public string DeviceModel { get; } /// /// Gets the device's manufacturer. /// Will be empty if the manufacturer couldn't be determined (For example: when running in a virtual machine). /// - public static string DeviceManufacturer { get; } + public string DeviceManufacturer { get; } /// /// Gets a value indicating whether the app is being used for the first time since it was installed. /// Use this to tell if you should do or display something different for the app's first use. /// - public static bool IsFirstRun { get; } + public bool IsFirstRun { get; } /// /// Gets a value indicating whether the app is being used for the first time since being upgraded from an older version. /// Use this to tell if you should display details about what has changed. /// - public static bool IsAppUpdated { get; } + public bool IsAppUpdated { get; } /// /// Gets the first version of the app that was installed. /// This will be the current version if a previous verison of the app was installed before accessing this property. /// - public static PackageVersion FirstVersionInstalled { get; } + public PackageVersion FirstVersionInstalled { get; } /// /// Gets the DateTime (in UTC) when the app was launched for the first time. /// - public static DateTime FirstUseTime { get; } + public DateTime FirstUseTime { get; } /// /// Gets the DateTime (in UTC) when the app was last launched, not including this instance. /// Will be if has not been called yet. /// - public static DateTime LastLaunchTime { get; private set; } + public DateTime LastLaunchTime { get; private set; } /// /// Gets the number of times the app has been launched. /// Will be 0 if has not been called yet. /// - public static long LaunchCount { get; private set; } + public long LaunchCount { get; private set; } /// /// Gets the number of times the app has been launched. /// Will be 0 if has not been called yet. /// - public static long TotalLaunchCount { get; private set; } + public long TotalLaunchCount { get; private set; } /// /// Gets the DateTime (in UTC) that this instance of the app was launched. /// Will be if has not been called yet. /// - public static DateTime LaunchTime { get; private set; } + public DateTime LaunchTime { get; private set; } /// /// Gets the DateTime (in UTC) when the launch count was last reset. /// Will be if has not been called yet. /// - public static DateTime LastResetTime { get; private set; } + public DateTime LastResetTime { get; private set; } /// /// Gets the length of time this instance of the app has been running. /// Will be if has not been called yet. /// - public static TimeSpan AppUptime + public TimeSpan AppUptime { get { @@ -156,10 +171,8 @@ public static TimeSpan AppUptime return new TimeSpan(uptimeSoFar + subsessionLength); } - else - { - return TimeSpan.MinValue; - } + + return TimeSpan.MinValue; } } @@ -167,7 +180,7 @@ public static TimeSpan AppUptime /// Tracks information about the app's launch. /// /// Details about the launch request and process. - public static void TrackAppUse(LaunchActivatedEventArgs args) + public void TrackAppUse(LaunchActivatedEventArgs args) { if (args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser || args.PreviousExecutionState == ApplicationExecutionState.NotRunning) @@ -220,22 +233,12 @@ void App_VisibilityChanged(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.Vi Windows.UI.Core.CoreWindow.GetForCurrentThread().VisibilityChanged += App_VisibilityChanged; } - /// - /// Launches the store app so the user can leave a review. - /// - /// A representing the asynchronous operation. - /// This method needs to be called from your UI thread. - public static async Task LaunchStoreForReviewAsync() - { - await Launcher.LaunchUriAsync(new Uri(string.Format("ms-windows-store://review/?PFN={0}", Package.Current.Id.FamilyName))); - } - /// /// Adds to the record of how long the app has been running. /// Use this to optionally include time spent in background tasks or extended execution. /// /// The amount to time to add - public static void AddToAppUptime(TimeSpan duration) + public void AddToAppUptime(TimeSpan duration) { var uptimeSoFar = _localObjectStorageHelper.Read(nameof(AppUptime)); _localObjectStorageHelper.Save(nameof(AppUptime), uptimeSoFar + duration.Ticks); @@ -244,7 +247,7 @@ public static void AddToAppUptime(TimeSpan duration) /// /// Resets the launch count. /// - public static void ResetLaunchCount() + public void ResetLaunchCount() { LastResetTime = DateTime.UtcNow; LaunchCount = 0; @@ -254,9 +257,9 @@ public static void ResetLaunchCount() } /// - /// Initializes static members of the class. + /// Initializes a new instance of the class. /// - static SystemInformation() + private SystemInformation() { ApplicationName = Package.Current.DisplayName; ApplicationVersion = Package.Current.Id.Version; @@ -290,7 +293,7 @@ static SystemInformation() InitializeValuesSetWithTrackAppUse(); } - private static bool DetectIfFirstUse() + private bool DetectIfFirstUse() { if (_localObjectStorageHelper.KeyExists(nameof(IsFirstRun))) { @@ -303,7 +306,7 @@ private static bool DetectIfFirstUse() } } - private static bool DetectIfAppUpdated() + private bool DetectIfAppUpdated() { var currentVersion = ApplicationVersion.ToFormattedString(); @@ -324,7 +327,7 @@ private static bool DetectIfAppUpdated() return false; } - private static DateTime DetectFirstUseTime() + private DateTime DetectFirstUseTime() { DateTime result; @@ -342,7 +345,7 @@ private static DateTime DetectFirstUseTime() return result; } - private static PackageVersion DetectFirstVersionInstalled() + private PackageVersion DetectFirstVersionInstalled() { PackageVersion result; @@ -359,7 +362,7 @@ private static PackageVersion DetectFirstVersionInstalled() return result; } - private static void InitializeValuesSetWithTrackAppUse() + private void InitializeValuesSetWithTrackAppUse() { LaunchTime = DateTime.MinValue; LaunchCount = 0;