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