123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- using System;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Collections.Generic;
- using System.IO;
- using InABox.Core;
- using Xamarin.Forms;
- using Xamarin.Essentials;
- namespace InABox.Mobile
- {
-
- public static class MobileUtils
- {
- public static void Init()
- {
- Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(CoreUtils.SyncfusionLicense(SyncfusionVersion.v26_x));
- }
- public static String GetDeviceID()
- {
- IDeviceID deviceID = DependencyService.Get<IDeviceID>();
- return deviceID.DeviceID();
- }
- public static IAppVersion AppVersion { get { return DependencyService.Get<IAppVersion>(); } }
- public static IImageTools ImageTools => DependencyService.Get<IImageTools>();
-
- private static async Task<bool> Retry(Func<Task<bool>> action, int interval, int retryCount = 3)
- {
- bool flag = false;
- List<Exception> source = new List<Exception>();
- TimeSpan delay = TimeSpan.FromMilliseconds((double)interval);
- for (int index = 0; index < retryCount; ++index)
- {
- try
- {
- flag = await action();
- if (flag)
- return true;
- Task.Delay(delay).Wait();
- }
- catch (Exception ex)
- {
- if (source.All<Exception>((Func<Exception, bool>)(x => x.Message != ex.Message)))
- source.Add(ex);
- Task.Delay(delay).Wait();
- }
- }
- if (!flag && !source.Any<Exception>())
- return false;
- if (source.Count<Exception>() == 1)
- throw source.First();
- if (source.Count<Exception>() > 1)
- throw new AggregateException((IEnumerable<Exception>)source);
- return flag;
- }
-
- public static async Task<bool> IsPermitted<TPermission>() where TPermission : Permissions.BasePermission, new()
- {
- try
- {
- PermissionStatus status = await Permissions.CheckStatusAsync<TPermission>();
- if (status == PermissionStatus.Granted)
- return true;
- var request = await Permissions.RequestAsync<TPermission>();
- return request == PermissionStatus.Granted;
- }
- catch (TaskCanceledException ex)
- {
- return false;
- }
- }
-
- // public static async Task<bool> CheckPermissions<TPermission>() where TPermission : Permissions.BasePermission, new()
- // {
- // var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
- // bool request = false;
- // if (permissionStatus == PermissionStatus.Denied)
- // {
- // if (String.Equals(Device.RuntimePlatform, Device.iOS))
- // {
- //
- // var title = $"{permission} Permission";
- // var question = $"To use this plugin the {permission} permission is required. Please go into Settings and turn on {permission} for the app.";
- // var positive = "Settings";
- // var negative = "Maybe Later";
- // var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
- // if (task == null)
- // return false;
- //
- // var result = await task;
- // if (result)
- // {
- // CrossPermissions.Current.OpenAppSettings();
- // }
- //
- // return false;
- // }
- //
- // request = true;
- //
- // }
- //
- // if (request || permissionStatus != PermissionStatus.Granted)
- // {
- // var newStatus = await CrossPermissions.Current.RequestPermissionsAsync(permission);
- // if (newStatus.ContainsKey(permission) && newStatus[permission] != PermissionStatus.Granted)
- // {
- // var title = $"{permission} Permission";
- // var question = $"To use the plugin the {permission} permission is required.";
- // var positive = "Settings";
- // var negative = "Maybe Later";
- // var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
- // if (task == null)
- // return false;
- //
- // var result = await task;
- // if (result)
- // {
- // CrossPermissions.Current.OpenAppSettings();
- // }
- // return false;
- // }
- // }
- //
- // return true;
- // }
- public static byte[] ImageSourceToByteArray(ImageSource source)
- {
- StreamImageSource streamImageSource = (StreamImageSource)source;
- System.Threading.CancellationToken cancellationToken = System.Threading.CancellationToken.None;
- Task<Stream> task = streamImageSource.Stream(cancellationToken);
- Stream stream = task.Result;
- byte[] bytes = new byte[stream.Length];
- stream.Read(bytes, 0, bytes.Length);
- return bytes;
- }
-
- public static string ImageSourceToBase64(ImageSource source)
- {
- var bytes = ImageSourceToByteArray(source);
- string s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks);
- return s;
- }
-
- }
- }
|