Exemplo n.º 1
0
        /// <summary>
        /// Creates secondary tile if it is not yet created or removes the tile if it already exists.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event arguments</param>
        private async void ApplicationBar_PinTile(object sender, RoutedEventArgs e)
        {
            bool removeTile = SecondaryTile.Exists(TILE_ID);

            if (removeTile)
            {
                await RemoveBackgroundTaskAsync("StepTriggered");
            }
            else
            {
                ApiSupportedCapabilities caps = await SenseHelper.GetSupportedCapabilitiesAsync();

                // Use StepCounterUpdate to trigger live tile update if it is supported. Otherwise we use time trigger
                if (caps.StepCounterTrigger)
                {
                    var myTrigger = new DeviceManufacturerNotificationTrigger(SenseTrigger.StepCounterUpdate, false);
                    await RegisterBackgroundTaskAsync(myTrigger, "StepTriggered", "BackgroundTasks.StepTriggerTask");
                }
                else
                {
                    BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();

                    IBackgroundTrigger trigger = new TimeTrigger(15, false);
                    await RegisterBackgroundTaskAsync(trigger, "StepTriggered", "BackgroundTasks.StepTriggerTask");
                }
            }
            await CreateOrRemoveTileAsync(removeTile);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Makes sure necessary settings are enabled in order to use SensorCore
        /// </summary>
        /// <returns>Asynchronous task</returns>
        public static async Task ValidateSettingsAsync()
        {
            if (await StepCounter.IsSupportedAsync())
            {
                // Starting from version 2 of Motion data settings Step counter and Acitivity monitor are always available. In earlier versions system
                // location setting and Motion data had to be enabled.
                MotionDataSettings settings = await SenseHelper.GetSettingsAsync();

                if (settings.Version < 2)
                {
                    if (!settings.LocationEnabled)
                    {
                        MessageDialog dlg = new MessageDialog("In order to count steps you need to enable location in system settings. Do you want to open settings now? If not, application will exit.", "Information");
                        dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchLocationSettingsAsync())));
                        dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) => { Application.Current.Exit(); })));
                        await dlg.ShowAsync();
                    }
                    if (!settings.PlacesVisited)
                    {
                        MessageDialog dlg = new MessageDialog("In order to count steps you need to enable Motion data in Motion data settings. Do you want to open settings now? If not, application will exit.", "Information");
                        dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchSenseSettingsAsync())));
                        dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) => { Application.Current.Exit(); })));
                        await dlg.ShowAsync();
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets number of steps for current day
        /// </summary>
        /// <returns><c>true</c> if steps were successfully fetched, <c>false</c> otherwise</returns>
        private async Task <bool> GetStepsAsync()
        {
            StepCounter stepCounter = null;

            try
            {
                stepCounter = await StepCounter.GetDefaultAsync();

                _steps = await stepCounter.GetStepCountForRangeAsync(
                    DateTime.Now.Date,
                    DateTime.Now - DateTime.Now.Date);
            }
            catch (Exception e)
            {
                _lastError = SenseHelper.GetSenseError(e.HResult);
                return(false);
            }
            finally
            {
                if (stepCounter != null)
                {
                    stepCounter.Dispose();
                }
            }
            return(true);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Performs asynchronous Sensorcore SDK operation and handles any exceptions
        /// </summary>
        /// <param name="action">Action for which the SensorCore will be activated.</param>
        /// <returns><c>true</c> if call was successful, <c>false</c> otherwise</returns>
        private async Task <bool> CallSensorCoreApiAsync(Func <Task> action)
        {
            Exception failure = null;

            try
            {
                await action();
            }
            catch (Exception e)
            {
                failure = e;
            }
            if (failure != null)
            {
                MessageDialog dlg = null;
                switch (SenseHelper.GetSenseError(failure.HResult))
                {
                case SenseError.LocationDisabled:
                {
                    dlg = new MessageDialog(_resourceLoader.GetString("FeatureDisabled/Location"), _resourceLoader.GetString("FeatureDisabled/Title"));
                    dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchLocationSettingsAsync())));
                    dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) => { /* do nothing */ })));
                    await dlg.ShowAsync();

                    new System.Threading.ManualResetEvent(false).WaitOne(500);
                    return(false);
                }

                case SenseError.SenseDisabled:
                {
                    dlg = new MessageDialog(_resourceLoader.GetString("FeatureDisabled/MotionData"), _resourceLoader.GetString("FeatureDisabled/Title"));
                    dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchSenseSettingsAsync())));
                    dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) => { /* do nothing */ })));
                    await dlg.ShowAsync();

                    return(false);
                }

                case SenseError.SenseNotAvailable:
                {
                    dlg = new MessageDialog(_resourceLoader.GetString("FeatureNotSupported/Message"), _resourceLoader.GetString("FeatureNotSupported/Title"));
                    await dlg.ShowAsync();

                    return(false);
                }

                default:
                {
                    dlg = new MessageDialog("Failure: " + SenseHelper.GetSenseError(failure.HResult), "");
                    await dlg.ShowAsync();

                    return(false);
                }
                }
            }
            else
            {
                return(true);
            }
        }
