TileSchedulePushPatternViewModel.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel.Composition;
  6. using System.Windows.Input;
  7. using System.IO;
  8. using Microsoft.Win32;
  9. using WindowsPhone.Recipes.Push.Messasges;
  10. using WindowsPhone.Recipes.Push.Server.Models;
  11. using WindowsPhone.Recipes.Push.Server.Services;
  12. namespace WindowsPhone.Recipes.Push.Server.ViewModels
  13. {
  14. /// <summary>
  15. /// Represents the Tile Schedule push notification pattern.
  16. /// </summary>
  17. /// <remarks>
  18. /// This push pattern is passive. The user schedule a tile image update request,
  19. /// by using the Windows Phone API, and at the time, MPNS fetches the image using
  20. /// the uri provided with the request.
  21. /// </remarks>
  22. [Export(typeof(PushPatternViewModel)), PartCreationPolicy(CreationPolicy.Shared)]
  23. internal sealed class TileSchedulePushPatternViewModel : PushPatternViewModel
  24. {
  25. #region Fields
  26. private string _imageFileName;
  27. #endregion
  28. #region Properties
  29. [Import]
  30. private ImageService ImageService { get; set; }
  31. /// <summary>
  32. /// Gets or sets a image file name sent by the user.
  33. /// </summary>
  34. public string ImageFileName
  35. {
  36. get { return _imageFileName; }
  37. set
  38. {
  39. if (_imageFileName != value)
  40. {
  41. _imageFileName = value;
  42. NotifyPropertyChanged("ImageFileName");
  43. }
  44. }
  45. }
  46. #endregion
  47. #region Ctor
  48. /// <summary>
  49. /// Initialize new instance of this type with defaults.
  50. /// </summary>
  51. public TileSchedulePushPatternViewModel()
  52. {
  53. InitializeDefaults();
  54. }
  55. #endregion
  56. #region Overrides
  57. protected override void OnActivated()
  58. {
  59. base.OnActivated();
  60. // Register to the PushService.TileUpdateRequest event. This event is raised
  61. // whenever a user asks to update its tile.
  62. PushService.TileUpdateRequest += PushService_TileUpdateRequest;
  63. // Register to the ImageService.ImageRequest event. This event is raised
  64. // whenever ImageService.GetTileImage is called.
  65. ImageService.ImageRequest += Service_ImageRequest;
  66. }
  67. protected override void OnDeactivated()
  68. {
  69. base.OnDeactivated();
  70. PushService.TileUpdateRequest -= PushService_TileUpdateRequest;
  71. ImageService.ImageRequest -= Service_ImageRequest;
  72. }
  73. protected override void OnSend()
  74. {
  75. // Nothing to do here.
  76. }
  77. #endregion
  78. #region Privates
  79. private void PushService_TileUpdateRequest(object sender, TileUpdateRequestEventArgs e)
  80. {
  81. // Send a tile notification message to the relevant device.
  82. var tileMsg = new TilePushNotificationMessage(MessageSendPriority.High)
  83. {
  84. BackgroundImageUri = new Uri(string.Format(ImageService.GetTileImageService, e.Parameter))
  85. };
  86. tileMsg.SendAsync(e.ChannelUri, Log, Log);
  87. }
  88. private void Service_ImageRequest(object sender, Services.ImageRequestEventArgs e)
  89. {
  90. ImageFileName = e.Parameter;
  91. // This event is raised by our local push-service as result of
  92. // the tile msg we've sent to a subscriber. This is the time
  93. // to pick the right tile image for the subscriber.
  94. string imageFile = Path.Combine("Resources/TileImages/Numbers", e.Parameter);
  95. if (File.Exists(imageFile))
  96. {
  97. using (var reader = File.OpenRead(imageFile))
  98. {
  99. byte[] imageBuffer = new byte[reader.Length];
  100. int bytesRead = reader.Read(imageBuffer, 0, imageBuffer.Length);
  101. e.ImageStream.Write(imageBuffer, 0, bytesRead);
  102. }
  103. }
  104. }
  105. private void InitializeDefaults()
  106. {
  107. DisplayName = "Tile Schedule";
  108. Description = "This push pattern is passive. The user schedule a tile image update request, by using the Windows Phone API, and at the time, MPNS fetches the image using the uri provided with the request.";
  109. }
  110. #endregion
  111. }
  112. }