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
|
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using InTheHand.WindowsMobile.Status;
using InTheHand.WindowsMobile.PocketOutlook;
using InTheHand.WindowsMobile.PocketOutlook.MessageInterception;
using InTheHand.Media;
namespace Test.Alertes
{
public partial class MainForm : Form
{
private const string SMS_SENDER = "+33xxxxxxx";
private const string SMS_BODY_START = "Test:";
private const string ALERTE_WAV = "Alert.wav";
private const int ALERTE_DURATION = 5;
private const string ERROR_INFO = "Une erreur s'est produite lors de la réception d'un message.";
private MessageInterceptor _smsInterception = null;
private bool _alerte = false;
private bool _smsEnable = false;
private DateTime _lastAlerte = DateTime.Now.AddYears(-1);
private object _alerteLock = new object();
const string _persistentIdentifier = "Test:";
public MainForm()
{
InitializeComponent();
_lastAlert = DateTime.Now;
smsBox.Image = Resource.Bouton_Off;
emailBox.Image = Resource.Bouton_Off;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(Resource.Background, this.ClientRectangle,
new Rectangle(0, 0, Resource.Background.Width, Resource.Background.Height),
GraphicsUnit.Pixel);
}
void stateSms_MessageReceived(object sender, MessageInterceptorEventArgs e)
{
try
{
SmsMessage msg = (SmsMessage)e.Message;
if (msg != null)
{
if (msg.From.Address.Trim().ToUpper().Equals(SMS_SENDER.ToUpper()))
{
if (msg.Body.Length > 0 && msg.Body.TrimStart().ToUpper().StartsWith(SMS_BODY_START.ToUpper()))
{
Alert();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ERROR_INFO + " " + ex.ToString());
}
}
private void Alerte()
{
lock (_alerteLock)
{
if (!_alerte)
{
try
{
if (_lastAlerte.CompareTo(DateTime.Now.AddSeconds(0 - ALERTE_DURATION - 10)) < 0)
{
_alerte = true;
SoundPlayer player = new SoundPlayer(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\" + ALERTE_WAV);
Sound.Volume currentVolume = Sound.GetVolume();
Sound.SetVolume(Sound.Volume.VERY_HIGH);
player.PlaySync();
Sound.SetVolume(currentVolume);
player.Dispose();
_lastAlerte = DateTime.Now;
_alerte = false;
}
}
catch (Exception ex)
{
_alerte = false;
MessageBox.Show(ERROR_INFO + " " + ex.ToString());
}
}
}
}
private void MainForm_Closing(object sender, CancelEventArgs e)
{
StopSms();
}
private void StopSms()
{
if(_smsInterception!=null)
{
_smsInterception.DisableApplicationLauncher();
_smsInterception.MessageReceived -= new MessageInterceptorEventHandler(stateSms_MessageReceived);
_smsInterception.Dispose();
_smsInterception = null;
}
_smsEnable = false;
}
private void StartSms()
{
//if (!_smsEnable)
//{
//_smsInterception = new MessageInterceptor(InterceptionAction.Notify);
//_smsInterception.MessageCondition = new MessageCondition(InTheHand.WindowsMobile.PocketOutlook.MessageInterception.MessageProperty.Sender, MessagePropertyComparisonType.Equal, SMS_SENDER, false);
//_smsInterception.EnableApplicationLauncher("AlertesSms");
//_smsInterception.MessageReceived += new MessageInterceptorEventHandler(stateSms_MessageReceived);
//_smsEnable = true;
if (!MessageInterceptor.IsApplicationLauncherEnabled(_persistentIdentifier))
{
// Persistent notification does not yet exist - must explicitly create
_smsInterception = new MessageInterceptor(InterceptionAction.NotifyAndDelete, false);
_smsInterception.MessageCondition = new MessageCondition(InTheHand.WindowsMobile.PocketOutlook.MessageInterception.MessageProperty.Sender, MessagePropertyComparisonType.Equal, SMS_SENDER, false);
// Make the interceptor persistent
_smsInterception.EnableApplicationLauncher(_persistentIdentifier);
}
else
{
// Persistent notification already defined - create this instance using the
// same characteristics
_smsInterception = new MessageInterceptor(_persistentIdentifier, false);
}
// Once the interceptor is created, add event handler. Whether the interceptor is constructed
// explicitly or constructed from the persistent notification identifier,
// the event handling behavior is the same.
_smsInterception.MessageReceived += new MessageInterceptorEventHandler(stateSms_MessageReceived);
//}
}
private void menuDisablePersistentNotification_Click(object sender, EventArgs e)
{
// Confirm that _smsInterceptor is a valid reference, that the current
// _smsInterceptor instance is associated with the correct persistent
// notification identifier, and that a persistent notification exists
// that has the specified identifier
if (_smsInterception != null && _smsInterception.ApplicationLaunchId == _persistentIdentifier && MessageInterceptor.IsApplicationLauncherEnabled(_persistentIdentifier))
{
_smsInterception.DisableApplicationLauncher();
}
}
private void smsBox_Click(object sender, EventArgs e)
{
smsBox.Enabled = false;
smsBox.Image = Resource.Bouton_Switch;
if (!_smsEnable)
{
StartSms();
smsBox.Image = Resource.Bouton_On;
}
else
{
StopSms();
smsBox.Image = Resource.Bouton_Off;
}
smsBox.Enabled = true;
}
}
} |
Partager