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 /// <since_tizen> 4 </since_tizen>
29 public static class SyncClient
34 /// <since_tizen> 4 </since_tizen>
40 /// Requests the sync manager to perform one time sync operation.
42 /// <since_tizen> 4 </since_tizen>
43 /// <param name="request"> The sync job information of the sync job request. </param>
44 /// <param name="syncOptions"> Sync options determine a way to operate the sync job and can be used as ORing. </param>
45 /// <exception cref="ArgumentNullException"> Thrown when any of the arugments are null. </exception>
46 /// <exception cref="InvalidOperationException"> Thrown when the application calling this API doesn't have a sync adapter. </exception>
47 /// <returns> An unique value which can manage sync jobs. The number of sync job ID is limite as it is less than hundred. </returns>
48 public static int RequestOnDemandSyncJob(SyncJobData request, SyncOption syncOptions)
50 if (request == null || request.SyncJobName == null)
52 throw new ArgumentNullException();
55 SafeAccountHandle accountHandle = (request.Account != null) ? request.Account.SafeAccountHandle : new SafeAccountHandle();
56 SafeBundleHandle bundleHandle = (request.UserData != null) ? request.UserData.SafeBundleHandle : new SafeBundleHandle();
59 int ret = Interop.Manager.RequestOnDemandSyncJob(accountHandle, request.SyncJobName, (int)syncOptions, bundleHandle, out id);
60 if (ret != (int)SyncManagerErrorCode.None)
62 Log.Error(ErrorFactory.LogTag, "Failed to request on demand sync job");
63 throw ErrorFactory.GetException(ret);
69 /// Requests the sync manager to perform periodic sync operations.
71 /// <since_tizen> 4 </since_tizen>
72 /// <param name="request"> The sync job information of the sync job request. </param>
73 /// <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>
74 /// <param name="syncOptions"> Sync options determine a way to operate the sync job and can be used as ORing. </param>
75 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
76 /// <exception cref="UnauthorizedAccessException"> In case of a privilege not defined. </exception>
77 /// <exception cref="ArgumentNullException"> Thrown when any of the arguments are null. </exception>
78 /// <exception cref="InvalidOperationException"> Thrown when the application calling this API doesn't have a sync adapter. </exception>
79 /// <returns> A unique value which can manage sync jobs. The number of sync job IDs is limited as it is less than hundred. </returns>
80 public static int AddPeriodicSyncJob(SyncJobData request, SyncPeriod period, SyncOption syncOptions)
82 if (request == null || request.SyncJobName == null)
84 throw new ArgumentNullException();
87 SafeAccountHandle accountHandle = (request.Account != null) ? request.Account.SafeAccountHandle : new SafeAccountHandle();
88 SafeBundleHandle bundleHandle = (request.UserData != null) ? request.UserData.SafeBundleHandle : new SafeBundleHandle();
91 int ret = Interop.Manager.AddPeriodicSyncJob(accountHandle, request.SyncJobName, (int) period, (int)syncOptions, bundleHandle, out id);
92 if (ret != (int)SyncManagerErrorCode.None)
94 Log.Error(ErrorFactory.LogTag, "Failed to add periodic sync job");
95 throw ErrorFactory.GetException(ret);
101 /// Requests the sync manager to perform sync operations whenever the corresponding DB is changed.
103 /// <since_tizen> 4 </since_tizen>
104 /// <param name="request"> The sync job information of the sync job request. </param>
105 /// <param name="syncOptions"> Sync options determine a way to operate the sync job and can be used as ORing. </param>
106 /// <privilege>http://tizen.org/privilege/calendar.read</privilege>
107 /// <privilege>http://tizen.org/privilege/contact.read</privilege>
108 /// <exception cref="UnauthorizedAccessException"> In case of a privilege is not defined. </exception>
109 /// <exception cref="ArgumentNullException"> Thrown when any of the arguments are null. </exception>
110 /// <exception cref="InvalidOperationException"> Thrown when the application calling this API doesn't have a sync adapter. </exception>
111 /// <returns> A unique value which can manage sync jobs. The number of sync job IDs is limited as it is less than hundred. </returns>
112 public static int AddDataChangeSyncJob(SyncJobData request, SyncOption syncOptions)
114 if (request == null || request.SyncJobName == null)
116 throw new ArgumentNullException();
119 SafeAccountHandle accountHandle = (request.Account != null) ? request.Account.SafeAccountHandle : new SafeAccountHandle();
120 SafeBundleHandle bundleHandle = (request.UserData != null) ? request.UserData.SafeBundleHandle : new SafeBundleHandle();
123 int ret = Interop.Manager.AddDataChangeSyncJob(accountHandle, request.SyncJobName, (int)syncOptions, bundleHandle, out id);
124 if (ret != (int)SyncManagerErrorCode.None)
126 Log.Error(ErrorFactory.LogTag, "Failed to add data change sync job");
127 throw ErrorFactory.GetException(ret);
133 /// Gets all the sync jobs registered with the sync manager.
135 /// <since_tizen> 4 </since_tizen>
137 /// Returns the list of SyncJobData corresponding to sync requests.
139 public static IEnumerable<KeyValuePair<int, SyncJobData>> GetAllSyncJobs()
141 IDictionary<int, SyncJobData> syncJobs = new Dictionary<int, SyncJobData>();
142 Interop.Manager.SyncManagerSyncJobCallback cb = (IntPtr accountHandle, string syncJobName, string syncCapability, int syncJobId, IntPtr syncJobUserData, IntPtr userData) =>
144 AccountManager.Account account = new AccountManager.Account(new SafeAccountHandle(accountHandle, true));
145 Bundle bundle = new Bundle(new SafeBundleHandle(syncJobUserData, true));
147 SyncJobData syncJobData = new SyncJobData();
148 syncJobData.Account = account;
149 if (syncJobName != null)
150 syncJobData.SyncJobName = syncJobName;
152 syncJobData.SyncJobName = syncCapability;
153 syncJobData.UserData = bundle;
155 syncJobs.Add(syncJobId, syncJobData);
159 int ret = Interop.Manager.ForeachSyncJob(cb, IntPtr.Zero);
160 if (ret != (int)SyncManagerErrorCode.None)
162 Log.Error(ErrorFactory.LogTag, "Failed to get registered sync job");
163 throw ErrorFactory.GetException(ret);
169 /// Requests the sync manager to remove the corresponding sync job based on the ID.
171 /// <since_tizen> 4 </since_tizen>
172 /// <param name="id"> A unique value of each sync job, it can be used to search a specific sync job and remove it. </param>
173 /// <exception cref="ArgumentException"> Thrown if the input arugments is invalid. </exception>
174 public static void RemoveSyncJob(int id)
176 int ret = Interop.Manager.RemoveSyncJob(id);
177 if (ret != (int)SyncManagerErrorCode.None)
179 Log.Error(ErrorFactory.LogTag, "Failed to remove sync job");
180 throw ErrorFactory.GetException(ret);