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 are 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 the contact capability string.
42 /// <since_tizen> 3 </since_tizen>
43 public static readonly string ContactCapability = "http://tizen.org/account/capability/contact";
46 /// This is the calendar capability string.
48 /// <since_tizen> 3 </since_tizen>
49 public static readonly string CalendarCapability = "http://tizen.org/account/capability/calendar";
52 /// This is the email capability string.
54 /// <since_tizen> 3 </since_tizen>
55 public static readonly string EmailCapability = "http://tizen.org/account/capability/email";
58 /// This is the photo capability string.
60 /// <since_tizen> 3 </since_tizen>
61 public static readonly string PhotoCapability = "http://tizen.org/account/capability/photo";
64 /// This is the video capability string.
66 /// <since_tizen> 3 </since_tizen>
67 public static readonly string VideoCapability = "http://tizen.org/account/capability/video";
70 /// This is the music capability string.
72 /// <since_tizen> 3 </since_tizen>
73 public static readonly string MusicCapability = "http://tizen.org/account/capability/music";
76 /// This is the document capability string.
78 /// <since_tizen> 3 </since_tizen>
79 public static readonly string DocumentCapability = "http://tizen.org/account/capability/document";
82 /// This is the message capability string.
84 /// <since_tizen> 3 </since_tizen>
85 public static readonly string MessageCapability = "http://tizen.org/account/capability/message";
88 /// This is the game capability string.
90 /// <since_tizen> 3 </since_tizen>
91 public static readonly string GameCapability = "http://tizen.org/account/capability/game";
94 /// Retrieves all the 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 /// <feature>http://tizen.org/feature/account</feature>
100 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
101 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
102 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
103 public static IEnumerable<Account> GetAccountsAsync()
105 List<Account> accounts = new List<Account>();
106 List<int> values = new List<int>();
107 Interop.Account.AccountCallback accountCallback = (IntPtr data, IntPtr userdata) =>
109 Account account = new Account(new SafeAccountHandle(data, true));
110 values.Add(account.AccountId);
115 AccountError res = (AccountError)Interop.AccountService.AccountForeachAccountFromDb(accountCallback, IntPtr.Zero);
116 if (res != AccountError.None)
118 throw AccountErrorFactory.CreateException(res, "Failed to AccountForeachAccountFromDb");
121 foreach (int i in values)
123 Account account = AccountService.GetAccountById(i);
124 accounts.Add(account);
131 /// Retrieves the account with the account ID.
133 /// <since_tizen> 3 </since_tizen>
134 /// <param name="accountId"> The account ID to be searched.</param>
135 /// <returns>Account instance with reference to the given ID.</returns>
136 /// <privilege>http://tizen.org/privilege/account.read</privilege>
137 /// <feature>http://tizen.org/feature/account</feature>
138 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given account ID.</exception>
139 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
140 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
141 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
142 public static Account GetAccountById(int accountId)
144 Account account = Account.CreateAccount();
145 SafeAccountHandle handle = account.SafeAccountHandle;
147 AccountError res = (AccountError)Interop.AccountService.QueryAccountById(accountId, ref handle);
148 if (res != AccountError.None)
150 throw AccountErrorFactory.CreateException(res, "Failed to get accounts from the database for account id: " + accountId);
152 Account ref_account = new Account(handle);
158 /// Retrieves all the AccountProviders details from the account database.
160 /// <since_tizen> 3 </since_tizen>
161 /// <returns>List of AccountProviders.</returns>
162 /// <privilege>http://tizen.org/privilege/account.read</privilege>
163 /// <feature>http://tizen.org/feature/account</feature>
164 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
165 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
166 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
167 public static IEnumerable<AccountProvider> GetAccountProviders()
169 List<string> values = new List<string>();
170 List<AccountProvider> providers = new List<AccountProvider>();
171 Interop.AccountProvider.AccountProviderCallback accountCallback = (IntPtr handle, IntPtr data) =>
173 AccountProvider provider = new AccountProvider(handle);
174 values.Add(provider.AppId);
179 AccountError res = (AccountError)Interop.AccountService.GetAllAccountproviders(accountCallback, IntPtr.Zero);
180 if (res != AccountError.None)
182 Log.Warn(AccountErrorFactory.LogTag, "Failed to get account providers from the database");
183 throw AccountErrorFactory.CreateException(res, "Failed to get account providers from the database");
186 foreach (string val in values)
188 AccountProvider provider = GetAccountProviderByAppId(val);
189 providers.Add(provider);
196 /// Retrieves the account provider information with the application ID.
198 /// <since_tizen> 3 </since_tizen>
199 /// <param name="appId">The application ID.</param>
200 /// <returns>The AccountProvider instance associated with the given application ID.</returns>
201 /// <privilege>http://tizen.org/privilege/account.read</privilege>
202 /// <feature>http://tizen.org/feature/account</feature>
203 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given appId.</exception>
204 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
205 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
206 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
207 public static AccountProvider GetAccountProviderByAppId(string appId)
210 Interop.AccountProvider.Create(out handle);
211 AccountError err = (AccountError)Interop.AccountService.GetAccountProviderByAppId(appId, out handle);
212 if (err != AccountError.None)
214 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderByAppId");
217 AccountProvider provider = new AccountProvider(handle);
222 /// Retrieves all the account providers information with feature.
224 /// <since_tizen> 3 </since_tizen>
225 /// <param name="feature">The capability value to search for account providers.</param>
226 /// <returns>Retrieves the AccountProviders information with the capability name.</returns>
227 /// <privilege>http://tizen.org/privilege/account.read</privilege>
228 /// <feature>http://tizen.org/feature/account</feature>
229 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given feature.</exception>
230 /// <exception cref="ArgumentException"> In case of invalid parameter.</exception>
231 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
232 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
233 public static IEnumerable<AccountProvider> GetAccountProvidersByFeature(string feature)
235 List<string> values = new List<string>();
236 List<AccountProvider> providers = new List<AccountProvider>();
237 Interop.AccountProvider.AccountProviderCallback providerCallback = (IntPtr handle, IntPtr data) =>
239 AccountProvider provider = new AccountProvider(handle);
240 values.Add(provider.AppId);
245 AccountError err = (AccountError)Interop.AccountService.GetAccountProviderByFeature(providerCallback, feature, IntPtr.Zero);
246 if (err != AccountError.None)
248 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderByFeature");
251 foreach (string val in values)
253 AccountProvider provider = GetAccountProviderByAppId(val);
254 providers.Add(provider);
261 /// Inserts into the Database with the new account Information.
263 /// <since_tizen> 3 </since_tizen>
264 /// <param name="account">New Account instance to be added.</param>
265 /// <returns>The account ID of the account instance.</returns>
266 /// <privilege>http://tizen.org/privilege/account.read</privilege>
267 /// <privilege>http://tizen.org/privilege/account.write </privilege>
268 /// <feature>http://tizen.org/feature/account</feature>
269 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
270 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
271 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
272 /// <exception cref="OutOfMemoryException"> In case of OutOfMemory error.</exception>
273 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
274 public static int AddAccount(Account account)
278 throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to AddAccount");
282 AccountError err = (AccountError)Interop.AccountService.AddAccount(account.SafeAccountHandle, out id);
283 if (err != AccountError.None)
285 throw AccountErrorFactory.CreateException(err, "Failed to AddAccount");
292 /// Updates the account details to the account database.
294 /// <since_tizen> 3 </since_tizen>
295 /// <param name="account">Account instance to be updated.</param>
296 /// <privilege>http://tizen.org/privilege/account.read</privilege>
297 /// <privilege>http://tizen.org/privilege/account.write </privilege>
298 /// <feature>http://tizen.org/feature/account</feature>
299 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
300 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
301 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
302 /// <exception cref="OutOfMemoryException"> In case of OutOfMemory error.</exception>
303 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
304 public static void UpdateAccount(Account account)
308 throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to UpdateAccount");
311 AccountError err = (AccountError)Interop.AccountService.UpdateAccountToDBById(account.SafeAccountHandle, account.AccountId);
312 if (err != AccountError.None)
314 throw AccountErrorFactory.CreateException(err, "Failed to UpdateAccount");
319 /// Deletes the account information from the database.
321 /// <since_tizen> 3 </since_tizen>
322 /// <param name="account">Account instance to be deleted from the database.</param>
323 /// <privilege>http://tizen.org/privilege/account.read</privilege>
324 /// <privilege>http://tizen.org/privilege/account.write</privilege>
325 /// <feature>http://tizen.org/feature/account</feature>
326 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
327 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
328 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
329 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
330 public static void DeleteAccount(Account account)
334 throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to DeleteAccount");
337 AccountError err = (AccountError)Interop.AccountService.DeleteAccountById(account.AccountId);
338 if (err != AccountError.None)
340 throw AccountErrorFactory.CreateException(err, "Failed to delete the account by Id: " + account.AccountId);
345 /// Deletes the account from the account database by user name.
347 /// <since_tizen> 3 </since_tizen>
348 /// <param name="userName">The user name of the account to delete.</param>
349 /// <param name="packageName">The package name of the account to delete.</param>
350 /// <privilege>http://tizen.org/privilege/account.read</privilege>
351 /// <privilege>http://tizen.org/privilege/account.write</privilege>
352 /// <feature>http://tizen.org/feature/account</feature>
353 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
354 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
355 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
356 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
357 public static void DeleteAccount(string userName, string packageName)
359 AccountError err = (AccountError)Interop.AccountService.DeleteAccountByUser(userName, packageName);
360 if (err != AccountError.None)
362 throw AccountErrorFactory.CreateException(err, "Failed to delete the account by userName: " + userName);
367 /// Deletes the account from the account database by package name.
369 /// <since_tizen> 3 </since_tizen>
370 /// <param name="packageName">The package name of the account to delete.</param>
371 /// <privilege>http://tizen.org/privilege/account.read</privilege>
372 /// <privilege>http://tizen.org/privilege/account.write</privilege>
373 /// <feature>http://tizen.org/feature/account</feature>
374 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
375 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
376 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
377 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
378 public static void DeleteAccount(string packageName)
380 AccountError err = (AccountError)Interop.AccountService.DeleteAccountByPackage(packageName);
381 if (err != AccountError.None)
383 throw AccountErrorFactory.CreateException(err, "Failed to delete the account by package name: " + packageName);
389 /// Retrieves all the accounts with the given user name.
391 /// <since_tizen> 3 </since_tizen>
392 /// <param name="userName">The user name to search.</param>
393 /// <returns>Accounts list matched with the user name.</returns>
394 /// <privilege>http://tizen.org/privilege/account.read</privilege>
395 /// <feature>http://tizen.org/feature/account</feature>
396 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given username.</exception>
397 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
398 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
399 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
400 public static IEnumerable<Account> GetAccountsByUserName(string userName)
402 List<Account> accounts = new List<Account>();
403 List<int> values = new List<int>();
404 Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
406 Account account = new Account(new SafeAccountHandle(handle, true));
407 values.Add(account.AccountId);
412 AccountError err = (AccountError)Interop.AccountService.QueryAccountByUserName(accountCallback, userName, IntPtr.Zero);
413 if (err != AccountError.None)
415 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountByUserName");
418 foreach (int i in values)
420 Account account = AccountService.GetAccountById(i);
421 accounts.Add(account);
428 /// Retrieves all the accounts with the given package name.
430 /// <since_tizen> 3 </since_tizen>
431 /// <param name="packageName"> The package name to search.</param>
432 /// <returns>Accounts list matched with the package name.</returns>
433 /// <privilege>http://tizen.org/privilege/account.read</privilege>
434 /// <feature>http://tizen.org/feature/account</feature>
435 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given package name.</exception>
436 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
437 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
438 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
439 public static IEnumerable<Account> GetAccountsByPackageName(string packageName)
441 List<Account> accounts = new List<Account>();
442 List<int> values = new List<int>();
443 Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
445 Account account = new Account(new SafeAccountHandle(handle, true));
446 values.Add(account.AccountId);
451 AccountError err = (AccountError)Interop.AccountService.QueryAccountByPackageName(accountCallback, packageName, IntPtr.Zero);
452 if (err != AccountError.None)
454 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountByPackageName");
457 foreach (int i in values)
459 Account account = AccountService.GetAccountById(i);
460 accounts.Add(account);
467 /// Retrieves all accounts with the given capability type.
469 /// <since_tizen> 3 </since_tizen>
470 /// <param name="type"> Capability type.</param>
471 /// <returns>Accounts list matched with the capability type.</returns>
472 /// <privilege>http://tizen.org/privilege/account.read</privilege>
473 /// <feature>http://tizen.org/feature/account</feature>
474 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for the given capability type.</exception>
475 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
476 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
477 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
478 public static IEnumerable<Account> GetAccountsByCapabilityType(string type)
480 List<Account> accounts = new List<Account>();
481 List<int> values = new List<int>();
482 Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
484 Account account = new Account(new SafeAccountHandle(handle, true));
485 values.Add(account.AccountId);
490 AccountError err = (AccountError)Interop.AccountService.GetAccountByCapabilityType(accountCallback, type, IntPtr.Zero);
491 if (err != AccountError.None)
493 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountByCapabilityType");
496 foreach (int i in values)
498 Account account = AccountService.GetAccountById(i);
499 accounts.Add(account);
506 /// Retrieves all the capabilities with the given account.
508 /// <since_tizen> 3 </since_tizen>
509 /// <param name="accountId">Account instance.</param>
510 /// <returns>Capabilities list as dictionary of the capability type and state.</returns>
511 /// <privilege>http://tizen.org/privilege/account.read</privilege>
512 /// <feature>http://tizen.org/feature/account</feature>
513 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given account ID.</exception>
514 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
515 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
516 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
517 public static Dictionary<string, CapabilityState> GetCapabilitiesById(int accountId)
519 Dictionary<string, CapabilityState> capabilities = new Dictionary<string, CapabilityState>();
520 Interop.Account.AccountCapabilityCallback capabilityCallback = (string type, int capabilityState, IntPtr data) =>
522 capabilities.Add(type, (CapabilityState)capabilityState);
526 AccountError err = (AccountError)Interop.AccountService.QueryAccountCapabilityById(capabilityCallback, accountId, IntPtr.Zero);
527 if (err != AccountError.None)
529 throw AccountErrorFactory.CreateException(err, "Failed to GetAllCapabilitiesById");
536 /// Gets the count of accounts in the account database.
538 /// <since_tizen> 3 </since_tizen>
539 /// <returns>The number of accounts in the database.</returns>
540 /// <privilege>http://tizen.org/privilege/account.read</privilege>
541 /// <feature>http://tizen.org/feature/account</feature>
542 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
543 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
544 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
545 public static int GetAccountsCount()
548 AccountError err = (AccountError)Interop.AccountService.GetAccountCount(out count);
549 if (err != AccountError.None)
551 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountCount");
558 /// Updates the sync status of the given account.
560 /// <since_tizen> 3 </since_tizen>
561 /// <param name="account"> Account for which the sync status needs to be updated.</param>
562 /// <param name="status">Sync State</param>
563 /// <privilege>http://tizen.org/privilege/account.read</privilege>
564 /// <privilege>http://tizen.org/privilege/account.write</privilege>
565 /// <feature>http://tizen.org/feature/account</feature>
566 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
567 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
568 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
569 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
570 public static void UpdateSyncStatusById(Account account, AccountSyncState status)
572 AccountError err = (AccountError)Interop.AccountService.UpdateAccountSyncStatusById(account.AccountId, (int)status);
573 if (err != AccountError.None)
575 throw AccountErrorFactory.CreateException(err, "Failed to UpdateSyncStatusById");
579 private static readonly Interop.AccountService.SubscribeCallback s_accountUpdatedCallback = (string eventType, int accountId, IntPtr userData) =>
581 AccountSubscriberEventArgs eventArgs = new AccountSubscriberEventArgs(eventType, accountId);
582 s_accountUpdated?.Invoke(null, eventArgs);
586 private static Interop.AccountService.SafeAccountSubscriberHandle s_subscriberHandle;
588 private static event EventHandler<AccountSubscriberEventArgs> s_accountUpdated;
590 /// ContentUpdated event is triggered when the media item info from DB changes.
592 /// <since_tizen> 3 </since_tizen>
594 /// ContentUpdate event is triggered if the MediaInformation updated/deleted or new information is inserted.
596 /// <param name="sender">An object that contains information about sender</param>
597 /// <param name="e">A ContentUpdatedEventArgs object that contains information about the update operation.</param>
598 /// <privilege>http://tizen.org/privilege/account.read</privilege>
599 /// <feature>http://tizen.org/feature/account</feature>
600 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
601 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
602 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
603 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
604 public static event EventHandler<AccountSubscriberEventArgs> AccountUpdated
608 if (s_accountUpdated == null)
610 if (s_subscriberHandle == null)
612 Interop.AccountService.CreateAccountSubscriber(out s_subscriberHandle);
615 AccountError ret = (AccountError)Interop.AccountService.RegisterSubscriber(s_subscriberHandle, s_accountUpdatedCallback, IntPtr.Zero);
617 if (ret != AccountError.None)
619 throw AccountErrorFactory.CreateException(ret, "Error in callback handling");
623 s_accountUpdated += value;
628 s_accountUpdated -= value;
629 if (s_accountUpdated == null)
631 AccountError ret = (AccountError)Interop.AccountService.UnregisterSubscriber(s_subscriberHandle);
632 if (ret != AccountError.None)
634 throw AccountErrorFactory.CreateException(ret, "Error in callback handling");
636 s_subscriberHandle = null;