Exemplo n.º 5
0
        private async Task <bool> CallSensorCoreApiAsync(Func <Task> action)
        {
            Exception failure = null;

            try
            {
                await action();
            }
            catch (Exception e)
            {
                failure = e;
            }

            if (failure != null)
            {
                switch (SenseHelper.GetSenseError(failure.HResult))
                {
                case SenseError.LocationDisabled:
                    MessageBoxResult res = MessageBox.Show(
                        "Location has been disabled. Do you want to open Location settings now?",
                        "Information",
                        MessageBoxButton.OKCancel
                        );
                    if (res == MessageBoxResult.OK)
                    {
                        await SenseHelper.LaunchLocationSettingsAsync();
                    }

                    return(false);

                case SenseError.SenseDisabled:

                    MessageBoxResult res2 = MessageBox.Show(
                        "Motion data has been disabled. Do you want to open Motion data settings now?",
                        "Information",
                        MessageBoxButton.OKCancel
                        );

                    if (res2 == MessageBoxResult.OK)
                    {
                        await SenseHelper.LaunchSenseSettingsAsync();
                    }

                    return(false);


                default:
                    MessageBoxResult res3 = MessageBox.Show(
                        "Error:" + SenseHelper.GetSenseError(failure.HResult),
                        "Information",
                        MessageBoxButton.OK);

                    return(false);
                }
            }
            else
            {
                return(true);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Performs asynchronous Sense SDK operation and handles any exceptions
        /// </summary>
        /// <param name="action">The function delegate to execute asynchronously when one task in the tasks completes</param>
        /// <returns><c>true</c> if call was successful, <c>false</c> otherwis:)
        /// e</returns>
        private async Task <bool> CallSensorCoreApiAsync(Func <Task> action)
        {
            Exception failure = null;

            try
            {
                await action();
            }
            catch (Exception e)
            {
                failure = e;
                Debug.WriteLine("Failure:" + e.Message);
            }
            if (failure != null)
            {
                try
                {
                    MessageDialog dialog = null;
                    switch (SenseHelper.GetSenseError(failure.HResult))
                    {
                    case SenseError.LocationDisabled:
                    {
                        dialog = new MessageDialog("In order to recognize activities you need to enable location in system settings. Do you want to open settings now? If not, application will exit.", "Information");
                        dialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchLocationSettingsAsync())));
                        dialog.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) => { Application.Current.Exit(); })));
                        await dialog.ShowAsync();

                        new System.Threading.ManualResetEvent(false).WaitOne(500);
                        return(false);
                    }

                    case SenseError.SenseDisabled:
                    {
                        dialog = new MessageDialog("In order to recognize activities you need to enable Motion data in Motion data settings. Do you want to open settings now? If not, application will exit.", "Information");
                        dialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchSenseSettingsAsync())));
                        dialog.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) => { Application.Current.Exit(); })));
                        await dialog.ShowAsync();

                        return(false);
                    }

                    default:
                        dialog = new MessageDialog("Failure: " + SenseHelper.GetSenseError(failure.HResult), "");
                        await dialog.ShowAsync();

                        return(false);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Failed to handle failure. Message:" + ex.Message);
                    return(false);
                }
            }
            else
            {
                return(true);
            }
        }
Exemplo n.º 7
0
        private async void LocationActivationButton_Click(object sender, RoutedEventArgs e)
        {
            await SenseHelper.LaunchLocationSettingsAsync();

            // Although asynchoneous, this completes before the user has actually done anything.
            // The application will loose control, the system settings will be displayed.
            // We will get the control back to our application via an OnNavigatedTo event.
        }
