コード例 #1
0
ファイル: MapPage.xaml.Sensors.cs プロジェクト: smych/places
        /// <summary>
        /// Performs asynchronous Sensorcore 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> otherwise</returns>
        public 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:
                    MessageDialog dialog = new MessageDialog("In order to collect and view visited places 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();

                    MessageDialog dlg = null;
                    if (settings.Version < 2)
                    {
                        // Device has old Motion data settings
                        dlg = new MessageDialog("In order to collect and view visited places you need to enable Motion data 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 places 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();

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

                case SenseError.GeneralFailure:
                    return(false);

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

                    return(false);

                default:
                    return(false);
                }
            }
            return(true);
        }
コード例 #2
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 PlaceMonitor.IsSupportedAsync()))
                {
                    MessageDialog dlg = new MessageDialog("Unfortunately this device does not support viewing 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 visited places 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 collect and view visited places you need to enable Motion data in Motion data settings. Do you want to open settings now? if no, application will exit", "Information");
                        }
                        else
                        {
                            dlg = new MessageDialog("In order to collect and view 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 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)
            {
            }

            //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 (_placeMonitor == null)
            {
                if (!await CallSenseApiAsync(async() =>
                {
                    _placeMonitor = await PlaceMonitor.GetDefaultAsync();
                }))
                {
                    Application.Current.Exit();
                }
            }

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

            UpdateScreenAsync();
        }
コード例 #3
0
ファイル: StepsEngine.cs プロジェクト: vincy94/steps
        /// <summary>
        /// Initializes StepCounterSimulator (requires Lumia.Sense.Testing)
        /// </summary>
        //public async Task InitializeSimulatorAsync()
        //{
        //    var obj = await SenseRecording.LoadFromFileAsync("Simulations\\short recording.txt");
        //    if (!await CallSensorCoreApiAsync(async () => { _stepCounter = await StepCounterSimulator.GetDefaultAsync(obj, DateTime.Now - TimeSpan.FromHours(12)); }))
        //    {
        //        Application.Current.Exit();
        //    }
        //    _sensorActive = true;
        //}

        /// <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);
            }
        }
コード例 #4
0
ファイル: MapPage.xaml.Sensors.cs プロジェクト: smych/places
        /// <summary>
        /// Makes sure necessary settings are enabled in order to use SensorCore
        /// </summary>
        /// <returns>Asynchronous task</returns>
        private async Task ValidateSettingsAsync()
        {
            try
            {
                if (!(await PlaceMonitor.IsSupportedAsync()))
                {
                    MessageDialog dlg = new MessageDialog("Unfortunately this device does not support viewing visited places");
                    await dlg.ShowAsync();

                    Application.Current.Exit();
                }
                else
                {
                    MotionDataSettings settings = await SenseHelper.GetSettingsAsync();

                    if (!settings.LocationEnabled)
                    {
                        MessageDialog dlg = new MessageDialog("In order to collect and view visited places 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 places you need to enable Motion data 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 places 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)
            {
            }
        }
コード例 #5
0
ファイル: StepsEngine.cs プロジェクト: vincy94/steps
        /// <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();
                    }
                }
            }
        }
コード例 #6
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:
                    return(false);
                }
            }
            else
            {
                return(true);
            }
        }
コード例 #7
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();
        }
コード例 #8
0
ファイル: ActivitySensor.cs プロジェクト: djamsoft/activities
        /// <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();
                    }
                }
            }
        }
コード例 #9
0
ファイル: ActivitySensor.cs プロジェクト: djamsoft/activities
        /// <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);
            }
        }