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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
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
60 changes: 58 additions & 2 deletions src/Uno.UWP/Extensions/PermissionsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,64 @@ 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 in manifest file.
pkar70 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 AreDeclaredInManifest(string[] permissions)
pkar70 marked this conversation as resolved.
Show resolved Hide resolved
{
foreach(var permission in permissions)
{
if (!IsDeclaredInManifest(permission))
{
return false;
}
}

return true;
pkar70 marked this conversation as resolved.
Show resolved Hide resolved
}

/// <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 in Manifest defined (no permission at all)");
pkar70 marked this conversation as resolved.
Show resolved Hide resolved
}

foreach(var permission in permissions)
{
if (!IsDeclaredInManifest(permission))
{
throw new UnauthorizedAccessException("no " + permission + " permission in Manifest defined");
pkar70 marked this conversation as resolved.
Show resolved Hide resolved
}
}

}

/// <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> CheckPermission(string permissionIdentifier)
pkar70 marked this conversation as resolved.
Show resolved Hide resolved
=> _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>
/// This operation is not cancellable.
Expand All @@ -56,6 +111,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 +123,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