Exemplo n.º 8
0
        /// <summary>
        /// Performs asynchronous SensorCore SDK operation and handles any exceptions
        /// </summary>
        /// <param name="action"></param>
        /// <returns><c>true</c> if call was successful, <c>false</c> otherwise</returns>
        private async Task <bool> CallSenseApiAsync(Func <Task> action)
        {
            Exception failure = null;

            try
            {
                await action();
            }
            catch (Exception e)
            {
                failure = e;
            }

            if (failure != null)
            {
                bool          status = false;
                MessageDialog dialog = null;
                switch (SenseHelper.GetSenseError(failure.HResult))
                {
                case SenseError.LocationDisabled:
                    dialog = new MessageDialog("In order to collect and view tracks of visited places you need to enable location in system settings. Do you want to open Location settings now? if no, application will close", "Information");
                    dialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) =>
                    {
                        status = true;
                        await SenseHelper.LaunchLocationSettingsAsync();
                    })));
                    dialog.Commands.Add(new UICommand("No"));
                    await dialog.ShowAsync();

                    return(status);

                case SenseError.SenseDisabled:
                    dialog = new MessageDialog("In order to collect and view tracks of visited places you need to enable Places visited in Motion data settings. Do you want to open Motion data settings now?  if no, application will close", "Information");
                    dialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) =>
                    {
                        status = true;
                        await SenseHelper.LaunchSenseSettingsAsync();
                    })));
                    dialog.Commands.Add(new UICommand("No"));
                    await dialog.ShowAsync();

                    return(status);

                case SenseError.IncompatibleSDK:
                    dialog = new MessageDialog("This application has become outdated. Please update to the latest version.", "Information");
                    await dialog.ShowAsync();

                    return(false);

                default:
                    return(false);
                }
            }
            else
            {
                return(true);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Gets number of steps for current day
        /// </summary>
        /// <returns><c>true</c> if steps were successfully fetched, <c>false</c> otherwise</returns>
        private async Task <bool> GetStepsAsync()
        {
            // First try the pedometer
            try
            {
                var readings = await Pedometer.GetSystemHistoryAsync(DateTime.Now.Date, DateTime.Now - DateTime.Now.Date);

                _steps = StepCountData.FromPedometerReadings(readings);
                return(true);
            }
            catch (Exception)
            {
                // Continue to the fallback
            }

            // Fall back to using Lumia Sensor Core.
            IStepCounter stepCounter = null;

            try
            {
                //var qualifiers = Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().QualifierValues;

                if (DeviceTypeHelper.GetDeviceFormFactorType() == DeviceFormFactorType.Phone)
                {
                    stepCounter = await StepCounter.GetDefaultAsync();

                    StepCount count = await stepCounter.GetStepCountForRangeAsync(DateTime.Now.Date, DateTime.Now - DateTime.Now.Date);

                    _steps = StepCountData.FromLumiaStepCount(count);
                }
                else
                {
                    var obj = await SenseRecording.LoadFromFileAsync("Simulations\\short recording.txt");

                    if (!await CallSensorCoreApiAsync(async() => {
                        stepCounter = await StepCounterSimulator.GetDefaultAsync(obj, DateTime.Now - TimeSpan.FromHours(12));
                        StepCount count = await stepCounter.GetStepCountForRangeAsync(DateTime.Now.Date, DateTime.Now - DateTime.Now.Date);
                        _steps = StepCountData.FromLumiaStepCount(count);
                    }))
                    {
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
                _lastError = SenseHelper.GetSenseError(e.HResult);
                return(false);
            }
            finally
            {
                if (stepCounter != null && typeof(StepCounter) == stepCounter.GetType())
                {
                    ((StepCounter)stepCounter).Dispose();
                }
            }
            return(true);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Check motion data settings
        /// </summary>
        private async void CheckMotionDataSettings()
        {
            if (!(await TrackPointMonitor.IsSupportedAsync()) || !(await PlaceMonitor.IsSupportedAsync()) || !(await StepCounter.IsSupportedAsync()) || !(await ActivityMonitor.IsSupportedAsync()))
            {
                MessageBoxResult dlg = MessageBox.Show("Unfortunately this device does not support SensorCore service");
                Application.Current.Terminate();
            }
            else
            {
                uint apiSet = await SenseHelper.GetSupportedApiSetAsync();

                MotionDataSettings settings = await SenseHelper.GetSettingsAsync();

                // Devices with old location settings
                if (!settings.LocationEnabled)
                {
                    MessageBoxResult dlg = MessageBox.Show("In order to recognize activities and view visited places you need to enable location in system settings. Do you want to open settings now? if no, applicatoin will exit", "Information", MessageBoxButton.OKCancel);
                    if (dlg == MessageBoxResult.OK)
                    {
                        await SenseHelper.LaunchLocationSettingsAsync();
                    }
                }
                if (!settings.PlacesVisited)
                {
                    MessageBoxResult dlg = new MessageBoxResult();
                    if (settings.Version < 2)
                    {
                        //device which has old motion data settings.
                        //this is equal to motion data settings on/off in old system settings(SDK1.0 based)
                        dlg = MessageBox.Show("In order to count steps you need to enable Motion data collection in Motion data settings. Do you want to open settings now?", "Information", MessageBoxButton.OKCancel);
                        if (dlg == MessageBoxResult.Cancel)
                        {
                            Application.Current.Terminate();
                        }
                    }
                    else
                    {
                        dlg = MessageBox.Show("In order to recognize activities you need to 'enable Places visited' and 'DataQuality to detailed' in Motion data settings. Do you want to open settings now? ", "Information", MessageBoxButton.OKCancel);
                    }
                    if (dlg == MessageBoxResult.OK)
                    {
                        await SenseHelper.LaunchSenseSettingsAsync();
                    }
                }
                else if (apiSet >= 3 && settings.DataQuality == DataCollectionQuality.Basic)
                {
                    MessageBoxResult dlg = MessageBox.Show("In order to recognize biking activity you need to enable detailed data collection in Motion data settings. Do you want to open settings now?", "Information", MessageBoxButton.OKCancel);
                    if (dlg == MessageBoxResult.OK)
                    {
                        await SenseHelper.LaunchSenseSettingsAsync();
                    }
                }
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Validate if settings have been configured correctly to run SensorCore.
        /// </summary>
        /// <returns>Asynchronous task/returns>
        public static async Task ValidateSettingsAsync()
        {
            if (!(await ActivityMonitor.IsSupportedAsync()))
            {
                MessageDialog dlg = new MessageDialog(_resourceLoader.GetString("FeatureNotSupported/Message"), _resourceLoader.GetString("FeatureNotSupported/Title"));
                await dlg.ShowAsync();

                Application.Current.Exit();
            }
            else
            {
                uint apiSet = await SenseHelper.GetSupportedApiSetAsync();

                MotionDataSettings settings = await SenseHelper.GetSettingsAsync();

                if (settings.Version < 2)
                {
                    // Device which has old Motion data settings which requires system location and Motion data be enabled in order to access
                    // ActivityMonitor.
                    if (!settings.LocationEnabled)
                    {
                        MessageDialog dlg = new MessageDialog("In order to recognize activities you need to enable location in system settings. Do you want to open settings now? If not, application will exit.", "Information");
                        dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchLocationSettingsAsync())));
                        dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) => { Application.Current.Exit(); })));
                        await dlg.ShowAsync();
                    }
                    else if (!settings.PlacesVisited)
                    {
                        MessageDialog dlg = new MessageDialog("In order to recognize activities you need to enable Motion data in Motion data settings. Do you want to open settings now? If not, application will exit.", "Information");
                        dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchSenseSettingsAsync())));
                        dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) => { Application.Current.Exit(); })));
                        await dlg.ShowAsync();
                    }
                }
                else if (apiSet >= 3)
                {
                    if (!settings.LocationEnabled)
                    {
                        MessageDialog dlg = new MessageDialog("In order to recognize biking you need to enable location in system settings. Do you want to open settings now?", "Helpful tip");
                        dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchLocationSettingsAsync())));
                        dlg.Commands.Add(new UICommand("No"));
                        await dlg.ShowAsync();
                    }
                    else if (settings.DataQuality == DataCollectionQuality.Basic)
                    {
                        MessageDialog dlg = new MessageDialog("In order to recognize biking you need to enable detailed data collection in Motion data settings. Do you want to open settings now?", "Helpful tip");
                        dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchSenseSettingsAsync())));
                        dlg.Commands.Add(new UICommand("No"));
                        await dlg.ShowAsync();
                    }
                }
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Performs asynchronous SensorCore SDK operation and handles any exceptions
        /// </summary>
        /// <param name="action"></param>
        /// <returns><c>true</c> if call was successful, <c>false</c> otherwise</returns>
        private async Task <bool> CallSensorcoreApiAsync(Func <Task> action)
        {
            Exception failure = null;

            try
            {
                await action();
            }
            catch (Exception e)
            {
                failure = e;
            }

            if (failure != null)
            {
                MessageDialog dialog;
                switch (SenseHelper.GetSenseError(failure.HResult))
                {
                case SenseError.LocationDisabled:
                    dialog = new MessageDialog("Location has been disabled. Do you want to open Location settings now?", "Information");
                    dialog.Commands.Add(new UICommand("Yes", async cmd => await SenseHelper.LaunchLocationSettingsAsync()));
                    dialog.Commands.Add(new UICommand("No"));
                    await dialog.ShowAsync();

                    new System.Threading.ManualResetEvent(false).WaitOne(500);
                    return(false);

                case SenseError.SenseDisabled:
                    dialog = new MessageDialog("Motion data has been disabled. Do you want to open Motion data settings now?", "Information");
                    dialog.Commands.Add(new UICommand("Yes", async cmd => await SenseHelper.LaunchSenseSettingsAsync()));
                    dialog.Commands.Add(new UICommand("No"));
                    await dialog.ShowAsync();

                    new System.Threading.ManualResetEvent(false).WaitOne(500);
                    return(false);

                case SenseError.SensorNotAvailable:
                    dialog = new MessageDialog("The sensor is not supported on this device", "Information");
                    await dialog.ShowAsync();

                    new System.Threading.ManualResetEvent(false).WaitOne(500);
                    return(false);

                default:
                    dialog = new MessageDialog("Failure: " + SenseHelper.GetSenseError(failure.HResult), "");
                    await dialog.ShowAsync();

                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Performs asynchronous Sensorcore SDK operation and handles any exceptions
        /// </summary>
        /// <param name="action">Action for which the SensorCore will be activated.</param>
        /// <returns><c>true</c> if call was successful, <c>false</c> otherwise</returns>
        public async Task <bool> CallSensorcoreApiAsync(Func <Task> action)
        {
            Exception failure = null;

            try
            {
                await action();
            }
            catch (Exception e)
            {
                failure = e;
            }
            if (failure != null)
            {
                MessageDialog dialog;
                switch (SenseHelper.GetSenseError(failure.HResult))
                {
                case SenseError.LocationDisabled:
                    dialog = new MessageDialog("In order to collect and view visited tracks you need to enable location in system settings. Do you want to open settings now? If not, application will exit.", "Information");
                    dialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchLocationSettingsAsync())));
                    dialog.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) => { Application.Current.Exit(); })));
                    await dialog.ShowAsync();

                    new System.Threading.ManualResetEvent(false).WaitOne(500);
                    return(false);

                case SenseError.SenseDisabled:
                {
                    MotionDataSettings settings = await SenseHelper.GetSettingsAsync();

                    if (settings.Version < 2)
                    {
                        // Device has old Motion data settings
                        dialog = new MessageDialog("In order to collect and view visited tracks you need to enable 'Motion data collection' in Motion data settings. Do you want to open settings now? If not, application will exit.", "Information");
                    }
                    else
                    {
                        dialog = new MessageDialog("In order to collect and view visited tracks you need to enable 'Places visited' in Motion data settings. Do you want to open settings now? if not, application will exit.", "Information");
                    }
                    dialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchSenseSettingsAsync())));
                    dialog.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) => { Application.Current.Exit(); })));
                    await dialog.ShowAsync();

                    new System.Threading.ManualResetEvent(false).WaitOne(500);
                    return(false);
                }

                default:
                    return(false);
                }
            }
            return(true);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Performs asynchronous SensorCore SDK operation and handles any exceptions
        /// </summary>
        /// <param name="action"></param>
        /// <returns><c>true</c> if call was successful, <c>false</c> otherwise</returns>
        private async Task <bool> CallSenseApiAsync(Func <Task> action)
        {
            Exception failure = null;

            try
            {
                await action();
            }
            catch (Exception e)
            {
                failure = e;
            }

            if (failure != null)
            {
                MessageDialog dialog = null;
                switch (SenseHelper.GetSenseError(failure.HResult))
                {
                case SenseError.LocationDisabled:
                    dialog = new MessageDialog("Location has been disabled. Do you want to open Location settings now?", "Information");
                    dialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchLocationSettingsAsync())));
                    dialog.Commands.Add(new UICommand("No"));
                    await dialog.ShowAsync();

                    return(true);

                case SenseError.SenseDisabled:
                    dialog = new MessageDialog("Motion data has been disabled. Do you want to open Motion data settings now?", "Information");
                    dialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchSenseSettingsAsync())));
                    dialog.Commands.Add(new UICommand("No"));
                    await dialog.ShowAsync();

                    return(true);

                case SenseError.IncompatibleSDK:
                    dialog = new MessageDialog("This application has become outdated. Please update to the latest version.", "Information");
                    await dialog.ShowAsync();

                    return(false);

                default:
                    dialog = new MessageDialog("Failure: " + SenseHelper.GetSenseError(failure.HResult), "");
                    await dialog.ShowAsync();

                    return(false);
                }
            }
            else
            {
                return(true);
            }
        }
