11/*
2+ The MIT License (MIT)
23Copyright (c) 2015 Microsoft
34
45Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -19,15 +20,19 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1920OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2021THE SOFTWARE.
2122 */
22-
2323using System ;
2424using Windows . UI . Xaml ;
2525using Windows . UI . Xaml . Controls ;
2626using Windows . UI . Xaml . Navigation ;
2727using Windows . UI . Popups ;
2828using Lumia . Sense ;
2929using System . Threading . Tasks ;
30+ using Windows . Web ;
31+ using Lumia . Sense . Testing ;
3032
33+ /// <summary>
34+ /// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556
35+ /// </summar
3136namespace HelloSensorCore
3237{
3338 /// <summary>
@@ -39,7 +44,7 @@ public sealed partial class MainPage : Page
3944 /// <summary>
4045 /// Step counter instance
4146 /// </summary>
42- private StepCounter _stepCounter ;
47+ private IStepCounter _stepCounter ;
4348 #endregion
4449
4550 public MainPage ( )
@@ -77,6 +82,52 @@ public MainPage()
7782 } ;
7883 }
7984
85+ /// <summary>
86+ /// Check motion data settings
87+ /// </summary>
88+ private async void CheckMotionDataSettings ( )
89+ {
90+ if ( ! ( await StepCounter . IsSupportedAsync ( ) ) )
91+ {
92+ MessageDialog dlg = new MessageDialog ( "Unfortunately this device does not support step counting" ) ;
93+ await dlg . ShowAsync ( ) ;
94+ Application . Current . Exit ( ) ;
95+ }
96+ else
97+ {
98+ // MotionDataSettings settings = await SenseHelper.GetSettingsAsync();
99+ // Starting from version 2 of Motion data settings Step counter and Acitivity monitor are always available. In earlier versions system
100+ // location setting and Motion data had to be enabled.
101+ uint apiSet = await SenseHelper . GetSupportedApiSetAsync ( ) ;
102+ MotionDataSettings settings = await SenseHelper . GetSettingsAsync ( ) ;
103+ if ( apiSet > 2 )
104+ {
105+ if ( ! settings . LocationEnabled )
106+ {
107+ 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" ) ;
108+ dlg . Commands . Add ( new UICommand ( "Yes" , new UICommandInvokedHandler ( async ( cmd ) => await SenseHelper . LaunchLocationSettingsAsync ( ) ) ) ) ;
109+ dlg . Commands . Add ( new UICommand ( "No" ) ) ;
110+ await dlg . ShowAsync ( ) ;
111+ }
112+ if ( ! settings . PlacesVisited )
113+ {
114+ MessageDialog dlg = null ;
115+ if ( settings . Version < 2 )
116+ {
117+ 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" ) ;
118+ }
119+ else
120+ {
121+ 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" ) ;
122+ }
123+ dlg . Commands . Add ( new UICommand ( "Yes" , new UICommandInvokedHandler ( async ( cmd ) => await SenseHelper . LaunchSenseSettingsAsync ( ) ) ) ) ;
124+ dlg . Commands . Add ( new UICommand ( "No" , new UICommandInvokedHandler ( ( cmd ) => { Application . Current . Exit ( ) ; } ) ) ) ;
125+ await dlg . ShowAsync ( ) ;
126+ }
127+ }
128+ }
129+ }
130+
80131 /// <summary>
81132 /// Performs asynchronous SensorCore SDK operation and handles any exceptions
82133 /// </summary>
@@ -93,7 +144,6 @@ private async Task<bool> CallSensorcoreApiAsync( Func<Task> action )
93144 {
94145 failure = e ;
95146 }
96-
97147 if ( failure != null )
98148 {
99149 MessageDialog dialog ;
@@ -106,21 +156,18 @@ private async Task<bool> CallSensorcoreApiAsync( Func<Task> action )
106156 await dialog . ShowAsync ( ) ;
107157 new System . Threading . ManualResetEvent ( false ) . WaitOne ( 500 ) ;
108158 return false ;
109-
110159 case SenseError . SenseDisabled :
111160 dialog = new MessageDialog ( "Motion data has been disabled. Do you want to open Motion data settings now?" , "Information" ) ;
112161 dialog . Commands . Add ( new UICommand ( "Yes" , async cmd => await SenseHelper . LaunchSenseSettingsAsync ( ) ) ) ;
113162 dialog . Commands . Add ( new UICommand ( "No" ) ) ;
114163 await dialog . ShowAsync ( ) ;
115164 new System . Threading . ManualResetEvent ( false ) . WaitOne ( 500 ) ;
116165 return false ;
117-
118166 case SenseError . SensorNotAvailable :
119167 dialog = new MessageDialog ( "The sensor is not supported on this device" , "Information" ) ;
120168 await dialog . ShowAsync ( ) ;
121169 new System . Threading . ManualResetEvent ( false ) . WaitOne ( 500 ) ;
122170 return false ;
123-
124171 default :
125172 dialog = new MessageDialog ( "Failure: " + SenseHelper . GetSenseError ( failure . HResult ) , "" ) ;
126173 await dialog . ShowAsync ( ) ;
@@ -157,5 +204,47 @@ await CallSensorcoreApiAsync( async () =>
157204 }
158205 } ) ;
159206 }
207+
208+ protected override void OnNavigatedTo ( NavigationEventArgs e )
209+ {
210+ base . OnNavigatedTo ( e ) ;
211+ CheckMotionDataSettings ( ) ;
212+ }
213+
214+ /// <summary>
215+ /// Play preinstalled recording button click handler
216+ /// </summary>
217+ /// <param name="sender">Sender object</param>
218+ /// <param name="args">Event arguments</param>
219+ private async void LoadButton_Click ( object sender , RoutedEventArgs e )
220+ {
221+ SenseRecording recording = null ;
222+ WebErrorStatus exceptionDetail = new WebErrorStatus ( ) ;
223+ try
224+ {
225+ recording = await SenseRecording . LoadFromUriAsync ( new Uri ( "https://github.com/Microsoft/steps/raw/master/Steps/Simulations/short%20walk.txt" ) ) ;
226+ }
227+ catch ( Exception ex )
228+ {
229+ exceptionDetail = WebError . GetStatus ( ex . GetBaseException ( ) . HResult ) ;
230+ }
231+ if ( exceptionDetail == WebErrorStatus . HostNameNotResolved )
232+ {
233+ MessageDialog dialog = new MessageDialog ( "Check your network connection. Host name could not be resolved." , "Information" ) ;
234+ await dialog . ShowAsync ( ) ;
235+ }
236+ if ( recording != null )
237+ {
238+ _stepCounter = await StepCounterSimulator . GetDefaultAsync ( recording ) ;
239+ MessageDialog dialog = new MessageDialog (
240+ "Recorded sensor type: " + recording . Type . ToString ( ) +
241+ "\r \n Description: " + recording . Description +
242+ "\r \n Recording date: " + recording . StartTime . ToString ( ) +
243+ "\r \n Duration: " + recording . Duration . ToString ( ) ,
244+ "Recording info"
245+ ) ;
246+ await dialog . ShowAsync ( ) ;
247+ }
248+ }
160249 }
161250}
0 commit comments