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>
36 public static class AccountService
39 /// This is contact capability string.
41 public static readonly string ContactCapability = "http://tizen.org/account/capability/contact";
44 /// This is calendar capability string.
46 public static readonly string CalendarCapability = "http://tizen.org/account/capability/calendar";
49 /// This is email capability string.
51 public static readonly string EmailCapability = "http://tizen.org/account/capability/email";
54 /// This is photo capability string.
56 public static readonly string PhotoCapability = "http://tizen.org/account/capability/photo";
59 /// This is video capability string.
61 public static readonly string VideoCapability = "http://tizen.org/account/capability/video";
64 /// This is music capability string.
66 public static readonly string MusicCapability = "http://tizen.org/account/capability/music";
69 /// This is document capability string.
71 public static readonly string DocumentCapability = "http://tizen.org/account/capability/document";
74 /// This is message capability string.
76 public static readonly string MessageCapability = "http://tizen.org/account/capability/message";
79 /// This is game capability string.
81 public static readonly string GameCapability = "http://tizen.org/account/capability/game";
84 /// Retrieves all accounts details from the account database.
86 /// <returns>List of Accounts</returns>
87 /// <privilege>http://tizen.org/privilege/account.read</privilege>
88 /// <exception cref="InvalidOperationException">In case of any DB error. </exception>
89 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
90 public static IEnumerable<Account> GetAccountsAsync()
92 List<Account> accounts = new List<Account>();
93 List<int> values = new List<int>();
94 Interop.Account.AccountCallback accountCallback = (IntPtr data, IntPtr userdata) =>
96 Account account = new Account(data);
97 values.Add(account.AccountId);
102 AccountError res = (AccountError)Interop.AccountService.AccountForeachAccountFromDb(accountCallback, IntPtr.Zero);
103 if (res != AccountError.None)
105 throw AccountErrorFactory.CreateException(res, "Failed to AccountForeachAccountFromDb");
108 foreach (int i in values)
110 Account account = AccountService.GetAccountById(i);
111 accounts.Add(account);
118 /// Retrieve an account with the account ID.
120 /// <param name="accountId"> The account Id to be searched.</param>
121 /// <returns>Account instance with reference to the given id.</returns>
122 /// <privilege>http://tizen.org/privilege/account.read</privilege>
123 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given account id</exception>
124 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
125 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
126 public static Account GetAccountById(int accountId)
128 Account account = Account.CreateAccount();
129 IntPtr handle = account.Handle;
130 AccountError res = (AccountError)Interop.AccountService.QueryAccountById(accountId, out handle);
131 if (res != AccountError.None)
133 throw AccountErrorFactory.CreateException(res, "Failed to get accounts from the database for account id: " + accountId);
136 account.Handle = handle;
141 /// Retrieves all AccountProviders details from the account database.
143 /// <returns>List of AccountProviders</returns>
144 /// <privilege>http://tizen.org/privilege/account.read</privilege>
145 /// <exception cref="InvalidOperationException">In case of any DB error</exception>
146 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
147 public static IEnumerable<AccountProvider> GetAccountProviders()
149 List<string> values = new List<string>();
150 List<AccountProvider> providers = new List<AccountProvider>();
151 Interop.AccountProvider.AccountProviderCallback accountCallback = (IntPtr handle, IntPtr data) =>
153 AccountProvider provider = new AccountProvider(handle);
154 values.Add(provider.AppId);
159 AccountError res = (AccountError)Interop.AccountService.GetAllAccountproviders(accountCallback, IntPtr.Zero);
160 if (res != AccountError.None)
162 Log.Warn(AccountErrorFactory.LogTag, "Failed to get account providers from the database");
163 throw AccountErrorFactory.CreateException(res, "Failed to get account providers from the database");
166 foreach (string val in values)
168 AccountProvider provider = GetAccountProviderByAppId(val);
169 providers.Add(provider);
176 /// Retrieves the account provider information with application Id.
178 /// <param name="appId">Application Id.</param>
179 /// <returns>The AccountProvider instance associated with the given application Id.</returns>
180 /// <privilege>http://tizen.org/privilege/account.read</privilege>
181 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given appid</exception>
182 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
183 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
184 public static AccountProvider GetAccountProviderByAppId(string appId)
187 Interop.AccountProvider.Create(out handle);
188 AccountError err = (AccountError)Interop.AccountService.GetAccountProviderByAppId(appId, out handle);
189 if (err != AccountError.None)
191 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderByAppId");
194 AccountProvider provider = new AccountProvider(handle);
199 /// Retrieves all the account providers information with feature.
201 /// <param name="feature">The capability value to search for account providers.</param>
202 /// <returns>Retrieves AccountProviders information with the capability name.</returns>
203 /// <privilege>http://tizen.org/privilege/account.read</privilege>
204 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given feature</exception>
205 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
206 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
207 public static IEnumerable<AccountProvider> GetAccountProvidersByFeature(string feature)
209 List<string> values = new List<string>();
210 List<AccountProvider> providers = new List<AccountProvider>();
211 Interop.AccountProvider.AccountProviderCallback providerCallback = (IntPtr handle, IntPtr data) =>
213 AccountProvider provider = new AccountProvider(handle);
214 values.Add(provider.AppId);
219 AccountError err = (AccountError)Interop.AccountService.GetAccountProviderByFeature(providerCallback, feature, IntPtr.Zero);
220 if (err != AccountError.None)
222 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderByFeature");
225 foreach (string val in values)
227 AccountProvider provider = GetAccountProviderByAppId(val);
228 providers.Add(provider);
235 /// Inserts into the Database with the new account Infomration.
237 /// <param name="account">New Account instance to be added.</param>
238 /// <privilege>http://tizen.org/privilege/account.read</privilege>
239 /// <privilege>http://tizen.org/privilege/account.write </privilege>
240 /// <exception cref="InvalidOperationException">In case of any DB error</exception>
241 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
242 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
243 /// <exception cref="OutOfMemoryException"> In case of OutOfMemory error.</exception>
244 public static int AddAccount(Account account)
248 throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to AddAccount");
252 AccountError err = (AccountError)Interop.AccountService.AddAccount(account.Handle, out id);
253 if (err != AccountError.None)
255 throw AccountErrorFactory.CreateException(err, "Failed to AddAccount");
262 /// Updates the account details to the account database.
264 /// <param name="account">account instance to be updated.</param>
265 /// <privilege>http://tizen.org/privilege/account.read</privilege>
266 /// <privilege>http://tizen.org/privilege/account.write </privilege>
267 /// <exception cref="InvalidOperationException">In case of any DB error </exception>
268 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
269 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
270 /// <exception cref="OutOfMemoryException"> In case of OutOfMemory error.</exception>
271 public static void UpdateAccount(Account account)
275 throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to UpdateAccount");
278 AccountError err = (AccountError)Interop.AccountService.UpdateAccountToDBById(account.Handle, account.AccountId);
279 if (err != AccountError.None)
281 throw AccountErrorFactory.CreateException(err, "Failed to UpdateAccount");
286 /// Deletes the account information from the Database.
288 /// <param name="account">Account instance to be deleted from the database.</param>
289 /// <privilege>http://tizen.org/privilege/account.read</privilege>
290 /// <privilege>http://tizen.org/privilege/account.write </privilege>
291 /// <exception cref="InvalidOperationException">In case of any DB error </exception>
292 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
293 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
294 public static void DeleteAccount(Account account)
298 throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to DeleteAccount");
301 AccountError err = (AccountError)Interop.AccountService.DeleteAccountById(account.AccountId);
302 if (err != AccountError.None)
304 throw AccountErrorFactory.CreateException(err, "Failed to delete the account by Id: " + account.AccountId);
309 /// Deletes an account from the account database by user name.
311 /// <param name="userName">The user name of the account to delete.</param>
312 /// <param name="packageName">The package name of the account to delete.</param>
313 /// <privilege>http://tizen.org/privilege/account.read</privilege>
314 /// <privilege>http://tizen.org/privilege/account.write </privilege>
315 /// <exception cref="InvalidOperationException">In case of any DB error</exception>
316 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
317 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
318 public static void DeleteAccount(string userName, string packageName)
320 AccountError err = (AccountError)Interop.AccountService.DeleteAccountByUser(userName, packageName);
321 if (err != AccountError.None)
323 throw AccountErrorFactory.CreateException(err, "Failed to delete the account by userName: " + userName);
328 /// Deletes an account from the account database by package name.
330 /// <param name="packageName">The package name of the account to delete.</param>
331 /// <privilege>http://tizen.org/privilege/account.read</privilege>
332 /// <privilege>http://tizen.org/privilege/account.write </privilege>
333 /// <exception cref="InvalidOperationException">In case of any DB error</exception>
334 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
335 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
336 public static void DeleteAccount(string packageName)
338 AccountError err = (AccountError)Interop.AccountService.DeleteAccountByPackage(packageName);
339 if (err != AccountError.None)
341 throw AccountErrorFactory.CreateException(err, "Failed to delete the account by package name: " + packageName);
347 /// Retrieves all accounts with the given user name.
349 /// <param name="userName">The user name to search .</param>
350 /// <returns>Accounts list matched with the user name</returns>
351 /// <privilege>http://tizen.org/privilege/account.read</privilege>
352 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given username</exception>
353 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
354 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
355 public static IEnumerable<Account> GetAccountsByUserName(string userName)
357 List<Account> accounts = new List<Account>();
358 List<int> values = new List<int>();
359 Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
361 Account account = new Account(handle);
362 values.Add(account.AccountId);
367 AccountError err = (AccountError)Interop.AccountService.QueryAccountByUserName(accountCallback, userName, IntPtr.Zero);
368 if (err != AccountError.None)
370 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountByUserName");
373 foreach (int i in values)
375 Account account = AccountService.GetAccountById(i);
376 accounts.Add(account);
383 /// Retrieves all accounts with the given package name.
385 /// <param name="packageName"> The package name to Search</param>
386 /// <returns>Accounts list matched with the package name</returns>
387 /// <privilege>http://tizen.org/privilege/account.read</privilege>
388 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given package name</exception>
389 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
390 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
391 public static IEnumerable<Account> GetAccountsByPackageName(string packageName)
393 List<Account> accounts = new List<Account>();
394 List<int> values = new List<int>();
395 Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
397 Account account = new Account(handle);
398 values.Add(account.AccountId);
403 AccountError err = (AccountError)Interop.AccountService.QueryAccountByPackageName(accountCallback, packageName, IntPtr.Zero);
404 if (err != AccountError.None)
406 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountByPackageName");
409 foreach (int i in values)
411 Account account = AccountService.GetAccountById(i);
412 accounts.Add(account);
419 /// Retrieves all accounts with the given cpability type.
421 /// <param name="type"> Capability type</param>
422 /// <returns>Accounts list matched with the capability type</returns>
423 /// <privilege>http://tizen.org/privilege/account.read</privilege>
424 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given capability type</exception>
425 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
426 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
427 public static IEnumerable<Account> GetAccountsByCapabilityType(string type)
429 List<Account> accounts = new List<Account>();
430 List<int> values = new List<int>();
431 Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
433 Account account = new Account(handle);
434 values.Add(account.AccountId);
439 AccountError err = (AccountError)Interop.AccountService.GetAccountByCapabilityType(accountCallback, type, IntPtr.Zero);
440 if (err != AccountError.None)
442 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountByCapabilityType");
445 foreach (int i in values)
447 Account account = AccountService.GetAccountById(i);
448 accounts.Add(account);
455 /// Retrieves all capabilities with the given account
457 /// <param name="accountId">account instance</param>
458 /// <returns>Capabilities list as Dictionary of Capability type and State.</returns>
459 /// <privilege>http://tizen.org/privilege/account.read</privilege>
460 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given account id</exception>
461 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
462 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
463 public static Dictionary<string, CapabilityState> GetCapabilitiesById(int accountId)
465 Dictionary<string, CapabilityState> capabilities = new Dictionary<string, CapabilityState>();
466 Interop.Account.AccountCapabilityCallback capabilityCallback = (string type, int capabilityState, IntPtr data) =>
468 capabilities.Add(type, (CapabilityState)capabilityState);
472 AccountError err = (AccountError)Interop.AccountService.QueryAccountCapabilityById(capabilityCallback, accountId, IntPtr.Zero);
473 if (err != AccountError.None)
475 throw AccountErrorFactory.CreateException(err, "Failed to GetAllCapabilitiesById");
482 /// Gets the count of accounts in the account database.
484 /// <returns>The number of accounts in the database</returns>
485 /// <privilege>http://tizen.org/privilege/account.read</privilege>
486 /// <exception cref="InvalidOperationException">In case of any DB error </exception>
487 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
488 public static int GetAccountsCount()
491 AccountError err = (AccountError)Interop.AccountService.GetAccountCount(out count);
492 if (err != AccountError.None)
494 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountCount");
501 /// Updates the sync status of the given account.
503 /// <param name="account"> Account for which sync status needs to be updated</param>
504 /// <param name="status">Sync State</param>
505 /// <privilege>http://tizen.org/privilege/account.read</privilege>
506 /// <privilege>http://tizen.org/privilege/account.write</privilege>
507 /// <exception cref="InvalidOperationException">In case of any DB error </exception>
508 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
509 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
510 public static void UpdateSyncStatusById(Account account, AccountSyncState status)
512 AccountError err = (AccountError)Interop.AccountService.UpdateAccountSyncStatusById(account.AccountId, (int)status);
513 if (err != AccountError.None)
515 throw AccountErrorFactory.CreateException(err, "Failed to UpdateSyncStatusById");
519 private static readonly Interop.AccountService.SubscribeCallback s_accountUpdatedCallback = (string eventType, int accountId, IntPtr userData) =>
521 AccountSucriberEventArgs eventArgs = new AccountSucriberEventArgs(eventType, accountId);
522 s_accountUpdated?.Invoke(null, eventArgs);
526 private static Interop.AccountService.SafeAccountSubscriberHandle s_subscriberHandle;
528 private static event EventHandler<AccountSucriberEventArgs> s_accountUpdated;
530 /// ContentUpdated event is triggered when the media item info from DB changes.
533 /// ContentUpdate event is triggered if the MediaInformaion updated/deleted or new Inforamtion is Inserted.
535 /// <param name="sender"></param>
536 /// <param name="e">A ContentUpdatedEventArgs object that contains information about the update operation.</param>
537 /// <privilege>http://tizen.org/privilege/account.read</privilege>
538 /// <exception cref="InvalidOperationException">In case of any DB error </exception>
539 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
540 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
541 public static event EventHandler<AccountSucriberEventArgs> AccountUpdated
545 if (s_accountUpdated == null)
547 if (s_subscriberHandle == null)
549 Interop.AccountService.CreateAccountSubscriber(out s_subscriberHandle);
552 AccountError ret = (AccountError)Interop.AccountService.RegisterSubscriber(s_subscriberHandle, s_accountUpdatedCallback, IntPtr.Zero);
554 if (ret != AccountError.None)
556 throw AccountErrorFactory.CreateException(ret, "Error in callback handling");
560 s_accountUpdated += value;
565 s_accountUpdated -= value;
566 if (s_accountUpdated == null)
568 AccountError ret = (AccountError)Interop.AccountService.UnregisterSubscriber(s_subscriberHandle);
569 if (ret != AccountError.None)
571 throw AccountErrorFactory.CreateException(ret, "Error in callback handling");