Exemplo n.º 15
0
        async Task UpdateDialog()
        {
            if (updatingDialog || (sensorCoreActivationStatus.ActivationRequestResult != ActivationRequestResults.NotAvailableYet))
            {
                return;
            }
            updatingDialog = true;
            MotionDataActivationBox.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            LocationActivationBox.Visibility   = Windows.UI.Xaml.Visibility.Collapsed;
            Exception failure = null;

            try
            {
                // GetDefaultAsync will throw if MotionData is disabled
                PlaceMonitor monitor = await Lumia.Sense.PlaceMonitor.GetDefaultAsync();

                // But confirm that MotionData is really enabled by calling ActivateAsync,
                // to cover the case where the MotionData has been disabled after the app has been launched.
                await monitor.ActivateAsync();
            }
            catch (Exception exception)
            {
                switch (SenseHelper.GetSenseError(exception.HResult))
                {
                case SenseError.LocationDisabled:
                    LocationActivationBox.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    break;

                case SenseError.SenseDisabled:
                    MotionDataActivationBox.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    break;

                default:
                    // do something clever here
                    break;
                }

                failure = exception;
            }

            if (failure == null)
            {
                // All is good now, dismiss the dialog.

                sensorCoreActivationStatus.ActivationRequestResult = ActivationRequestResults.AllEnabled;
                this.Frame.GoBack();
            }
            updatingDialog = false;
        }
