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 /// <privilege>http://tizen.org/privilege/account.read</privilege>
266 /// <privilege>http://tizen.org/privilege/account.write </privilege>
267 /// <feature>http://tizen.org/feature/account</feature>
268 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
269 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
270 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
271 /// <exception cref="OutOfMemoryException"> In case of OutOfMemory error.</exception>
272 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
273 public static int AddAccount(Account account)
277 throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to AddAccount");
281 AccountError err = (AccountError)Interop.AccountService.AddAccount(account.SafeAccountHandle, out id);
282 if (err != AccountError.None)
284 throw AccountErrorFactory.CreateException(err, "Failed to AddAccount");
291 /// Updates the account details to the account database.
293 /// <since_tizen> 3 </since_tizen>
294 /// <param name="account">Account instance to be updated.</param>
295 /// <privilege>http://tizen.org/privilege/account.read</privilege>
296 /// <privilege>http://tizen.org/privilege/account.write </privilege>
297 /// <feature>http://tizen.org/feature/account</feature>
298 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
299 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
300 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
301 /// <exception cref="OutOfMemoryException"> In case of OutOfMemory error.</exception>
302 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
303 public static void UpdateAccount(Account account)
307 throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to UpdateAccount");
310 AccountError err = (AccountError)Interop.AccountService.UpdateAccountToDBById(account.SafeAccountHandle, account.AccountId);
311 if (err != AccountError.None)
313 throw AccountErrorFactory.CreateException(err, "Failed to UpdateAccount");
318 /// Deletes the account information from the database.
320 /// <since_tizen> 3 </since_tizen>
321 /// <param name="account">Account instance to be deleted from the database.</param>
322 /// <privilege>http://tizen.org/privilege/account.read</privilege>
323 /// <privilege>http://tizen.org/privilege/account.write</privilege>
324 /// <feature>http://tizen.org/feature/account</feature>
325 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
326 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
327 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
328 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
329 public static void DeleteAccount(Account account)
333 throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to DeleteAccount");
336 AccountError err = (AccountError)Interop.AccountService.DeleteAccountById(account.AccountId);
337 if (err != AccountError.None)
339 throw AccountErrorFactory.CreateException(err, "Failed to delete the account by Id: " + account.AccountId);
344 /// Deletes the account from the account database by user name.
346 /// <since_tizen> 3 </since_tizen>
347 /// <param name="userName">The user name of the account to delete.</param>
348 /// <param name="packageName">The package name of the account to delete.</param>
349 /// <privilege>http://tizen.org/privilege/account.read</privilege>
350 /// <privilege>http://tizen.org/privilege/account.write</privilege>
351 /// <feature>http://tizen.org/feature/account</feature>
352 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
353 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
354 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
355 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
356 public static void DeleteAccount(string userName, string packageName)
358 AccountError err = (AccountError)Interop.AccountService.DeleteAccountByUser(userName, packageName);
359 if (err != AccountError.None)
361 throw AccountErrorFactory.CreateException(err, "Failed to delete the account by userName: " + userName);
366 /// Deletes the account from the account database by package name.
368 /// <since_tizen> 3 </since_tizen>
369 /// <param name="packageName">The package name of the account to delete.</param>
370 /// <privilege>http://tizen.org/privilege/account.read</privilege>
371 /// <privilege>http://tizen.org/privilege/account.write</privilege>
372 /// <feature>http://tizen.org/feature/account</feature>
373 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
374 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
375 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
376 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
377 public static void DeleteAccount(string packageName)
379 AccountError err = (AccountError)Interop.AccountService.DeleteAccountByPackage(packageName);
380 if (err != AccountError.None)
382 throw AccountErrorFactory.CreateException(err, "Failed to delete the account by package name: " + packageName);
388 /// Retrieves all the accounts with the given user name.
390 /// <since_tizen> 3 </since_tizen>
391 /// <param name="userName">The user name to search.</param>
392 /// <returns>Accounts list matched with the user name.</returns>
393 /// <privilege>http://tizen.org/privilege/account.read</privilege>
394 /// <feature>http://tizen.org/feature/account</feature>
395 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given username.</exception>
396 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
397 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
398 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
399 public static IEnumerable<Account> GetAccountsByUserName(string userName)
401 List<Account> accounts = new List<Account>();
402 List<int> values = new List<int>();
403 Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
405 Account account = new Account(new SafeAccountHandle(handle, true));
406 values.Add(account.AccountId);
411 AccountError err = (AccountError)Interop.AccountService.QueryAccountByUserName(accountCallback, userName, IntPtr.Zero);
412 if (err != AccountError.None)
414 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountByUserName");
417 foreach (int i in values)
419 Account account = AccountService.GetAccountById(i);
420 accounts.Add(account);
427 /// Retrieves all the accounts with the given package name.
429 /// <since_tizen> 3 </since_tizen>
430 /// <param name="packageName"> The package name to search.</param>
431 /// <returns>Accounts list matched with the package name.</returns>
432 /// <privilege>http://tizen.org/privilege/account.read</privilege>
433 /// <feature>http://tizen.org/feature/account</feature>
434 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given package name.</exception>
435 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
436 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
437 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
438 public static IEnumerable<Account> GetAccountsByPackageName(string packageName)
440 List<Account> accounts = new List<Account>();
441 List<int> values = new List<int>();
442 Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
444 Account account = new Account(new SafeAccountHandle(handle, true));
445 values.Add(account.AccountId);
450 AccountError err = (AccountError)Interop.AccountService.QueryAccountByPackageName(accountCallback, packageName, IntPtr.Zero);
451 if (err != AccountError.None)
453 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountByPackageName");
456 foreach (int i in values)
458 Account account = AccountService.GetAccountById(i);
459 accounts.Add(account);
466 /// Retrieves all accounts with the given capability type.
468 /// <since_tizen> 3 </since_tizen>
469 /// <param name="type"> Capability type.</param>
470 /// <returns>Accounts list matched with the capability type.</returns>
471 /// <privilege>http://tizen.org/privilege/account.read</privilege>
472 /// <feature>http://tizen.org/feature/account</feature>
473 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for the given capability type.</exception>
474 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
475 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
476 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
477 public static IEnumerable<Account> GetAccountsByCapabilityType(string type)
479 List<Account> accounts = new List<Account>();
480 List<int> values = new List<int>();
481 Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
483 Account account = new Account(new SafeAccountHandle(handle, true));
484 values.Add(account.AccountId);
489 AccountError err = (AccountError)Interop.AccountService.GetAccountByCapabilityType(accountCallback, type, IntPtr.Zero);
490 if (err != AccountError.None)
492 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountByCapabilityType");
495 foreach (int i in values)
497 Account account = AccountService.GetAccountById(i);
498 accounts.Add(account);
505 /// Retrieves all the capabilities with the given account.
507 /// <since_tizen> 3 </since_tizen>
508 /// <param name="accountId">Account instance.</param>
509 /// <returns>Capabilities list as dictionary of the capability type and state.</returns>
510 /// <privilege>http://tizen.org/privilege/account.read</privilege>
511 /// <feature>http://tizen.org/feature/account</feature>
512 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given account ID.</exception>
513 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
514 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
515 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
516 public static Dictionary<string, CapabilityState> GetCapabilitiesById(int accountId)
518 Dictionary<string, CapabilityState> capabilities = new Dictionary<string, CapabilityState>();
519 Interop.Account.AccountCapabilityCallback capabilityCallback = (string type, int capabilityState, IntPtr data) =>
521 capabilities.Add(type, (CapabilityState)capabilityState);
525 AccountError err = (AccountError)Interop.AccountService.QueryAccountCapabilityById(capabilityCallback, accountId, IntPtr.Zero);
526 if (err != AccountError.None)
528 throw AccountErrorFactory.CreateException(err, "Failed to GetAllCapabilitiesById");
535 /// Gets the count of accounts in the account database.
537 /// <since_tizen> 3 </since_tizen>
538 /// <returns>The number of accounts in the database.</returns>
539 /// <privilege>http://tizen.org/privilege/account.read</privilege>
540 /// <feature>http://tizen.org/feature/account</feature>
541 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
542 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
543 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
544 public static int GetAccountsCount()
547 AccountError err = (AccountError)Interop.AccountService.GetAccountCount(out count);
548 if (err != AccountError.None)
550 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountCount");
557 /// Updates the sync status of the given account.
559 /// <since_tizen> 3 </since_tizen>
560 /// <param name="account"> Account for which the sync status needs to be updated.</param>
561 /// <param name="status">Sync State</param>
562 /// <privilege>http://tizen.org/privilege/account.read</privilege>
563 /// <privilege>http://tizen.org/privilege/account.write</privilege>
564 /// <feature>http://tizen.org/feature/account</feature>
565 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
566 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
567 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
568 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
569 public static void UpdateSyncStatusById(Account account, AccountSyncState status)
571 AccountError err = (AccountError)Interop.AccountService.UpdateAccountSyncStatusById(account.AccountId, (int)status);
572 if (err != AccountError.None)
574 throw AccountErrorFactory.CreateException(err, "Failed to UpdateSyncStatusById");
578 private static readonly Interop.AccountService.SubscribeCallback s_accountUpdatedCallback = (string eventType, int accountId, IntPtr userData) =>
580 AccountSubscriberEventArgs eventArgs = new AccountSubscriberEventArgs(eventType, accountId);
581 s_accountUpdated?.Invoke(null, eventArgs);
585 private static Interop.AccountService.SafeAccountSubscriberHandle s_subscriberHandle;
587 private static event EventHandler<AccountSubscriberEventArgs> s_accountUpdated;
589 /// ContentUpdated event is triggered when the media item info from DB changes.
591 /// <since_tizen> 3 </since_tizen>
593 /// ContentUpdate event is triggered if the MediaInformaion updated/deleted or new information is inserted.
595 /// <param name="sender"></param>
596 /// <param name="e">A ContentUpdatedEventArgs object that contains information about the update operation.</param>
597 /// <privilege>http://tizen.org/privilege/account.read</privilege>
598 /// <feature>http://tizen.org/feature/account</feature>
599 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
600 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
601 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
602 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
603 public static event EventHandler<AccountSubscriberEventArgs> AccountUpdated
607 if (s_accountUpdated == null)
609 if (s_subscriberHandle == null)
611 Interop.AccountService.CreateAccountSubscriber(out s_subscriberHandle);
614 AccountError ret = (AccountError)Interop.AccountService.RegisterSubscriber(s_subscriberHandle, s_accountUpdatedCallback, IntPtr.Zero);
616 if (ret != AccountError.None)
618 throw AccountErrorFactory.CreateException(ret, "Error in callback handling");
622 s_accountUpdated += value;
627 s_accountUpdated -= value;
628 if (s_accountUpdated == null)
630 AccountError ret = (AccountError)Interop.AccountService.UnregisterSubscriber(s_subscriberHandle);
631 if (ret != AccountError.None)
633 throw AccountErrorFactory.CreateException(ret, "Error in callback handling");
635 s_subscriberHandle = null;