MobileUtils.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using System.Collections.Generic;
  5. using InABox.Core;
  6. using Xamarin.Forms;
  7. using Xamarin.Essentials;
  8. namespace InABox.Mobile
  9. {
  10. public static class MobileUtils
  11. {
  12. public static void Init()
  13. {
  14. Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(CoreUtils.SyncfusionLicense(SyncfusionVersion.v22_1));
  15. }
  16. public static String GetDeviceID()
  17. {
  18. IDeviceID deviceID = DependencyService.Get<IDeviceID>();
  19. return deviceID.DeviceID();
  20. }
  21. public static IAppVersion AppVersion { get { return DependencyService.Get<IAppVersion>(); } }
  22. public static IImageTools ImageTools => DependencyService.Get<IImageTools>();
  23. private static async Task<bool> Retry(Func<Task<bool>> action, int interval, int retryCount = 3)
  24. {
  25. bool flag = false;
  26. List<Exception> source = new List<Exception>();
  27. TimeSpan delay = TimeSpan.FromMilliseconds((double)interval);
  28. for (int index = 0; index < retryCount; ++index)
  29. {
  30. try
  31. {
  32. flag = await action();
  33. if (flag)
  34. return true;
  35. Task.Delay(delay).Wait();
  36. }
  37. catch (Exception ex)
  38. {
  39. if (source.All<Exception>((Func<Exception, bool>)(x => x.Message != ex.Message)))
  40. source.Add(ex);
  41. Task.Delay(delay).Wait();
  42. }
  43. }
  44. if (!flag && !source.Any<Exception>())
  45. return false;
  46. if (source.Count<Exception>() == 1)
  47. throw source.First();
  48. if (source.Count<Exception>() > 1)
  49. throw new AggregateException((IEnumerable<Exception>)source);
  50. return flag;
  51. }
  52. public static async Task<bool> IsPermitted<TPermission>() where TPermission : Permissions.BasePermission, new()
  53. {
  54. try
  55. {
  56. PermissionStatus status = await Permissions.CheckStatusAsync<TPermission>();
  57. if (status == PermissionStatus.Granted)
  58. return true;
  59. var request = await Permissions.RequestAsync<TPermission>();
  60. return request == PermissionStatus.Granted;
  61. }
  62. catch (TaskCanceledException ex)
  63. {
  64. return false;
  65. }
  66. }
  67. // public static async Task<bool> CheckPermissions<TPermission>() where TPermission : Permissions.BasePermission, new()
  68. // {
  69. // var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
  70. // bool request = false;
  71. // if (permissionStatus == PermissionStatus.Denied)
  72. // {
  73. // if (String.Equals(Device.RuntimePlatform, Device.iOS))
  74. // {
  75. //
  76. // var title = $"{permission} Permission";
  77. // var question = $"To use this plugin the {permission} permission is required. Please go into Settings and turn on {permission} for the app.";
  78. // var positive = "Settings";
  79. // var negative = "Maybe Later";
  80. // var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
  81. // if (task == null)
  82. // return false;
  83. //
  84. // var result = await task;
  85. // if (result)
  86. // {
  87. // CrossPermissions.Current.OpenAppSettings();
  88. // }
  89. //
  90. // return false;
  91. // }
  92. //
  93. // request = true;
  94. //
  95. // }
  96. //
  97. // if (request || permissionStatus != PermissionStatus.Granted)
  98. // {
  99. // var newStatus = await CrossPermissions.Current.RequestPermissionsAsync(permission);
  100. // if (newStatus.ContainsKey(permission) && newStatus[permission] != PermissionStatus.Granted)
  101. // {
  102. // var title = $"{permission} Permission";
  103. // var question = $"To use the plugin the {permission} permission is required.";
  104. // var positive = "Settings";
  105. // var negative = "Maybe Later";
  106. // var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
  107. // if (task == null)
  108. // return false;
  109. //
  110. // var result = await task;
  111. // if (result)
  112. // {
  113. // CrossPermissions.Current.OpenAppSettings();
  114. // }
  115. // return false;
  116. // }
  117. // }
  118. //
  119. // return true;
  120. // }
  121. }
  122. }