Exemplo n.º 16
0
        /// <summary>
        /// Makes sure necessary settings are enabled in order to use SensorCore
        /// </summary>
        /// <returns>Asynchronous task</returns>
        private async Task ValidateSettingsAsync()
        {
            try
            {
                if (!(await TrackPointMonitor.IsSupportedAsync()))
                {
                    MessageDialog dlg = new MessageDialog("Unfortunately this device does not support viewing tracks");
                    await dlg.ShowAsync();

                    Application.Current.Exit();
                }
                else
                {
                    _apiSet = await SenseHelper.GetSupportedApiSetAsync();

                    MotionDataSettings settings = await SenseHelper.GetSettingsAsync();

                    if (!settings.LocationEnabled)
                    {
                        MessageDialog dlg = new MessageDialog("In order to collect and view visited tracks you need to enable location in system settings. Do you want to open settings now? if not, application will exit", "Information");
                        dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchLocationSettingsAsync())));
                        dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) => { Application.Current.Exit(); })));
                        await dlg.ShowAsync();
                    }
                    if (!settings.PlacesVisited)
                    {
                        MessageDialog dlg = null;
                        if (settings.Version < 2)
                        {
                            // Device has old Motion data settings
                            dlg = new MessageDialog("In order to collect and view visited tracks you need to enable 'Motion data collection' in Motion data settings. Do you want to open settings now? If not, application will exit.", "Information");
                        }
                        else
                        {
                            dlg = new MessageDialog("In order to collect and view visited tracks you need to enable 'Places visited' in Motion data settings. Do you want to open settings now? If not, application will exit.", "Information");
                        }
                        dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchSenseSettingsAsync())));
                        dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) => { Application.Current.Exit(); })));
                        await dlg.ShowAsync();
                    }
                }
            }
            catch (Exception)
            {
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// Check motion data settings
        /// </summary>
        private async void CheckMotionDataSettings()
        {
            if (!(await StepCounter.IsSupportedAsync()))
            {
                MessageDialog dlg = new MessageDialog("Unfortunately this device does not support step counting");
                await dlg.ShowAsync();

                Application.Current.Exit();
            }
            else
            {
                // MotionDataSettings settings = await SenseHelper.GetSettingsAsync();
                // Starting from version 2 of Motion data settings Step counter and Acitivity monitor are always available. In earlier versions system
                // location setting and Motion data had to be enabled.
                uint apiSet = await SenseHelper.GetSupportedApiSetAsync();

                MotionDataSettings settings = await SenseHelper.GetSettingsAsync();

                if (apiSet > 2)
                {
                    if (!settings.LocationEnabled)
                    {
                        MessageDialog dlg = new MessageDialog("In order to count steps you need to enable location in system settings. Do you want to open settings now?", "Information");
                        dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchLocationSettingsAsync())));
                        dlg.Commands.Add(new UICommand("No"));
                        await dlg.ShowAsync();
                    }
                    if (!settings.PlacesVisited)
                    {
                        MessageDialog dlg = null;
                        if (settings.Version < 2)
                        {
                            dlg = new MessageDialog("In order to count steps you need to enable Motion data collection in Motion data settings. Do you want to open settings now?", "Information");
                        }
                        else
                        {
                            dlg = new MessageDialog("In order to collect and view visited places you need to enable Places visited in Motion data settings. Do you want to open settings now? if no, application will exit", "Information");
                        }
                        dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchSenseSettingsAsync())));
                        dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) => { Application.Current.Exit(); })));
                        await dlg.ShowAsync();
                    }
                }
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// Gets number of steps for current day
        /// </summary>
        /// <returns><c>true</c> if steps were successfully fetched, <c>false</c> otherwise</returns>
        private async Task <bool> GetStepsAsync()
        {
            // First try the pedometer
            try
            {
                var readings = await Pedometer.GetSystemHistoryAsync(DateTime.Now.Date, DateTime.Now - DateTime.Now.Date);

                _steps = StepCountData.FromPedometerReadings(readings);
                return(true);
            }
            catch (Exception)
            {
                // Continue to the fallback
            }

            // Fall back to using Lumia Sensor Core.
            StepCounter stepCounter = null;

            try
            {
                stepCounter = await StepCounter.GetDefaultAsync();

                StepCount count = await stepCounter.GetStepCountForRangeAsync(
                    DateTime.Now.Date,
                    DateTime.Now - DateTime.Now.Date);

                _steps = StepCountData.FromLumiaStepCount(count);
            }
            catch (Exception e)
            {
                _lastError = SenseHelper.GetSenseError(e.HResult);
                return(false);
            }
            finally
            {
                if (stepCounter != null)
                {
                    stepCounter.Dispose();
                }
            }
            return(true);
        }
