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;
32 /// AccountProvider destructor.
34 /// <since_tizen> 3 </since_tizen>
35 /// <param name="handle"> The account handle.</param>
36 internal AccountProvider(IntPtr handle)
42 /// AccountProvider deconstructor.
44 /// <since_tizen> 3 </since_tizen>
50 internal IntPtr Handle
65 /// <since_tizen> 3 </since_tizen>
71 AccountError res = (AccountError)Interop.AccountProvider.GetAppId(Handle, out id);
72 if (res != AccountError.None)
74 Log.Warn(AccountErrorFactory.LogTag, "Failed to get AppId for the AccountProvider");
82 /// ServiceProvider ID of the account provider.
84 /// <since_tizen> 3 </since_tizen>
85 public string ServiceProviderId
90 AccountError res = (AccountError)Interop.AccountProvider.GetServiceProviderId(Handle, out id);
91 if (res != AccountError.None)
93 Log.Warn(AccountErrorFactory.LogTag, "Failed to get ServiceProviderId for the AccountProvider");
101 /// Icon path of the account provider.
103 /// <since_tizen> 3 </since_tizen>
104 public string IconPath
109 AccountError res = (AccountError)Interop.AccountProvider.GetAccountProviderIconPath(Handle, out path);
110 if (res != AccountError.None)
112 Log.Warn(AccountErrorFactory.LogTag, "Failed to get IconPath for the AccountProvider");
120 /// Small icon path of the account provider.
122 /// <since_tizen> 3 </since_tizen>
123 public string SmallIconPath
128 AccountError res = (AccountError)Interop.AccountProvider.GetAccountProviderSmallIconPath(Handle, out path);
129 if (res != AccountError.None)
131 Log.Warn(AccountErrorFactory.LogTag, "Failed to get SmallIconPath for the AccountProvider");
139 /// Flag for the account provider if it supports multiple accounts.
141 /// <since_tizen> 3 </since_tizen>
142 public bool MultipleAccountSupport
147 AccountError res = (AccountError)Interop.AccountProvider.GetMultipleAccountSupport(Handle, out multiple);
148 if (res != AccountError.None)
150 Log.Warn(AccountErrorFactory.LogTag, "Failed to get SmallIconPath for the AccountProvider");
153 return (multiple == 0) ? false : true;
158 /// Retrieves all the capability information of the account provider.
160 /// <since_tizen> 3 </since_tizen>
161 /// <privilege>http://tizen.org/privilege/account.read</privilege>
162 /// <feature>http://tizen.org/feature/account</feature>
164 /// The list of capability information.
166 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
167 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
168 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
169 public IEnumerable<string> GetAllCapabilities()
171 List<string> capabilities = new List<string>();
173 Interop.AccountProvider.AccountProviderFeatureCallback callback = (string appId, string key, IntPtr data) =>
175 capabilities.Add(key);
179 res = (AccountError)Interop.AccountProvider.GetAccountProviderFeatures(Handle, callback, IntPtr.Zero);
180 if (res != AccountError.None)
182 throw AccountErrorFactory.CreateException(res, "Failed to GetAllCapabilities for AccountProvider");
189 /// Gets the specific label information detail of the account provider.
191 /// <since_tizen> 3 </since_tizen>
192 /// <param name="locale">
193 /// 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.
194 /// For example, "ko_KR" or "ko-kr" for Korean, "en_US" or "en-us" for American English.
196 /// <returns>The label text given for the locale.</returns>
197 /// <privilege>http://tizen.org/privilege/account.read</privilege>
198 /// <feature>http://tizen.org/feature/account</feature>
199 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given locale.</exception>
200 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
201 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
202 public string GetLabel(string locale)
205 AccountError res = (AccountError)Interop.AccountProvider.GetlabelbyLocale(Handle, locale, out label);
206 if (res != AccountError.None)
208 throw AccountErrorFactory.CreateException(res, "Failed to GetLabel for AccountProvider");
215 /// Gets the specific label information detail of the account provider.
217 /// <since_tizen> 3 </since_tizen>
218 /// <param name="appId">
219 /// The application ID to search.
221 /// <returns> All the labels information for the given application ID.</returns>
222 /// <privilege>http://tizen.org/privilege/account.read</privilege>
223 /// <feature>http://tizen.org/feature/account</feature>
224 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given the application ID.</exception>
225 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
226 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
227 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
228 public static Dictionary<string, string> GetLabelsByAppId(string appId)
231 Dictionary<string, string> labels = new Dictionary<string, string>();
232 Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
234 labels.Add(locale, label);
238 AccountError err = (AccountError)Interop.AccountProvider.GetLablesByAppId(callback, appId, IntPtr.Zero);
239 if (err != AccountError.None)
241 throw AccountErrorFactory.CreateException(err, "Failed to GetLablesByAppId");
248 /// Gets the label information detail of the account provider.
250 /// <since_tizen> 3 </since_tizen>
251 /// <returns> All the labels information for the given account provider.</returns>
252 /// <privilege>http://tizen.org/privilege/account.read</privilege>
253 /// <feature>http://tizen.org/feature/account</feature>
254 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
255 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
256 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
257 public Dictionary<string, string> GetLabels()
260 Dictionary<string, string> labels = new Dictionary<string, string>();
261 Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
263 labels.Add(locale, label);
267 AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderLabels(Handle, callback, IntPtr.Zero);
268 if (err != AccountError.None)
270 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderLabels");
277 /// Checks whether the given appId exists in the account provider DB.
279 /// <since_tizen> 3 </since_tizen>
280 /// <param name="appId">The application ID to check.</param>
281 /// <returns>returns true If App is supported </returns>
282 /// <privilege>http://tizen.org/privilege/account.read</privilege>
283 /// <feature>http://tizen.org/feature/account</feature>
284 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for the given application ID.</exception>
285 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
286 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
287 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
288 public bool IsAppSupported(string appId)
290 bool isSupported = false;
291 AccountError res = (AccountError)Interop.AccountProvider.GetAppIdExists(appId);
293 if (res != AccountError.None)
295 throw AccountErrorFactory.CreateException(res, "Failed to GetLabel for AccountProvider");
306 /// Checks whether the given application ID supports the capability.
308 /// <since_tizen> 3 </since_tizen>
309 /// <param name="appId">The application ID.</param>
310 /// <param name="capability">The capability information.</param>
312 /// TRUE if the application supports the given capability,
313 /// otherwise FALSE if the application does not support the given capability
315 /// <privilege>http://tizen.org/privilege/account.read</privilege>
316 /// <feature>http://tizen.org/feature/account</feature>
317 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
318 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
319 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
320 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
321 public static bool IsFeatureSupportedByApp(string appId, string capability)
323 bool supported = Interop.AccountProvider.IsFeatureSupported(appId, capability);
326 //Get last result and validate error code.
327 AccountError err = (AccountError)ErrorFacts.GetLastResult();
328 if ((err != AccountError.None) && (err != AccountError.RecordNotFound))
330 throw AccountErrorFactory.CreateException(err, "Failed to get IsFeatureSupported");
338 /// Retrieves capability information with the application ID.
340 /// <since_tizen> 3 </since_tizen>
341 /// <param name="appId">The application ID.</param>
342 /// <returns> Capability information list for the given appId.</returns>
343 /// <privilege>http://tizen.org/privilege/account.read</privilege>
344 /// <feature>http://tizen.org/feature/account</feature>
345 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for the given application ID.</exception>
346 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
347 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
348 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
349 public static IEnumerable<string> GetFeaturesByAppId(string appId)
352 List<string> features = new List<string>();
353 Interop.AccountProvider.AccountProviderFeatureCallback callback = (string applicationId, string key, IntPtr userData) =>
359 AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderFeaturesByAppId(callback, appId, IntPtr.Zero);
360 if (err != AccountError.None)
362 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderFeaturesByAppId");
365 return (IEnumerable<string>)features;
369 /// Overloaded Dispose API for destroying the AccountProvider Handle.
371 /// <since_tizen> 3 </since_tizen>
372 public void Dispose()
375 GC.SuppressFinalize(this);
379 /// Dispose API for destroying the AccountProvider handle.
381 /// <since_tizen> 3 </since_tizen>
382 /// <param name="disposing">The boolean value for destoying AccountProvider handle.</param>
383 protected virtual void Dispose(bool disposing)
387 if (_handle != IntPtr.Zero)
389 Interop.AccountProvider.Destroy(_handle);
390 _handle = IntPtr.Zero;