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); } }
/// <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); } }
/// <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); }
/// <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); } }
/// <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); }
/// <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); } }
/// <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); }
/// <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); }
/// <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); } }
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; }
/// <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); }
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(); } }
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(); } }
/// <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); }