MobileUtils.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. private static async Task<bool> Retry(Func<Task<bool>> action, int interval, int retryCount = 3)
  23. {
  24. bool flag = false;
  25. List<Exception> source = new List<Exception>();
  26. TimeSpan delay = TimeSpan.FromMilliseconds((double)interval);
  27. for (int index = 0; index < retryCount; ++index)
  28. {
  29. try
  30. {
  31. flag = await action();
  32. if (flag)
  33. return true;
  34. Task.Delay(delay).Wait();
  35. }
  36. catch (Exception ex)
  37. {
  38. if (source.All<Exception>((Func<Exception, bool>)(x => x.Message != ex.Message)))
  39. source.Add(ex);
  40. Task.Delay(delay).Wait();
  41. }
  42. }
  43. if (!flag && !source.Any<Exception>())
  44. return false;
  45. if (source.Count<Exception>() == 1)
  46. throw source.First();
  47. if (source.Count<Exception>() > 1)
  48. throw new AggregateException((IEnumerable<Exception>)source);
  49. return flag;
  50. }
  51. public static async Task<bool> IsPermitted<TPermission>() where TPermission : Permissions.BasePermission, new()
  52. {
  53. try
  54. {
  55. PermissionStatus status = await Permissions.CheckStatusAsync<TPermission>();
  56. if (status == PermissionStatus.Granted)
  57. return true;
  58. var request = await Permissions.RequestAsync<TPermission>();
  59. return request == PermissionStatus.Granted;
  60. }
  61. catch (TaskCanceledException ex)
  62. {
  63. return false;
  64. }
  65. }
  66. // public static async Task<bool> CheckPermissions<TPermission>() where TPermission : Permissions.BasePermission, new()
  67. // {
  68. // var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
  69. // bool request = false;
  70. // if (permissionStatus == PermissionStatus.Denied)
  71. // {
  72. // if (String.Equals(Device.RuntimePlatform, Device.iOS))
  73. // {
  74. //
  75. // var title = $"{permission} Permission";
  76. // var question = $"To use this plugin the {permission} permission is required. Please go into Settings and turn on {permission} for the app.";
  77. // var positive = "Settings";
  78. // var negative = "Maybe Later";
  79. // var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
  80. // if (task == null)
  81. // return false;
  82. //
  83. // var result = await task;
  84. // if (result)
  85. // {
  86. // CrossPermissions.Current.OpenAppSettings();
  87. // }
  88. //
  89. // return false;
  90. // }
  91. //
  92. // request = true;
  93. //
  94. // }
  95. //
  96. // if (request || permissionStatus != PermissionStatus.Granted)
  97. // {
  98. // var newStatus = await CrossPermissions.Current.RequestPermissionsAsync(permission);
  99. // if (newStatus.ContainsKey(permission) && newStatus[permission] != PermissionStatus.Granted)
  100. // {
  101. // var title = $"{permission} Permission";
  102. // var question = $"To use the plugin the {permission} permission is required.";
  103. // var positive = "Settings";
  104. // var negative = "Maybe Later";
  105. // var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
  106. // if (task == null)
  107. // return false;
  108. //
  109. // var result = await task;
  110. // if (result)
  111. // {
  112. // CrossPermissions.Current.OpenAppSettings();
  113. // }
  114. // return false;
  115. // }
  116. // }
  117. //
  118. // return true;
  119. // }
  120. }
  121. }