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.
18 using Tizen.Applications;
19 using Tizen.Account.AccountManager;
21 namespace Tizen.Account.SyncManager
24 /// This class contains the delegates to be called upon scheduling a sync operation.
26 /// <since_tizen> 4 </since_tizen>
27 public class SyncAdapter
29 Interop.Adapter.SyncAdapterStartSyncCallback _startSyncCallback;
30 Interop.Adapter.SyncAdapterCancelSyncCallback _cancelSyncCallback;
33 /// The callback function for the sync adapter's start sync request.
35 /// <since_tizen> 4 </since_tizen>
36 /// <param name="syncParameters"> The sync job parameters corresponding to the sync request. </param>
37 /// <returns> true if the sync operation is success, @c false otherwise. </returns>
38 public delegate bool StartSyncCallback(SyncJobData syncParameters);
41 /// The callback function for the sync adapter's cancel sync request.
43 /// <since_tizen> 4 </since_tizen>
44 /// <param name="syncParameters"> The sync job parameters corresponding to the sync request. </param>
45 public delegate void CancelSyncCallback(SyncJobData syncParameters);
48 /// Sets the client (sync adapter) callback functions.
50 /// <since_tizen> 4 </since_tizen>
51 /// <param name="startSyncCb"> A callback function to be called by the sync manager for performing the sync operation. </param>
52 /// <param name="cancelSyncCb"> A callback function to be called by the sync manager for cancelling the sync operation. </param>
53 /// <exception cref="ArgumentNullException"> Thrown when any of the arguments are null. </exception>
54 /// <exception cref="InvalidOperationException"> Thrown when the application calling this API cannot be a sync adapter. </exception>
55 public void SetSyncEventCallbacks(StartSyncCallback startSyncCb, CancelSyncCallback cancelSyncCb)
57 if (startSyncCb == null || cancelSyncCb == null)
59 throw new ArgumentNullException();
62 _startSyncCallback = (IntPtr accountHandle, string syncJobName, string syncCapability, IntPtr syncJobUserData) =>
64 Log.Debug(ErrorFactory.LogTag, "Start sync event received");
66 AccountManager.Account account = new AccountManager.Account(new SafeAccountHandle(accountHandle, true));
67 Bundle bundle = new Bundle(new SafeBundleHandle(syncJobUserData, true));
69 SyncJobData syncJobData = new SyncJobData()
72 SyncJobName = syncJobName,
76 return startSyncCb(syncJobData);
79 _cancelSyncCallback = (IntPtr accountHandle, string syncJobName, string syncCapability, IntPtr syncJobUserData) =>
81 Log.Debug(ErrorFactory.LogTag, "cancel sync event received");
83 AccountManager.Account account = new AccountManager.Account(new SafeAccountHandle(accountHandle, true));
84 Bundle bundle = new Bundle(new SafeBundleHandle(syncJobUserData, true));
86 SyncJobData syncJobData = new SyncJobData()
89 SyncJobName = syncJobName,
93 cancelSyncCb(syncJobData);
96 int ret = Interop.Adapter.SetCallbacks(_startSyncCallback, _cancelSyncCallback);
97 if (ret != (int)SyncManagerErrorCode.None)
99 Log.Error(ErrorFactory.LogTag, "Failed to set callbacks");
100 throw ErrorFactory.GetException(ret);
105 /// Unsets the client (sync adapter) callback functions.
107 /// <since_tizen> 4 </since_tizen>
108 /// <exception cref="System.Exception"> Thrown when sync manager internal error occurs. </exception>
109 public void UnsetSyncEventCallbacks()
111 int ret = Interop.Adapter.UnsetCallbacks();
112 if (ret != (int)SyncManagerErrorCode.None)
114 Log.Error(ErrorFactory.LogTag, "Failed to unset callbacks");
115 throw ErrorFactory.GetException(ret);