MobileUtils.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using Xamarin.Forms;
  7. using Plugin.Permissions;
  8. using Plugin.Permissions.Abstractions;
  9. using comal.timesheets;
  10. using Xamarin.Essentials;
  11. using PermissionStatus = Plugin.Permissions.Abstractions.PermissionStatus;
  12. using InABox.Core;
  13. using InABox.Clients;
  14. using InABox.Configuration;
  15. using Comal.Classes;
  16. using InABox.Rpc;
  17. namespace InABox.Mobile
  18. {
  19. public enum PermissionType
  20. {
  21. Unknown,
  22. Calendar,
  23. Camera,
  24. Contacts,
  25. Location,
  26. Microphone,
  27. Phone,
  28. Photos,
  29. Reminders,
  30. Sensors,
  31. Sms,
  32. Storage,
  33. Speech,
  34. LocationAlways,
  35. LocationWhenInUse,
  36. MediaLibrary
  37. }
  38. public static class MobileUtils
  39. {
  40. public static void Init()
  41. {
  42. Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(InABox.Core.CoreUtils.SyncfusionLicense(Core.SyncfusionVersion.v20_3));
  43. }
  44. public static String GetDeviceID()
  45. {
  46. IDeviceID deviceID = DependencyService.Get<IDeviceID>();
  47. return deviceID.DeviceID();
  48. }
  49. public static IAppVersion AppVersion { get { return DependencyService.Get<IAppVersion>(); } }
  50. private static void RemoveSessionID()
  51. {
  52. if (App.Current.Properties.ContainsKey("SessionID"))
  53. App.Current.Properties.Remove("SessionID");
  54. }
  55. private static async Task<bool> Retry(Func<Task<bool>> action, int interval, int retryCount = 3)
  56. {
  57. bool flag = false;
  58. List<Exception> source = new List<Exception>();
  59. TimeSpan delay = TimeSpan.FromMilliseconds((double)interval);
  60. for (int index = 0; index < retryCount; ++index)
  61. {
  62. try
  63. {
  64. flag = await action();
  65. if (flag)
  66. return true;
  67. Task.Delay(delay).Wait();
  68. }
  69. catch (Exception ex)
  70. {
  71. if (source.All<Exception>((Func<Exception, bool>)(x => x.Message != ex.Message)))
  72. source.Add(ex);
  73. Task.Delay(delay).Wait();
  74. }
  75. }
  76. if (!flag && !source.Any<Exception>())
  77. return false;
  78. if (source.Count<Exception>() == 1)
  79. throw source[0];
  80. if (source.Count<Exception>() > 1)
  81. throw new AggregateException((IEnumerable<Exception>)source);
  82. return flag;
  83. }
  84. public static async Task<bool> IsPermitted(PermissionType permissiontype)
  85. {
  86. try
  87. {
  88. Permission permission = (Permission)permissiontype;
  89. PermissionStatus status = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
  90. if (status == PermissionStatus.Granted)
  91. return true;
  92. Dictionary<Permission, PermissionStatus> statuses = await CrossPermissions.Current.RequestPermissionsAsync(permission);
  93. if (statuses[permission] == PermissionStatus.Granted)
  94. return true;
  95. return false;
  96. }
  97. catch (System.Threading.Tasks.TaskCanceledException ex)
  98. {
  99. return false;
  100. }
  101. //request.Wait();
  102. //var statuses = request.Result;
  103. //return (statuses.ContainsKey(permission) && (statuses[permission] == PermissionStatus.Granted));
  104. //PermissionStatus status = PermissionStatus.Unknown;
  105. //var result = Retry(async () =>
  106. //{
  107. // status = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
  108. // if (status != PermissionStatus.Granted)
  109. // {
  110. // var results = await CrossPermissions.Current.RequestPermissionsAsync(permission);
  111. // if (results.ContainsKey(permission))
  112. // status = results[permission];
  113. // }
  114. // return status == PermissionStatus.Granted;
  115. //}, 5000, 3);
  116. //result.Wait();
  117. //return result.Result;
  118. }
  119. public static async Task<bool> CheckPermissions(PermissionType permissiontype)
  120. {
  121. Permission permission = (Permission)permissiontype;
  122. var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
  123. bool request = false;
  124. if (permissionStatus == PermissionStatus.Denied)
  125. {
  126. if (String.Equals(Device.RuntimePlatform, Device.iOS))
  127. {
  128. var title = $"{permission} Permission";
  129. var question = $"To use this plugin the {permission} permission is required. Please go into Settings and turn on {permission} for the app.";
  130. var positive = "Settings";
  131. var negative = "Maybe Later";
  132. var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
  133. if (task == null)
  134. return false;
  135. var result = await task;
  136. if (result)
  137. {
  138. CrossPermissions.Current.OpenAppSettings();
  139. }
  140. return false;
  141. }
  142. request = true;
  143. }
  144. if (request || permissionStatus != PermissionStatus.Granted)
  145. {
  146. var newStatus = await CrossPermissions.Current.RequestPermissionsAsync(permission);
  147. if (newStatus.ContainsKey(permission) && newStatus[permission] != PermissionStatus.Granted)
  148. {
  149. var title = $"{permission} Permission";
  150. var question = $"To use the plugin the {permission} permission is required.";
  151. var positive = "Settings";
  152. var negative = "Maybe Later";
  153. var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
  154. if (task == null)
  155. return false;
  156. var result = await task;
  157. if (result)
  158. {
  159. CrossPermissions.Current.OpenAppSettings();
  160. }
  161. return false;
  162. }
  163. }
  164. return true;
  165. }
  166. }
  167. }