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.AverageSizeOfRecord;
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);
65 /// The count of contact entity.
72 int error = Interop.List.ContactsListGetCount(_listHandle, out count);
73 if ((int)ContactsError.None != error)
75 Log.Error(Globals.LogTag, "ContactsList Count Failed with error " + error);
81 #region IDisposable Support
82 private bool disposedValue = false; // To detect redundant calls
84 protected virtual void Dispose(bool disposing)
88 int error = Interop.List.ContactsListDestroy(_listHandle, true);
89 if ((int)ContactsError.None != error)
91 Log.Error(Globals.LogTag, "ContactsListDestroy Failed with error " + error);
95 GC.RemoveMemoryPressure(_memoryPressure);
100 /// Releases all resources used by the ContactsList.
101 /// It should be called after finished using of the object.
103 public void Dispose()
110 /// Adds a record to the contacts list.
112 /// <param name="record">The record to add</param>
113 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
114 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
115 public void AddRecord(ContactsRecord record)
117 int error = Interop.List.ContactsListAdd(_listHandle, record._recordHandle);
118 if ((int)ContactsError.None != error)
120 Log.Error(Globals.LogTag, "AddRecord Failed with error " + error);
121 throw ContactsErrorFactory.CheckAndCreateException(error);
123 record._disposedValue = true;
124 _memoryPressure += ContactsViews.AverageSizeOfRecord;
128 /// Removes a record from the contacts list.
130 /// <param name="record">The record to remove</param>
131 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
132 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
133 public void RemoveRecord(ContactsRecord record)
135 int error = Interop.List.ContactsListRemove(_listHandle, record._recordHandle);
136 if ((int)ContactsError.None != error)
138 Log.Error(Globals.LogTag, "RemoveRecord Failed with error " + error);
139 throw ContactsErrorFactory.CheckAndCreateException(error);
141 record._disposedValue = false;
142 _memoryPressure -= ContactsViews.AverageSizeOfRecord;
146 /// Retrieves a record from the contacts list.
151 public ContactsRecord GetCurrentRecord()
154 int error = Interop.List.ContactsListGetCurrentRecordP(_listHandle, out handle);
155 if ((int)ContactsError.None != error)
157 Log.Error(Globals.LogTag, "GetCurrentRecord Failed with error " + error);
158 throw ContactsErrorFactory.CheckAndCreateException(error);
160 return new ContactsRecord(handle, true);
164 /// Moves a contacts list to the previous position.
167 /// When the cursor is already at the first position, it returns false.
169 public bool MovePrevious()
171 int error = Interop.List.ContactsListPrev(_listHandle);
173 if ((int)ContactsError.None == error)
177 else if (Count > 0 && (int)ContactsError.NoData == error)
179 Log.Debug(Globals.LogTag, "Nodata MovePrevious" + error);
184 Log.Error(Globals.LogTag, "MovePrevious Failed with error " + error);
185 throw ContactsErrorFactory.CheckAndCreateException(error);
190 /// Moves a contacts list to the next position.
193 /// When the cursor is already at the last position, it returns false.
195 public bool MoveNext()
197 int error = Interop.List.ContactsListNext(_listHandle);
199 if ((int)ContactsError.None == error)
203 else if (Count > 0 && (int)ContactsError.NoData == error)
205 Log.Debug(Globals.LogTag, "Nodata MoveNext" + error);
210 Log.Error(Globals.LogTag, "MoveNext Failed with error " + error);
211 throw ContactsErrorFactory.CheckAndCreateException(error);
216 /// Moves a contacts list to the first position.
218 public void MoveFirst()
220 int error = Interop.List.ContactsListFirst(_listHandle);
221 if ((int)ContactsError.None != error)
223 Log.Error(Globals.LogTag, "MoveFirst Failed with error " + error);
224 throw ContactsErrorFactory.CheckAndCreateException(error);
229 /// Moves a contacts list to the last position.
231 public void MoveLast()
233 int error = Interop.List.ContactsListLast(_listHandle);
234 if ((int)ContactsError.None != error)
236 Log.Error(Globals.LogTag, "MoveFirst Failed with error " + error);
237 throw ContactsErrorFactory.CheckAndCreateException(error);