Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: some macro methods for Android PermissionHelper #4186

Closed
wants to merge 5 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions src/Uno.UWP/Extensions/PermissionsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,54 @@ public static bool IsDeclaredInManifest(string permission)
var requestedPermissions = packageInfo?.RequestedPermissions;

return requestedPermissions?.Any(r => r.Equals(permission, StringComparison.OrdinalIgnoreCase)) ?? false;
}
}

/// <summary>
/// Checks if the given Android permissions are declared the app manifest.
MartinZikmund marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <param name="permission">Array of permissions.</param>
/// <returns>true if all permissions are defined</returns>
public static bool AreAllPermissionsDeclaredInManifest(string[] permissions)
=> permissions.All(IsDeclaredInManifest);


/// <summary>
/// Ensures that the given Android permissions are declared in manifest file.
pkar70 marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <param name="permission">Array of permissions.</param>
public static void EnsuresPermissionsAreDeclaredInManifest(string[] permissions)
{
var context = Application.Context;
var packageInfo = context.PackageManager.GetPackageInfo(context.PackageName, PackageInfoFlags.Permissions);
var requestedPermissions = packageInfo?.RequestedPermissions;

if(requestedPermissions is null)
{
throw new UnauthorizedAccessException("No permissions are defined in the manifest (no permission at all)");
}

foreach(var permission in permissions)
{
if (!IsDeclaredInManifest(permission))
{
throw new UnauthorizedAccessException($"The permission {permission} is not defined in the manifest");
}
}

}

/// <summary>
/// Validate if a given permission was granted to the app but not request it to the user
/// <remarks>
/// This should not be invoked directly from the application code.
/// You should use the extension methods in <see cref="PermissionsServiceExtensions"/>.
/// </remarks>
/// </summary>
/// <param name="permissionIdentifier">A permission identifier defined in Manifest.Permission.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation</returns>
public static Task<bool> CheckPermissionAsync(string permissionIdentifier)
=> _checkPermission(CancellationToken.None, permissionIdentifier);
pkar70 marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Validate if a given permission was granted to the app and if not, request it to the user.
/// <remarks>
Expand All @@ -56,6 +102,7 @@ public static bool IsDeclaredInManifest(string permission)
public static Task<bool> TryGetPermission(CancellationToken ct, string permissionIdentifier)
=> _tryGetPermission(ct, permissionIdentifier);


/// <summary>
/// Validate if a given permission was granted to the app but not request it to the user
/// <remarks>
Expand All @@ -67,7 +114,7 @@ public static Task<bool> TryGetPermission(CancellationToken ct, string permissio
/// <param name="permissionIdentifier">A permission identifier defined in Manifest.Permission.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation</returns>
public static Task<bool> CheckPermission(CancellationToken ct, string permissionIdentifier)
=> _checkPermission(ct, permissionIdentifier);
=> _checkPermission(ct, permissionIdentifier);

/// <summary>
/// Manifest.Permission.AccessFineLocation
Expand Down