/
Message.cs
350 lines (314 loc) · 11.4 KB
/
Message.cs
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
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Tizen.Messaging.Messages
{
/// <summary>
/// This class represents all the messages.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public abstract class Message : IDisposable
{
internal IntPtr _messageHandle = IntPtr.Zero;
private bool disposed = false;
private int _memoryPressureSize = IntPtr.Size * 11 + sizeof(int) * 5 + sizeof(bool) * 5 + sizeof(short) * 2 + sizeof(byte) * 1176;
private ICollection<MessagesAddress> _from = new Collection<MessagesAddress>();
internal ICollection<MessagesAddress> _to = new Collection<MessagesAddress>();
internal ICollection<MessagesAddress> _cc = new Collection<MessagesAddress>();
internal ICollection<MessagesAddress> _bcc = new Collection<MessagesAddress>();
internal Message(MessageType type)
{
int ret = Interop.Messages.CreateMessage((int)type, out _messageHandle);
if (ret != (int)MessagesError.None)
{
Log.Error(Globals.LogTag, "Failed to create message handle, Error - " + (MessagesError)ret);
MessagesErrorFactory.ThrowMessagesException(ret);
}
GC.AddMemoryPressure(_memoryPressureSize);
}
internal Message(IntPtr messageHandle)
{
_messageHandle = messageHandle;
GetAllAddresses();
GC.AddMemoryPressure(_memoryPressureSize);
}
internal void FillHandle()
{
SetAddresses();
(this as MmsMessage)?.SetAttachments();
}
/// <summary>
/// Destructor
/// </summary>
~Message()
{
Dispose(false);
}
/// <summary>
/// Releases all resources used by the Message.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
GC.RemoveMemoryPressure(_memoryPressureSize);
}
private void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
// Free managed objects
}
// Free unmanaged objects
if (_messageHandle != IntPtr.Zero)
{
Interop.Messages.DestroyMessage(_messageHandle);
_messageHandle = IntPtr.Zero;
}
disposed = true;
}
internal IntPtr GetHandle()
{
return _messageHandle;
}
private void SetAddresses()
{
foreach (var it in _to)
{
AddAddress(it);
}
foreach (var it in _cc)
{
AddAddress(it);
}
foreach (var it in _bcc)
{
AddAddress(it);
}
}
private void AddAddress(MessagesAddress address)
{
int ret = Interop.Messages.AddAddress(_messageHandle, address.Number, (int)address.Type);
if (ret != (int)MessagesError.None)
{
Log.Error(Globals.LogTag, "Failed to add address, Error - " + (MessagesError)ret);
MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
}
}
private void GetAllAddresses()
{
int count;
int ret = Interop.Messages.GetAddressCount(_messageHandle, out count);
if (ret != (int)MessagesError.None)
{
Log.Error(Globals.LogTag, "Failed to get address count, Error - " + (MessagesError)ret);
MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
}
string number;
int type;
var To = new Collection<MessagesAddress>();
var Cc = new Collection<MessagesAddress>();
var Bcc = new Collection<MessagesAddress>();
var From = new Collection<MessagesAddress>();
for (int i = 0; i < count; i++)
{
ret = Interop.Messages.GetAddress(_messageHandle, i, out number, out type);
if (ret != (int)MessagesError.None)
{
Log.Error(Globals.LogTag, "Failed to get address, Error - " + (MessagesError)ret);
MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
}
var addressItem = new MessagesAddress((RecipientType)type, number);
switch ((RecipientType)type)
{
case RecipientType.To:
To.Add(addressItem);
break;
case RecipientType.Cc:
Cc.Add(addressItem);
break;
case RecipientType.Bcc:
Bcc.Add(addressItem);
break;
default:
From.Add(addressItem);
break;
}
}
_to = To;
_cc = Cc;
_bcc = Bcc;
_from = From;
}
/// <summary>
/// The message ID.
/// </summary>
/// <remarks>
/// After creating the Message object, the default value of this property is 0. After sending, this value is changed.
/// </remarks>
/// <since_tizen> 3 </since_tizen>
public int Id
{
get
{
int id = 0;
int ret = Interop.Messages.GetMessageId(_messageHandle, out id);
if (ret != (int)MessagesError.None)
{
Log.Error(Globals.LogTag, "Failed to get message id, Error - " + (MessagesError)ret);
}
return id;
}
}
/// <summary>
/// The destination port of the message.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public int Port
{
get
{
int port = 0;
int ret = Interop.Messages.GetMessagePort(_messageHandle, out port);
if (ret != (int)MessagesError.None)
{
Log.Error(Globals.LogTag, "Failed to get message port, Error - " + (MessagesError)ret);
}
return port;
}
}
/// <summary>
/// The message box type.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public MessageBoxType BoxType
{
get
{
int boxType = (int)MessageBoxType.All;
int ret = Interop.Messages.GetMboxType(_messageHandle, out boxType);
if (ret != (int)MessagesError.None)
{
Log.Error(Globals.LogTag, "Failed to get message box type, Error - " + (MessagesError)ret);
}
return (MessageBoxType)boxType;
}
set
{
int ret = Interop.Messages.SetMboxType(_messageHandle, (int)value);
if (ret != (int)MessagesError.None)
{
Log.Error(Globals.LogTag, "Failed to set message box type, Error - " + (MessagesError)ret);
MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
}
}
}
/// <summary>
/// The text of the message.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public string Text
{
get
{
string text = null;
int ret = Interop.Messages.GetText(_messageHandle, out text);
if (ret != (int)MessagesError.None)
{
Log.Error(Globals.LogTag, "Failed to get text, Error - " + (MessagesError)ret);
}
return text;
}
set
{
int ret = Interop.Messages.SetText(_messageHandle, value);
if (ret != (int)MessagesError.None)
{
Log.Error(Globals.LogTag, "Failed to set text, Error - " + (MessagesError)ret);
MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
}
}
}
/// <summary>
/// The time of the message.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public DateTime Time
{
get
{
int time = 0;
int ret = Interop.Messages.GetTime(_messageHandle, out time);
if (ret != (int)MessagesError.None)
{
Log.Error(Globals.LogTag, "Failed to get time, Error - " + (MessagesError)ret);
}
return (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddSeconds(time).ToLocalTime();
}
set
{
int ret = Interop.Messages.SetTime(_messageHandle, (int)(value.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds));
if (ret != (int)MessagesError.None)
{
Log.Error(Globals.LogTag, "Failed to set time, Error - " + (MessagesError)ret);
MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
}
}
}
/// <summary>
/// The SIM slot index of the message.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public SimSlotId SimId
{
get
{
int simId = (int)SimSlotId.Unknown;
int ret = Interop.Messages.GetSimId(_messageHandle, out simId);
if (ret != (int)MessagesError.None)
{
Log.Error(Globals.LogTag, "Failed to get simId, Error - " + (MessagesError)ret);
}
return (SimSlotId)simId;
}
set
{
int ret = Interop.Messages.SetSimId(_messageHandle, (int)value);
if (ret != (int)MessagesError.None)
{
Log.Error(Globals.LogTag, "Failed to set simId, Error - " + (MessagesError)ret);
MessagesErrorFactory.ThrowMessagesException(ret, _messageHandle);
}
}
}
/// <summary>
/// Indicates the sender of the message.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public IReadOnlyCollection<MessagesAddress> From
{
get
{
return _from as IReadOnlyCollection<MessagesAddress>;
}
}
}
}