Exemplo n.º 19
0
        /// <summary>
        /// Makes sure necessary settings are enabled in order to use SensorCore
        /// </summary>
        /// <returns>Asynchronous task</returns>
        public async Task ValidateSettingsAsync()
        {
            if (!await StepCounter.IsSupportedAsync())
            {
                MessageBoxResult dlg = MessageBox.Show("Unfortunately this device does not support step counting");
                Application.Current.Terminate();
            }
            else
            {
                // Starting from version 2 of Motion data settings Step counter and Acitivity monitor are always available. In earlier versions system
                // location setting and Motion data had to be enabled.
                MotionDataSettings settings = await SenseHelper.GetSettingsAsync();

                if (settings.Version < 2)
                {
                    if (!settings.LocationEnabled)
                    {
                        MessageBoxResult dlg = MessageBox.Show("In order to count steps you need to enable location in system settings. Do you want to open settings now? If not, application will exit.", "Information", MessageBoxButton.OKCancel);
                        if (dlg == MessageBoxResult.OK)
                        {
                            await SenseHelper.LaunchLocationSettingsAsync();
                        }
                        else
                        {
                            Application.Current.Terminate();
                        }
                    }
                    if (!settings.PlacesVisited)
                    {
                        MessageBoxResult rc = MessageBox.Show("In order to count steps you need to enable Motion data collection in Motion data settings. Do you want to open settings now? If not, application will exit.", "Information", MessageBoxButton.OKCancel);
                        if (rc == MessageBoxResult.OK)
                        {
                            await SenseHelper.LaunchSenseSettingsAsync();
                        }
                        else
                        {
                            Application.Current.Terminate();
                        }
                    }
                }
            }
        }
