/// <summary>
        /// This is the click handler for the 'ProfileLocalUsageDataButton' button.  You would replace this with your own handler
        /// if you have a button or buttons on this page.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProfileLocalUsageData_Click(object sender, RoutedEventArgs e)
        {
            //
            //Get Internet Connection Profile and display local data usage for the profile for the past 1 hour
            //

            try
            {
                Granularity = ParseDataUsageGranularity(((ComboBoxItem)GranularityComboBox.SelectedItem).Content.ToString());
                NetworkUsageStates.Roaming = ParseTriStates(((ComboBoxItem)RoamingComboBox.SelectedItem).Content.ToString());
                NetworkUsageStates.Shared  = ParseTriStates(((ComboBoxItem)SharedComboBox.SelectedItem).Content.ToString());
                StartTime = (StartDatePicker.Date.Date + StartTimePicker.Time);
                EndTime   = (EndDatePicker.Date.Date + EndTimePicker.Time);

                if (InternetConnectionProfile == null)
                {
                    rootPage.NotifyUser("Not connected to Internet\n", NotifyType.StatusMessage);
                }
                else
                {
                    InternetConnectionProfile.GetNetworkUsageAsync(StartTime,
                                                                   EndTime, Granularity, NetworkUsageStates).Completed = GetNetworkUsageAsyncHandler;
                }
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser("Unexpected exception occurred: " + ex.ToString(), NotifyType.ErrorMessage);
            }
        }
        public static async Task <ConnectionInfo> FromConnectionProfile(ConnectionProfile profile)
        {
            var connectionInfo = new ConnectionInfo
            {
                Name              = profile.ProfileName,
                IsWlan            = profile.IsWlanConnectionProfile,
                IsWwan            = profile.IsWwanConnectionProfile,
                ConnectivityLevel =
                    profile.GetNetworkConnectivityLevel().ToString(),
                DomainConnectivityLevel =
                    profile.GetDomainConnectivityLevel().ToString()
            };

            var costType = profile.GetConnectionCost();

            connectionInfo.CostType = costType.NetworkCostType.ToString();
            connectionInfo.Flags    = string.Format(
                "{0} {1} {2}",
                costType.ApproachingDataLimit ? "Approaching Data Limit" : string.Empty,
                costType.OverDataLimit ? "Over Data Limit" : string.Empty,
                costType.Roaming ? "Roaming" : string.Empty).Trim();

            connectionInfo.NetworkAdapterId = profile.ServiceProviderGuid;

            if (profile.NetworkAdapter != null)
            {
                connectionInfo.IncomingBitsPerSecond = (long)profile.NetworkAdapter.InboundMaxBitsPerSecond;
                connectionInfo.OutgoingBitsPerSecond = (long)profile.NetworkAdapter.OutboundMaxBitsPerSecond;
                connectionInfo.NetworkType           = profile.NetworkAdapter.NetworkItem.GetNetworkTypes().ToString();
            }

            if (profile.NetworkSecuritySettings != null)
            {
                connectionInfo.AuthenticationType = profile.NetworkSecuritySettings.NetworkAuthenticationType.ToString();
                connectionInfo.EncryptionType     = profile.NetworkSecuritySettings.NetworkEncryptionType.ToString();
            }

            connectionInfo.SignalBars = profile.GetSignalBars();

            connectionInfo.DataPlan = DataPlanInfo.FromProfile(profile);

            var usage =
                await
                profile.GetNetworkUsageAsync(
                    DateTimeOffset.Now.AddDays(-1),
                    DateTimeOffset.Now,
                    DataUsageGranularity.Total,
                    new NetworkUsageStates { Roaming = TriStates.DoNotCare, Shared = TriStates.DoNotCare });

            if (usage != null && usage.Count > 0)
            {
                connectionInfo.BytesReceivedLastDay = usage[0].BytesReceived;
                connectionInfo.BytesSentLastDay     = usage[0].BytesSent;
            }

            return(connectionInfo);
        }
        async private void GetConnectivityIntervalsAsyncHandler(IAsyncOperation <IReadOnlyList <ConnectivityInterval> > asyncInfo, AsyncStatus asyncStatus)
        {
            if (asyncStatus == AsyncStatus.Completed)
            {
                try
                {
                    String outputString = string.Empty;
                    IReadOnlyList <ConnectivityInterval> connectivityIntervals = asyncInfo.GetResults();

                    if (connectivityIntervals == null)
                    {
                        rootPage.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            rootPage.NotifyUser("The Start Time cannot be later than the End Time, or in the future", NotifyType.StatusMessage);
                        });
                        return;
                    }

                    // Get the NetworkUsage for each ConnectivityInterval
                    foreach (ConnectivityInterval connectivityInterval in connectivityIntervals)
                    {
                        outputString += PrintConnectivityInterval(connectivityInterval);

                        DateTimeOffset startTime = connectivityInterval.StartTime;
                        DateTimeOffset endTime   = startTime + connectivityInterval.ConnectionDuration;
                        IReadOnlyList <NetworkUsage> networkUsages = await InternetConnectionProfile.GetNetworkUsageAsync(startTime, endTime, Granularity, NetworkUsageStates);

                        foreach (NetworkUsage networkUsage in networkUsages)
                        {
                            outputString += PrintNetworkUsage(networkUsage, startTime);
                            startTime    += networkUsage.ConnectionDuration;
                        }
                    }

                    rootPage.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        rootPage.NotifyUser(outputString, NotifyType.StatusMessage);
                    });
                }
                catch (Exception ex)
                {
                    rootPage.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        rootPage.NotifyUser("An unexpected error occurred: " + ex.Message, NotifyType.ErrorMessage);
                    });
                }
            }
            else
            {
                rootPage.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    rootPage.NotifyUser("GetConnectivityIntervalsAsync failed with message:\n" + asyncInfo.ErrorCode.Message, NotifyType.ErrorMessage);
                });
            }
        }
Exemple #4
0
 private async Task <NetworkUsage> GetDataUsegesAsync(DateTime startDate, DateTime endDate, ConnectionProfile internetConnectionProfile)
 {
     try
     {
         NetworkUsageStates NetworkUsageStates = new NetworkUsageStates();
         NetworkUsageStates.Roaming = TriStates.DoNotCare;
         NetworkUsageStates.Shared  = TriStates.DoNotCare;
         return((await internetConnectionProfile.GetNetworkUsageAsync(startDate, endDate, DataUsageGranularity.Total, NetworkUsageStates)).FirstOrDefault());
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
        private static async Task <IReadOnlyList <NetworkUsage> > GetNetworkUsageAsync(ConnectionProfile connectionProfile, DateTime startTime, DateTime endTime, DataUsageGranularity granularity, NetworkUsageStates networkUsageStates)
        {
            try
            {
                DateTimeOffset StartTimeOffset = new DateTimeOffset(startTime);
                DateTimeOffset EndTimeOffset   = new DateTimeOffset(endTime);

                return(await connectionProfile.GetNetworkUsageAsync(StartTimeOffset, EndTimeOffset, granularity, networkUsageStates));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }