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;
20 namespace Tizen.Account.AccountManager
23 /// The AccountManager APIs is separated into two major sections:
24 /// 1. Registering an account provider while an application is installed. This information will be used for the Add account screen.
25 /// 2. Adding an account information when an application signs in successfully to share the account information to the Tizen system. This information will be shown in the Tizen settings account menu.
27 /// The APIs of both of the sections consist of the following functionality:
29 /// <item> Create an account or account provider </item>
30 /// <item> Update an account or account provider(Only available for the creator) </item>
31 /// <item> Delete an account or account provider(Only available for the creator) </item>
32 /// <item> Read an account or account provider with some filter </item>
35 /// <since_tizen> 3 </since_tizen>
37 public static class AccountService
40 /// This is contact capability string.
42 /// <since_tizen> 3 </since_tizen>
43 public static readonly string ContactCapability = "http://tizen.org/account/capability/contact";
46 /// This is calendar capability string.
48 /// <since_tizen> 3 </since_tizen>
49 public static readonly string CalendarCapability = "http://tizen.org/account/capability/calendar";
52 /// This is email capability string.
54 /// <since_tizen> 3 </since_tizen>
55 public static readonly string EmailCapability = "http://tizen.org/account/capability/email";
58 /// This is photo capability string.
60 /// <since_tizen> 3 </since_tizen>
61 public static readonly string PhotoCapability = "http://tizen.org/account/capability/photo";
64 /// This is video capability string.
66 /// <since_tizen> 3 </since_tizen>
67 public static readonly string VideoCapability = "http://tizen.org/account/capability/video";
70 /// This is music capability string.
72 /// <since_tizen> 3 </since_tizen>
73 public static readonly string MusicCapability = "http://tizen.org/account/capability/music";
76 /// This is document capability string.
78 /// <since_tizen> 3 </since_tizen>
79 public static readonly string DocumentCapability = "http://tizen.org/account/capability/document";
82 /// This is message capability string.
84 /// <since_tizen> 3 </since_tizen>
85 public static readonly string MessageCapability = "http://tizen.org/account/capability/message";
88 /// This is game capability string.
90 /// <since_tizen> 3 </since_tizen>
91 public static readonly string GameCapability = "http://tizen.org/account/capability/game";
94 /// Retrieves all accounts details from the account database.
96 /// <since_tizen> 3 </since_tizen>
97 /// <returns>List of Accounts</returns>
98 /// <privilege>http://tizen.org/privilege/account.read</privilege>
99 /// <exception cref="InvalidOperationException">In case of any DB error. </exception>
100 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
101 public static IEnumerable<Account> GetAccountsAsync()
103 List<Account> accounts = new List<Account>();
104 List<int> values = new List<int>();
105 Interop.Account.AccountCallback accountCallback = (IntPtr data, IntPtr userdata) =>
107 Account account = new Account(data);
108 values.Add(account.AccountId);
113 AccountError res = (AccountError)Interop.AccountService.AccountForeachAccountFromDb(accountCallback, IntPtr.Zero);
114 if (res != AccountError.None)
116 throw AccountErrorFactory.CreateException(res, "Failed to AccountForeachAccountFromDb");
119 foreach (int i in values)
121 Account account = AccountService.GetAccountById(i);
122 accounts.Add(account);
129 /// Retrieve an account with the account ID.
131 /// <since_tizen> 3 </since_tizen>
132 /// <param name="accountId"> The account Id to be searched.</param>
133 /// <returns>Account instance with reference to the given id.</returns>
134 /// <privilege>http://tizen.org/privilege/account.read</privilege>
135 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given account id</exception>
136 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
137 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
138 public static Account GetAccountById(int accountId)
140 Account account = Account.CreateAccount();
141 IntPtr handle = account.Handle;
142 AccountError res = (AccountError)Interop.AccountService.QueryAccountById(accountId, out handle);
143 if (res != AccountError.None)
145 throw AccountErrorFactory.CreateException(res, "Failed to get accounts from the database for account id: " + accountId);
148 account.Handle = handle;
153 /// Retrieves all AccountProviders details from the account database.
155 /// <since_tizen> 3 </since_tizen>
156 /// <returns>List of AccountProviders</returns>
157 /// <privilege>http://tizen.org/privilege/account.read</privilege>
158 /// <exception cref="InvalidOperationException">In case of any DB error</exception>
159 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
160 public static IEnumerable<AccountProvider> GetAccountProviders()
162 List<string> values = new List<string>();
163 List<AccountProvider> providers = new List<AccountProvider>();
164 Interop.AccountProvider.AccountProviderCallback accountCallback = (IntPtr handle, IntPtr data) =>
166 AccountProvider provider = new AccountProvider(handle);
167 values.Add(provider.AppId);
172 AccountError res = (AccountError)Interop.AccountService.GetAllAccountproviders(accountCallback, IntPtr.Zero);
173 if (res != AccountError.None)
175 Log.Warn(AccountErrorFactory.LogTag, "Failed to get account providers from the database");
176 throw AccountErrorFactory.CreateException(res, "Failed to get account providers from the database");
179 foreach (string val in values)
181 AccountProvider provider = GetAccountProviderByAppId(val);
182 providers.Add(provider);
189 /// Retrieves the account provider information with application Id.
191 /// <since_tizen> 3 </since_tizen>
192 /// <param name="appId">Application Id.</param>
193 /// <returns>The AccountProvider instance associated with the given application Id.</returns>
194 /// <privilege>http://tizen.org/privilege/account.read</privilege>
195 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given appid</exception>
196 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
197 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
198 public static AccountProvider GetAccountProviderByAppId(string appId)
201 Interop.AccountProvider.Create(out handle);
202 AccountError err = (AccountError)Interop.AccountService.GetAccountProviderByAppId(appId, out handle);
203 if (err != AccountError.None)
205 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderByAppId");
208 AccountProvider provider = new AccountProvider(handle);
213 /// Retrieves all the account providers information with feature.
215 /// <since_tizen> 3 </since_tizen>
216 /// <param name="feature">The capability value to search for account providers.</param>
217 /// <returns>Retrieves AccountProviders information with the capability name.</returns>
218 /// <privilege>http://tizen.org/privilege/account.read</privilege>
219 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given feature</exception>
220 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
221 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
222 public static IEnumerable<AccountProvider> GetAccountProvidersByFeature(string feature)
224 List<string> values = new List<string>();
225 List<AccountProvider> providers = new List<AccountProvider>();
226 Interop.AccountProvider.AccountProviderCallback providerCallback = (IntPtr handle, IntPtr data) =>
228 AccountProvider provider = new AccountProvider(handle);
229 values.Add(provider.AppId);
234 AccountError err = (AccountError)Interop.AccountService.GetAccountProviderByFeature(providerCallback, feature, IntPtr.Zero);
235 if (err != AccountError.None)
237 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderByFeature");
240 foreach (string val in values)
242 AccountProvider provider = GetAccountProviderByAppId(val);
243 providers.Add(provider);
250 /// Inserts into the Database with the new account Infomration.
252 /// <since_tizen> 3 </since_tizen>
253 /// <param name="account">New Account instance to be added.</param>
254 /// <privilege>http://tizen.org/privilege/account.read</privilege>
255 /// <privilege>http://tizen.org/privilege/account.write </privilege>
256 /// <exception cref="InvalidOperationException">In case of any DB error</exception>
257 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
258 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
259 /// <exception cref="OutOfMemoryException"> In case of OutOfMemory error.</exception>
260 public static int AddAccount(Account account)
264 throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to AddAccount");
268 AccountError err = (AccountError)Interop.AccountService.AddAccount(account.Handle, out id);
269 if (err != AccountError.None)
271 throw AccountErrorFactory.CreateException(err, "Failed to AddAccount");
278 /// Updates the account details to the account database.
280 /// <since_tizen> 3 </since_tizen>
281 /// <param name="account">account instance to be updated.</param>
282 /// <privilege>http://tizen.org/privilege/account.read</privilege>
283 /// <privilege>http://tizen.org/privilege/account.write </privilege>
284 /// <exception cref="InvalidOperationException">In case of any DB error </exception>
285 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
286 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
287 /// <exception cref="OutOfMemoryException"> In case of OutOfMemory error.</exception>
288 public static void UpdateAccount(Account account)
292 throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to UpdateAccount");
295 AccountError err = (AccountError)Interop.AccountService.UpdateAccountToDBById(account.Handle, account.AccountId);
296 if (err != AccountError.None)
298 throw AccountErrorFactory.CreateException(err, "Failed to UpdateAccount");
303 /// Deletes the account information from the Database.
305 /// <since_tizen> 3 </since_tizen>
306 /// <param name="account">Account instance to be deleted from the database.</param>
307 /// <privilege>http://tizen.org/privilege/account.read</privilege>
308 /// <privilege>http://tizen.org/privilege/account.write </privilege>
309 /// <exception cref="InvalidOperationException">In case of any DB error </exception>
310 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
311 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
312 public static void DeleteAccount(Account account)
316 throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to DeleteAccount");
319 AccountError err = (AccountError)Interop.AccountService.DeleteAccountById(account.AccountId);
320 if (err != AccountError.None)
322 throw AccountErrorFactory.CreateException(err, "Failed to delete the account by Id: " + account.AccountId);
327 /// Deletes an account from the account database by user name.
329 /// <since_tizen> 3 </since_tizen>
330 /// <param name="userName">The user name of the account to delete.</param>
331 /// <param name="packageName">The package name of the account to delete.</param>
332 /// <privilege>http://tizen.org/privilege/account.read</privilege>
333 /// <privilege>http://tizen.org/privilege/account.write </privilege>
334 /// <exception cref="InvalidOperationException">In case of any DB error</exception>
335 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
336 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
337 public static void DeleteAccount(string userName, string packageName)
339 AccountError err = (AccountError)Interop.AccountService.DeleteAccountByUser(userName, packageName);
340 if (err != AccountError.None)
342 throw AccountErrorFactory.CreateException(err, "Failed to delete the account by userName: " + userName);
347 /// Deletes an account from the account database by package name.
349 /// <since_tizen> 3 </since_tizen>
350 /// <param name="packageName">The package name of the account to delete.</param>
351 /// <privilege>http://tizen.org/privilege/account.read</privilege>
352 /// <privilege>http://tizen.org/privilege/account.write </privilege>
353 /// <exception cref="InvalidOperationException">In case of any DB error</exception>
354 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
355 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
356 public static void DeleteAccount(string packageName)
358 AccountError err = (AccountError)Interop.AccountService.DeleteAccountByPackage(packageName);
359 if (err != AccountError.None)
361 throw AccountErrorFactory.CreateException(err, "Failed to delete the account by package name: " + packageName);
367 /// Retrieves all accounts with the given user name.
369 /// <since_tizen> 3 </since_tizen>
370 /// <param name="userName">The user name to search .</param>
371 /// <returns>Accounts list matched with the user name</returns>
372 /// <privilege>http://tizen.org/privilege/account.read</privilege>
373 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given username</exception>
374 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
375 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
376 public static IEnumerable<Account> GetAccountsByUserName(string userName)
378 List<Account> accounts = new List<Account>();
379 List<int> values = new List<int>();
380 Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
382 Account account = new Account(handle);
383 values.Add(account.AccountId);
388 AccountError err = (AccountError)Interop.AccountService.QueryAccountByUserName(accountCallback, userName, IntPtr.Zero);
389 if (err != AccountError.None)
391 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountByUserName");
394 foreach (int i in values)
396 Account account = AccountService.GetAccountById(i);
397 accounts.Add(account);
404 /// Retrieves all accounts with the given package name.
406 /// <since_tizen> 3 </since_tizen>
407 /// <param name="packageName"> The package name to Search</param>
408 /// <returns>Accounts list matched with the package name</returns>
409 /// <privilege>http://tizen.org/privilege/account.read</privilege>
410 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given package name</exception>
411 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
412 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
413 public static IEnumerable<Account> GetAccountsByPackageName(string packageName)
415 List<Account> accounts = new List<Account>();
416 List<int> values = new List<int>();
417 Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
419 Account account = new Account(handle);
420 values.Add(account.AccountId);
425 AccountError err = (AccountError)Interop.AccountService.QueryAccountByPackageName(accountCallback, packageName, IntPtr.Zero);
426 if (err != AccountError.None)
428 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountByPackageName");
431 foreach (int i in values)
433 Account account = AccountService.GetAccountById(i);
434 accounts.Add(account);
441 /// Retrieves all accounts with the given cpability type.
443 /// <since_tizen> 3 </since_tizen>
444 /// <param name="type"> Capability type</param>
445 /// <returns>Accounts list matched with the capability type</returns>
446 /// <privilege>http://tizen.org/privilege/account.read</privilege>
447 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given capability type</exception>
448 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
449 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
450 public static IEnumerable<Account> GetAccountsByCapabilityType(string type)
452 List<Account> accounts = new List<Account>();
453 List<int> values = new List<int>();
454 Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
456 Account account = new Account(handle);
457 values.Add(account.AccountId);
462 AccountError err = (AccountError)Interop.AccountService.GetAccountByCapabilityType(accountCallback, type, IntPtr.Zero);
463 if (err != AccountError.None)
465 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountByCapabilityType");
468 foreach (int i in values)
470 Account account = AccountService.GetAccountById(i);
471 accounts.Add(account);
478 /// Retrieves all capabilities with the given account
480 /// <since_tizen> 3 </since_tizen>
481 /// <param name="accountId">account instance</param>
482 /// <returns>Capabilities list as Dictionary of Capability type and State.</returns>
483 /// <privilege>http://tizen.org/privilege/account.read</privilege>
484 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given account id</exception>
485 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
486 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
487 public static Dictionary<string, CapabilityState> GetCapabilitiesById(int accountId)
489 Dictionary<string, CapabilityState> capabilities = new Dictionary<string, CapabilityState>();
490 Interop.Account.AccountCapabilityCallback capabilityCallback = (string type, int capabilityState, IntPtr data) =>
492 capabilities.Add(type, (CapabilityState)capabilityState);
496 AccountError err = (AccountError)Interop.AccountService.QueryAccountCapabilityById(capabilityCallback, accountId, IntPtr.Zero);
497 if (err != AccountError.None)
499 throw AccountErrorFactory.CreateException(err, "Failed to GetAllCapabilitiesById");
506 /// Gets the count of accounts in the account database.
508 /// <since_tizen> 3 </since_tizen>
509 /// <returns>The number of accounts in the database</returns>
510 /// <privilege>http://tizen.org/privilege/account.read</privilege>
511 /// <exception cref="InvalidOperationException">In case of any DB error </exception>
512 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
513 public static int GetAccountsCount()
516 AccountError err = (AccountError)Interop.AccountService.GetAccountCount(out count);
517 if (err != AccountError.None)
519 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountCount");
526 /// Updates the sync status of the given account.
528 /// <since_tizen> 3 </since_tizen>
529 /// <param name="account"> Account for which sync status needs to be updated</param>
530 /// <param name="status">Sync State</param>
531 /// <privilege>http://tizen.org/privilege/account.read</privilege>
532 /// <privilege>http://tizen.org/privilege/account.write</privilege>
533 /// <exception cref="InvalidOperationException">In case of any DB error </exception>
534 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
535 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
536 public static void UpdateSyncStatusById(Account account, AccountSyncState status)
538 AccountError err = (AccountError)Interop.AccountService.UpdateAccountSyncStatusById(account.AccountId, (int)status);
539 if (err != AccountError.None)
541 throw AccountErrorFactory.CreateException(err, "Failed to UpdateSyncStatusById");
545 private static readonly Interop.AccountService.SubscribeCallback s_accountUpdatedCallback = (string eventType, int accountId, IntPtr userData) =>
547 AccountSubscriberEventArgs eventArgs = new AccountSubscriberEventArgs(eventType, accountId);
548 s_accountUpdated?.Invoke(null, eventArgs);
552 private static Interop.AccountService.SafeAccountSubscriberHandle s_subscriberHandle;
554 private static event EventHandler<AccountSubscriberEventArgs> s_accountUpdated;
556 /// ContentUpdated event is triggered when the media item info from DB changes.
558 /// <since_tizen> 3 </since_tizen>
560 /// ContentUpdate event is triggered if the MediaInformaion updated/deleted or new Inforamtion is Inserted.
562 /// <param name="sender"></param>
563 /// <param name="e">A ContentUpdatedEventArgs object that contains information about the update operation.</param>
564 /// <privilege>http://tizen.org/privilege/account.read</privilege>
565 /// <exception cref="InvalidOperationException">In case of any DB error </exception>
566 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
567 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
568 public static event EventHandler<AccountSubscriberEventArgs> AccountUpdated
572 if (s_accountUpdated == null)
574 if (s_subscriberHandle == null)
576 Interop.AccountService.CreateAccountSubscriber(out s_subscriberHandle);
579 AccountError ret = (AccountError)Interop.AccountService.RegisterSubscriber(s_subscriberHandle, s_accountUpdatedCallback, IntPtr.Zero);
581 if (ret != AccountError.None)
583 throw AccountErrorFactory.CreateException(ret, "Error in callback handling");
587 s_accountUpdated += value;
592 s_accountUpdated -= value;
593 if (s_accountUpdated == null)
595 AccountError ret = (AccountError)Interop.AccountService.UnregisterSubscriber(s_subscriberHandle);
596 if (ret != AccountError.None)
598 throw AccountErrorFactory.CreateException(ret, "Error in callback handling");
600 s_subscriberHandle = null;