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)
230 AccountErrorFactory.CheckAccountFeature();
232 Dictionary<string, string> labels = new Dictionary<string, string>();
233 Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
235 labels.Add(locale, label);
239 AccountError err = (AccountError)Interop.AccountProvider.GetLablesByAppId(callback, appId, IntPtr.Zero);
240 if (err != AccountError.None)
242 throw AccountErrorFactory.CreateException(err, "Failed to GetLablesByAppId");
249 /// Gets the label information detail of the account provider.
251 /// <since_tizen> 3 </since_tizen>
252 /// <returns> All the labels information for the given account provider.</returns>
253 /// <privilege>http://tizen.org/privilege/account.read</privilege>
254 /// <feature>http://tizen.org/feature/account</feature>
255 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
256 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
257 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
258 public Dictionary<string, string> GetLabels()
261 Dictionary<string, string> labels = new Dictionary<string, string>();
262 Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
264 labels.Add(locale, label);
268 AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderLabels(Handle, callback, IntPtr.Zero);
269 if (err != AccountError.None)
271 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderLabels");
278 /// Checks whether the given appId exists in the account provider DB.
280 /// <since_tizen> 3 </since_tizen>
281 /// <param name="appId">The application ID to check.</param>
282 /// <returns>returns true If App is supported </returns>
283 /// <privilege>http://tizen.org/privilege/account.read</privilege>
284 /// <feature>http://tizen.org/feature/account</feature>
285 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for the given application ID.</exception>
286 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
287 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
288 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
289 public bool IsAppSupported(string appId)
291 bool isSupported = false;
292 AccountError res = (AccountError)Interop.AccountProvider.GetAppIdExists(appId);
294 if (res != AccountError.None)
296 throw AccountErrorFactory.CreateException(res, "Failed to GetLabel for AccountProvider");
307 /// Checks whether the given application ID supports the capability.
309 /// <since_tizen> 3 </since_tizen>
310 /// <param name="appId">The application ID.</param>
311 /// <param name="capability">The capability information.</param>
313 /// TRUE if the application supports the given capability,
314 /// otherwise FALSE if the application does not support the given capability
316 /// <privilege>http://tizen.org/privilege/account.read</privilege>
317 /// <feature>http://tizen.org/feature/account</feature>
318 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
319 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
320 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
321 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
322 public static bool IsFeatureSupportedByApp(string appId, string capability)
324 AccountErrorFactory.CheckAccountFeature();
326 bool supported = Interop.AccountProvider.IsFeatureSupported(appId, capability);
329 //Get last result and validate error code.
330 AccountError err = (AccountError)ErrorFacts.GetLastResult();
331 if ((err != AccountError.None) && (err != AccountError.RecordNotFound))
333 throw AccountErrorFactory.CreateException(err, "Failed to get IsFeatureSupported");
341 /// Retrieves capability information with the application ID.
343 /// <since_tizen> 3 </since_tizen>
344 /// <param name="appId">The application ID.</param>
345 /// <returns> Capability information list for the given appId.</returns>
346 /// <privilege>http://tizen.org/privilege/account.read</privilege>
347 /// <feature>http://tizen.org/feature/account</feature>
348 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for the given application ID.</exception>
349 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
350 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
351 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
352 public static IEnumerable<string> GetFeaturesByAppId(string appId)
354 AccountErrorFactory.CheckAccountFeature();
356 List<string> features = new List<string>();
357 Interop.AccountProvider.AccountProviderFeatureCallback callback = (string applicationId, string key, IntPtr userData) =>
363 AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderFeaturesByAppId(callback, appId, IntPtr.Zero);
364 if (err != AccountError.None)
366 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderFeaturesByAppId");
369 return (IEnumerable<string>)features;
373 /// Overloaded Dispose API for destroying the AccountProvider Handle.
375 /// <since_tizen> 3 </since_tizen>
376 public void Dispose()
379 GC.SuppressFinalize(this);
383 /// Dispose API for destroying the AccountProvider handle.
385 /// <since_tizen> 3 </since_tizen>
386 /// <param name="disposing">The boolean value for destoying AccountProvider handle.</param>
387 protected virtual void Dispose(bool disposing)
391 if (_handle != IntPtr.Zero)
393 Interop.AccountProvider.Destroy(_handle);
394 _handle = IntPtr.Zero;