Release 4.0.0-preview1-00130
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Nfc / Tizen.Network.Nfc / NfcManager.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Threading.Tasks;
20 using Tizen.System;
21
22 namespace Tizen.Network.Nfc
23 {
24     /// <summary>
25     /// A class for the NFC management. It allows applications to use the NFC service.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     /// <privilege>http://tizen.org/privilege/nfc</privilege>
29     static public class NfcManager
30     {
31         /// <summary>
32         /// Checks whether the NFC is supported.
33         /// </summary>
34         /// <since_tizen> 3 </since_tizen>
35         static public bool IsSupported
36         {
37             get
38             {
39                 bool isNfcSupported = false;
40
41                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
42
43                 if (!isNfcSupported)
44                 {
45                     throw new NotSupportedException();
46                 }
47
48                 try
49                 {
50                     return NfcManagerImpl.Instance.IsSupported;
51                 }
52                 catch (TypeInitializationException e)
53                 {
54                     throw e.InnerException;
55                 }
56             }
57         }
58
59         /// <summary>
60         /// The NFC Activation state.
61         /// </summary>
62         /// <since_tizen> 3 </since_tizen>
63         static public bool IsActivated
64         {
65             get
66             {
67                 bool isNfcSupported = false;
68
69                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
70
71                 if (!isNfcSupported)
72                 {
73                     throw new NotSupportedException();
74                 }
75
76                 try
77                 {
78                     return NfcManagerImpl.Instance.IsActivated;
79                 }
80                 catch (TypeInitializationException e)
81                 {
82                     throw e.InnerException;
83                 }
84             }
85         }
86
87         /// <summary>
88         /// The Tag Filter type.
89         /// </summary>
90         /// <since_tizen> 3 </since_tizen>
91         static public NfcTagFilterType TagFilterType
92         {
93             get
94             {
95                 bool isNfcSupported = false;
96                 bool isTagSupported = false;
97
98                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
99                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.tag", out isTagSupported);
100
101                 if (!isNfcSupported || !isTagSupported)
102                 {
103                     throw new NotSupportedException();
104                 }
105
106                 try
107                 {
108                     return NfcManagerImpl.Instance.TagFilterType;
109                 }
110                 catch (TypeInitializationException e)
111                 {
112                     throw e.InnerException;
113                 }
114             }
115             set
116             {
117                 bool isNfcSupported = false;
118                 bool isTagSupported = false;
119
120                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
121                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.tag", out isTagSupported);
122
123                 if (!isNfcSupported || !isTagSupported)
124                 {
125                     throw new NotSupportedException();
126                 }
127
128                 try
129                 {
130                     NfcManagerImpl.Instance.TagFilterType = value;
131                 }
132                 catch (TypeInitializationException e)
133                 {
134                     throw e.InnerException;
135                 }
136             }
137         }
138
139         /// <summary>
140         /// The Secure Element type.
141         /// </summary>
142         /// <since_tizen> 3 </since_tizen>
143         /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
144         static public NfcSecureElementType SecureElementType
145         {
146             get
147             {
148                 bool isNfcSupported = false;
149                 bool isCeSupported = false;
150
151                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
152                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.cardemulation", out isCeSupported);
153
154                 if (!isNfcSupported || !isCeSupported)
155                 {
156                     throw new NotSupportedException();
157                 }
158
159                 try
160                 {
161                     return NfcManagerImpl.Instance.SecureElementType;
162                 }
163                 catch (TypeInitializationException e)
164                 {
165                     throw e.InnerException;
166                 }
167             }
168             set
169             {
170                 bool isNfcSupported = false;
171                 bool isCeSupported = false;
172
173                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
174                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.cardemulation", out isCeSupported);
175
176                 if (!isNfcSupported || !isCeSupported)
177                 {
178                     throw new NotSupportedException();
179                 }
180
181                 try
182                 {
183                     NfcManagerImpl.Instance.SecureElementType = value;
184                 }
185                 catch (TypeInitializationException e)
186                 {
187                     throw e.InnerException;
188                 }
189             }
190         }
191
192         /// <summary>
193         /// Enables or disables the system handling for the tag and target discovered event.
194         /// </summary>
195         /// <since_tizen> 3 </since_tizen>
196         /// <privilege>http://tizen.org/privilege/nfc</privilege>
197         static public bool SystemHandlerEnabled
198         {
199             get
200             {
201                 bool isNfcSupported = false;
202
203                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
204
205                 if (!isNfcSupported)
206                 {
207                     throw new NotSupportedException();
208                 }
209
210                 try
211                 {
212                     return NfcManagerImpl.Instance.SystemHandlerEnabled;
213                 }
214                 catch (TypeInitializationException e)
215                 {
216                     throw e.InnerException;
217                 }
218             }
219             set
220             {
221                 bool isNfcSupported = false;
222
223                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
224
225                 if (!isNfcSupported)
226                 {
227                     throw new NotSupportedException();
228                 }
229
230                 try
231                 {
232                     NfcManagerImpl.Instance.SystemHandlerEnabled = value;
233                 }
234                 catch (TypeInitializationException e)
235                 {
236                     throw e.InnerException;
237                 }
238             }
239         }
240
241         /// <summary>
242         /// The cached NDEF message.
243         /// </summary>
244         /// <since_tizen> 3 </since_tizen>
245         static public NfcNdefMessage CachedNdefMessage
246         {
247             get
248             {
249                 bool isNfcSupported = false;
250
251                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
252
253                 if (!isNfcSupported)
254                 {
255                     throw new NotSupportedException();
256                 }
257
258                 try
259                 {
260                     return NfcManagerImpl.Instance.CachedNdefMessage;
261                 }
262                 catch (TypeInitializationException e)
263                 {
264                     throw e.InnerException;
265                 }
266             }
267         }
268
269         /// <summary>
270         /// Gets the Tag adapter object.
271         /// </summary>
272         /// <since_tizen> 3 </since_tizen>
273         static public NfcTagAdapter GetTagAdapter()
274         {
275             bool isNfcSupported = false;
276             bool isTagSupported = false;
277
278             SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
279             SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.tag", out isTagSupported);
280
281             if (!isNfcSupported || !isTagSupported)
282             {
283                 throw new NotSupportedException();
284             }
285
286             try
287             {
288                 return NfcManagerImpl.Instance.TagAdapter;
289             }
290             catch (TypeInitializationException e)
291             {
292                 throw e.InnerException;
293             }
294         }
295
296         /// <summary>
297         /// Gets the P2P adapter object.
298         /// </summary>
299         /// <since_tizen> 3 </since_tizen>
300         static public NfcP2pAdapter GetP2pAdapter()
301         {
302             bool isNfcSupported = false;
303             bool isP2pSupported = false;
304
305             SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
306             SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.p2p", out isP2pSupported);
307
308             if (!isNfcSupported || !isP2pSupported)
309             {
310                 throw new NotSupportedException();
311             }
312
313             try
314             {
315                 return NfcManagerImpl.Instance.P2pAdapter;
316             }
317             catch (TypeInitializationException e)
318             {
319                 throw e.InnerException;
320             }
321         }
322
323         /// <summary>
324         /// Gets the Card Emulation adapter object.
325         /// </summary>
326         /// <since_tizen> 3 </since_tizen>
327         static public NfcCardEmulationAdapter GetCardEmulationAdapter()
328         {
329             bool isNfcSupported = false;
330             bool isCeSupported = false;
331
332             SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
333             SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.cardemulation", out isCeSupported);
334
335             if (!isNfcSupported || !isCeSupported)
336             {
337                 throw new NotSupportedException();
338             }
339
340             try
341             {
342                 return NfcManagerImpl.Instance.CardEmulationAdapter;
343             }
344             catch (TypeInitializationException e)
345             {
346                 throw e.InnerException;
347             }
348         }
349
350         /// <summary>
351         /// Activates the NFC asynchronously.
352         /// </summary>
353         /// <since_tizen> 3 </since_tizen>
354         /// <returns>A task indicates whether the Activate method is done or not.</returns>
355         /// <privilege>http://tizen.org/privilege/nfc.admin</privilege>
356         static public Task SetActivationAsync(bool activation)
357         {
358             bool isNfcSupported = false;
359
360             SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
361
362             if (!isNfcSupported)
363             {
364                 throw new NotSupportedException();
365             }
366
367             try
368             {
369                 return NfcManagerImpl.Instance.SetActivationAsync(activation);
370             }
371             catch (TypeInitializationException e)
372             {
373                 throw e.InnerException;
374             }
375         }
376
377         /// <summary>
378         /// The Activation changed event.
379         /// </summary>
380         /// <since_tizen> 3 </since_tizen>
381         static public event EventHandler<ActivationChangedEventArgs> ActivationChanged
382         {
383             add
384             {
385                 bool isNfcSupported = false;
386
387                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
388
389                 if (!isNfcSupported)
390                 {
391                     throw new NotSupportedException();
392                 }
393
394                 try
395                 {
396                     NfcManagerImpl.Instance.ActivationChanged += value;
397                 }
398                 catch (TypeInitializationException e)
399                 {
400                     throw e.InnerException;
401                 }
402             }
403             remove
404             {
405                 bool isNfcSupported = false;
406
407                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
408
409                 if (!isNfcSupported)
410                 {
411                     throw new NotSupportedException();
412                 }
413
414                 try
415                 {
416                     NfcManagerImpl.Instance.ActivationChanged -= value;
417                 }
418                 catch (TypeInitializationException e)
419                 {
420                     throw e.InnerException;
421                 }
422             }
423         }
424
425         /// <summary>
426         /// The NDEF discovered event.
427         /// </summary>
428         /// <since_tizen> 3 </since_tizen>
429         static public event EventHandler<NdefMessageDiscoveredEventArgs> NdefMessageDiscovered
430         {
431             add
432             {
433                 bool isNfcSupported = false;
434
435                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
436
437                 if (!isNfcSupported)
438                 {
439                     throw new NotSupportedException();
440                 }
441
442                 try
443                 {
444                     NfcManagerImpl.Instance.NdefMessageDiscovered += value;
445                 }
446                 catch (TypeInitializationException e)
447                 {
448                     throw e.InnerException;
449                 }
450             }
451             remove
452             {
453                 bool isNfcSupported = false;
454
455                 SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
456
457                 if (!isNfcSupported)
458                 {
459                     throw new NotSupportedException();
460                 }
461
462                 try
463                 {
464                     NfcManagerImpl.Instance.NdefMessageDiscovered -= value;
465                 }
466                 catch (TypeInitializationException e)
467                 {
468                     throw e.InnerException;
469                 }
470             }
471         }
472     }
473 }