Beispiel #1
0
        /// <summary>
        /// Initializes sensors
        /// </summary>
        /// <returns>Asynchronous task</returns>
        private async Task InitializeAsync()
        {
            if (await CallSensorcoreApiAsync(async() => { _placeMonitor = await Monitor.GetDefaultAsync(); }))
            {
                // Update list of known places
                await UpdateKnownPlacesAsync(null);

                HomeButton.IsEnabled     = true;
                WorkButton.IsEnabled     = true;
                FrequentButton.IsEnabled = true;
                CurrentButton.IsEnabled  = true;
                _placeHistory            = await GetPlacesHistoryAsync();
            }
            else
            {
                Application.Current.Exit();
                return;
            }

            // Init Geolocator
            try
            {
                // Get a geolocator object
                _geoLocator = new Geolocator();
                // Get cancellation token
                _cancellationTokenSource = new CancellationTokenSource();
                CancellationToken token       = _cancellationTokenSource.Token;
                Geoposition       geoposition = await _geoLocator.GetGeopositionAsync().AsTask(token);

                _currentLocation = new Geopoint(new BasicGeoposition()
                {
                    Latitude  = geoposition.Coordinate.Point.Position.Latitude,
                    Longitude = geoposition.Coordinate.Point.Position.Longitude
                });
                // Focus on the current location
                CurrentButton_Click(this, null);
            }
            finally
            {
                _cancellationTokenSource = null;
            }
        }
        /// <summary>
        /// Initializes sensors
        /// </summary>
        /// <returns>Asynchronous task</returns>
        private async Task InitializeAsync()
        {
            if( await CallSensorcoreApiAsync( async () => { _placeMonitor = await Monitor.GetDefaultAsync(); } ) )
            {
                // Update list of known places
                await UpdateKnownPlacesAsync( null );
                HomeButton.IsEnabled = true;
                WorkButton.IsEnabled = true;
                FrequentButton.IsEnabled = true;
                CurrentButton.IsEnabled = true;
                _placeHistory = await GetPlacesHistoryAsync();
            }
            else
            {
                Application.Current.Exit();
                return;
            }

            // Init Geolocator
            try
            {
                // Get a geolocator object
                _geoLocator = new Geolocator();
                // Get cancellation token
                _cancellationTokenSource = new CancellationTokenSource();
                CancellationToken token = _cancellationTokenSource.Token;
                Geoposition geoposition = await _geoLocator.GetGeopositionAsync().AsTask( token );
                _currentLocation = new Geopoint( new BasicGeoposition()
                {
                    Latitude = geoposition.Coordinate.Point.Position.Latitude,
                    Longitude = geoposition.Coordinate.Point.Position.Longitude
                } );
                // Focus on the current location
                CurrentButton_Click( this, null );
            }
            finally
            {
                _cancellationTokenSource = null;
            }
        }
        /// <summary>
        /// Initialize SensorCore and find the current position
        /// </summary>
        private async void InitCore()
        {
            if (_monitor == null)
            {
                // This is not implemented by the simulator, uncomment for the PlaceMonitor
                if (await Monitor.IsSupportedAsync())
                {
                    // Init SensorCore
                    if (await CallSensorcoreApiAsync(async() => { _monitor = await Monitor.GetDefaultAsync(); }))
                    {
                        Debug.WriteLine("PlaceMonitor initialized.");

                        // Update list of known places
                        await UpdateKnownPlacesAsync();

                        HomeButton.IsEnabled     = true;
                        WorkButton.IsEnabled     = true;
                        FrequentButton.IsEnabled = true;
                        CurrentButton.IsEnabled  = true;
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    var           loader = new Windows.ApplicationModel.Resources.ResourceLoader();
                    MessageDialog dialog = new MessageDialog(loader.GetString("NoMotionDataSupport/Text"), loader.GetString("Information/Text"));
                    dialog.Commands.Add(new UICommand(loader.GetString("OkButton/Text")));
                    await dialog.ShowAsync();

                    new System.Threading.ManualResetEvent(false).WaitOne(500);
                    Application.Current.Exit();
                }
                // Init Geolocator
                try
                {
                    _accessInfo = DeviceAccessInformation.CreateFromDeviceClass(DeviceClass.Location);
                    _accessInfo.AccessChanged += OnAccessChanged;
                    // Get a geolocator object
                    _geolocator = new Geolocator();
                    // Get cancellation token
                    _cancellationTokenSource = new CancellationTokenSource();
                    CancellationToken token       = _cancellationTokenSource.Token;
                    Geoposition       geoposition = await _geolocator.GetGeopositionAsync().AsTask(token);

                    _currentLocation = new Geopoint(new BasicGeoposition()
                    {
                        Latitude  = geoposition.Coordinate.Point.Position.Latitude,
                        Longitude = geoposition.Coordinate.Point.Position.Longitude
                    });

                    // Focus on the current location
                    OnCurrentClicked(this, null);
                }
                catch (UnauthorizedAccessException)
                {
                    if (DeviceAccessStatus.DeniedByUser == _accessInfo.CurrentStatus)
                    {
                        Debug.WriteLine("Location has been disabled by the user. Enable access through the settings charm.");
                    }
                    else if (DeviceAccessStatus.DeniedBySystem == _accessInfo.CurrentStatus)
                    {
                        Debug.WriteLine("Location has been disabled by the system. The administrator of the device must enable location access through the location control panel.");
                    }
                    else if (DeviceAccessStatus.Unspecified == _accessInfo.CurrentStatus)
                    {
                        Debug.WriteLine("Location has been disabled by unspecified source. The administrator of the device may need to enable location access through the location control panel, then enable access through the settings charm.");
                    }
                }
                catch (TaskCanceledException)
                {
                    // task cancelled
                }
                catch (Exception ex)
                {
                    if ((uint)ex.HResult == 0x80004004)
                    {
                        // the application does not have the right capability or the location master switch is off
                        Debug.WriteLine("location is disabled in phone settings.");
                    }
                }
                finally
                {
                    _cancellationTokenSource = null;
                }
            }
            // Activate and deactivate the SensorCore when the visibility of the app changes
            Window.Current.VisibilityChanged += async(oo, ee) =>
            {
                if (_monitor != null)
                {
                    if (!ee.Visible)
                    {
                        await CallSensorcoreApiAsync(async() => { await _monitor.DeactivateAsync(); });
                    }
                    else
                    {
                        await CallSensorcoreApiAsync(async() => { await _monitor.ActivateAsync(); });
                    }
                }
            };
        }