Release 4.0.0-preview1-00051
[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 Ndef Message information. It allows applications to use 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 record in 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 a object for the access point.
53         /// </summary>
54         /// <since_tizen> 3 </since_tizen>
55         /// <exception cref="NotSupportedException">Thrown when Nfc is not supported.</exception>
56         /// <exception cref="InvalidOperationException">Thrown when the method failed due to 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         ~NfcNdefMessage()
74         {
75             Dispose(false);
76         }
77
78         public void Dispose()
79         {
80             Dispose(true);
81             GC.SuppressFinalize(this);
82         }
83
84         private void Dispose(bool disposing)
85         {
86             if (disposed)
87                 return;
88
89             if (disposing)
90             {
91                 // Free managed objects.
92                 int ret = Interop.Nfc.NdefMessage.Destroy(_messageHandle);
93
94                 if (ret != (int)NfcError.None)
95                 {
96                     Log.Error(Globals.LogTag, "Failed to destroy ndef message, Error - " + (NfcError)ret);
97                 }
98             }
99             //Free unmanaged objects
100             disposed = true;
101         }
102
103         /// <summary>
104         /// Appends a record into NDEF message.
105         /// </summary>
106         /// <since_tizen> 3 </since_tizen>
107         /// <returns>Whether appending the record succeeded.</returns>
108         /// <param name="record">The NfcNdefRecord object that will be appended into NDEF message.</param>
109         public bool AppendRecord(NfcNdefRecord record)
110         {
111             bool isSuccess = true;
112
113             int ret = Interop.Nfc.NdefMessage.AppendRecord(_messageHandle, record.GetHandle());
114             if (ret != (int)NfcError.None)
115             {
116                 Log.Error(Globals.LogTag, "Failed to append record, Error - " + (NfcError)ret);
117                 isSuccess = false;
118             }
119             else
120             {
121                 _recordList.Add(record);
122             }
123
124             return isSuccess;
125         }
126
127         /// <summary>
128         /// Inserts a record at index into NDEF message.
129         /// </summary>
130         /// <since_tizen> 3 </since_tizen>
131         /// <returns>Whether insterting the record succeeded.</returns>
132         /// <param name="index">The index of record ( starts from 0 ).</param>
133         /// <param name="record">The NfcNdefRecord object that will be appended into NDEF message.</param>
134         public bool InsertRecord(int index, NfcNdefRecord record)
135         {
136             bool isSuccess = true;
137
138             int ret = Interop.Nfc.NdefMessage.InsertRecord(_messageHandle, index, record.GetHandle());
139             if (ret != (int)NfcError.None)
140             {
141                 Log.Error(Globals.LogTag, "Failed to insert record, Error - " + (NfcError)ret);
142                 isSuccess = false;
143             }
144             else
145             {
146                 _recordList.Add(record);
147             }
148
149             return isSuccess;
150         }
151
152         /// <summary>
153         /// Inserts a record at index into NDEF message.
154         /// </summary>
155         /// <since_tizen> 3 </since_tizen>
156         /// <returns>Whether removing the record succeeded.</returns>
157         /// <param name="index">The index of record ( starts from 0 ).</param>
158         public bool RemoveRecord(int index)
159         {
160             bool isSuccess = true;
161
162             int ret = Interop.Nfc.NdefMessage.RemoveRecord(_messageHandle, index);
163             if (ret != (int)NfcError.None)
164             {
165                 Log.Error(Globals.LogTag, "Failed to remove record, Error - " + (NfcError)ret);
166                 isSuccess = false;
167             }
168
169             return isSuccess;
170         }
171
172         /// <summary>
173         /// Gets record by index.
174         /// </summary>
175         /// <since_tizen> 3 </since_tizen>
176         /// <returns>The NfcNdefRecord object.</returns>
177         /// <param name="index">The index of record ( starts from 0 ).</param>
178         public NfcNdefRecord GetRecord(int index)
179         {
180             IntPtr recordHandle;
181             NfcNdefRecord recordObject = null;
182
183             int ret = Interop.Nfc.NdefMessage.GetRecord(_messageHandle, index, out recordHandle);
184             if (ret != (int)NfcError.None)
185             {
186                 Log.Error(Globals.LogTag, "Failed to remove record, Error - " + (NfcError)ret);
187             }
188
189             foreach (NfcNdefRecord recordElement in _recordList)
190             {
191                 if(recordElement.GetHandle() == recordHandle)
192                 {
193                     Log.Debug(Globals.LogTag, "Find record handle");
194                     recordObject = recordElement;
195                     break;
196                 }
197             }
198
199             return recordObject;
200         }
201
202         internal IntPtr GetHandle()
203         {
204             return _messageHandle;
205         }
206     }
207 }