/// <summary>
        /// Static method to get the default activity sensor present in the system.
        /// </summary>
        public static async Task <IActivitySensor> GetDefaultAsync()
        {
            IActivitySensor sensor = null;

            try
            {
                // Check if there is an activity sensor in the system
                ActivitySensor activitySensor = await ActivitySensor.GetDefaultAsync();

                // If there is one then create OSActivitySensor.
                if (activitySensor != null)
                {
                    sensor = new OSActivitySensor(activitySensor);
                }
            }
            catch (System.UnauthorizedAccessException)
            {
                // If there is an activity sensor but the user has disabled motion data
                // then check if the user wants to open settngs and enable motion data.
                MessageDialog dialog = new MessageDialog("Motion access has been disabled in system settings. Do you want to open settings now?", "Information");
                dialog.Commands.Add(new UICommand("Yes", async cmd => await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-motion"))));
                dialog.Commands.Add(new UICommand("No"));
                await dialog.ShowAsync();

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

            // If the OS activity sensor is not present then create the LumiaActivitySensor.
            // This will use ActivityMonitor from SensorCore.
            if (sensor == null)
            {
                // Check if all the required settings have been configured correctly
                await LumiaActivitySensor.ValidateSettingsAsync();

                sensor = new LumiaActivitySensor();
            }
            return(sensor);
        }
Exemple #2
0
        /// <summary>
        /// Constructor
        /// </summary>
        public MainPage()
        {
            InitializeComponent();
            this.NavigationCacheMode = NavigationCacheMode.Required;

            Window.Current.VisibilityChanged += async(sender, args) =>
            {
                if (!args.Visible)
                {
                    // Application put to background, deactivate sensor
                    if (_sensor != null)
                    {
                        await _sensor.DeactivateAsync();
                    }
                }
                else
                {
                    // Create sensor instance if already not created
                    if (_sensor == null)
                    {
                        _sensor = await ActivitySensorFactory.GetDefaultAsync();

                        // Bind data
                        DataContext = _sensor.GetActivityDataInstance();
                    }

                    // Register delegate to get reading changes
                    _sensor.ReadingChanged += activity_ReadingChanged;

                    // Activate the sensor
                    await _sensor.ActivateAsync();

                    // Update screen
                    await UpdateSummaryAsync();
                }
            };
        }