Exemplo n.º 20
0
        /// <summary>
        /// Initializes sensor
        /// </summary>
        /// <returns>Asynchronous task</returns>
        private async Task Initialize()
        {
            if (!await StepCounter.IsSupportedAsync())
            {
                MessageDialog dlg = new MessageDialog("Unfortunately this device does not support step counting");
                await dlg.ShowAsync();
            }
            else
            {
                MotionDataSettings settings = await SenseHelper.GetSettingsAsync();

                // Starting from version 2 of Motion data settings Step counter and Acitivity monitor are always available. In earlier versions system
                // location setting and Motion data had to be enabled.
                if (settings.Version < 2)
                {
                    if (!settings.LocationEnabled)
                    {
                        MessageDialog dlg = new MessageDialog("In order to count steps you need to enable location in system settings. Do you want to open settings now?", "Information");
                        dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchLocationSettingsAsync())));
                        dlg.Commands.Add(new UICommand("No"));
                        await dlg.ShowAsync();
                    }
                    else if (!settings.PlacesVisited)
                    {
                        MessageDialog dlg = new MessageDialog("In order to count steps you need to enable Motion data collection in Motion data settings. Do you want to open settings now?", "Information");
                        dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchSenseSettingsAsync())));
                        dlg.Commands.Add(new UICommand("No"));
                        await dlg.ShowAsync();
                    }
                }
            }

            if (!await CallSenseApiAsync(async() =>
            {
                _stepCounter = await StepCounter.GetDefaultAsync();
            }))
            {
                Application.Current.Exit();
            }
            await SetSelectedDayAsync(_selectedDay);
        }
Exemplo n.º 21
0
        private async Task InitializeSensorAsync()
        {
            Exception failure = null;

            if (!await StepCounter.IsSupportedAsync())
            {
                MessageBox.Show(
                    "Your device doesn't support Motion Data. Application will be closed",
                    "Information", MessageBoxButton.OK);
                Application.Current.Terminate();
            }

            try
            {
                _stepCounter = await StepCounter.GetDefaultAsync();
            }
            catch (Exception e)
            {
                failure = e;
            }

            if (failure != null)
            {
                switch (SenseHelper.GetSenseError(failure.HResult))
                {
                case SenseError.LocationDisabled:
                case SenseError.SenseDisabled:
                    NavigationService.Navigate(new Uri("/ActivateSensorCore;component/Pages/ActivateSensorCore.xaml", UriKind.Relative));
                    break;

                default:
                    throw (failure);
                }
            }
            else
            {
                await _stepCounter.ActivateAsync();
                await UpdateModelAsync();
            }
        }
Exemplo n.º 22
0
        async void UpdateDialog()
        {
            MotionDataActivationBox.Visibility       = System.Windows.Visibility.Collapsed;
            ActivationResult.ActivationRequestResult = ActivationRequestResults.AskMeLater;

            Exception failure = null;

            try
            {
                await Lumia.Sense.PlaceMonitor.GetDefaultAsync();
            }
            catch (Exception exception)
            {
                switch (SenseHelper.GetSenseError(exception.HResult))
                {
                case SenseError.LocationDisabled:
                    ShowLocationActivationBox();
                    break;

                case SenseError.SenseDisabled:
                    ShowMotionDataActivationBox();
                    break;

                default:
                    // do something clever here
                    break;
                }

                failure = exception;
            }
            if (failure == null)
            {
                // All is now good, dismiss the dialog.

                ActivationResult.ActivationRequestResult = ActivationRequestResults.AllEnabled;
                NavigationService.GoBack();
            }
        }
