MobileUtils.cs 5.8 KB

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