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 the 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 the 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 the account provider if it 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>
152 /// <feature>http://tizen.org/feature/account</feature>
154 /// The list of capability information.
156 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
157 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
158 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
159 public IEnumerable<string> GetAllCapabilities()
161 List<string> capabilities = new List<string>();
163 Interop.AccountProvider.AccountProviderFeatureCallback callback = (string appId, string key, IntPtr data) =>
165 capabilities.Add(key);
169 res = (AccountError)Interop.AccountProvider.GetAccountProviderFeatures(Handle, callback, IntPtr.Zero);
170 if (res != AccountError.None)
172 throw AccountErrorFactory.CreateException(res, "Failed to GetAllCapabilities for AccountProvider");
179 /// Gets the specific label information detail of the account provider.
181 /// <since_tizen> 3 </since_tizen>
182 /// <param name="locale">
183 /// 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.
184 /// For example, "ko_KR" or "ko-kr" for Korean, "en_US" or "en-us" for American English.
186 /// <returns>The label text given for the locale.</returns>
187 /// <privilege>http://tizen.org/privilege/account.read</privilege>
188 /// <feature>http://tizen.org/feature/account</feature>
189 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given locale.</exception>
190 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
191 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
192 public string GetLabel(string locale)
195 AccountError res = (AccountError)Interop.AccountProvider.GetlabelbyLocale(Handle, locale, out label);
196 if (res != AccountError.None)
198 throw AccountErrorFactory.CreateException(res, "Failed to GetLabel for AccountProvider");
205 /// Gets the specific label information detail of the account provider.
207 /// <since_tizen> 3 </since_tizen>
208 /// <param name="appId">
209 /// The application ID to search.
211 /// <returns> All the labels information for the given application ID.</returns>
212 /// <privilege>http://tizen.org/privilege/account.read</privilege>
213 /// <feature>http://tizen.org/feature/account</feature>
214 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for given the application ID.</exception>
215 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
216 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
217 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
218 public static Dictionary<string, string> GetLabelsByAppId(string appId)
221 Dictionary<string, string> labels = new Dictionary<string, string>();
222 Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
224 labels.Add(locale, label);
228 AccountError err = (AccountError)Interop.AccountProvider.GetLablesByAppId(callback, appId, IntPtr.Zero);
229 if (err != AccountError.None)
231 throw AccountErrorFactory.CreateException(err, "Failed to GetLablesByAppId");
238 /// Gets the label information detail of the account provider.
240 /// <since_tizen> 3 </since_tizen>
241 /// <returns> All the labels information for the given account provider.</returns>
242 /// <privilege>http://tizen.org/privilege/account.read</privilege>
243 /// <feature>http://tizen.org/feature/account</feature>
244 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
245 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
246 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
247 public Dictionary<string, string> GetLabels()
250 Dictionary<string, string> labels = new Dictionary<string, string>();
251 Interop.AccountProvider.LabelCallback callback = (string applicationId, string label, string locale, IntPtr userData) =>
253 labels.Add(locale, label);
257 AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderLabels(Handle, callback, IntPtr.Zero);
258 if (err != AccountError.None)
260 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderLabels");
267 /// Checks whether the given appId exists in the account provider DB.
269 /// <since_tizen> 3 </since_tizen>
270 /// <param name="appId">The application ID to check.</param>
271 /// <returns>returns true If App is supported </returns>
272 /// <privilege>http://tizen.org/privilege/account.read</privilege>
273 /// <feature>http://tizen.org/feature/account</feature>
274 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for the given application ID.</exception>
275 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
276 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
277 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
278 public bool IsAppSupported(string appId)
280 bool isSupported = false;
281 AccountError res = (AccountError)Interop.AccountProvider.GetAppIdExists(appId);
283 if (res != AccountError.None)
285 throw AccountErrorFactory.CreateException(res, "Failed to GetLabel for AccountProvider");
296 /// Checks whether the given application ID supports the capability.
298 /// <since_tizen> 3 </since_tizen>
299 /// <param name="appId">The application ID.</param>
300 /// <param name="capability">The capability information.</param>
302 /// TRUE if the application supports the given capability,
303 /// otherwise FALSE if the application does not support the given capability
305 /// <privilege>http://tizen.org/privilege/account.read</privilege>
306 /// <feature>http://tizen.org/feature/account</feature>
307 /// <exception cref="InvalidOperationException">In case of any DB error.</exception>
308 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
309 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
310 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
311 public static bool IsFeatureSupportedByApp(string appId, string capability)
313 bool supported = Interop.AccountProvider.IsFeatureSupported(appId, capability);
316 //Get last result and validate error code.
317 AccountError err = (AccountError)ErrorFacts.GetLastResult();
318 if ((err != AccountError.None) && (err != AccountError.RecordNotFound))
320 throw AccountErrorFactory.CreateException(err, "Failed to get IsFeatureSupported");
328 /// Retrieves capability information with the application ID.
330 /// <since_tizen> 3 </since_tizen>
331 /// <param name="appId">The application ID.</param>
332 /// <returns> Capability information list for the given appId.</returns>
333 /// <privilege>http://tizen.org/privilege/account.read</privilege>
334 /// <feature>http://tizen.org/feature/account</feature>
335 /// <exception cref="InvalidOperationException">In case of any DB error or record not found for the given application ID.</exception>
336 /// <exception cref="ArgumentException"> In case of an invalid parameter.</exception>
337 /// <exception cref="UnauthorizedAccessException"> In case of privilege not defined.</exception>
338 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
339 public static IEnumerable<string> GetFeaturesByAppId(string appId)
342 List<string> features = new List<string>();
343 Interop.AccountProvider.AccountProviderFeatureCallback callback = (string applicationId, string key, IntPtr userData) =>
349 AccountError err = (AccountError)Interop.AccountProvider.GetAccountProviderFeaturesByAppId(callback, appId, IntPtr.Zero);
350 if (err != AccountError.None)
352 throw AccountErrorFactory.CreateException(err, "Failed to GetAccountProviderFeaturesByAppId");
355 return (IEnumerable<string>)features;
359 /// Overloaded Dispose API for destroying the AccountProvider Handle.
361 /// <since_tizen> 3 </since_tizen>
362 public void Dispose()
365 GC.SuppressFinalize(this);
368 protected virtual void Dispose(bool disposing)
372 if (_handle != IntPtr.Zero)
374 Interop.AccountProvider.Destroy(_handle);
375 _handle = IntPtr.Zero;