forked from ms-iot/samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlideshowPage.xaml.cs
More file actions
351 lines (314 loc) · 15 KB
/
SlideshowPage.xaml.cs
File metadata and controls
351 lines (314 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
Copyright(c) Microsoft Open Technologies, Inc.All rights reserved.
The MIT License(MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.Popups;
using Windows.UI.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Web.Http;
using System.Xml.Linq;
using System.Threading;
using System.Text;
namespace DigitalSignageUAP
{
struct DisplayObject
{
public StorageFile file; // for image, and video that are saved to local
public Uri uri; //for webpage
public int duration; // only need this when it's a image or URI
}
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class SlideshowPage : Page
{
const string szRootFolder = "DigitalSignage";
const string szAudioFolder = "Audio";
const string szDisplayFolder = "Display";
List<string> imageExtensions = new List<string>(new string[] { ".bmp", ".gif", ".ico", ".jpg", ".png", ".wdp", ".tiff" }); // MSDN
readonly string defaultConfigFilePath = @"Assets\config.xml";
const string configValueName = "ConfigFilePath";
static List<object> audioList;
static List<object> displayList;
int currentIndexOfAudio = 0;
int currentIndexOfDisplay = 0;
static DispatcherTimer AcceptUserInputTimer;
DispatcherTimer DisplayImageWEBTimer;
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
static string currentConfigFilePath;
ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
BitmapImage imageSource = new BitmapImage();
/// <summary>
///
/// </summary>
public SlideshowPage()
{
this.InitializeComponent();
// if we do not have the config in app's local settings, it means it's first time app launch
// write the config path to local settings
if (localSettings.Values[configValueName] == null)
{
localSettings.Values[configValueName] = defaultConfigFilePath;
}
// get the current config path from local settings
currentConfigFilePath = (string)localSettings.Values[configValueName];
this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
this.PointerMoved += SlideshowPage_PointerMoved;
DisplayImageWEBTimer = new DispatcherTimer();
DisplayImageWEBTimer.Tick += DisplayImageWEBTimer_Tick;
AcceptUserInputTimer = new DispatcherTimer();
AcceptUserInputTimer.Interval = new TimeSpan(0, 0, 5);
AcceptUserInputTimer.Tick += AcceptUserInputTimer_Tick;
audioInstance.MediaEnded += audioInstance_MediaEnded;
videoInstance.MediaEnded += videoInstance_MediaEnded;
displayList = new List<object>();
audioList = new List<object>();
AcceptUserInputTimer.Start();
StartSlideShow();
GlobalTimerWrapper.StartReloadContentTimer(this); // when slideshow is being played back, we want it resync the config file and reload content at 12:00AM
}
private void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
{
if (AcceptUserInputTimer.IsEnabled == false &&
(videoInstance.Visibility == Visibility.Visible ||
audioInstance.Visibility == Visibility.Visible ||
imageInstance.Visibility == Visibility.Visible))
{
audioInstance.Visibility = Visibility.Collapsed;
imageInstance.Visibility = Visibility.Collapsed;
videoInstance.Visibility = Visibility.Collapsed;
this.Frame.Navigate(typeof(MainPage));
}
}
void updateTimer_Tick(object sender, object e)
{
}
void DisplayImageWEBTimer_Tick(object sender, object e)
{
DisplayNext(); // move to the next one in display queue
}
void AcceptUserInputTimer_Tick(object sender, object e)
{
AcceptUserInputTimer.Stop();
}
void SlideshowPage_PointerMoved(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
if (AcceptUserInputTimer.IsEnabled == false)
{
audioInstance.Visibility = Visibility.Collapsed;
imageInstance.Visibility = Visibility.Collapsed;
videoInstance.Visibility = Visibility.Collapsed;
this.Frame.Navigate(typeof(MainPage));
}
}
public async void StartSlideShow()
{
await GetConfigAndParse();
DisplayNext();
}
/// <summary>
/// upload telemetry when navigating to this page
/// </summary>
/// <param name="e"></param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
videoInstance.Visibility = Visibility.Visible;
audioInstance.Visibility = Visibility.Visible;
imageInstance.Visibility = Visibility.Visible;
base.OnNavigatedTo(e);
}
/// <summary>
/// upload telemetry when navigating from this page (leaving this page)
/// </summary>
/// <param name="e"></param>
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
}
/// <summary>
///
/// </summary>
/// <param name="command"></param>
void InvalidConfigDialogCommandInvokeHandler(IUICommand command)
{
audioInstance.Visibility = Visibility.Collapsed;
imageInstance.Visibility = Visibility.Collapsed;
videoInstance.Visibility = Visibility.Collapsed;
this.Frame.Navigate(typeof(MainPage));
}
/// <summary>
/// check if next one is image, continue the audio and display the image for seconds defined by the timer interval
/// of next is video, pause the audio, and play back the video
/// </summary>
async void DisplayNext()
{
if (displayList.Count == 0)
{
MessageDialog dialog = new MessageDialog("You've entered an invalid or empty config file, it's been reset to default.");
localSettings.Values[configValueName] = defaultConfigFilePath;
dialog.Commands.Add(new UICommand("OK", new UICommandInvokedHandler(InvalidConfigDialogCommandInvokeHandler)));
dialog.ShowAsync(); // show a dialog with only one button to return to homepage
return;
}
DisplayObject currentDO = (DisplayObject) displayList[currentIndexOfDisplay];
if (currentDO.uri != null) // we're dealing with a WEB Page, show the WebView instance
{
videoInstance.Stop();
videoInstance.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
imageInstance.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
DisplayImageWEBTimer.Interval = new TimeSpan(0, 0, currentDO.duration);
DisplayImageWEBTimer.Start();
PlayAudio();
}
else // it must be StorageFile, i.e. image or video
{
if (imageExtensions.Contains(currentDO.file.FileType.ToLower())) // image, will start or resume audio play back
{
videoInstance.Stop();
videoInstance.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
imageInstance.Visibility = Windows.UI.Xaml.Visibility.Visible;
imageSource = new BitmapImage(new Uri(currentDO.file.Path));
imageInstance.Width = imageSource.DecodePixelHeight = (int)this.ActualWidth;
imageInstance.Source = imageSource;
DisplayImageWEBTimer.Interval = new TimeSpan(0, 0, currentDO.duration);
DisplayImageWEBTimer.Start();
PlayAudio();
}
else // video, we'll pause audio playback
{
audioInstance.Pause();
videoInstance.Source = new Uri(currentDO.file.Path);
audioInstance.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
imageInstance.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
videoInstance.Visibility = Windows.UI.Xaml.Visibility.Visible;
videoInstance.Play();
}
}
currentIndexOfDisplay = (++currentIndexOfDisplay) % displayList.Count; // make the index in a loop
}
/// <summary>
/// when an audio is finished, call this function to move to next audio
/// this could also be called when a video is finished, pick up from where it was paused
/// </summary>
void PlayAudio()
{
if (audioList.Count == 0)
return;
if (audioInstance.CurrentState == MediaElementState.Paused)
{
videoInstance.Visibility = Visibility.Collapsed;
audioInstance.Visibility = Visibility.Visible;
audioInstance.Play();
}
else if (audioInstance.CurrentState != MediaElementState.Playing)
{
audioInstance.Source = new System.Uri(((DisplayObject)audioList[currentIndexOfAudio]).file.Path);
videoInstance.Visibility = Visibility.Collapsed;
audioInstance.Visibility = Visibility.Visible;
audioInstance.Play();
currentIndexOfAudio = (++currentIndexOfAudio) % audioList.Count;
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public async Task GetConfigAndParse()
{
displayList.Clear(); // clear the list since this function is called when the app resyncs content at mid night
audioList.Clear(); // see above
StorageFolder displayFolder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync("DigitalSignal\\Display", CreationCollisionOption.OpenIfExists);
StorageFolder audioFolder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync("DigitalSignal\\Audio", CreationCollisionOption.OpenIfExists);
try
{
HttpClient configClient = new HttpClient();
string configStr = File.ReadAllText(currentConfigFilePath);
XElement xele = XElement.Parse(configStr);
foreach (XElement xe in xele.Elements())
{
StorageFolder tmp = xe.Name == "Audio" ? audioFolder : displayFolder;
List<object> tmpList = xe.Name == "Audio" ? audioList : displayList;
foreach (XElement fileElement in xe.Elements()) // audio
{
if (fileElement.Attribute("type") != null && fileElement.Attribute("type").Value == "webpage") // the display is a webpage, not image, not video, we create and store a new Uri Object. Only url display type has type attribute
{
DisplayObject DO = new DisplayObject();
DO.uri = new Uri(fileElement.Attribute("path").Value);
DO.duration = Convert.ToInt32(fileElement.Attribute("duration").Value);
tmpList.Add(DO);
}
else
{
DisplayObject DO = new DisplayObject();
string filename = fileElement.Attribute("path").Value.Substring(fileElement.Attribute("path").Value.LastIndexOf('/') + 1);
StorageFile file = await tmp.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
byte[] bytes = File.ReadAllBytes(filename);
await FileIO.WriteBufferAsync(file, WindowsRuntimeBufferExtensions.AsBuffer(bytes));
if (fileElement.Attribute("duration") != null) // this is an image
DO.duration = Convert.ToInt32(fileElement.Attribute("duration").Value);
DO.file = file;
tmpList.Add(DO);
}
}
}
}
catch (Exception)
{
// use this to capture any config xml parsing error, we don't expose what they are
// but only popup a dialog to user saying something is wrong..
}
}
/// <summary>
/// when a video is finished, move to next displayObject
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void videoInstance_MediaEnded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
// videoInstance.IsFullWindow = false;
DisplayNext();
}
/// <summary>
/// a audio loop, when one audio is finished, move to next one
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void audioInstance_MediaEnded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
PlayAudio();
}
}
}