using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsPhone.Recipes.Push.Messasges.Properties;
namespace WindowsPhone.Recipes.Push.Messasges
{
///
/// Represents a tile push notification message.
///
///
/// Every phone application has one assigned 'tile' – a visual, dynamic
/// representation of the application or its content. A tile displays in
/// the Start screen if the end user has pinned it.
///
/// This class members are thread safe.
///
public sealed class TilePushNotificationMessage : PushNotificationMessage
{
#region Constants
/// Calculated tile message headers size.
/// This should ne updated if changing the protocol.
private const int TileMessageHeadersSize = 146;
/// Tile push notification message maximum payload size.
public const int MaxPayloadSize = MaxMessageSize - TileMessageHeadersSize;
/// The minimum value.
public const int MinCount = 0;
/// The maximum value.
public const int MaxCount = 99;
/// Windows phone target.
private const string WindowsPhoneTarget = "token";
/// A well formed structure of the tile notification message.
private const string PayloadString =
"" +
"" +
"" +
"{0}" +
"{1}" +
"{2}" +
"" +
"";
#endregion
#region Fields
/// The phone's local path, or a remote path for the background image.
private Uri _backgroundImageUri;
/// An integer value to be displayed in the tile.
private int _count = MinCount;
/// The title text should be displayed in the tile.
private string _title;
#endregion
#region Properties
///
/// Gets or sets the phone's local path, or a remote path for the background image.
///
///
/// If the uri references a remote resource, the maximum allowed size of the tile
/// image is 80 KB, with a maximum download time of 15 seconds.
///
public Uri BackgroundImageUri
{
get
{
return _backgroundImageUri;
}
set
{
SafeSet(ref _backgroundImageUri, value);
}
}
///
/// Gets or sets an integer value from 1 to 99 to be displayed in the tile, or 0 to clear count.
///
public int Count
{
get
{
return _count;
}
set
{
if (value < MinCount || value > MaxCount)
{
throw new ArgumentOutOfRangeException(string.Format(Resources.CountValueIsNotValid, value, MinCount, MaxCount));
}
SafeSet(ref _count, value);
}
}
///
/// Gets or sets the title text should be displayed in the tile. Null keeps the existing title.
///
///
/// The Title must fit a single line of text and should not be wider than the actual tile.
/// Imperatively a good number of letters would be 18-20 characters long.
///
public string Title
{
get
{
return _title;
}
set
{
SafeSet(ref _title, value);
}
}
///
/// Tile push notification message class id.
///
protected override int NotificationClassId
{
get { return 1; }
}
#endregion
#region Ctor
///
/// Initializes a new instance of this type.
///
/// The send priority of this message in the MPNS.
public TilePushNotificationMessage(MessageSendPriority sendPriority = MessageSendPriority.Normal)
: base(sendPriority)
{
}
#endregion
#region Overrides
///
/// Create the tile message payload.
///
/// The message payload bytes.
protected override byte[] OnCreatePayload()
{
var payloadString = string.Format(PayloadString, BackgroundImageUri, Count, Title);
return Encoding.ASCII.GetBytes(payloadString);
}
///
/// Initialize the request with tile specific headers.
///
/// The message request.
protected override void OnInitializeRequest(System.Net.HttpWebRequest request)
{
request.Headers[Headers.WindowsPhoneTarget] = WindowsPhoneTarget;
}
protected override void VerifyPayloadSize(byte[] payload)
{
if (payload.Length > MaxPayloadSize)
{
throw new ArgumentOutOfRangeException(string.Format(Resources.PayloadSizeIsTooBig, MaxPayloadSize));
}
}
#endregion
}
}