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

Add bindings for Permission Manager (checkPermission()) #484

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions ndk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased

- image_reader: Add `ImageReader::new_with_data_space()` constructor and `ImageReader::data_space()` getter from API level 34. (#474)
- Add bindings for Permission Manager (`checkPermission()`). (#484)

# 0.9.0 (2024-04-26)

Expand Down
1 change: 1 addition & 0 deletions ndk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod media;
pub mod media_error;
pub mod native_activity;
pub mod native_window;
pub mod permission_manager;
pub mod shared_memory;
pub mod surface_texture;
pub mod sync;
Expand Down
53 changes: 53 additions & 0 deletions ndk/src/permission_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//! Bindings for permission checks
//!
//! <https://developer.android.com/ndk/reference/group/permission>
#![cfg(feature = "api-level-31")]

use std::{ffi::CStr, mem::MaybeUninit};

use num_enum::{FromPrimitive, IntoPrimitive};

/// Permission check return status values.
#[repr(i32)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)]
#[non_exhaustive]
pub enum PermissionManagerStatus {
/// This is returned if the permission check encountered an unspecified error.
ErrorUnknown = ffi::PERMISSION_MANAGER_STATUS_ERROR_UNKNOWN,
/// This is returned if the permission check failed because the service is unavailable.
ServiceUnavailable = ffi::PERMISSION_MANAGER_STATUS_SERVICE_UNAVAILABLE,

#[doc(hidden)]
#[num_enum(catch_all)]
__Unknown(i32) = 0,
}

/// Checks whether the package with the given pid/uid has been granted a permission.
///
/// Note that the Java API of [`Context#checkPermission()`] is usually faster due to caching,
/// thus is preferred over this API wherever possible.
///
/// [`Context#checkPermission()`]: https://developer.android.com/reference/android/content/Context#checkPermission(java.lang.String,%20int,%20int)
///
/// # Parameters
/// - `permission`: the permission to be checked.
/// - `pid`: the process id of the package to be checked.
/// - `uid`: the uid of the package to be checked.
#[doc(alias = "APermissionManager_checkPermission")]
pub fn check_permission(
permission: &CStr,
pid: i32,
uid: u32,
) -> Result<bool, PermissionManagerStatus> {
let mut result = MaybeUninit::uninit();
match unsafe {
ffi::APermissionManager_checkPermission(permission.as_ptr(), pid, uid, result.as_mut_ptr())
} {
ffi::PERMISSION_MANAGER_STATUS_OK => Ok(match unsafe { result.assume_init() } {
ffi::PERMISSION_MANAGER_PERMISSION_GRANTED => true,
ffi::PERMISSION_MANAGER_PERMISSION_DENIED => false,
x => unreachable!("Unexpected `PERMISSION` result output {x}"),
}),
x => Err(x.into()),
}
}
Loading