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 System.Collections.Generic;
19 using Tizen.Applications;
20 using Tizen.Account.AccountManager;
22 namespace Tizen.Account.SyncManager
25 /// The SyncClient APIs for managing the sync operations. Applications will call these APIs to schedule their sync operations.
26 /// The sync service maintains sync requests from all the applications and invokes their respective callback methods to perform account synchronization operations.
28 public static class SyncClient
38 /// Requests the sync manager to perform one time sync operation.
40 /// <param name="request"> The sync job information of the sync job request. </param>
41 /// <param name="syncOptions"> Sync options determine a way to operate the sync job and can be used as ORing. </param>
42 /// <exception cref="ArgumentNullException"> Thrown when any of the arugments are null. </exception>
43 /// <exception cref="InvalidOperationException"> Thrown when the application calling this API doesn't have a sync adapter. </exception>
44 /// <returns> An unique value which can manage sync jobs. The number of sync job ID is limite as it is less than hundred. </returns>
45 public static int RequestOnDemandSyncJob(SyncJobData request, SyncOption syncOptions)
47 if (request == null || request.SyncJobName == null)
49 throw new ArgumentNullException();
52 SafeAccountHandle accountHandle = (request.Account != null) ? request.Account.SafeAccountHandle : new SafeAccountHandle();
53 SafeBundleHandle bundleHandle = (request.UserData != null) ? request.UserData.SafeBundleHandle : new SafeBundleHandle();
56 int ret = Interop.Manager.RequestOnDemandSyncJob(accountHandle, request.SyncJobName, (int)syncOptions, bundleHandle, out id);
57 if (ret != (int)SyncManagerErrorCode.None)
59 Log.Error(ErrorFactory.LogTag, "Failed to request on demand sync job");
60 throw ErrorFactory.GetException(ret);
66 /// Requests the sync manager to perform periodic sync operations.
68 /// <param name="request"> The sync job information of the sync job request. </param>
69 /// <param name="period"> Determines the time interval of the periodic sync. The periodic sync operation can be triggered in that interval, but it does not guarantee the exact time. The minimum value is 30 minutes. </param>
70 /// <param name="syncOptions"> Sync options determine a way to operate the sync job and can be used as ORing. </param>
71 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
72 /// <exception cref="UnauthorizedAccessException"> In case of a privilege not defined. </exception>
73 /// <exception cref="ArgumentNullException"> Thrown when any of the arguments are null. </exception>
74 /// <exception cref="InvalidOperationException"> Thrown when the application calling this API doesn't have a sync adapter. </exception>
75 /// <returns> A unique value which can manage sync jobs. The number of sync job IDs is limited as it is less than hundred. </returns>
76 public static int AddPeriodicSyncJob(SyncJobData request, SyncPeriod period, SyncOption syncOptions)
78 if (request == null || request.SyncJobName == null)
80 throw new ArgumentNullException();
83 SafeAccountHandle accountHandle = (request.Account != null) ? request.Account.SafeAccountHandle : new SafeAccountHandle();
84 SafeBundleHandle bundleHandle = (request.UserData != null) ? request.UserData.SafeBundleHandle : new SafeBundleHandle();
87 int ret = Interop.Manager.AddPeriodicSyncJob(accountHandle, request.SyncJobName, (int) period, (int)syncOptions, bundleHandle, out id);
88 if (ret != (int)SyncManagerErrorCode.None)
90 Log.Error(ErrorFactory.LogTag, "Failed to add periodic sync job");
91 throw ErrorFactory.GetException(ret);
97 /// Requests the sync manager to perform sync operations whenever the corresponding DB is changed.
99 /// <param name="request"> The sync job information of the sync job request. </param>
100 /// <param name="syncOptions"> Sync options determine a way to operate the sync job and can be used as ORing. </param>
101 /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
102 /// <privilege>http://tizen.org/privilege/contact.read</privilege>
103 /// <exception cref="UnauthorizedAccessException"> In case of a privilege is not defined. </exception>
104 /// <exception cref="ArgumentNullException"> Thrown when any of the arguments are null. </exception>
105 /// <exception cref="InvalidOperationException"> Thrown when the application calling this API doesn't have a sync adapter. </exception>
106 /// <returns> A unique value which can manage sync jobs. The number of sync job IDs is limited as it is less than hundred. </returns>
107 public static int AddDataChangeSyncJob(SyncJobData request, SyncOption syncOptions)
109 if (request == null || request.SyncJobName == null)
111 throw new ArgumentNullException();
114 SafeAccountHandle accountHandle = (request.Account != null) ? request.Account.SafeAccountHandle : new SafeAccountHandle();
115 SafeBundleHandle bundleHandle = (request.UserData != null) ? request.UserData.SafeBundleHandle : new SafeBundleHandle();
118 int ret = Interop.Manager.AddDataChangeSyncJob(accountHandle, request.SyncJobName, (int)syncOptions, bundleHandle, out id);
119 if (ret != (int)SyncManagerErrorCode.None)
121 Log.Error(ErrorFactory.LogTag, "Failed to add data change sync job");
122 throw ErrorFactory.GetException(ret);
128 /// Gets all the sync jobs registered with the sync manager.
131 /// Returns the list of SyncJobData corresponding to sync requests.
133 public static IEnumerable<KeyValuePair<int, SyncJobData>> GetAllSyncJobs()
135 IDictionary<int, SyncJobData> syncJobs = new Dictionary<int, SyncJobData>();
136 Interop.Manager.SyncManagerSyncJobCallback cb = (IntPtr accountHandle, string syncJobName, string syncCapability, int syncJobId, IntPtr syncJobUserData, IntPtr userData) =>
138 AccountManager.Account account = new AccountManager.Account(new SafeAccountHandle(accountHandle, true));
139 Bundle bundle = new Bundle(new SafeBundleHandle(syncJobUserData, true));
141 SyncJobData syncJobData = new SyncJobData();
142 syncJobData.Account = account;
143 if (syncJobName != null)
144 syncJobData.SyncJobName = syncJobName;
146 syncJobData.SyncJobName = syncCapability;
147 syncJobData.UserData = bundle;
149 syncJobs.Add(syncJobId, syncJobData);
153 int ret = Interop.Manager.ForeachSyncJob(cb, IntPtr.Zero);
154 if (ret != (int)SyncManagerErrorCode.None)
156 Log.Error(ErrorFactory.LogTag, "Failed to get registered sync job");
157 throw ErrorFactory.GetException(ret);
163 /// Requests the sync manager to remove the corresponding sync job based on the ID.
165 /// <param name="id"> A unique value of each sync job, it can be used to search a specific sync job and remove it. </param>
166 /// <exception cref="ArgumentException"> Thrown if the input arugments is invalid. </exception>
167 public static void RemoveSyncJob(int id)
169 int ret = Interop.Manager.RemoveSyncJob(id);
170 if (ret != (int)SyncManagerErrorCode.None)
172 Log.Error(ErrorFactory.LogTag, "Failed to remove sync job");
173 throw ErrorFactory.GetException(ret);