Release 4.0.0-preview1-00051
[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 NFC management. It allows applications to use 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         /// Whether NFC is supported.
33         /// </summary>
34         /// <since_tizen> 3 </since_tizen>
35         static public bool IsSupported
36         {
37             get
38             {
39                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
40
41                 if (!isNfcSupported)
42                 {
43                     throw new NotSupportedException();
44                 }
45
46                 try
47                 {
48                     return NfcManagerImpl.Instance.IsSupported;
49                 }
50                 catch (TypeInitializationException e)
51                 {
52                     throw e.InnerException;
53                 }
54             }
55         }
56
57         /// <summary>
58         /// NFC Activation state.
59         /// </summary>
60         /// <since_tizen> 3 </since_tizen>
61         static public bool IsActivated
62         {
63             get
64             {
65                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
66
67                 if (!isNfcSupported)
68                 {
69                     throw new NotSupportedException();
70                 }
71
72                 try
73                 {
74                     return NfcManagerImpl.Instance.IsActivated;
75                 }
76                 catch (TypeInitializationException e)
77                 {
78                     throw e.InnerException;
79                 }
80             }
81         }
82
83         /// <summary>
84         /// The Tag Filter type.
85         /// </summary>
86         /// <since_tizen> 3 </since_tizen>
87         static public NfcTagFilterType TagFilterType
88         {
89             get
90             {
91                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
92                 bool isTagSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.tag", out isTagSupported);
93
94                 if (!isNfcSupported || !isTagSupported)
95                 {
96                     throw new NotSupportedException();
97                 }
98
99                 try
100                 {
101                     return NfcManagerImpl.Instance.TagFilterType;
102                 }
103                 catch (TypeInitializationException e)
104                 {
105                     throw e.InnerException;
106                 }
107             }
108             set
109             {
110                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
111                 bool isTagSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.tag", out isTagSupported);
112
113                 if (!isNfcSupported || !isTagSupported)
114                 {
115                     throw new NotSupportedException();
116                 }
117
118                 try
119                 {
120                     NfcManagerImpl.Instance.TagFilterType = value;
121                 }
122                 catch (TypeInitializationException e)
123                 {
124                     throw e.InnerException;
125                 }
126             }
127         }
128
129         /// <summary>
130         /// The Secure Element type.
131         /// </summary>
132         /// <since_tizen> 3 </since_tizen>
133         /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
134         static public NfcSecureElementType SecureElementType
135         {
136             get
137             {
138                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
139                 bool isCeSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.cardemulation", out isCeSupported);
140
141                 if (!isNfcSupported || !isCeSupported)
142                 {
143                     throw new NotSupportedException();
144                 }
145
146                 try
147                 {
148                     return NfcManagerImpl.Instance.SecureElementType;
149                 }
150                 catch (TypeInitializationException e)
151                 {
152                     throw e.InnerException;
153                 }
154             }
155             set
156             {
157                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
158                 bool isCeSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.cardemulation", out isCeSupported);
159
160                 if (!isNfcSupported || !isCeSupported)
161                 {
162                     throw new NotSupportedException();
163                 }
164
165                 try
166                 {
167                     NfcManagerImpl.Instance.SecureElementType = value;
168                 }
169                 catch (TypeInitializationException e)
170                 {
171                     throw e.InnerException;
172                 }
173             }
174         }
175
176         /// <summary>
177         /// Enable or disable the system handling for tag and target discovered event.
178         /// </summary>
179         /// <since_tizen> 3 </since_tizen>
180         /// <privilege>http://tizen.org/privilege/nfc</privilege>
181         static public bool SystemHandlerEnabled
182         {
183             get
184             {
185                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
186
187                 if (!isNfcSupported)
188                 {
189                     throw new NotSupportedException();
190                 }
191
192                 try
193                 {
194                     return NfcManagerImpl.Instance.SystemHandlerEnabled;
195                 }
196                 catch (TypeInitializationException e)
197                 {
198                     throw e.InnerException;
199                 }
200             }
201             set
202             {
203                 bool isNfcSupported = 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                     NfcManagerImpl.Instance.SystemHandlerEnabled = value;
213                 }
214                 catch (TypeInitializationException e)
215                 {
216                     throw e.InnerException;
217                 }
218             }
219         }
220
221         /// <summary>
222         /// The cached Ndef Message.
223         /// </summary>
224         /// <since_tizen> 3 </since_tizen>
225         static public NfcNdefMessage CachedNdefMessage
226         {
227             get
228             {
229                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
230
231                 if (!isNfcSupported)
232                 {
233                     throw new NotSupportedException();
234                 }
235
236                 try
237                 {
238                     return NfcManagerImpl.Instance.CachedNdefMessage;
239                 }
240                 catch (TypeInitializationException e)
241                 {
242                     throw e.InnerException;
243                 }
244             }
245         }
246
247         /// <summary>
248         /// Gets Tag adapter object.
249         /// </summary>
250         /// <since_tizen> 3 </since_tizen>
251         static public NfcTagAdapter GetTagAdapter()
252         {
253             bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
254             bool isTagSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.tag", out isTagSupported);
255
256             if (!isNfcSupported || !isTagSupported)
257             {
258                 throw new NotSupportedException();
259             }
260
261             try
262             {
263                 return NfcManagerImpl.Instance.TagAdapter;
264             }
265             catch (TypeInitializationException e)
266             {
267                 throw e.InnerException;
268             }
269         }
270
271         /// <summary>
272         /// Gets P2p adapter object.
273         /// </summary>
274         /// <since_tizen> 3 </since_tizen>
275         static public NfcP2pAdapter GetP2pAdapter()
276         {
277             bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
278             bool isP2pSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.p2p", out isP2pSupported);
279
280             if (!isNfcSupported || !isP2pSupported)
281             {
282                 throw new NotSupportedException();
283             }
284
285             try
286             {
287                 return NfcManagerImpl.Instance.P2pAdapter;
288             }
289             catch (TypeInitializationException e)
290             {
291                 throw e.InnerException;
292             }
293         }
294
295         /// <summary>
296         /// Gets Card Emulation adepter object.
297         /// </summary>
298         /// <since_tizen> 3 </since_tizen>
299         static public NfcCardEmulationAdapter GetCardEmulationAdapter()
300         {
301             bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
302             bool isCeSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.cardemulation", out isCeSupported);
303
304             if (!isNfcSupported || !isCeSupported)
305             {
306                 throw new NotSupportedException();
307             }
308
309             try
310             {
311                 return NfcManagerImpl.Instance.CardEmulationAdapter;
312             }
313             catch (TypeInitializationException e)
314             {
315                 throw e.InnerException;
316             }
317         }
318
319         /// <summary>
320         /// Activates Nfc asynchronously.
321         /// </summary>
322         /// <since_tizen> 3 </since_tizen>
323         /// <returns>A task indicates whether the Activate method is done or not.</returns>
324         /// <privilege>http://tizen.org/privilege/nfc.admin</privilege>
325         static public Task SetActivationAsync(bool activation)
326         {
327             bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
328
329             if (!isNfcSupported)
330             {
331                 throw new NotSupportedException();
332             }
333
334             try
335             {
336                 return NfcManagerImpl.Instance.SetActivationAsync(activation);
337             }
338             catch (TypeInitializationException e)
339             {
340                 throw e.InnerException;
341             }
342         }
343
344         /// <summary>
345         /// The Activation changed event.
346         /// </summary>
347         /// <since_tizen> 3 </since_tizen>
348         static public event EventHandler<ActivationChangedEventArgs> ActivationChanged
349         {
350             add
351             {
352                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
353
354                 if (!isNfcSupported)
355                 {
356                     throw new NotSupportedException();
357                 }
358
359                 try
360                 {
361                     NfcManagerImpl.Instance.ActivationChanged += value;
362                 }
363                 catch (TypeInitializationException e)
364                 {
365                     throw e.InnerException;
366                 }
367             }
368             remove
369             {
370                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
371
372                 if (!isNfcSupported)
373                 {
374                     throw new NotSupportedException();
375                 }
376
377                 try
378                 {
379                     NfcManagerImpl.Instance.ActivationChanged -= value;
380                 }
381                 catch (TypeInitializationException e)
382                 {
383                     throw e.InnerException;
384                 }
385             }
386         }
387
388         /// <summary>
389         /// The Ndef discovered event.
390         /// </summary>
391         /// <since_tizen> 3 </since_tizen>
392         static public event EventHandler<NdefMessageDiscoveredEventArgs> NdefMessageDiscovered
393         {
394             add
395             {
396                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
397
398                 if (!isNfcSupported)
399                 {
400                     throw new NotSupportedException();
401                 }
402
403                 try
404                 {
405                     NfcManagerImpl.Instance.NdefMessageDiscovered += value;
406                 }
407                 catch (TypeInitializationException e)
408                 {
409                     throw e.InnerException;
410                 }
411             }
412             remove
413             {
414                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
415
416                 if (!isNfcSupported)
417                 {
418                     throw new NotSupportedException();
419                 }
420
421                 try
422                 {
423                     NfcManagerImpl.Instance.NdefMessageDiscovered -= value;
424                 }
425                 catch (TypeInitializationException e)
426                 {
427                     throw e.InnerException;
428                 }
429             }
430         }
431     }
432 }