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 public class SyncAdapter
28 Interop.Adapter.SyncAdapterStartSyncCallback _startSyncCallback;
29 Interop.Adapter.SyncAdapterCancelSyncCallback _cancelSyncCallback;
32 /// The callback function for the sync adapter's start sync request.
34 /// <param name="syncParameters"> The sync job parameters corresponding to the sync request. </param>
35 /// <returns> true if the sync operation is success, @c false otherwise. </returns>
36 public delegate bool StartSyncCallback(SyncJobData syncParameters);
39 /// The callback function for the sync adapter's cancel sync request.
41 /// <param name="syncParameters"> The sync job parameters corresponding to the sync request. </param>
42 public delegate void CancelSyncCallback(SyncJobData syncParameters);
45 /// Sets the client (sync adapter) callback functions.
47 /// <param name="startSyncCb"> A callback function to be called by the sync manager for performing the sync operation. </param>
48 /// <param name="cancelSyncCb"> A callback function to be called by the sync manager for cancelling the sync operation. </param>
49 /// <exception cref="ArgumentNullException"> Thrown when any of the arguments are null. </exception>
50 /// <exception cref="InvalidOperationException"> Thrown when the application calling this API cannot be a sync adapter. </exception>
51 public void SetSyncEventCallbacks(StartSyncCallback startSyncCb, CancelSyncCallback cancelSyncCb)
53 if (startSyncCb == null || cancelSyncCb == null)
55 throw new ArgumentNullException();
58 _startSyncCallback = (IntPtr accountHandle, string syncJobName, string syncCapability, IntPtr syncJobUserData) =>
60 Log.Debug(ErrorFactory.LogTag, "Start sync event received");
62 AccountManager.Account account = new AccountManager.Account(new SafeAccountHandle(accountHandle, true));
63 Bundle bundle = new Bundle(new SafeBundleHandle(syncJobUserData, true));
65 SyncJobData syncJobData = new SyncJobData()
68 SyncJobName = syncJobName,
72 return startSyncCb(syncJobData);
75 _cancelSyncCallback = (IntPtr accountHandle, string syncJobName, string syncCapability, IntPtr syncJobUserData) =>
77 Log.Debug(ErrorFactory.LogTag, "cancel sync event received");
79 AccountManager.Account account = new AccountManager.Account(new SafeAccountHandle(accountHandle, true));
80 Bundle bundle = new Bundle(new SafeBundleHandle(syncJobUserData, true));
82 SyncJobData syncJobData = new SyncJobData()
85 SyncJobName = syncJobName,
89 cancelSyncCb(syncJobData);
92 int ret = Interop.Adapter.SetCallbacks(_startSyncCallback, _cancelSyncCallback);
93 if (ret != (int)SyncManagerErrorCode.None)
95 Log.Error(ErrorFactory.LogTag, "Failed to set callbacks");
96 throw ErrorFactory.GetException(ret);
101 /// Unsets the client (sync adapter) callback functions.
103 /// <exception cref="System.Exception"> Thrown when sync manager internal error occurs. </exception>
104 public void UnsetSyncEventCallbacks()
106 int ret = Interop.Adapter.UnsetCallbacks();
107 if (ret != (int)SyncManagerErrorCode.None)
109 Log.Error(ErrorFactory.LogTag, "Failed to unset callbacks");
110 throw ErrorFactory.GetException(ret);