/*
* 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;
using Tizen.Internals.Errors;
namespace Tizen.Account.AccountManager
{
///
/// The account ID.
///
/// 3
public class AccountProvider : IDisposable
{
internal IntPtr _handle;
///
/// AccountProvider destructor.
///
/// 3
/// The account handle.
internal AccountProvider(IntPtr handle)
{
Handle = handle;
}
///
/// AccountProvider deconstructor.
///
/// 3
~AccountProvider()
{
Dispose(false);
}
internal IntPtr Handle
{
get
{
return _handle;
}
set
{
_handle = value;
}
}
///
/// The account ID.
///
/// 3
public string AppId
{
get
{
string id = "";
AccountError res = (AccountError)Interop.AccountProvider.GetAppId(Handle, out id);
if (res != AccountError.None)
{
Log.Warn(AccountErrorFactory.LogTag, "Failed to get AppId for the AccountProvider");
}
return id;
}
}
///
/// ServiceProvider ID of the account provider.
///
/// 3
public string ServiceProviderId
{
get
{
string id = "";
AccountError res = (AccountError)Interop.AccountProvider.GetServiceProviderId(Handle, out id);
if (res != AccountError.None)
{
Log.Warn(AccountErrorFactory.LogTag, "Failed to get ServiceProviderId for the AccountProvider");
}
return id;
}
}
///
/// Icon path of the account provider.
///
/// 3
public string IconPath
{
get
{
string path = "";
AccountError res = (AccountError)Interop.AccountProvider.GetAccountProviderIconPath(Handle, out path);
if (res != AccountError.None)
{
Log.Warn(AccountErrorFactory.LogTag, "Failed to get IconPath for the AccountProvider");
}
return path;
}
}
///
/// Small icon path of the account provider.
///
/// 3
public string SmallIconPath
{
get
{
string path = "";
AccountError res = (AccountError)Interop.AccountProvider.GetAccountProviderSmallIconPath(Handle, out path);
if (res != AccountError.None)
{
Log.Warn(AccountErrorFactory.LogTag, "Failed to get SmallIconPath for the AccountProvider");
}
return path;
}
}
///
/// Flag for the account provider if it supports multiple accounts.
///
/// 3
public bool MultipleAccountSupport
{
get
{
int multiple = 0;
AccountError res = (AccountError)Interop.AccountProvider.GetMultipleAccountSupport(Handle, out multiple);
if (res != AccountError.None)
{
Log.Warn(AccountErrorFactory.LogTag, "Failed to get SmallIconPath for the AccountProvider");
}
return (multiple == 0) ? false : true;
}
}
///
/// Retrieves all the capability information of the account provider.
///
/// 3
/// http://tizen.org/privilege/account.read
/// http://tizen.org/feature/account
///
/// The list of capability information.
///
/// In case of any DB error.
/// In case of privilege not defined.
/// The required feature is not supported.
public IEnumerable GetAllCapabilities()
{
List capabilities = new List();
AccountError res;
Interop.AccountProvider.AccountProviderFeatureCallback callback = (string appId, string key, IntPtr data) =>
{
capabilities.Add(key);
return true;
};
res = (AccountError)Interop.AccountProvider.GetAccountProviderFeatures(Handle, callback, IntPtr.Zero);
if (res != AccountError.None)
{
throw AccountErrorFactory.CreateException(res, "Failed to GetAllCapabilities for AccountProvider");
}
return capabilities;
}
///
/// Gets the specific label information detail of the account provider.
///
/// 3
///
/// The locale is specified as an ISO 3166 alpha-2 two letter country-code followed by ISO 639-1 for the two-letter language code.
/// For example, "ko_KR" or "ko-kr" for Korean, "en_US" or "en-us" for American English.
///
/// The label text given for the locale.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/feature/account
/// In case of any DB error or record not found for given locale.
/// In case of privilege not defined.
/// The required feature is not supported.
public string GetLabel(string locale)
{
string label;
AccountError res = (AccountError)Interop.AccountProvider.GetlabelbyLocale(Handle, locale, out label);
if (res != AccountError.None)
{
throw AccountErrorFactory.CreateException(res, "Failed to GetLabel for AccountProvider");
}
return label;
}
///
/// Gets the specific label information detail of the account provider.
///
/// 3
///
/// The application ID to search.
///
/// All the labels information for 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 the application ID.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// The required feature is not supported.
public static Dictionary GetLabelsByAppId(string appId)
{
AccountErrorFactory.CheckAccountFeature();
Dictionary labels = new Dictionary();
Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
{
labels.Add(locale, label);
return true;
};
AccountError err = (AccountError)Interop.AccountProvider.GetLablesByAppId(callback, appId, IntPtr.Zero);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to GetLablesByAppId");
}
return labels;
}
///
/// Gets the label information detail of the account provider.
///
/// 3
/// All the labels information for the given account provider.
/// 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 Dictionary GetLabels()
{
Dictionary labels = new Dictionary();
Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
{
labels.Add(locale, label);
return true;
};
AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderLabels(Handle, callback, IntPtr.Zero);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderLabels");
}
return labels;
}
///
/// Checks whether the given appId exists in the account provider DB.
///
/// 3
/// The application ID to check.
/// returns true If App is supported
/// http://tizen.org/privilege/account.read
/// http://tizen.org/feature/account
/// In case of any DB error or record not found for the given application ID.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// The required feature is not supported.
public bool IsAppSupported(string appId)
{
bool isSupported = false;
AccountError res = (AccountError)Interop.AccountProvider.GetAppIdExists(appId);
if (res != AccountError.None)
{
throw AccountErrorFactory.CreateException(res, "Failed to GetLabel for AccountProvider");
}
else
{
isSupported = true;
}
return isSupported;
}
///
/// Checks whether the given application ID supports the capability.
///
/// 3
/// The application ID.
/// The capability information.
///
/// TRUE if the application supports the given capability,
/// otherwise FALSE if the application does not support the given capability
///
/// 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 bool IsFeatureSupportedByApp(string appId, string capability)
{
AccountErrorFactory.CheckAccountFeature();
bool supported = Interop.AccountProvider.IsFeatureSupported(appId, capability);
if (!supported)
{
//Get last result and validate error code.
AccountError err = (AccountError)ErrorFacts.GetLastResult();
if ((err != AccountError.None) && (err != AccountError.RecordNotFound))
{
throw AccountErrorFactory.CreateException(err, "Failed to get IsFeatureSupported");
}
}
return supported;
}
///
/// Retrieves capability information with the application ID.
///
/// 3
/// The application ID.
/// Capability information list for the given appId.
/// http://tizen.org/privilege/account.read
/// http://tizen.org/feature/account
/// In case of any DB error or record not found for the given application ID.
/// In case of an invalid parameter.
/// In case of privilege not defined.
/// The required feature is not supported.
public static IEnumerable GetFeaturesByAppId(string appId)
{
AccountErrorFactory.CheckAccountFeature();
List features = new List();
Interop.AccountProvider.AccountProviderFeatureCallback callback = (string applicationId, string key, IntPtr userData) =>
{
features.Add(key);
return true;
};
AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderFeaturesByAppId(callback, appId, IntPtr.Zero);
if (err != AccountError.None)
{
throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderFeaturesByAppId");
}
return (IEnumerable)features;
}
///
/// Overloaded Dispose API for destroying the AccountProvider Handle.
///
/// 3
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Dispose API for destroying the AccountProvider handle.
///
/// 3
/// The boolean value for destoying AccountProvider handle.
protected virtual void Dispose(bool disposing)
{
if (!disposing)
{
if (_handle != IntPtr.Zero)
{
Interop.AccountProvider.Destroy(_handle);
_handle = IntPtr.Zero;
}
}
}
}
}