/// <summary> /// Callback that must be set when request permissions has finished /// </summary> private void RequestPermissionsActivity_PermissionsRequestCompleted(int requestCode, string[] permissions, acp.Permission[] grantResults) { if (requestCode != PermissionCode) { return; } if (_Tcs == null) { return; } for (var i = 0; i < permissions.Length; i++) { if (_Tcs.Task.Status == TaskStatus.Canceled) { return; } var permission = GetPermissionForManifestName(permissions[i]); if (permission == Permission.Unknown) { continue; } lock (_Locker) { if (permission == Permission.Microphone) { if (!_Results.ContainsKey(Permission.Speech)) { _Results.Add(Permission.Speech, grantResults[i] == acp.Permission.Granted ? PermissionStatus.Granted : PermissionStatus.Denied); } } if (!_Results.ContainsKey(permission)) { _Results.Add(permission, grantResults[i] == acp.Permission.Granted ? PermissionStatus.Granted : PermissionStatus.Denied); } } } _Tcs.SetResult(_Results); }
/// <summary> /// Requests the permissions from the users /// </summary> /// <returns>The permissions and their status.</returns> /// <param name="permissions">Permissions to request.</param> public Task <PermissionStatusDictionary> RequestPermissionsAsync(params Permission[] permissions) { var results = permissions.ToDictionary(permission => permission, permission => PermissionStatus.Granted); var retVal = new PermissionStatusDictionary(); foreach (var key in results.Keys) { retVal.Add(key, results[key]); } return(Task.FromResult(retVal)); }
/// <summary> /// Requests the permissions from the users /// </summary> /// <returns>The permissions and their status.</returns> /// <param name="permissions">Permissions to request.</param> public async Task <PermissionStatusDictionary> RequestPermissionsAsync(params Permission[] permissions) { var results = new PermissionStatusDictionary(); foreach (var permission in permissions) { if (results.ContainsKey(permission)) { continue; } switch (permission) { case Permission.Calendar: results.Add(permission, await RequestEventPermission(EKEntityType.Event).ConfigureAwait(false)); break; case Permission.Camera: try { var authCamera = await AVCaptureDevice.RequestAccessForMediaTypeAsync(AVMediaType.Video).ConfigureAwait(false); results.Add(permission, authCamera ? PermissionStatus.Granted : PermissionStatus.Denied); } catch (Exception ex) { Debug.WriteLine("Unable to get camera permission: " + ex); results.Add(permission, PermissionStatus.Unknown); } break; case Permission.Contacts: results.Add(permission, await RequestContactsPermission().ConfigureAwait(false)); break; case Permission.Location: results.Add(permission, await RequestLocationPermission().ConfigureAwait(false)); break; case Permission.Microphone: try { var authMic = await AVCaptureDevice.RequestAccessForMediaTypeAsync(AVMediaType.Audio).ConfigureAwait(false); results.Add(permission, authMic ? PermissionStatus.Granted : PermissionStatus.Denied); } catch (Exception ex) { Debug.WriteLine("Unable to get microphone permission: " + ex); results.Add(permission, PermissionStatus.Unknown); } break; case Permission.Photos: results.Add(permission, await RequestPhotosPermission().ConfigureAwait(false)); break; case Permission.Reminders: results.Add(permission, await RequestEventPermission(EKEntityType.Reminder).ConfigureAwait(false)); break; case Permission.Sensors: results.Add(permission, await RequestSensorsPermission().ConfigureAwait(false)); break; case Permission.Speech: results.Add(permission, await RequestSpeechPermission().ConfigureAwait(false)); break; } if (!results.ContainsKey(permission)) { results.Add(permission, PermissionStatus.Granted); } } return(results); }
/// <summary> /// Requests the permissions from the users /// </summary> public async Task <PermissionStatusDictionary> RequestPermissionsAsync(params Permission[] permissions) { if (_Tcs != null && !_Tcs.Task.IsCompleted) { _Tcs.SetCanceled(); _Tcs = null; } lock (_Locker) { _Results = new PermissionStatusDictionary(); } if (_Activity == null) { Debug.WriteLine("Unable to detect current Activity. Please ensure Plugin.CurrentActivity is installed in your Android project and your Application class is registering with Application.IActivityLifecycleCallbacks."); foreach (var permission in permissions) { if (_Results.ContainsKey(permission)) { continue; } _Results.Add(permission, PermissionStatus.Unknown); } return(_Results); } var permissionsToRequest = new List <string>(); foreach (var permission in permissions) { var result = await CheckPermissionStatusAsync(permission).ConfigureAwait(false); if (result != PermissionStatus.Granted) { var names = GetManifestNames(permission); //check to see if we can find manifest names //if we can't add as unknown and continue if ((names?.Count ?? 0) == 0) { lock (_Locker) { _Results.Add(permission, PermissionStatus.Unknown); } continue; } permissionsToRequest.AddRange(names); } else { //if we are granted you are good! lock (_Locker) { _Results.Add(permission, PermissionStatus.Granted); } } } if (permissionsToRequest.Count == 0) { return(_Results); } _Tcs = new TaskCompletionSource <PermissionStatusDictionary>(); ActivityCompat.RequestPermissions(_Activity, permissionsToRequest.ToArray(), PermissionCode); return(await _Tcs.Task.ConfigureAwait(true)); }