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;
19 using Tizen.Internals.Errors;
21 namespace Tizen.Account.AccountManager
26 /// <since_tizen> 3 </since_tizen>
27 public class AccountProvider : IDisposable
29 internal IntPtr _handle;
30 internal AccountProvider(IntPtr handle)
40 internal IntPtr Handle
55 /// <since_tizen> 3 </since_tizen>
61 AccountError res = (AccountError)Interop.AccountProvider.GetAppId(Handle, out id);
62 if (res != AccountError.None)
64 Log.Warn(AccountErrorFactory.LogTag, "Failed to get AppId for the AccountProvider");
72 /// Serviceprovider Id of the account provider.
74 /// <since_tizen> 3 </since_tizen>
75 public string ServiceProviderId
80 AccountError res = (AccountError)Interop.AccountProvider.GetServiceProviderId(Handle, out id);
81 if (res != AccountError.None)
83 Log.Warn(AccountErrorFactory.LogTag, "Failed to get ServiceProviderId for the AccountProvider");
91 /// Icon path of an account provider.
93 /// <since_tizen> 3 </since_tizen>
94 public string IconPath
99 AccountError res = (AccountError)Interop.AccountProvider.GetAccountProviderIconPath(Handle, out path);
100 if (res != AccountError.None)
102 Log.Warn(AccountErrorFactory.LogTag, "Failed to get IconPath for the AccountProvider");
110 /// Small icon path of an account provider.
112 /// <since_tizen> 3 </since_tizen>
113 public string SmallIconPath
118 AccountError res = (AccountError)Interop.AccountProvider.GetAccountProviderSmallIconPath(Handle, out path);
119 if (res != AccountError.None)
121 Log.Warn(AccountErrorFactory.LogTag, "Failed to get SmallIconPath for the AccountProvider");
129 /// Flag for account provider If supports multiple accounts.
131 /// <since_tizen> 3 </since_tizen>
132 public bool MultipleAccountSupport
137 AccountError res = (AccountError)Interop.AccountProvider.GetMultipleAccountSupport(Handle, out multiple);
138 if (res != AccountError.None)
140 Log.Warn(AccountErrorFactory.LogTag, "Failed to get SmallIconPath for the AccountProvider");
143 return (multiple == 0) ? false : true;
148 /// Retrieves all the capability information of the account provider.
150 /// <since_tizen> 3 </since_tizen>
151 /// <privilege>http://tizen.org/privilege/account.read</privilege>
153 /// list of capability information.
155 /// <exception cref="InvalidOperationException">In case of any DB error</exception>
156 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
157 public IEnumerable<string> GetAllCapabilities()
159 List<string> capabilities = new List<string>();
161 Interop.AccountProvider.AccountProviderFeatureCallback callback = (string appId, string key, IntPtr data) =>
163 capabilities.Add(key);
167 res = (AccountError)Interop.AccountProvider.GetAccountProviderFeatures(Handle, callback, IntPtr.Zero);
168 if (res != AccountError.None)
170 throw AccountErrorFactory.CreateException(res, "Failed to GetAllCapabilities for AccountProvider");
177 /// Gets the specific label information detail of an account provider.
179 /// <since_tizen> 3 </since_tizen>
180 /// <param name="locale">
181 /// 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.
182 /// For example, "ko_KR" or "ko-kr" for Korean, "en_US" or "en-us" for American English.
184 /// <returns>The label text given for the locale</returns>
185 /// <privilege>http://tizen.org/privilege/account.read</privilege>
186 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given locale</exception>
187 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
188 public string GetLabel(string locale)
191 AccountError res = (AccountError)Interop.AccountProvider.GetlabelbyLocale(Handle, locale, out label);
192 if (res != AccountError.None)
194 throw AccountErrorFactory.CreateException(res, "Failed to GetLabel for AccountProvider");
201 /// Gets the specific label information detail of an account provider.
203 /// <since_tizen> 3 </since_tizen>
204 /// <param name="appId">
205 /// The application ID to search
207 /// <returns> All the labels information for the given application Id.</returns>
208 /// <privilege>http://tizen.org/privilege/account.read</privilege>
209 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given appid</exception>
210 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
211 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
212 public static Dictionary<string, string> GetLabelsByAppId(string appId)
215 Dictionary<string, string> labels = new Dictionary<string, string>();
216 Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
218 labels.Add(locale, label);
222 AccountError err = (AccountError)Interop.AccountProvider.GetLablesByAppId(callback, appId, IntPtr.Zero);
223 if (err != AccountError.None)
225 throw AccountErrorFactory.CreateException(err, "Failed to GetLablesByAppId");
232 /// Gets the label information detail of an account provider.
234 /// <since_tizen> 3 </since_tizen>
235 /// <returns> All the labels information for the given account provider.</returns>
236 /// <privilege>http://tizen.org/privilege/account.read</privilege>
237 /// <exception cref="InvalidOperationException">In case of any DB error</exception>
238 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
239 public Dictionary<string, string> GetLabels()
242 Dictionary<string, string> labels = new Dictionary<string, string>();
243 Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
245 labels.Add(locale, label);
249 AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderLabels(Handle, callback, IntPtr.Zero);
250 if (err != AccountError.None)
252 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderLabels");
259 /// Checks whether the given appId exists in the account provider DB.
261 /// <since_tizen> 3 </since_tizen>
262 /// <param name="appId">The application ID to check.</param>
263 /// <returns>returns true If App is supported </returns>
264 /// <privilege>http://tizen.org/privilege/account.read</privilege>
265 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given appid</exception>
266 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
267 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
268 public bool IsAppSupported(string appId)
270 bool isSupported = false;
271 AccountError res = (AccountError)Interop.AccountProvider.GetAppIdExists(appId);
273 if (res != AccountError.None)
275 throw AccountErrorFactory.CreateException(res, "Failed to GetLabel for AccountProvider");
286 /// Checks whether the given application ID supports the capability.
288 /// <since_tizen> 3 </since_tizen>
289 /// <param name="appId">The application Id</param>
290 /// <param name="capability">The capability information</param>
292 /// TRUE if the application supports the given capability,
293 /// otherwise FALSE if the application does not support the given capability
295 /// <privilege>http://tizen.org/privilege/account.read</privilege>
296 /// <exception cref="InvalidOperationException">In case of any DB error</exception>
297 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
298 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
299 public static bool IsFeatureSupportedByApp(string appId, string capability)
301 bool supported = Interop.AccountProvider.IsFeatureSupported(appId, capability);
304 //Get last result and validate error code.
305 AccountError err = (AccountError)ErrorFacts.GetLastResult();
306 if ((err != AccountError.None) && (err != AccountError.RecordNotFound))
308 throw AccountErrorFactory.CreateException(err, "Failed to get IsFeatureSupported");
316 /// Retrieves capability information with application ID.
318 /// <since_tizen> 3 </since_tizen>
319 /// <param name="appId">application Id</param>
320 /// <returns> Capability information list for the given appId.</returns>
321 /// <privilege>http://tizen.org/privilege/account.read</privilege>
322 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given appid</exception>
323 /// <exception cref="ArgumentException"> In case of invalid parameter</exception>
324 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
325 public static IEnumerable<string> GetFeaturesByAppId(string appId)
328 List<string> features = new List<string>();
329 Interop.AccountProvider.AccountProviderFeatureCallback callback = (string applicationId, string key, IntPtr userData) =>
335 AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderFeaturesByAppId(callback, appId, IntPtr.Zero);
336 if (err != AccountError.None)
338 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderFeaturesByAppId");
341 return (IEnumerable<string>)features;
345 /// Overloaded Dispose API for destroying the AccountProvider Handle.
347 /// <since_tizen> 3 </since_tizen>
348 public void Dispose()
351 GC.SuppressFinalize(this);
354 private void Dispose(bool disposing)
358 if (_handle != IntPtr.Zero)
360 Interop.AccountProvider.Destroy(_handle);
361 _handle = IntPtr.Zero;