Version up : 1.0.3 to 1.0.4
[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                     throw new NotSupportedException();
42                 }
43
44                 try
45                 {
46                     return NfcManagerImpl.Instance.IsSupported;
47                 }
48                 catch (TypeInitializationException e)
49                 {
50                     throw e.InnerException;
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                     throw new NotSupportedException();
67                 }
68
69                 try
70                 {
71                     return NfcManagerImpl.Instance.IsActivated;
72                 }
73                 catch (TypeInitializationException e)
74                 {
75                     throw e.InnerException;
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                     throw new NotSupportedException();
93                 }
94
95                 try
96                 {
97                     return NfcManagerImpl.Instance.TagFilterType;
98                 }
99                 catch (TypeInitializationException e)
100                 {
101                     throw e.InnerException;
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                     throw new NotSupportedException();
112                 }
113
114                 try
115                 {
116                     NfcManagerImpl.Instance.TagFilterType = value;
117                 }
118                 catch (TypeInitializationException e)
119                 {
120                     throw e.InnerException;
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                     throw new NotSupportedException();
138                 }
139
140                 try
141                 {
142                     return NfcManagerImpl.Instance.SecureElementType;
143                 }
144                 catch (TypeInitializationException e)
145                 {
146                     throw e.InnerException;
147                 }
148             }
149             set
150             {
151                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
152                 bool isCeSupported = 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                     NfcManagerImpl.Instance.SecureElementType = value;
162                 }
163                 catch (TypeInitializationException e)
164                 {
165                     throw e.InnerException;
166                 }
167             }
168         }
169
170         /// <summary>
171         /// Enable or disable the system handling for tag and target discovered event.
172         /// </summary>
173         static public bool SystemHandlerEnabled
174         {
175             get
176             {
177                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
178
179                 if (!isNfcSupported)
180                 {
181                     throw new NotSupportedException();
182                 }
183
184                 try
185                 {
186                     return NfcManagerImpl.Instance.SystemHandlerEnabled;
187                 }
188                 catch (TypeInitializationException e)
189                 {
190                     throw e.InnerException;
191                 }
192             }
193             set
194             {
195                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
196
197                 if (!isNfcSupported)
198                 {
199                     throw new NotSupportedException();
200                 }
201
202                 try
203                 {
204                     NfcManagerImpl.Instance.SystemHandlerEnabled = value;
205                 }
206                 catch (TypeInitializationException e)
207                 {
208                     throw e.InnerException;
209                 }
210             }
211         }
212
213         /// <summary>
214         /// The cached Ndef Message.
215         /// </summary>
216         static public NfcNdefMessage CachedNdefMessage
217         {
218             get
219             {
220                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
221
222                 if (!isNfcSupported)
223                 {
224                     throw new NotSupportedException();
225                 }
226
227                 try
228                 {
229                     return NfcManagerImpl.Instance.CachedNdefMessage;
230                 }
231                 catch (TypeInitializationException e)
232                 {
233                     throw e.InnerException;
234                 }
235             }
236         }
237
238         /// <summary>
239         /// Gets Tag adapter object.
240         /// </summary>
241         static public NfcTagAdapter GetTagAdapter()
242         {
243             bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
244             bool isTagSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.tag", out isTagSupported);
245
246             if (!isNfcSupported || !isTagSupported)
247             {
248                 throw new NotSupportedException();
249             }
250
251             try
252             {
253                 return NfcManagerImpl.Instance.TagAdapter;
254             }
255             catch (TypeInitializationException e)
256             {
257                 throw e.InnerException;
258             }
259         }
260
261         /// <summary>
262         /// Gets P2p adapter object.
263         /// </summary>
264         static public NfcP2pAdapter GetP2pAdapter()
265         {
266             bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
267             bool isP2pSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.p2p", out isP2pSupported);
268
269             if (!isNfcSupported || !isP2pSupported)
270             {
271                 throw new NotSupportedException();
272             }
273
274             try
275             {
276                 return NfcManagerImpl.Instance.P2pAdapter;
277             }
278             catch (TypeInitializationException e)
279             {
280                 throw e.InnerException;
281             }
282         }
283
284         /// <summary>
285         /// Gets Card Emulation adepter object.
286         /// </summary>
287         static public NfcCardEmulationAdapter GetCardEmulationAdapter()
288         {
289             bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
290             bool isCeSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc.cardemulation", out isCeSupported);
291
292             if (!isNfcSupported || !isCeSupported)
293             {
294                 throw new NotSupportedException();
295             }
296
297             try
298             {
299                 return NfcManagerImpl.Instance.CardEmulationAdapter;
300             }
301             catch (TypeInitializationException e)
302             {
303                 throw e.InnerException;
304             }
305         }
306
307         /// <summary>
308         /// Activates Nfc asynchronously. 
309         /// </summary>
310         /// <returns>A task indicates whether the Activate method is done or not.</returns>
311         static public Task SetActivateAsync(bool activation)
312         {
313             bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
314
315             if (!isNfcSupported)
316             {
317                 throw new NotSupportedException();
318             }
319
320             try
321             {
322                 return NfcManagerImpl.Instance.SetActivateAsync(activation);
323             }
324             catch (TypeInitializationException e)
325             {
326                 throw e.InnerException;
327             }
328         }
329
330         /// <summary>
331         /// The Activation changed event.
332         /// </summary>
333         static public event EventHandler<ActivationChangedEventArgs> ActivationChanged
334         {
335             add
336             {
337                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
338
339                 if (!isNfcSupported)
340                 {
341                     throw new NotSupportedException();
342                 }
343
344                 try
345                 {
346                     NfcManagerImpl.Instance.ActivationChanged += value;
347                 }
348                 catch (TypeInitializationException e)
349                 {
350                     throw e.InnerException;
351                 }
352             }
353             remove
354             {
355                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
356
357                 if (!isNfcSupported)
358                 {
359                     throw new NotSupportedException();
360                 }
361
362                 try
363                 {
364                     NfcManagerImpl.Instance.ActivationChanged -= value;
365                 }
366                 catch (TypeInitializationException e)
367                 {
368                     throw e.InnerException;
369                 }
370             }
371         }
372
373         /// <summary>
374         /// The Ndef discovered event.
375         /// </summary>
376         static public event EventHandler<NdefMessageDiscoveredEventArgs> NdefMessageDiscovered
377         {
378             add
379             {
380                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
381
382                 if (!isNfcSupported)
383                 {
384                     throw new NotSupportedException();
385                 }
386
387                 try
388                 {
389                     NfcManagerImpl.Instance.NdefMessageDiscovered += value;
390                 }
391                 catch (TypeInitializationException e)
392                 {
393                     throw e.InnerException;
394                 }
395             }
396             remove
397             {
398                 bool isNfcSupported = SystemInfo.TryGetValue("http://tizen.org/feature/network.nfc", out isNfcSupported);
399
400                 if (!isNfcSupported)
401                 {
402                     throw new NotSupportedException();
403                 }
404
405                 try
406                 {
407                     NfcManagerImpl.Instance.NdefMessageDiscovered -= value;
408                 }
409                 catch (TypeInitializationException e)
410                 {
411                     throw e.InnerException;
412                 }
413             }
414         }
415     }
416 }