Fix XML Doc Build Warning for Nfc
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Nfc / Tizen.Network.Nfc / NfcNdefMessage.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.Runtime.InteropServices;
19 using System.Collections.Generic;
20
21 namespace Tizen.Network.Nfc
22 {
23     /// <summary>
24     /// A class for the NDEF Message information. It allows applications to use the NDEF Message information.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class NfcNdefMessage : IDisposable
28     {
29         private bool disposed = false;
30         private IntPtr _messageHandle = IntPtr.Zero;
31         private List<NfcNdefRecord> _recordList = new List<NfcNdefRecord>();
32
33         /// <summary>
34         /// The number of records in the NDEF message.
35         /// </summary>
36         /// <since_tizen> 3 </since_tizen>
37         public int RecordCount
38         {
39             get
40             {
41                 int recordCount;
42                 int ret = Interop.Nfc.NdefMessage.GetRecordCount(_messageHandle, out recordCount);
43                 if (ret != (int)NfcError.None)
44                 {
45                     Log.Error(Globals.LogTag, "Failed to get record count, Error - " + (NfcError)ret);
46                 }
47                 return recordCount;
48             }
49         }
50
51         /// <summary>
52         /// Creates an object for the access point.
53         /// </summary>
54         /// <since_tizen> 3 </since_tizen>
55         /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
56         /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
57         public NfcNdefMessage()
58         {
59             int ret = Interop.Nfc.NdefMessage.Create(out _messageHandle);
60
61             if (ret != (int)NfcError.None)
62             {
63                 Log.Error(Globals.LogTag, "Failed to create Ndef message, Error - " + (NfcError)ret);
64                 NfcErrorFactory.ThrowNfcException(ret);
65             }
66         }
67
68         internal NfcNdefMessage(IntPtr messageHandle)
69         {
70             _messageHandle = messageHandle;
71         }
72
73         /// <summary>
74         /// NfcNdefMessage destructor.
75         /// </summary>
76         ~NfcNdefMessage()
77         {
78             Dispose(false);
79         }
80
81         /// <summary>
82         /// Dispose
83         /// </summary>
84         public void Dispose()
85         {
86             Dispose(true);
87             GC.SuppressFinalize(this);
88         }
89
90         private void Dispose(bool disposing)
91         {
92             if (disposed)
93                 return;
94
95             if (disposing)
96             {
97                 // Free managed objects.
98                 int ret = Interop.Nfc.NdefMessage.Destroy(_messageHandle);
99
100                 if (ret != (int)NfcError.None)
101                 {
102                     Log.Error(Globals.LogTag, "Failed to destroy ndef message, Error - " + (NfcError)ret);
103                 }
104             }
105             //Free unmanaged objects
106             disposed = true;
107         }
108
109         /// <summary>
110         /// Appends a record into the NDEF message.
111         /// </summary>
112         /// <since_tizen> 3 </since_tizen>
113         /// <returns>Whether the record is appended successfully.</returns>
114         /// <param name="record">The NfcNdefRecord object that will be appended into the NDEF message.</param>
115         public bool AppendRecord(NfcNdefRecord record)
116         {
117             bool isSuccess = true;
118
119             int ret = Interop.Nfc.NdefMessage.AppendRecord(_messageHandle, record.GetHandle());
120             if (ret != (int)NfcError.None)
121             {
122                 Log.Error(Globals.LogTag, "Failed to append record, Error - " + (NfcError)ret);
123                 isSuccess = false;
124             }
125             else
126             {
127                 _recordList.Add(record);
128             }
129
130             return isSuccess;
131         }
132
133         /// <summary>
134         /// Inserts a record at the index into the NDEF message.
135         /// </summary>
136         /// <since_tizen> 3 </since_tizen>
137         /// <returns>Whether inserting the record succeeded.</returns>
138         /// <param name="index">The index of a record ( starts from 0 ).</param>
139         /// <param name="record">The NfcNdefRecord object that will be appended into the NDEF message.</param>
140         public bool InsertRecord(int index, NfcNdefRecord record)
141         {
142             bool isSuccess = true;
143
144             int ret = Interop.Nfc.NdefMessage.InsertRecord(_messageHandle, index, record.GetHandle());
145             if (ret != (int)NfcError.None)
146             {
147                 Log.Error(Globals.LogTag, "Failed to insert record, Error - " + (NfcError)ret);
148                 isSuccess = false;
149             }
150             else
151             {
152                 _recordList.Add(record);
153             }
154
155             return isSuccess;
156         }
157
158         /// <summary>
159         /// Removes a record at the index into the NDEF message.
160         /// </summary>
161         /// <since_tizen> 3 </since_tizen>
162         /// <returns>Whether removing the record succeeded.</returns>
163         /// <param name="index">The index of a record ( starts from 0 ).</param>
164         public bool RemoveRecord(int index)
165         {
166             bool isSuccess = true;
167
168             int ret = Interop.Nfc.NdefMessage.RemoveRecord(_messageHandle, index);
169             if (ret != (int)NfcError.None)
170             {
171                 Log.Error(Globals.LogTag, "Failed to remove record, Error - " + (NfcError)ret);
172                 isSuccess = false;
173             }
174
175             return isSuccess;
176         }
177
178         /// <summary>
179         /// Gets a record by the index.
180         /// </summary>
181         /// <since_tizen> 3 </since_tizen>
182         /// <returns>The NfcNdefRecord object.</returns>
183         /// <param name="index">The index of a record ( starts from 0 ).</param>
184         public NfcNdefRecord GetRecord(int index)
185         {
186             IntPtr recordHandle;
187             NfcNdefRecord recordObject = null;
188
189             int ret = Interop.Nfc.NdefMessage.GetRecord(_messageHandle, index, out recordHandle);
190             if (ret != (int)NfcError.None)
191             {
192                 Log.Error(Globals.LogTag, "Failed to remove record, Error - " + (NfcError)ret);
193             }
194
195             foreach (NfcNdefRecord recordElement in _recordList)
196             {
197                 if(recordElement.GetHandle() == recordHandle)
198                 {
199                     Log.Debug(Globals.LogTag, "Find record handle");
200                     recordObject = recordElement;
201                     break;
202                 }
203             }
204
205             return recordObject;
206         }
207
208         internal IntPtr GetHandle()
209         {
210             return _messageHandle;
211         }
212     }
213 }