Exemplo n.º 23
0
        /// <summary>
        /// Performs asynchronous Sensorcore SDK operation and handles any exceptions
        /// </summary>
        /// <param name="action"></param>
        /// <returns><c>true</c> if call was successful, <c>false</c> otherwise</returns>
        private async Task <bool> CallSensorcoreApiAsync(Func <Task> action)
        {
            Exception failure = null;

            try
            {
                await action();
            }
            catch (Exception e)
            {
                failure = e;
            }

            if (failure != null)
            {
                switch (SenseHelper.GetSenseError(failure.HResult))
                {
                case SenseError.LocationDisabled:
                case SenseError.SenseDisabled:
                    if (!_app.SensorCoreActivationStatus.Ongoing)
                    {
                        this.Frame.Navigate(typeof(ActivateSensorCore));
                    }

                    return(false);

                default:
                    MessageDialog dialog = new MessageDialog("Failure: " + SenseHelper.GetSenseError(failure.HResult) + " while initializing Motion data. Application will exit.", "");
                    await dialog.ShowAsync();

                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 24
0
        /// <summary>
        /// initializes the sensor services
        /// </summary>
        /// <returns></returns>
        private async Task Initialize()
        {
            //following code assumes that device has new software(SensorCoreSDK1.1 based)
            try
            {
                if (!(await TrackPointMonitor.IsSupportedAsync()))
                {
                    MessageDialog dlg = new MessageDialog("Unfortunately this device does not support Trackpoints of visited places");
                    await dlg.ShowAsync();

                    Application.Current.Exit();
                }
                else
                {
                    uint apiSet = await SenseHelper.GetSupportedApiSetAsync();

                    MotionDataSettings settings = await SenseHelper.GetSettingsAsync();

                    if (!settings.LocationEnabled)
                    {
                        MessageDialog dlg = new MessageDialog("In order to collect and view tracks of visited places you need to enable location in system settings. Do you want to open settings now? if no, applicatoin will close", "Information");
                        dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchLocationSettingsAsync())));
                        dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) =>
                        {
                            Application.Current.Exit();
                        })));
                        await dlg.ShowAsync();
                    }

                    if (!settings.PlacesVisited)
                    {
                        MessageDialog dlg = null;
                        if (settings.Version < 2)
                        {
                            //device which has old motion data settings.
                            //this is equal to motion data settings on/off in old system settings(SDK1.0 based)
                            dlg = new MessageDialog("In order to collect and view tracks of visited places you need to enable Motion data in Motion data settings. Do you want to open settings now? if no, application will close", "Information");
                        }
                        else
                        {
                            dlg = new MessageDialog("In order to collect and view tracks of visited places you need to 'enable Places visited' and 'DataQuality to detailed' in Motion data settings. Do you want to open settings now? if no, application will close", "Information");
                        }
                        dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchSenseSettingsAsync())));
                        dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) =>
                        {
                            Application.Current.Exit();
                        })));
                        await dlg.ShowAsync();
                    }
                }
            }
            catch (Exception)
            {
            }

            //in case if the device has old software(earlier than SDK1.1) or system settings changed after sometime, CallSenseApiAsync() method handles the system settings prompts.
            if (_trackMonitor == null)
            {
                if (!await CallSenseApiAsync(async() =>
                {
                    _trackMonitor = await TrackPointMonitor.GetDefaultAsync();
                }))
                {
                    Application.Current.Exit();
                }
            }

            //setting current loation in the map
            try
            {
                TracksMapControl.MapServiceToken = "4eSgIBUeMtjFyJP6YxkyPQ";
                Geoposition geoposition = await new Geolocator().GetGeopositionAsync(maximumAge: TimeSpan.FromMinutes(5), timeout: TimeSpan.FromSeconds(5));
                TracksMapControl.Center = geoposition.Coordinate.Point;
            }
            catch (Exception)
            {
                // if current position can't get, setting default position to Espoo, Finland.
                TracksMapControl.Center = new Geopoint(new BasicGeoposition()
                {
                    Latitude  = 60.17,
                    Longitude = 24.83
                });
            }
            await GetHistory();

            UpdateScreenAsync();
        }
Exemplo n.º 25
0
        /// <summary>
        /// Initializes sensors
        /// </summary>
        /// <returns>Asynchronous task</returns>
        private async Task Initialize()
        {
            if (!(await ActivityMonitor.IsSupportedAsync()))
            {
                MessageDialog dlg = new MessageDialog("Unfortunately this device does not support activities.");
                await dlg.ShowAsync();

                Application.Current.Exit();
            }
            else
            {
                uint apiSet = await SenseHelper.GetSupportedApiSetAsync();

                MotionDataSettings settings = await SenseHelper.GetSettingsAsync();

                // devices with old sensorCore SDK service
                if (settings.Version < 2 && !settings.LocationEnabled)
                {
                    MessageDialog dlg = new MessageDialog("In order to recognize activities you need to enable location in system settings. Do you want to open settings now? if no, applicatoin will exit", "Information");
                    dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchLocationSettingsAsync())));
                    dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) =>
                    {
                        Application.Current.Exit();
                    })));
                    await dlg.ShowAsync();
                }

                if (!settings.PlacesVisited)
                {
                    MessageDialog dlg = null;
                    if (settings.Version < 2)
                    {
                        //device which has old motion data settings.
                        //this is equal to motion data settings on/off in old system settings(SDK1.0 based)
                        dlg = new MessageDialog("In order to recognize activities you need to enable Motion data in Motion data settings. Do you want to open settings now? if no, application will exit", "Information");
                        dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler((cmd) =>
                        {
                            Application.Current.Exit();
                        })));
                    }
                    else
                    {
                        dlg = new MessageDialog("In order to recognize activities you need to 'enable Places visited' and 'DataQuality to detailed' in Motion data settings. Do you want to open settings now? ", "Information");
                        dlg.Commands.Add(new UICommand("No"));
                    }
                    dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchSenseSettingsAsync())));
                    await dlg.ShowAsync();
                }
                else if (apiSet >= 3 && settings.DataQuality == DataCollectionQuality.Basic)
                {
                    MessageDialog dlg = new MessageDialog("In order to recognize biking you need to enable detailed data collection in Motion data settings. Do you want to open settings now?", "Information");
                    dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(async(cmd) => await SenseHelper.LaunchSenseSettingsAsync())));
                    dlg.Commands.Add(new UICommand("No"));
                    await dlg.ShowAsync();
                }
            }

            //in case if the device has old software(earlier than SDK1.1) or system settings changed after sometime, CallSenseApiAsync() method handles the system settings prompts.
            if (_activityMonitor == null)
            {
                if (!await CallSenseApiAsync(async() =>
                {
                    _activityMonitor = await ActivityMonitor.GetDefaultAsync();
                }))
                {
                    Application.Current.Exit();
                }
            }
            await UpdateScreenAsync();
        }