From 220b448ea4aa6aa5d6df70adda4c2584d9a6a316 Mon Sep 17 00:00:00 2001 From: heguokai Date: Fri, 27 Jun 2025 20:57:09 +0800 Subject: [PATCH 1/2] 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..aee9046cb 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..c86224f4d 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 5c846a6c2..786e8a0cc 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( @@ -823,6 +824,23 @@ export default namespace notificationManager { }); } + 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 addSlots(slots: Array): Promise { if (!slots || slots.length === 0) { throw errorParamInvalid; -- Gitee From 15931ecc9e11392ea3c008199323c0e5b0931275 Mon Sep 17 00:00:00 2001 From: heguokai Date: Fri, 27 Jun 2025 21:04:55 +0800 Subject: [PATCH 2/2] modify Signed-off-by: heguokai --- frameworks/ets/ani/include/manager/ani_slot.h | 2 +- frameworks/ets/ani/src/manager/ani_slot.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/ets/ani/include/manager/ani_slot.h b/frameworks/ets/ani/include/manager/ani_slot.h index aee9046cb..9ee4a9d50 100644 --- a/frameworks/ets/ani/include/manager/ani_slot.h +++ b/frameworks/ets/ani/include/manager/ani_slot.h @@ -32,7 +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); +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_slot.cpp b/frameworks/ets/ani/src/manager/ani_slot.cpp index c86224f4d..fafe8fdb1 100644 --- a/frameworks/ets/ani/src/manager/ani_slot.cpp +++ b/frameworks/ets/ani/src/manager/ani_slot.cpp @@ -357,7 +357,7 @@ void AniSetSlotFlagsByBundle(ani_env *env, ani_object obj, ani_double slotFlags) } } -ani_object AniGetSlotByBundle(ani_env *env, ani_object bundleOption, ani_enum_item type) +ani_object AniGetSlotByBundle(ani_env *env, ani_object bundleOption, ani_enum_item type) { ANS_LOGD("AniGetSlotByBundle enter"); Notification::NotificationBundleOption option; -- Gitee