2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 namespace Tizen.Pims.Contacts
22 /// A list of records with the same type
24 public class ContactsList:IDisposable
26 private Int64 _memoryPressure = 20;
27 internal IntPtr _listHandle;
28 internal ContactsList(IntPtr handle)
33 int error = Interop.List.ContactsListGetCount(_listHandle, out count);
34 if ((int)ContactsError.None != error)
36 Log.Error(Globals.LogTag, "ContactsList Failed with error " + error);
37 throw ContactsErrorFactory.CheckAndCreateException(error);
39 _memoryPressure += count * ContactsViews.Record.AverageSize;
40 GC.AddMemoryPressure(_memoryPressure);
44 /// Creates a contacts record list.
46 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
47 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
50 int error = Interop.List.ContactsListCreate(out _listHandle);
51 if ((int)ContactsError.None != error)
53 Log.Error(Globals.LogTag, "ContactsList Failed with error " + error);
54 throw ContactsErrorFactory.CheckAndCreateException(error);
56 GC.AddMemoryPressure(_memoryPressure);
68 /// The count of contact entity.
70 /// <value>The count of contact entity.</value>
76 int error = Interop.List.ContactsListGetCount(_listHandle, out count);
77 if ((int)ContactsError.None != error)
79 Log.Error(Globals.LogTag, "ContactsList Count Failed with error " + error);
85 #region IDisposable Support
86 private bool disposedValue = false; // To detect redundant calls
89 /// Releases all resources used by the ContactsList.
91 /// <param name="disposing">Disposing by User</param>
92 protected virtual void Dispose(bool disposing)
97 //Release your own managed resources here.
98 //You should release all of your own disposable objects here
103 int error = Interop.List.ContactsListDestroy(_listHandle, true);
104 if ((int)ContactsError.None != error)
106 Log.Error(Globals.LogTag, "ContactsListDestroy Failed with error " + error);
109 disposedValue = true;
110 GC.RemoveMemoryPressure(_memoryPressure);
115 /// Releases all resources used by the ContactsList.
116 /// It should be called after finished using of the object.
118 public void Dispose()
121 GC.SuppressFinalize(this);
126 /// Adds a record to the contacts list.
128 /// <param name="record">The record to add</param>
129 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
130 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
131 public void AddRecord(ContactsRecord record)
133 int error = Interop.List.ContactsListAdd(_listHandle, record._recordHandle);
134 if ((int)ContactsError.None != error)
136 Log.Error(Globals.LogTag, "AddRecord Failed with error " + error);
137 throw ContactsErrorFactory.CheckAndCreateException(error);
139 record._disposedValue = true;
140 _memoryPressure += ContactsViews.Record.AverageSize;
144 /// Removes a record from the contacts list.
146 /// <param name="record">The record to remove</param>
147 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
148 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
149 public void RemoveRecord(ContactsRecord record)
151 int error = Interop.List.ContactsListRemove(_listHandle, record._recordHandle);
152 if ((int)ContactsError.None != error)
154 Log.Error(Globals.LogTag, "RemoveRecord Failed with error " + error);
155 throw ContactsErrorFactory.CheckAndCreateException(error);
157 record._disposedValue = false;
158 _memoryPressure -= ContactsViews.Record.AverageSize;
162 /// Retrieves a record from the contacts list.
167 public ContactsRecord GetCurrentRecord()
170 int error = Interop.List.ContactsListGetCurrentRecordP(_listHandle, out handle);
171 if ((int)ContactsError.None != error)
173 Log.Error(Globals.LogTag, "GetCurrentRecord Failed with error " + error);
174 throw ContactsErrorFactory.CheckAndCreateException(error);
176 return new ContactsRecord(handle, true);
180 /// Moves a contacts list to the previous position.
183 /// When the cursor is already at the first position, it returns false.
185 public bool MovePrevious()
187 int error = Interop.List.ContactsListPrev(_listHandle);
189 if ((int)ContactsError.None == error)
193 else if (Count > 0 && (int)ContactsError.NoData == error)
195 Log.Debug(Globals.LogTag, "Nodata MovePrevious" + error);
200 Log.Error(Globals.LogTag, "MovePrevious Failed with error " + error);
201 throw ContactsErrorFactory.CheckAndCreateException(error);
206 /// Moves a contacts list to the next position.
209 /// When the cursor is already at the last position, it returns false.
211 public bool MoveNext()
213 int error = Interop.List.ContactsListNext(_listHandle);
215 if ((int)ContactsError.None == error)
219 else if (Count > 0 && (int)ContactsError.NoData == error)
221 Log.Debug(Globals.LogTag, "Nodata MoveNext" + error);
226 Log.Error(Globals.LogTag, "MoveNext Failed with error " + error);
227 throw ContactsErrorFactory.CheckAndCreateException(error);
232 /// Moves a contacts list to the first position.
234 public void MoveFirst()
236 int error = Interop.List.ContactsListFirst(_listHandle);
237 if ((int)ContactsError.None != error)
239 Log.Error(Globals.LogTag, "MoveFirst Failed with error " + error);
240 throw ContactsErrorFactory.CheckAndCreateException(error);
245 /// Moves a contacts list to the last position.
247 public void MoveLast()
249 int error = Interop.List.ContactsListLast(_listHandle);
250 if ((int)ContactsError.None != error)
252 Log.Error(Globals.LogTag, "MoveFirst Failed with error " + error);
253 throw ContactsErrorFactory.CheckAndCreateException(error);