2 * Copyright (c) 2018 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.
17 using System.Collections.Generic;
18 using Tizen.Applications.DataControl.Core;
19 using System.Threading;
20 using System.Runtime.InteropServices;
22 namespace Tizen.Applications.DataControl
25 /// Represents the Consumer class for the DataControl consumer application.
27 /// <since_tizen> 3 </since_tizen>
28 public abstract class Consumer : IDisposable
31 private Interop.DataControl.SafeDataControlHandle _handle;
32 private string _dataID, _providerID;
33 private int _changeCallbackID = 0;
34 private const string LogTag = "Tizen.Applications.DataControl";
35 private bool _disposed = false;
36 private static Mutex _lock = new Mutex();
37 private static Mutex _requestLock = new Mutex();
38 private Interop.DataControl.DataChangeCallback _dataChangeCallback;
39 private Interop.DataControl.AddCallbackResultCallback _addCallbackResultCallback;
41 private static class CallbackManager
43 private static IDictionary<string, Interop.DataControl.MapResponseCallbacks> _mapResponseCallbacks = new Dictionary<string, Interop.DataControl.MapResponseCallbacks>();
44 private static IDictionary<string, Interop.DataControl.MapBulkAddResponseCallback> _mapBulkResponseCallback = new Dictionary<string, Interop.DataControl.MapBulkAddResponseCallback>();
45 private static IDictionary<string, Interop.DataControl.SqlResponseCallbacks> _sqlResponseCallbacks = new Dictionary<string, Interop.DataControl.SqlResponseCallbacks>();
46 private static IDictionary<string, Interop.DataControl.SqlBulkInsertResponseCallback> _sqlBulkResponseCallback = new Dictionary<string, Interop.DataControl.SqlBulkInsertResponseCallback>();
47 private static IDictionary<int, Consumer> _reqConsumerDictionary = new Dictionary<int, Consumer>();
48 private static IDictionary<string, int> _reqProviderList = new Dictionary<string, int>();
49 private static void InsertResponse(int reqId, IntPtr provider, long insertedRowId, bool providerResult, string error, IntPtr userData)
51 Log.Debug(LogTag, $"InsertResponse {reqId.ToString()}");
52 Consumer consumer = null;
53 if (!UnregisterReqId(reqId, ref consumer))
60 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}, rowID : {insertedRowId.ToString()}");
63 consumer.OnInsertResult(new InsertResult(insertedRowId, providerResult));
66 private static void BulkInsertResponse(int reqId, IntPtr provider, IntPtr bulkResults, bool providerResult, string error, IntPtr userData)
68 Log.Debug(LogTag, $"BulkInsertResponse {reqId.ToString()}");
71 Consumer consumer = null;
72 if (!UnregisterReqId(reqId, ref consumer))
79 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
82 if (bulkResults != IntPtr.Zero)
84 brd = new BulkResultData(new Interop.DataControl.SafeBulkResultDataHandle(bulkResults, false));
88 brd = new BulkResultData();
89 Log.Error(LogTag, $"reqId {reqId.ToString()}, bulkResults is null");
92 consumer.OnBulkInsertResult(new BulkInsertResult(brd, providerResult));
95 private static void SelectResponse(int reqId, IntPtr provider, IntPtr cursor, bool providerResult, string error, IntPtr userData)
97 Log.Debug(LogTag, $"SelectResponse {reqId.ToString()}");
100 Consumer consumer = null;
101 if (!UnregisterReqId(reqId, ref consumer))
108 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
111 if (cursor != IntPtr.Zero)
115 dmc = CloneCursor(new CloneCursorCore(new Interop.DataControl.SafeCursorHandle(cursor, true)));
119 dmc = new MatrixCursor();
120 Log.Error(LogTag, $"reqId {reqId.ToString()}, {ex.ToString()}");
125 dmc = new MatrixCursor();
126 Log.Error(LogTag, $"reqId {reqId.ToString()}, cursor is null");
129 consumer.OnSelectResult(new SelectResult(dmc, providerResult));
132 private static void UpdateResponse(int reqId, IntPtr provider, bool providerResult, string error, IntPtr userData)
134 Consumer consumer = null;
135 if (!UnregisterReqId(reqId, ref consumer))
142 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
145 consumer.OnUpdateResult(new UpdateResult(providerResult));
148 private static void DeleteResponse(int reqId, IntPtr provider, bool providerResult, string error, IntPtr userData)
150 Consumer consumer = null;
151 if (!UnregisterReqId(reqId, ref consumer))
158 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
161 consumer.OnDeleteResult(new DeleteResult(providerResult));
164 static void IntPtrToStringArray(IntPtr unmanagedArray, int size, out string[] managedArray)
166 managedArray = new string[size];
167 IntPtr[] IntPtrArray = new IntPtr[size];
168 Marshal.Copy(unmanagedArray, IntPtrArray, 0, size);
169 for (int iterator = 0; iterator < size; iterator++)
171 managedArray[iterator] = Marshal.PtrToStringAnsi(IntPtrArray[iterator]);
175 private static void MapGetResponse(int reqId, IntPtr provider, IntPtr valueList, int valueCount, bool providerResult, string error, IntPtr userData)
177 Log.Debug(LogTag, $"MapGetResponse {reqId.ToString()}");
180 Consumer consumer = null;
181 if (!UnregisterReqId(reqId, ref consumer))
188 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
191 if (valueList != IntPtr.Zero)
193 string[] stringArray;
194 IntPtrToStringArray(valueList, valueCount, out stringArray);
195 mgr = new MapGetResult(stringArray, providerResult);
199 mgr = new MapGetResult(new string[0], providerResult);
200 Log.Error(LogTag, $"reqId {reqId.ToString()}, valueList is null");
203 consumer.OnMapGetResult(mgr);
206 private static void MapBulkAddResponse(int reqId, IntPtr provider, IntPtr bulkResults, bool providerResult, string error, IntPtr userData)
208 Log.Debug(LogTag, $"MapBulkAddResponse {reqId.ToString()}");
211 Consumer consumer = null;
212 if (!UnregisterReqId(reqId, ref consumer))
219 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
222 if (bulkResults != IntPtr.Zero)
224 brd = new BulkResultData(new Interop.DataControl.SafeBulkResultDataHandle(bulkResults, false));
228 brd = new BulkResultData();
229 Log.Error(LogTag, $"reqId {reqId.ToString()}, bulkResults is null");
232 consumer.OnMapBulkAddResult(new MapBulkAddResult(brd, providerResult));
235 private static void MapAddResponse(int reqId, IntPtr provider, bool providerResult, string error, IntPtr userData)
237 Log.Debug(LogTag, $"MapAddResponse {reqId.ToString()}");
239 Consumer consumer = null;
240 if (!UnregisterReqId(reqId, ref consumer))
247 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
250 consumer.OnMapAddResult(new MapAddResult(providerResult));
253 private static void MapSetResponse(int reqId, IntPtr provider, bool providerResult, string error, IntPtr userData)
255 Log.Debug(LogTag, $"MapSetResponse {reqId.ToString()}");
257 Consumer consumer = null;
258 if (!UnregisterReqId(reqId, ref consumer))
265 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
268 consumer.OnMapSetResult(new MapSetResult(providerResult));
271 private static void MapRemoveResponse(int reqId, IntPtr provider, bool providerResult, string error, IntPtr userData)
273 Consumer consumer = null;
274 if (!UnregisterReqId(reqId, ref consumer))
281 Log.Error(LogTag, $"reqId {reqId.ToString()}, error : {error}");
284 consumer.OnMapRemoveResult(new MapRemoveResult(providerResult));
287 private static MatrixCursor CloneCursor(CloneCursorCore coreCursor)
289 int size = coreCursor.GetColumnCount();
291 string[] name = new string[size];
292 object[] newRow = new object[size];
293 ColumnType[] type = new ColumnType[size];
295 for (i = 0; i < size; i++)
297 name[i] = coreCursor.GetColumnName(i);
298 type[i] = coreCursor.GetColumnType(i);
301 MatrixCursor dmc = new MatrixCursor(name, type);
303 if (coreCursor.GetRowCount() <= 0)
311 for (i = 0; i < size; i++)
315 case ColumnType.ColumnTypeInt:
316 newRow[i] = coreCursor.GetInt64Value(i);
318 case ColumnType.ColumnTypeDouble:
319 newRow[i] = coreCursor.GetDoubleValue(i);
321 case ColumnType.ColumnTypeBlob:
322 newRow[i] = coreCursor.GetBlobValue(i);
324 case ColumnType.ColumnTypeString:
325 newRow[i] = coreCursor.GetStringValue(i);
332 while (coreCursor.Next());
337 internal static void RegisterReqId(int reqId, Consumer consumer)
339 _requestLock.WaitOne();
340 _reqConsumerDictionary.Add(reqId, consumer);
341 _requestLock.ReleaseMutex();
344 internal static bool UnregisterReqId(int reqId, ref Consumer consumer)
346 _requestLock.WaitOne();
347 if (!_reqConsumerDictionary.ContainsKey(reqId))
349 Log.Error(LogTag, $"Invalid reqId {reqId.ToString()}");
350 _requestLock.ReleaseMutex();
354 consumer = _reqConsumerDictionary[reqId];
355 _reqConsumerDictionary.Remove(reqId);
356 _requestLock.ReleaseMutex();
360 internal static void RegisterCallback(Interop.DataControl.SafeDataControlHandle handle, string providerId)
363 Interop.DataControl.SqlResponseCallbacks sqlCallbacks;
364 Interop.DataControl.SqlBulkInsertResponseCallback sqlBulkCallbacks;
365 Interop.DataControl.MapResponseCallbacks mapCallbacks;
366 Interop.DataControl.MapBulkAddResponseCallback mapBulkCallbacks;
367 bool sqlRegistered = false;
368 bool mapRegistered = false;
370 if (_reqProviderList.ContainsKey(providerId))
372 _reqProviderList[providerId]++;
373 Log.Error(LogTag, "The data control is already set");
377 sqlCallbacks.Insert = new Interop.DataControl.SqlInsertResponseCallback(InsertResponse);
378 sqlCallbacks.Select = new Interop.DataControl.SqlSelectResponseCallback(SelectResponse);
379 sqlCallbacks.Update = new Interop.DataControl.SqlUpdateResponseCallback(UpdateResponse);
380 sqlCallbacks.Delete = new Interop.DataControl.SqlDeleteResponseCallback(DeleteResponse);
381 ret = Interop.DataControl.RegisterSqlResponseCallback(handle, ref sqlCallbacks, IntPtr.Zero);
382 if (ret != ResultType.Success)
384 Log.Error(LogTag, "Registering the sql callback function is failed : " + ret);
388 _sqlResponseCallbacks.Add(providerId, sqlCallbacks);
389 sqlRegistered = true;
392 sqlBulkCallbacks = new Interop.DataControl.SqlBulkInsertResponseCallback(BulkInsertResponse);
393 ret = Interop.DataControl.RegisterSqlBulkResponseCallback(handle, sqlBulkCallbacks, IntPtr.Zero);
394 if (ret != ResultType.Success)
396 Log.Error(LogTag, "Registering the sql bulk callback function is failed : " + ret);
400 _sqlBulkResponseCallback.Add(providerId, sqlBulkCallbacks);
403 mapCallbacks.Add = new Interop.DataControl.MapAddResponseCallback(MapAddResponse);
404 mapCallbacks.Set = new Interop.DataControl.MapSetResponseCallback(MapSetResponse);
405 mapCallbacks.Get = new Interop.DataControl.MapGetResponseCallback(MapGetResponse);
406 mapCallbacks.Remove = new Interop.DataControl.MapRemoveResponseCallback(MapRemoveResponse);
407 ret = Interop.DataControl.RegisterMapResponse(handle, ref mapCallbacks, IntPtr.Zero);
409 if (ret != ResultType.Success)
411 Log.Error(LogTag, "Registering the map callback function is failed : " + ret);
415 _mapResponseCallbacks.Add(providerId, mapCallbacks);
416 mapRegistered = true;
419 mapBulkCallbacks = new Interop.DataControl.MapBulkAddResponseCallback(MapBulkAddResponse);
420 ret = Interop.DataControl.RegisterMapBulkResponseCallback(handle, mapBulkCallbacks, IntPtr.Zero);
421 if (ret != ResultType.Success)
423 Log.Error(LogTag, "Registering the map bulk callback function is failed : " + ret);
427 _mapBulkResponseCallback.Add(providerId, mapBulkCallbacks);
430 if (!mapRegistered && !sqlRegistered)
432 ErrorFactory.ThrowException(ret, true, "Registering the response callback function is failed");
435 _reqProviderList.Add(providerId, 1);
438 internal static void UnregisterCallback(Interop.DataControl.SafeDataControlHandle handle, string providerId)
442 if (!_reqProviderList.ContainsKey(providerId))
444 Log.Error(LogTag, "The provider id is not contained : " + providerId);
448 _reqProviderList[providerId]--;
449 count = _reqProviderList[providerId];
452 _reqProviderList.Remove(providerId);
454 _mapResponseCallbacks.Remove(providerId);
455 Interop.DataControl.UnregisterMapResponse(handle);
457 _mapBulkResponseCallback.Remove(providerId);
458 Interop.DataControl.UnregisterMapBulkResponseCallback(handle);
460 _sqlResponseCallbacks.Remove(providerId);
461 Interop.DataControl.UnregisterSqlResponseCallback(handle);
463 _sqlBulkResponseCallback.Remove(providerId);
464 Interop.DataControl.UnregisterSqlBulkResponseCallback(handle);
471 /// Sends the insert request to the provider application.
473 /// <remarks>The OnInsertResult will recieve the result of this API.</remarks>
474 /// <param name="insertData">The insert data.</param>
475 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
476 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
477 /// <exception cref="ArgumentOutOfRangeException">Thrown when the message has exceeded the maximum limit (1MB).</exception>
478 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
479 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
480 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
481 /// <since_tizen> 3 </since_tizen>
482 public void Insert(Bundle insertData)
487 if (insertData == null || insertData.SafeBundleHandle.IsInvalid)
489 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "insertData");
493 ret = Interop.DataControl.Insert(_handle, insertData.SafeBundleHandle, out reqId);
494 _lock.ReleaseMutex();
495 if (ret != ResultType.Success)
497 ErrorFactory.ThrowException(ret, false, "Insert");
500 CallbackManager.RegisterReqId(reqId, this);
504 /// Sends the select request to the provider application.
506 /// <remarks>The OnSelectResult will recieve the result of this API.</remarks>
507 /// <param name="columnList">Select the target column list.</param>
508 /// <param name="where">The Where statement for the select query.</param>
509 /// <param name="order">The Order statement for the select query.</param>
510 /// <param name="pageNumber">Select the target page number.</param>
511 /// <param name="countPerPage">Select the row count per page.</param>
512 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
513 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied..</exception>
514 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
515 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
516 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
517 /// <since_tizen> 3 </since_tizen>
518 public void Select(string[] columnList, string where, string order, int pageNumber = 1, int countPerPage = 20)
522 if (columnList == null || columnList.Length == 0)
524 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "column_list");
527 for (i = 0; i < columnList.Length; i++)
529 if (string.IsNullOrEmpty(columnList[i]))
531 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "column_list index " + i.ToString());
536 ret = Interop.DataControl.Select(_handle, columnList, columnList.Length, where, order, pageNumber, countPerPage, out reqId);
537 _lock.ReleaseMutex();
538 if (ret != ResultType.Success)
540 ErrorFactory.ThrowException(ret, false, "Select");
542 Log.Info(LogTag, "select end. " + reqId.ToString());
544 CallbackManager.RegisterReqId(reqId, this);
548 /// Sends the delete request to the provider application.
550 /// <remarks>The OnDeleteResult will recieve the result of this API</remarks>
551 /// <param name="where">The Where statement for the delete query.</param>
552 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
553 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
554 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
555 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
556 /// <since_tizen> 3 </since_tizen>
557 public void Delete(string where)
563 ret = Interop.DataControl.Delete(_handle, where, out reqId);
564 _lock.ReleaseMutex();
565 if (ret != ResultType.Success)
567 ErrorFactory.ThrowException(ret, false, "Delete");
570 CallbackManager.RegisterReqId(reqId, this);
574 /// Sends the update request to the provider application.
576 /// <remarks>The OnUpdateResult will recieve result of this API.</remarks>
577 /// <param name="updateData">The update data.</param>
578 /// <param name="where">The Where statement for the query.</param>
579 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
580 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
581 /// <exception cref="ArgumentOutOfRangeException">Thrown when the message has exceeded the maximum limit (1MB).</exception>
582 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
583 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
584 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
585 /// <since_tizen> 3 </since_tizen>
586 public void Update(Bundle updateData, string where)
591 if (updateData == null || updateData.SafeBundleHandle.IsInvalid)
593 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "insertData");
596 if (string.IsNullOrEmpty(where))
598 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "where");
602 ret = Interop.DataControl.Update(_handle, updateData.SafeBundleHandle, where, out reqId);
603 _lock.ReleaseMutex();
604 if (ret != ResultType.Success)
606 ErrorFactory.ThrowException(ret, false, "Update");
609 CallbackManager.RegisterReqId(reqId, this);
613 /// Sends the bulk insert request to the provider application.
615 /// <remarks>The OnBulkInsertResult will recieve the result of this API.</remarks>
616 /// <param name="insertData">The bulk insert data.</param>
617 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
618 /// <exception cref="UnauthorizedAccessException">Thrown in case oif a permission is denied.</exception>
619 /// <exception cref="ArgumentOutOfRangeException">Thrown when the message has exceeded the maximum limit (1MB).</exception>
620 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
621 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
622 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
623 /// <since_tizen> 3 </since_tizen>
624 public void BulkInsert(BulkData insertData)
629 if (insertData == null || insertData.SafeBulkDataHandle.IsInvalid)
631 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "insertData");
635 ret = Interop.DataControl.BulkInsert(_handle, insertData.SafeBulkDataHandle, out reqId);
636 _lock.ReleaseMutex();
637 if (ret != ResultType.Success)
639 ErrorFactory.ThrowException(ret, false, "BulkInsert");
642 CallbackManager.RegisterReqId(reqId, this);
646 /// Sends the map add request to the provider application.
648 /// <remarks>The OnMapAddResult will recieve the result of this API.</remarks>
649 /// <param name="key">The key of the value to add.</param>
650 /// <param name="value">The value to add.</param>
651 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
652 /// <exception cref="UnauthorizedAccessException">Thrown in case of if a permission is denied.</exception>
653 /// <exception cref="ArgumentOutOfRangeException">Thrown when the message has exceeded the maximum limit (1MB).</exception>
654 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
655 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
656 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
657 /// <since_tizen> 3 </since_tizen>
658 public void MapAdd(string key, string value)
663 if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
665 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
669 ret = Interop.DataControl.MapAdd(_handle, key, value, out reqId);
670 _lock.ReleaseMutex();
671 if (ret != ResultType.Success)
673 ErrorFactory.ThrowException(ret, false, "MapAdd");
676 CallbackManager.RegisterReqId(reqId, this);
680 /// Sends the map get request to the provider application.
682 /// <remarks>The OnMapGetResult will recieve the result of this API.</remarks>
683 /// <param name="key">The key of the value list to obtain.</param>
684 /// <param name="pageNumber">The page number of the value set.</param>
685 /// <param name="countPerPage">The desired maximum count of the data items per page.</param>
686 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
687 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
688 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
689 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
690 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
691 /// <since_tizen> 3 </since_tizen>
692 public void MapGet(string key, int pageNumber = 1, int countPerPage = 20)
697 if (string.IsNullOrEmpty(key) || pageNumber <= 0 || countPerPage <= 0)
699 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
703 ret = Interop.DataControl.MapGet(_handle, key, out reqId, pageNumber, countPerPage);
704 _lock.ReleaseMutex();
705 if (ret != ResultType.Success)
707 ErrorFactory.ThrowException(ret, false, "MapGet");
710 CallbackManager.RegisterReqId(reqId, this);
714 /// Sends the map remove request to the provider application.
716 /// <remarks>The OnMapRemoveResult will recieve the result of this API.</remarks>
717 /// <param name="key">The key of the value to remove.</param>
718 /// <param name="value">The value to remove.</param>
719 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
720 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
721 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
722 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
723 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
724 /// <since_tizen> 3 </since_tizen>
725 public void MapRemove(string key, string value)
730 if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
732 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
736 ret = Interop.DataControl.MapRemove(_handle, key, value, out reqId);
737 _lock.ReleaseMutex();
738 if (ret != ResultType.Success)
740 ErrorFactory.ThrowException(ret, false, "MapRemove");
743 CallbackManager.RegisterReqId(reqId, this);
747 /// Sends the map set request to the provider application.
749 /// <remarks>The OnMapSetResult will recieve the result of this API.</remarks>
750 /// <param name="key">The key of the value to replace.</param>
751 /// <param name="oldValue">The value to be replaced.</param>
752 /// <param name="newValue"> The new value that replaces the existing value.</param>
753 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
754 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
755 /// <exception cref="ArgumentOutOfRangeException">Thrown when message has exceeded the maximum limit (1MB).</exception>
756 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
757 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
758 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
759 /// <since_tizen> 3 </since_tizen>
760 public void MapSet(string key, string oldValue, string newValue)
765 if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(oldValue) || string.IsNullOrEmpty(newValue))
767 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
771 ret = Interop.DataControl.MapSet(_handle, key, oldValue, newValue, out reqId);
772 _lock.ReleaseMutex();
773 if (ret != ResultType.Success)
775 ErrorFactory.ThrowException(ret, false, "MapSet");
778 CallbackManager.RegisterReqId(reqId, this);
782 /// Sends the map bulk add request to the provider application.
784 /// <remarks>The OnMapBulkAddResult will recieve the result of this API.</remarks>
785 /// <param name="addData">The map bulk add data.</param>
786 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
787 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
788 /// <exception cref="ArgumentOutOfRangeException">Thrown when the message has exceeded the maximum limit (1MB).</exception>
789 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
790 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
791 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
792 /// <since_tizen> 3 </since_tizen>
793 public void MapBulkAdd(BulkData addData)
798 if (addData == null || addData.SafeBulkDataHandle.IsInvalid)
800 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "addData");
804 ret = Interop.DataControl.BulkAdd(_handle, addData.SafeBulkDataHandle, out reqId);
805 _lock.ReleaseMutex();
806 if (ret != ResultType.Success)
808 ErrorFactory.ThrowException(ret, false, "BulkAdd");
811 CallbackManager.RegisterReqId(reqId, this);
814 private void DataChange(IntPtr handle, ChangeType type, IntPtr data, IntPtr userData)
816 OnDataChange(type, new Bundle(new SafeBundleHandle(data, false)));
819 private void DataChangeListenResult(IntPtr handle, ResultType type, int callbackId, IntPtr userData)
821 OnDataChangeListenResult(new DataChangeListenResult(type));
825 /// Listens the DataChange event.
827 /// <remarks>The OnDataChangeListenResult will recieve the result of this API.</remarks>
828 /// <remarks>If success, the OnDataChange will recieve the DataChange event.</remarks>
829 /// <exception cref="UnauthorizedAccessException">Thrown in case if a permission is denied.</exception>
830 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
831 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
832 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
833 /// <since_tizen> 3 </since_tizen>
834 public void DataChangeListen()
838 /* Only one callback is allowed for every obejct */
839 if (_changeCallbackID > 0)
841 _lock.ReleaseMutex();
844 _dataChangeCallback = new Interop.DataControl.DataChangeCallback(DataChange);
845 _addCallbackResultCallback = new Interop.DataControl.AddCallbackResultCallback(DataChangeListenResult);
846 ret = Interop.DataControl.AddDataChangeCallback(_handle, _dataChangeCallback, IntPtr.Zero,
847 _addCallbackResultCallback , IntPtr.Zero, out _changeCallbackID);
848 _lock.ReleaseMutex();
849 if (ret != ResultType.Success)
851 ErrorFactory.ThrowException(ret, false, "DataChangeListen");
856 /// Initializes the Consumer class with the providerId and the ataId.
858 /// <param name="providerId">The DataControl Provider ID.</param>
859 /// <param name="dataId">The DataControl Data ID.</param>
860 /// <exception cref="ArgumentException">Thrown in case of an invalid parmaeter.</exception>
861 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
862 /// <since_tizen> 3 </since_tizen>
863 public Consumer(string providerId, string dataId)
867 if (string.IsNullOrEmpty(providerId))
869 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "providerId");
872 if (string.IsNullOrEmpty(dataId))
874 ErrorFactory.ThrowException(ResultType.InvalidParameter, false, "dataId");
877 ret = Interop.DataControl.DataControlCreate(out _handle);
878 if (ret != ResultType.Success)
880 ErrorFactory.ThrowException(ret, false, "Creating data control handle is failed");
883 Interop.DataControl.DataControlSetProviderId(_handle, providerId);
884 Interop.DataControl.DataControlSetDataId(_handle, dataId);
886 CallbackManager.RegisterCallback(_handle, providerId);
887 _lock.ReleaseMutex();
889 _providerID = providerId;
893 /// Destructor of the Consumer class.
901 /// Overrides this method if you want to handle the behavior when the DataChangeListen result is received.
903 /// <since_tizen> 3 </since_tizen>
904 protected virtual void OnDataChangeListenResult(DataChangeListenResult result)
906 Log.Info(LogTag, "The OnDataChangeListenResult is not implemented.");
910 /// Overrides this method if you want to handle the behavior when the data change event is received.
912 /// <since_tizen> 3 </since_tizen>
913 protected virtual void OnDataChange(ChangeType type, Bundle data)
915 Log.Info(LogTag, "The OnDataChange is not implemented.");
919 /// Overrides this method if you want to handle the behavior when the select response is received.
921 /// <since_tizen> 3 </since_tizen>
922 protected abstract void OnSelectResult(SelectResult result);
925 /// Overrides this method if you want to handle the behavior when the insert response is received.
927 /// <since_tizen> 3 </since_tizen>
928 protected abstract void OnInsertResult(InsertResult result);
931 /// Overrides this method if you want to handle the behavior when the update response is received.
933 /// <since_tizen> 3 </since_tizen>
934 protected abstract void OnUpdateResult(UpdateResult result);
937 /// Overrides this method if want to handle the behavior when the delete response is received.
939 /// <since_tizen> 3 </since_tizen>
940 protected abstract void OnDeleteResult(DeleteResult result);
942 /// Overrides this method if you want to handle the behavior when the BulkInsert response is received.
944 /// <since_tizen> 3 </since_tizen>
945 protected virtual void OnBulkInsertResult(BulkInsertResult result)
947 Log.Info(LogTag, "The OnBulkInsertResult is not implemented.");
951 /// Overrides this method if you want to handle the behavior when the map get response is received.
953 /// <since_tizen> 3 </since_tizen>
954 protected virtual void OnMapGetResult(MapGetResult result)
956 Log.Info(LogTag, "The OnMapGetResult is not implemented.");
960 /// Overrides this method if you want to handle the behavior when the map add response is received.
962 /// <since_tizen> 3 </since_tizen>
963 protected virtual void OnMapAddResult(MapAddResult result)
965 Log.Info(LogTag, "The OnMapAddResult is not implemented.");
969 /// Overrides this method if you want to handle the behavior when the map set response is received.
971 /// <since_tizen> 3 </since_tizen>
972 protected virtual void OnMapSetResult(MapSetResult result)
974 Log.Info(LogTag, "The OnMapSetResult is not implemented.");
978 /// Overrides this method if you want to handle the behavior when the map remove response is received.
980 /// <since_tizen> 3 </since_tizen>
981 protected virtual void OnMapRemoveResult(MapRemoveResult result)
983 Log.Info(LogTag, "The OnMapRemoveResult is not implemented.");
987 /// Overrides this method if you want to handle the behavior when the BulkAdd response is received.
989 /// <since_tizen> 3 </since_tizen>
990 protected virtual void OnMapBulkAddResult(MapBulkAddResult result)
992 Log.Info(LogTag, "The OnMapBulkAddResult is not implemented.");
996 /// Releases the unmanaged resources used by the Consumer class specifying whether to perform a normal dispose operation.
998 /// <param name="disposing">true for a normal dispose operation; false to finalize the handle.</param>
999 /// <since_tizen> 3 </since_tizen>
1000 protected virtual void Dispose(bool disposing)
1004 if (_changeCallbackID > 0)
1006 Interop.DataControl.RemoveDataChangeCallback(_handle, _changeCallbackID);
1010 CallbackManager.UnregisterCallback(_handle, _providerID);
1011 _lock.ReleaseMutex();
1018 GC.SuppressFinalize(this);
1023 /// Releases all resources used by the Consumer class.
1025 /// <since_tizen> 3 </since_tizen>
1026 public void Dispose()