123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using System;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Collections.Generic;
- using System.Threading;
- using Xamarin.Forms;
- using Plugin.Permissions;
- using Plugin.Permissions.Abstractions;
- using comal.timesheets;
- using Xamarin.Essentials;
- using PermissionStatus = Plugin.Permissions.Abstractions.PermissionStatus;
- using InABox.Core;
- using InABox.Clients;
- using InABox.Configuration;
- using Comal.Classes;
- using InABox.Rpc;
- namespace InABox.Mobile
- {
- public enum PermissionType
- {
- Unknown,
- Calendar,
- Camera,
- Contacts,
- Location,
- Microphone,
- Phone,
- Photos,
- Reminders,
- Sensors,
- Sms,
- Storage,
- Speech,
- LocationAlways,
- LocationWhenInUse,
- MediaLibrary
- }
- public static class MobileUtils
- {
- public static void Init()
- {
- Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(InABox.Core.CoreUtils.SyncfusionLicense(Core.SyncfusionVersion.v20_3));
- }
- public static String GetDeviceID()
- {
- IDeviceID deviceID = DependencyService.Get<IDeviceID>();
- return deviceID.DeviceID();
- }
- public static IAppVersion AppVersion { get { return DependencyService.Get<IAppVersion>(); } }
-
-
-
- private static void RemoveSessionID()
- {
- if (App.Current.Properties.ContainsKey("SessionID"))
- App.Current.Properties.Remove("SessionID");
- }
- 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[0];
- if (source.Count<Exception>() > 1)
- throw new AggregateException((IEnumerable<Exception>)source);
- return flag;
- }
- public static async Task<bool> IsPermitted(PermissionType permissiontype)
- {
- try
- {
- Permission permission = (Permission)permissiontype;
- PermissionStatus status = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
- if (status == PermissionStatus.Granted)
- return true;
- Dictionary<Permission, PermissionStatus> statuses = await CrossPermissions.Current.RequestPermissionsAsync(permission);
- if (statuses[permission] == PermissionStatus.Granted)
- return true;
- return false;
- }
- catch (System.Threading.Tasks.TaskCanceledException ex)
- {
- return false;
- }
- //request.Wait();
- //var statuses = request.Result;
- //return (statuses.ContainsKey(permission) && (statuses[permission] == PermissionStatus.Granted));
- //PermissionStatus status = PermissionStatus.Unknown;
- //var result = Retry(async () =>
- //{
- // status = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
- // if (status != PermissionStatus.Granted)
- // {
- // var results = await CrossPermissions.Current.RequestPermissionsAsync(permission);
- // if (results.ContainsKey(permission))
- // status = results[permission];
- // }
- // return status == PermissionStatus.Granted;
- //}, 5000, 3);
- //result.Wait();
- //return result.Result;
- }
- public static async Task<bool> CheckPermissions(PermissionType permissiontype)
- {
- Permission permission = (Permission)permissiontype;
- 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;
- }
-
- }
- }
|