/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
namespace Tizen.Account.AccountManager
{
///
/// The AccountManager APIs are separated into two major sections:
/// 1. Registering an account provider while an application is installed. This information will be used for the Add account screen.
/// 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.
///
/// The APIs of both of the sections consist of the following functionality:
///
/// - Create an account or account provider.
/// - Update an account or account provider (Only available for the creator).
/// - Delete an account or account provider (Only available for the creator).
/// - Read an account or account provider with some filter.
///
///
/// 3
public static class AccountService
{
///
/// This is the contact capability string.
///
/// 3
public static readonly string ContactCapability = "http://tizen.org/account/capability/contact";
///
/// This is the calendar capability string.
///
/// 3
public static readonly string CalendarCapability = "http://tizen.org/account/capability/calendar";
///
/// This is the email capability string.
///
/// 3
public static readonly string EmailCapability = "http://tizen.org/account/capability/email";
///
/// This is the photo capability string.
///
/// 3
public static readonly string PhotoCapability = "http://tizen.org/account/capability/photo";
///
/// This is the video capability string.
///
/// 3
public static readonly string VideoCapability = "http://tizen.org/account/capability/video";
///
/// This is the music capability string.
///
/// 3
public static readonly string MusicCapability = "http://tizen.org/account/capability/music";
///
/// This is the document capability string.
///
/// 3
public static readonly string DocumentCapability = "http://tizen.org/account/capability/document";
///
/// This is the message capability string.
///
/// 3
public static readonly string MessageCapability = "http://tizen.org/account/capability/message";
///
/// This is the game capability string.
///
/// 3
public static readonly string GameCapability = "http://tizen.org/account/capability/game";
///
/// Retrieves all the accounts details from the account database.
///
/// 3
/// List of accounts.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/feature/account
/// In case of any DB error.
/// In case of privilege not defined.
/// The required feature is not supported.
public static IEnumerable GetAccountsAsync()
{
List accounts = new List();
List values = new List();
Interop.Account.AccountCallback accountCallback = (IntPtr data, IntPtr userdata) =>
{
Account account = new Account(new SafeAccountHandle(data, true));
values.Add(account.AccountId);
account.Dispose();
return true;
};
AccountError res = (AccountError)Interop.AccountService.AccountForeachAccountFromDb(accountCallback, IntPtr.Zero);
if (res != AccountError.None)
{
throw AccountErrorFactory.CreateException(res, "Failed to AccountForeachAccountFromDb");
}
foreach (int i in values)
{
Account account = AccountService.GetAccountById(i);
accounts.Add(account);
}
return accounts;
}
///
/// Retrieves the account with the account ID.
///
/// 3
/// The account ID to be searched.
/// Account instance with reference to the given ID.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/feature/account
/// In case of any DB error or record not found for given account ID.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// The required feature is not supported.
public static Account GetAccountById(int accountId)
{
Account account = Account.CreateAccount();
SafeAccountHandle handle = account.SafeAccountHandle;
AccountError res = (AccountError)Interop.AccountService.QueryAccountById(accountId, ref handle);
if (res != AccountError.None)
{
throw AccountErrorFactory.CreateException(res, "Failed to get accounts from the database for account id: " + accountId);
}
Account ref_account = new Account(handle);
return ref_account;
}
///
/// Retrieves all the AccountProviders details from the account database.
///
/// 3
/// List of AccountProviders.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/feature/account
/// In case of any DB error.
/// In case of privilege not defined.
/// The required feature is not supported.
public static IEnumerable GetAccountProviders()
{
List values = new List();
List providers = new List();
Interop.AccountProvider.AccountProviderCallback accountCallback = (IntPtr handle, IntPtr data) =>
{
AccountProvider provider = new AccountProvider(handle);
values.Add(provider.AppId);
provider.Dispose();
return true;
};
AccountError res = (AccountError)Interop.AccountService.GetAllAccountproviders(accountCallback, IntPtr.Zero);
if (res != AccountError.None)
{
Log.Warn(AccountErrorFactory.LogTag, "Failed to get account providers from the database");
throw AccountErrorFactory.CreateException(res, "Failed to get account providers from the database");
}
foreach (string val in values)
{
AccountProvider provider = GetAccountProviderByAppId(val);
providers.Add(provider);
}
return providers;
}
///
/// Retrieves the account provider information with the application ID.
///
/// 3
/// The application ID.
/// The AccountProvider instance associated with the given application ID.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/feature/account
/// In case of any DB error or record not found for given appId.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// The required feature is not supported.
public static AccountProvider GetAccountProviderByAppId(string appId)
{
IntPtr handle;
Interop.AccountProvider.Create(out handle);
AccountError err = (AccountError)Interop.AccountService.GetAccountProviderByAppId(appId, out handle);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderByAppId");
}
AccountProvider provider = new AccountProvider(handle);
return provider;
}
///
/// Retrieves all the account providers information with feature.
///
/// 3
/// The capability value to search for account providers.
/// Retrieves the AccountProviders information with the capability name.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/feature/account
/// In case of any DB error or record not found for given feature.
/// In case of invalid parameter.
/// In case of privilege not defined.
/// The required feature is not supported.
public static IEnumerable GetAccountProvidersByFeature(string feature)
{
List values = new List();
List providers = new List();
Interop.AccountProvider.AccountProviderCallback providerCallback = (IntPtr handle, IntPtr data) =>
{
AccountProvider provider = new AccountProvider(handle);
values.Add(provider.AppId);
provider.Dispose();
return true;
};
AccountError err = (AccountError)Interop.AccountService.GetAccountProviderByFeature(providerCallback, feature, IntPtr.Zero);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderByFeature");
}
foreach (string val in values)
{
AccountProvider provider = GetAccountProviderByAppId(val);
providers.Add(provider);
}
return providers;
}
///
/// Inserts into the Database with the new account Information.
///
/// 3
/// New Account instance to be added.
/// The account ID of the account instance.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/privilege/account.write
/// http://tizen.org/feature/account
/// In case of any DB error.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// In case of OutOfMemory error.
/// The required feature is not supported.
public static int AddAccount(Account account)
{
if (account == null)
{
throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to AddAccount");
}
int id = -1;
AccountError err = (AccountError)Interop.AccountService.AddAccount(account.SafeAccountHandle, out id);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to AddAccount");
}
return id;
}
///
/// Updates the account details to the account database.
///
/// 3
/// Account instance to be updated.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/privilege/account.write
/// http://tizen.org/feature/account
/// In case of any DB error.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// In case of OutOfMemory error.
/// The required feature is not supported.
public static void UpdateAccount(Account account)
{
if (account == null)
{
throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to UpdateAccount");
}
AccountError err = (AccountError)Interop.AccountService.UpdateAccountToDBById(account.SafeAccountHandle, account.AccountId);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to UpdateAccount");
}
}
///
/// Deletes the account information from the database.
///
/// 3
/// Account instance to be deleted from the database.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/privilege/account.write
/// http://tizen.org/feature/account
/// In case of any DB error.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// The required feature is not supported.
public static void DeleteAccount(Account account)
{
if (account == null)
{
throw AccountErrorFactory.CreateException(AccountError.InvalidParameter, "Failed to DeleteAccount");
}
AccountError err = (AccountError)Interop.AccountService.DeleteAccountById(account.AccountId);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to delete the account by Id: " + account.AccountId);
}
}
///
/// Deletes the account from the account database by user name.
///
/// 3
/// The user name of the account to delete.
/// The package name of the account to delete.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/privilege/account.write
/// http://tizen.org/feature/account
/// In case of any DB error.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// The required feature is not supported.
public static void DeleteAccount(string userName, string packageName)
{
AccountError err = (AccountError)Interop.AccountService.DeleteAccountByUser(userName, packageName);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to delete the account by userName: " + userName);
}
}
///
/// Deletes the account from the account database by package name.
///
/// 3
/// The package name of the account to delete.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/privilege/account.write
/// http://tizen.org/feature/account
/// In case of any DB error.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// The required feature is not supported.
public static void DeleteAccount(string packageName)
{
AccountError err = (AccountError)Interop.AccountService.DeleteAccountByPackage(packageName);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to delete the account by package name: " + packageName);
}
}
///
/// Retrieves all the accounts with the given user name.
///
/// 3
/// The user name to search.
/// Accounts list matched with the user name.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/feature/account
/// In case of any DB error or record not found for given username.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// The required feature is not supported.
public static IEnumerable GetAccountsByUserName(string userName)
{
List accounts = new List();
List values = new List();
Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
{
Account account = new Account(new SafeAccountHandle(handle, true));
values.Add(account.AccountId);
account.Dispose();
return true;
};
AccountError err = (AccountError)Interop.AccountService.QueryAccountByUserName(accountCallback, userName, IntPtr.Zero);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to GetAccountByUserName");
}
foreach (int i in values)
{
Account account = AccountService.GetAccountById(i);
accounts.Add(account);
}
return accounts;
}
///
/// Retrieves all the accounts with the given package name.
///
/// 3
/// The package name to search.
/// Accounts list matched with the package name.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/feature/account
/// In case of any DB error or record not found for given package name.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// The required feature is not supported.
public static IEnumerable GetAccountsByPackageName(string packageName)
{
List accounts = new List();
List values = new List();
Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
{
Account account = new Account(new SafeAccountHandle(handle, true));
values.Add(account.AccountId);
account.Dispose();
return true;
};
AccountError err = (AccountError)Interop.AccountService.QueryAccountByPackageName(accountCallback, packageName, IntPtr.Zero);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to GetAccountByPackageName");
}
foreach (int i in values)
{
Account account = AccountService.GetAccountById(i);
accounts.Add(account);
}
return accounts;
}
///
/// Retrieves all accounts with the given capability type.
///
/// 3
/// Capability type.
/// Accounts list matched with the capability type.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/feature/account
/// In case of any DB error or record not found for the given capability type.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// The required feature is not supported.
public static IEnumerable GetAccountsByCapabilityType(string type)
{
List accounts = new List();
List values = new List();
Interop.Account.AccountCallback accountCallback = (IntPtr handle, IntPtr data) =>
{
Account account = new Account(new SafeAccountHandle(handle, true));
values.Add(account.AccountId);
account.Dispose();
return true;
};
AccountError err = (AccountError)Interop.AccountService.GetAccountByCapabilityType(accountCallback, type, IntPtr.Zero);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to GetAccountByCapabilityType");
}
foreach (int i in values)
{
Account account = AccountService.GetAccountById(i);
accounts.Add(account);
}
return accounts;
}
///
/// Retrieves all the capabilities with the given account.
///
/// 3
/// Account instance.
/// Capabilities list as dictionary of the capability type and state.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/feature/account
/// In case of any DB error or record not found for given account ID.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// The required feature is not supported.
public static Dictionary GetCapabilitiesById(int accountId)
{
Dictionary capabilities = new Dictionary();
Interop.Account.AccountCapabilityCallback capabilityCallback = (string type, int capabilityState, IntPtr data) =>
{
capabilities.Add(type, (CapabilityState)capabilityState);
return true;
};
AccountError err = (AccountError)Interop.AccountService.QueryAccountCapabilityById(capabilityCallback, accountId, IntPtr.Zero);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to GetAllCapabilitiesById");
}
return capabilities;
}
///
/// Gets the count of accounts in the account database.
///
/// 3
/// The number of accounts in the database.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/feature/account
/// In case of any DB error.
/// In case of privilege not defined.
/// The required feature is not supported.
public static int GetAccountsCount()
{
int count = 0;
AccountError err = (AccountError)Interop.AccountService.GetAccountCount(out count);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to GetAccountCount");
}
return count;
}
///
/// Updates the sync status of the given account.
///
/// 3
/// Account for which the sync status needs to be updated.
/// Sync State
/// http://tizen.org/privilege/account.read
/// http://tizen.org/privilege/account.write
/// http://tizen.org/feature/account
/// In case of any DB error.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// The required feature is not supported.
public static void UpdateSyncStatusById(Account account, AccountSyncState status)
{
AccountError err = (AccountError)Interop.AccountService.UpdateAccountSyncStatusById(account.AccountId, (int)status);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to UpdateSyncStatusById");
}
}
private static readonly Interop.AccountService.SubscribeCallback s_accountUpdatedCallback = (string eventType, int accountId, IntPtr userData) =>
{
AccountSubscriberEventArgs eventArgs = new AccountSubscriberEventArgs(eventType, accountId);
s_accountUpdated?.Invoke(null, eventArgs);
return true;
};
private static Interop.AccountService.SafeAccountSubscriberHandle s_subscriberHandle;
private static event EventHandler s_accountUpdated;
///
/// ContentUpdated event is triggered when the media item info from DB changes.
///
/// 3
///
/// ContentUpdate event is triggered if the MediaInformation updated/deleted or new information is inserted.
///
/// http://tizen.org/privilege/account.read
/// http://tizen.org/feature/account
/// In case of any DB error.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// The required feature is not supported.
public static event EventHandler AccountUpdated
{
add
{
if (s_accountUpdated == null)
{
if (s_subscriberHandle == null)
{
Interop.AccountService.CreateAccountSubscriber(out s_subscriberHandle);
}
AccountError ret = (AccountError)Interop.AccountService.RegisterSubscriber(s_subscriberHandle, s_accountUpdatedCallback, IntPtr.Zero);
if (ret != AccountError.None)
{
throw AccountErrorFactory.CreateException(ret, "Error in callback handling");
}
}
s_accountUpdated += value;
}
remove
{
s_accountUpdated -= value;
if (s_accountUpdated == null)
{
AccountError ret = (AccountError)Interop.AccountService.UnregisterSubscriber(s_subscriberHandle);
if (ret != AccountError.None)
{
throw AccountErrorFactory.CreateException(ret, "Error in callback handling");
}
s_subscriberHandle = null;
}
}
}
}
}