From 0db9dd97ca5ad6cb418e19773a87c7e7ccdb88b9 Mon Sep 17 00:00:00 2001 From: heguokai Date: Fri, 27 Jun 2025 23:17:10 +0800 Subject: [PATCH] add function of getSlotByBundle for ANI Signed-off-by: heguokai --- frameworks/ets/ani/include/manager/ani_slot.h | 1 + .../ets/ani/src/manager/ani_manager.cpp | 2 ++ frameworks/ets/ani/src/manager/ani_slot.cpp | 26 +++++++++++++++++++ .../ets/ets/@ohos.notificationManager.ets | 18 +++++++++++++ 4 files changed, 47 insertions(+) diff --git a/frameworks/ets/ani/include/manager/ani_slot.h b/frameworks/ets/ani/include/manager/ani_slot.h index eecf03272..9ee4a9d50 100644 --- a/frameworks/ets/ani/include/manager/ani_slot.h +++ b/frameworks/ets/ani/include/manager/ani_slot.h @@ -32,6 +32,7 @@ void AniAddSlotBySlotType(ani_env *env, ani_enum_item enumObj); void AniAddSlots(ani_env *env, ani_object notificationSlotArrayObj); ani_object AniGetSlot(ani_env *env, ani_enum_item enumObj); ani_object AniGetSlots(ani_env *env); +ani_object AniGetSlotByBundle(ani_env *env, ani_object bundleOption, ani_enum_item type); void AniRemoveSlot(ani_env *env, ani_enum_item enumObj); void AniRemoveAllSlots(ani_env *env); void AniSetSlotByBundle(ani_env *env, ani_object bundleOptionObj, ani_object slotObj); diff --git a/frameworks/ets/ani/src/manager/ani_manager.cpp b/frameworks/ets/ani/src/manager/ani_manager.cpp index 7cecd7021..e78a804c1 100644 --- a/frameworks/ets/ani/src/manager/ani_manager.cpp +++ b/frameworks/ets/ani/src/manager/ani_manager.cpp @@ -122,6 +122,8 @@ static std::array kitManagerFunctions = { reinterpret_cast(AniAddSlots)}, ani_native_function {"nativeGetSlot", nullptr, reinterpret_cast(AniGetSlot)}, + ani_native_function {"nativeGetSlotByBundle", nullptr, + reinterpret_cast(AniGetSlotByBundle)}, ani_native_function {"nativeGetSlots", nullptr, reinterpret_cast(AniGetSlots)}, ani_native_function {"nativeRemoveSlot", nullptr, diff --git a/frameworks/ets/ani/src/manager/ani_slot.cpp b/frameworks/ets/ani/src/manager/ani_slot.cpp index 1c7bf7d98..fafe8fdb1 100644 --- a/frameworks/ets/ani/src/manager/ani_slot.cpp +++ b/frameworks/ets/ani/src/manager/ani_slot.cpp @@ -356,5 +356,31 @@ void AniSetSlotFlagsByBundle(ani_env *env, ani_object obj, ani_double slotFlags) AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); } } + +ani_object AniGetSlotByBundle(ani_env *env, ani_object bundleOption, ani_enum_item type) +{ + ANS_LOGD("AniGetSlotByBundle enter"); + Notification::NotificationBundleOption option; + Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType::OTHER; + if (!NotificationSts::UnwrapBundleOption(env, bundleOption, option) + || !NotificationSts::SlotTypeEtsToC(env, type, slotType)) { + NotificationSts::ThrowStsErroWithMsg(env, "GetSlotByBundle : erro arguments."); + return nullptr; + } + sptr slot = nullptr; + int returncode = Notification::NotificationHelper::GetNotificationSlotForBundle(option, slotType, slot); + if (returncode != ERR_OK) { + int externalCode = NotificationSts::GetExternalCode(returncode); + ANS_LOGE("GetSlotByBundle -> error, errorCode: %{public}d", externalCode); + AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); + return nullptr; + } + ani_object infoObj; + if (!NotificationSts::WrapNotificationSlot(env, slot, infoObj) || infoObj == nullptr) { + NotificationSts::ThrowStsErroWithMsg(env, "WrapNotificationSlot Failed"); + return nullptr; + } + return infoObj; +} } } \ No newline at end of file diff --git a/frameworks/ets/ets/@ohos.notificationManager.ets b/frameworks/ets/ets/@ohos.notificationManager.ets index b5ed6687f..dcf37daca 100644 --- a/frameworks/ets/ets/@ohos.notificationManager.ets +++ b/frameworks/ets/ets/@ohos.notificationManager.ets @@ -159,6 +159,7 @@ export default namespace notificationManager { export native function nativeGetSlotFlagsByBundle(bundle: BundleOption): int; export native function nativeSetSlotFlagsByBundle(bundle: BundleOption, slotFlags: number): void; export native function nativeGetSlotsByBundle(bundle: BundleOption): Array; + export native function nativeGetSlotByBundle(bundle: BundleOption, type: SlotType): NotificationSlot; export native function nativeIsNotificationSlotEnabled(bundle: BundleOption, type: SlotType): boolean; export native function nativeSetNotificationEnableSlot( @@ -961,6 +962,23 @@ export default namespace notificationManager { return pPromise; } + export function getSlotByBundle(bundle: BundleOption, slotType: SlotType): Promise { + let error: BusinessError = isInvalidParameter(bundle); + if (error.code !== ERROR_OK) { + throw error; + } + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): NotificationSlot => { return nativeGetSlotByBundle(bundle, slotType); }); + p.then((data: NullishType): void => { + let ret : NotificationSlot = data as NotificationSlot; + resolve(ret); + }, (error: Error): void => { + reject(error); + }); + }); + return pPromise; + } + export function publish(request: NotificationRequest, callback: AsyncCallback): void { let error: BusinessError = isInvalidParameter(request); if (error.code !== ERROR_OK) { -- Gitee