Skip to main content

ohos_bundle_binding/
lib.rs

1use std::ffi::CStr;
2
3use ohos_bundle_sys::{
4    OH_NativeBundle_GetAppId, OH_NativeBundle_GetAppIdentifier,
5    OH_NativeBundle_GetCurrentApplicationInfo,
6};
7
8#[cfg(feature = "api-13")]
9use ohos_bundle_sys::OH_NativeBundle_GetMainElementName;
10
11#[cfg(feature = "api-14")]
12use ohos_bundle_sys::OH_NativeBundle_GetCompatibleDeviceType;
13
14#[derive(Debug, Clone, Copy)]
15pub struct BundleInfo<'a> {
16    pub bundle_name: &'a str,
17    pub fingerprint: &'a str,
18}
19
20/// get current application info
21pub fn get_bundle_info() -> BundleInfo<'static> {
22    let info = unsafe { OH_NativeBundle_GetCurrentApplicationInfo() };
23    BundleInfo {
24        bundle_name: unsafe { CStr::from_ptr(info.bundleName).to_str().unwrap_or("") },
25        fingerprint: unsafe { CStr::from_ptr(info.fingerprint).to_str().unwrap_or("") },
26    }
27}
28
29/// get appid
30pub fn get_appid() -> &'static str {
31    unsafe {
32        let id = OH_NativeBundle_GetAppId();
33        CStr::from_ptr(id).to_str().unwrap_or("")
34    }
35}
36
37/// get identifier which can't be changed
38pub fn get_app_identifier() -> &'static str {
39    unsafe {
40        let id = OH_NativeBundle_GetAppIdentifier();
41        CStr::from_ptr(id).to_str().unwrap_or("")
42    }
43}
44
45#[cfg(feature = "api-13")]
46#[derive(Debug, Clone, Copy)]
47pub struct MainElementName {
48    pub bundle_name: &'static str,
49    pub module_name: &'static str,
50    pub ability_name: &'static str,
51}
52
53/// get main element name
54#[cfg(feature = "api-13")]
55pub fn get_main_element_name() -> MainElementName {
56    unsafe {
57        let name = OH_NativeBundle_GetMainElementName();
58        MainElementName {
59            bundle_name: CStr::from_ptr(name.bundleName).to_str().unwrap_or(""),
60            module_name: CStr::from_ptr(name.moduleName).to_str().unwrap_or(""),
61            ability_name: CStr::from_ptr(name.abilityName).to_str().unwrap_or(""),
62        }
63    }
64}
65
66/// get compatible device type
67#[cfg(feature = "api-14")]
68pub fn get_compatible_device_type() -> &'static str {
69    unsafe {
70        let t = OH_NativeBundle_GetCompatibleDeviceType();
71        CStr::from_ptr(t).to_str().unwrap_or("")
72    }
73}