Skip to content

Commit

Permalink
Add bindings for Permission Manager (checkPermission())
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Aug 10, 2024
1 parent e4a0b94 commit 4adcffe
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
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()),
}
}

0 comments on commit 4adcffe

Please sign in to comment.