diff --git a/frameworks/ets/ani/include/manager/ani_slot.h b/frameworks/ets/ani/include/manager/ani_slot.h index 044d5a877020672a7c216bd0f332379010bc95c3..eecf03272219636acbc2e1099e75a32c8ca50c57 100644 --- a/frameworks/ets/ani/include/manager/ani_slot.h +++ b/frameworks/ets/ani/include/manager/ani_slot.h @@ -27,6 +27,15 @@ ani_boolean AniIsNotificationSlotEnabled(ani_env *env, ani_object bundleOption, void AniSetNotificationEnableSlot(ani_env *env, ani_object bundleOption, ani_enum_item type, ani_boolean enable); void AniSetNotificationEnableSlotWithForce(ani_env *env, ani_object bundleOption, ani_enum_item type, ani_boolean enable, ani_boolean isForceControl); +void AniAddSlotByNotificationSlot(ani_env *env, ani_object notificationSlotObj); +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); +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); +ani_double AniGetSlotNumByBundle(ani_env *env, ani_object bundleOption); } // namespace NotificationManagerSts } // namespace OHOS #endif diff --git a/frameworks/ets/ani/include/sts_common.h b/frameworks/ets/ani/include/sts_common.h index 10a56d84a53dded1bd208b31684fd801e5a3368a..8bfe1dac6b764c903707eecd6a959dcac813a7ce 100644 --- a/frameworks/ets/ani/include/sts_common.h +++ b/frameworks/ets/ani/include/sts_common.h @@ -47,6 +47,8 @@ ani_status GetPropertyRef(ani_env *env, ani_object obj, const char *name, ani_boolean &isUndefined, ani_ref &outRef); ani_status GetPropertyStringArray(ani_env *env, ani_object param, const char *name, ani_boolean &isUndefined, std::vector &res); +ani_status GetPropertyNumberArray(ani_env *env, ani_object param, const char *name, + ani_boolean &isUndefined, std::vector &res); bool SetFieldString(ani_env *env, ani_class cls, ani_object &object, const std::string fieldName, const std::string value); diff --git a/frameworks/ets/ani/include/sts_slot.h b/frameworks/ets/ani/include/sts_slot.h index f9d311058a7829a0575d7f22b3bb515238beb825..2c4da3de6ef8f6a2398637633471154a34ceb0b2 100644 --- a/frameworks/ets/ani/include/sts_slot.h +++ b/frameworks/ets/ani/include/sts_slot.h @@ -33,6 +33,11 @@ bool SetOptionalFieldSlotLevel(ani_env *env, const ani_class cls, ani_object &ob bool WrapNotificationSlot(ani_env *env, sptr slot, ani_object &outAniObj); bool WrapNotificationSlotArray(ani_env *env, const std::vector>& slots, ani_object &outAniObj); +bool ParseNotificationSlotByBasicType(ani_env *env, ani_object notificationSlotObj, + NotificationSlot &slot); +bool UnwrapNotificationSlot(ani_env *env, ani_object notificationSlotObj, NotificationSlot &slot); +bool UnwrapNotificationSlotArrayByAniObj(ani_env *env, ani_object notificationSlotArrayObj, + std::vector &slots); } // namespace NotificationSts } // OHOS #endif \ No newline at end of file diff --git a/frameworks/ets/ani/src/manager/ani_local_live_view.cpp b/frameworks/ets/ani/src/manager/ani_local_live_view.cpp index d2b76a4ed6f59b7f8fd6ea95e3ad18af0e98f9b4..d3c4203c80ad7bc0013b2d147039627e8d24cc14 100644 --- a/frameworks/ets/ani/src/manager/ani_local_live_view.cpp +++ b/frameworks/ets/ani/src/manager/ani_local_live_view.cpp @@ -36,7 +36,7 @@ void AniTriggerSystemLiveView( return; } NotificationSts::ButtonOption buttonOption; - if (!NotificationSts::UnWarpNotificationButtonOption(env, buttonOptionsObj, buttonOption)) { + if (NotificationSts::UnWarpNotificationButtonOption(env, buttonOptionsObj, buttonOption) != ANI_OK) { OHOS::AbilityRuntime::ThrowStsError(env, OHOS::Notification::ERROR_INTERNAL_ERROR, NotificationSts::FindAnsErrMsg(OHOS::Notification::ERROR_INTERNAL_ERROR)); ANS_LOGE("AniTriggerSystemLiveView buttonOption ERROR_INTERNAL_ERROR"); diff --git a/frameworks/ets/ani/src/manager/ani_manager.cpp b/frameworks/ets/ani/src/manager/ani_manager.cpp index ed6f74a8d3d02e5f15a71414af7c1f0afda5f2cb..3d312623cee82d213b70ce0105d3bbbb6d36685d 100644 --- a/frameworks/ets/ani/src/manager/ani_manager.cpp +++ b/frameworks/ets/ani/src/manager/ani_manager.cpp @@ -80,6 +80,24 @@ static std::array kitManagerFunctions = { ":I", reinterpret_cast(AniOn)}, ani_native_function {"nativeOff", "Lstd/core/String;Lstd/core/Function1;:I", reinterpret_cast(AniOff)}, + ani_native_function {"nativeAddSlotByNotificationSlot", nullptr, + reinterpret_cast(AniAddSlotByNotificationSlot)}, + ani_native_function {"nativeAddSlotBySlotType", nullptr, + reinterpret_cast(AniAddSlotBySlotType)}, + ani_native_function {"nativeAddSlots", nullptr, + reinterpret_cast(AniAddSlots)}, + ani_native_function {"nativeGetSlot", nullptr, + reinterpret_cast(AniGetSlot)}, + ani_native_function {"nativeGetSlots", nullptr, + reinterpret_cast(AniGetSlots)}, + ani_native_function {"nativeRemoveSlot", nullptr, + reinterpret_cast(AniRemoveSlot)}, + ani_native_function {"nativeRemoveAllSlots", nullptr, + reinterpret_cast(AniRemoveAllSlots)}, + ani_native_function {"nativeSetSlotByBundle", nullptr, + reinterpret_cast(AniSetSlotByBundle)}, + ani_native_function {"nativeGetSlotNumByBundle", nullptr, + reinterpret_cast(AniGetSlotNumByBundle)}, }; void AniNotificationManagerRegistryInit(ani_env *env) diff --git a/frameworks/ets/ani/src/manager/ani_slot.cpp b/frameworks/ets/ani/src/manager/ani_slot.cpp index 86224ee2b86aa0b91b11372090831d61a39822b4..622984eeaa639f64ea470b8ac0dfe304ed882bac 100644 --- a/frameworks/ets/ani/src/manager/ani_slot.cpp +++ b/frameworks/ets/ani/src/manager/ani_slot.cpp @@ -27,11 +27,14 @@ namespace OHOS { namespace NotificationManagerSts { +namespace { +constexpr int32_t RETURN_EXCEPTION_VALUE = -1; +} // namespace ani_object AniGetSlotsByBundle(ani_env *env, ani_object bundleOption) { ANS_LOGD("sts GetSlotsByBundle enter"); - int returncode = 0; + int returncode = CJSystemapi::Notification::SUCCESS_CODE; std::vector> slots; BundleOption option; if (NotificationSts::UnwrapBundleOption(env, bundleOption, option)) { @@ -42,7 +45,7 @@ ani_object AniGetSlotsByBundle(ani_env *env, ani_object bundleOption) } int externalCode = CJSystemapi::Notification::ErrorToExternal(returncode); - if (externalCode != 0) { + if (externalCode != CJSystemapi::Notification::SUCCESS_CODE) { ANS_LOGE("sts GetSlotsByBundle error, errorCode: %{public}d", externalCode); OHOS::AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); return nullptr; @@ -56,6 +59,199 @@ ani_object AniGetSlotsByBundle(ani_env *env, ani_object bundleOption) return outAniObj; } +void AniAddSlots(ani_env *env, ani_object notificationSlotArrayObj) +{ + ANS_LOGD("AniAddSlots enter"); + std::vector slots; + if (!NotificationSts::UnwrapNotificationSlotArrayByAniObj(env, notificationSlotArrayObj, slots)) { + ANS_LOGE("UnwrapNotificationSlotArrayByAniObj failed"); + NotificationSts::ThrowStsErroWithMsg(env, "sts AddSlots ERROR_INTERNAL_ERROR"); + return; + } + int returncode = Notification::NotificationHelper::AddNotificationSlots(slots); + int externalCode = CJSystemapi::Notification::ErrorToExternal(returncode); + if (externalCode != CJSystemapi::Notification::SUCCESS_CODE) { + ANS_LOGE("AniAddSlots -> error, errorCode: %{public}d", externalCode); + AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); + return; + } + ANS_LOGD("AniAddSlots leave"); +} + +void AniAddSlotByNotificationSlot(ani_env *env, ani_object notificationSlotObj) +{ + ANS_LOGD("AniAddSlotByNotificationSlot enter"); + int returncode = CJSystemapi::Notification::SUCCESS_CODE; + Notification::NotificationSlot slot; + if (NotificationSts::UnwrapNotificationSlot(env, notificationSlotObj, slot)) { + returncode = Notification::NotificationHelper::AddNotificationSlot(slot); + } else { + NotificationSts::ThrowStsErroWithMsg(env, "sts AddSlot ERROR_INTERNAL_ERROR"); + return; + } + int externalCode = CJSystemapi::Notification::ErrorToExternal(returncode); + if (externalCode != CJSystemapi::Notification::SUCCESS_CODE) { + ANS_LOGE("sts AddSlot error, errorCode: %{public}d", externalCode); + OHOS::AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); + return; + } + ANS_LOGD("AniAddSlotByNotificationSlot leave"); +} + +void AniAddSlotBySlotType(ani_env *env, ani_enum_item enumObj) +{ + ANS_LOGD("AniAddSlotBySlotType enter"); + Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType::OTHER; + if (!NotificationSts::SlotTypeEtsToC(env, enumObj, slotType)) { + NotificationSts::ThrowStsErroWithMsg(env, "AddSlotByType ERROR_INTERNAL_ERROR"); + return; + } + int returncode = Notification::NotificationHelper::AddSlotByType(slotType); + int externalCode = CJSystemapi::Notification::ErrorToExternal(returncode); + if (externalCode != CJSystemapi::Notification::SUCCESS_CODE) { + ANS_LOGE("AniAddSlotBySlotType -> error, errorCode: %{public}d", externalCode); + AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); + } + ANS_LOGD("AniAddSlotBySlotType leave"); + return; +} + +ani_object AniGetSlot(ani_env *env, ani_enum_item enumObj) +{ + ANS_LOGD("AniGetSlot enter"); + Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType::OTHER; + if (!NotificationSts::SlotTypeEtsToC(env, enumObj, slotType)) { + ANS_LOGE("SlotTypeEtsToC failed"); + NotificationSts::ThrowStsErroWithMsg(env, "sts GetSlot ERROR_INTERNAL_ERROR"); + return nullptr; + } + sptr slot = nullptr; + int returncode = Notification::NotificationHelper::GetNotificationSlot(slotType, slot); + int externalCode = CJSystemapi::Notification::ErrorToExternal(returncode); + if (externalCode != CJSystemapi::Notification::SUCCESS_CODE) { + ANS_LOGE("AniGetSlot -> error, errorCode: %{public}d", externalCode); + AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); + } + ani_object slotObj; + if (!NotificationSts::WrapNotificationSlot(env, slot, slotObj)) { + ANS_LOGE("WrapNotificationSlot faild"); + NotificationSts::ThrowStsErroWithMsg(env, "sts GetSlot ERROR_INTERNAL_ERROR"); + return nullptr; + } + ANS_LOGD("AniGetSlot leave"); + return slotObj; +} + +ani_object AniGetSlots(ani_env *env) +{ + ANS_LOGD("AniGetSlots enter"); + std::vector> slots; + int returncode = Notification::NotificationHelper::GetNotificationSlots(slots); + int externalCode = CJSystemapi::Notification::ErrorToExternal(returncode); + if (externalCode != CJSystemapi::Notification::SUCCESS_CODE) { + ANS_LOGE("AniGetSlots -> error, errorCode: %{public}d", externalCode); + AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); + return nullptr; + } + ani_object outAniObj; + if (!NotificationSts::WrapNotificationSlotArray(env, slots, outAniObj)) { + ANS_LOGE("WrapNotificationSlotArray faild"); + NotificationSts::ThrowStsErroWithMsg(env, "AniGetSlots:failed to WrapNotificationSlotArray"); + return nullptr; + } + ANS_LOGD("AniGetSlots leave"); + return outAniObj; +} + +void AniRemoveSlot(ani_env *env, ani_enum_item enumObj) +{ + ANS_LOGD("AniRemoveSlot enter"); + Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType::OTHER; + if (!NotificationSts::SlotTypeEtsToC(env, enumObj, slotType)) { + ANS_LOGE("SlotTypeEtsToC failed"); + NotificationSts::ThrowStsErroWithMsg(env, "sts GetSlot ERROR_INTERNAL_ERROR"); + return; + } + int returncode = Notification::NotificationHelper::RemoveNotificationSlot(slotType); + int externalCode = CJSystemapi::Notification::ErrorToExternal(returncode); + if (externalCode != CJSystemapi::Notification::SUCCESS_CODE) { + ANS_LOGE("AniRemoveSlot -> error, errorCode: %{public}d", externalCode); + AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); + return; + } + ANS_LOGD("AniRemoveSlot leave"); +} + +void AniRemoveAllSlots(ani_env *env) +{ + ANS_LOGD("AniRemoveAllSlots enter"); + int returncode = Notification::NotificationHelper::RemoveAllSlots(); + int externalCode = CJSystemapi::Notification::ErrorToExternal(returncode); + if (externalCode != CJSystemapi::Notification::SUCCESS_CODE) { + ANS_LOGE("AniRemoveAllSlots -> error, errorCode: %{public}d", externalCode); + AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); + return; + } + ANS_LOGD("AniRemoveAllSlots leave"); +} + +void AniSetSlotByBundle(ani_env *env, ani_object bundleOptionObj, ani_object slotObj) +{ + ANS_LOGD("AniSetSlotByBundle enter"); + Notification::NotificationBundleOption option; + if (!NotificationSts::UnwrapBundleOption(env, bundleOptionObj, option)) { + ANS_LOGE("UnwrapBundleOption failed"); + NotificationSts::ThrowStsErroWithMsg(env, "AniSetNotificationEnableSlot ERROR_INTERNAL_ERROR"); + return; + } + + Notification::NotificationSlot slot; + if (!NotificationSts::UnwrapNotificationSlot(env, slotObj, slot)) { + ANS_LOGE("UnwrapNotificationSlot failed"); + NotificationSts::ThrowStsErroWithMsg(env, "sts SetSlotByBundle ERROR_INTERNAL_ERROR"); + return; + } + + std::vector> slotsVct; + sptr slotPtr = new (std::nothrow) Notification::NotificationSlot(slot); + if (slotPtr == nullptr) { + ANS_LOGE("Failed to create NotificationSlot ptr"); + NotificationSts::ThrowStsErroWithMsg(env, "sts AniSetSlotByBundle ERROR_INTERNAL_ERROR"); + return; + } + slotsVct.emplace_back(slotPtr); + + int returncode = Notification::NotificationHelper::UpdateNotificationSlots(option, slotsVct); + int externalCode = CJSystemapi::Notification::ErrorToExternal(returncode); + if (externalCode != CJSystemapi::Notification::SUCCESS_CODE) { + ANS_LOGE("sts SetSlotByBundle error, errorCode: %{public}d", externalCode); + OHOS::AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); + return; + } + ANS_LOGD("AniSetSlotByBundle leave"); +} + +ani_double AniGetSlotNumByBundle(ani_env *env, ani_object bundleOption) +{ + ANS_LOGD("AniGetSlotNumByBundle enter"); + Notification::NotificationBundleOption option; + if (!NotificationSts::UnwrapBundleOption(env, bundleOption, option)) { + ANS_LOGE("UnwrapBundleOption failed"); + NotificationSts::ThrowStsErroWithMsg(env, "AniGetSlotNumByBundle ERROR_INTERNAL_ERROR"); + return RETURN_EXCEPTION_VALUE; + } + uint64_t num = 0; + int returncode = Notification::NotificationHelper::GetNotificationSlotNumAsBundle(option, num); + int externalCode = CJSystemapi::Notification::ErrorToExternal(returncode); + if (externalCode != CJSystemapi::Notification::SUCCESS_CODE) { + ANS_LOGE("sts GetSlotNumByBundle error, errorCode: %{public}d", externalCode); + OHOS::AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); + return RETURN_EXCEPTION_VALUE; + } + ani_double retNum = static_cast(num); + ANS_LOGD("AniGetSlotNumByBundle leave"); + return retNum; +} void AniSetNotificationEnableSlot(ani_env *env, ani_object bundleOption, ani_enum_item type, ani_boolean enable) { ANS_LOGD("AniSetNotificationEnableSlot enter "); @@ -75,7 +271,7 @@ void AniSetNotificationEnableSlot(ani_env *env, ani_object bundleOption, ani_enu NotificationSts::AniBooleanToBool(enable), isForceControl); int externalCode = CJSystemapi::Notification::ErrorToExternal(returncode); - if (externalCode != 0) { + if (externalCode != CJSystemapi::Notification::SUCCESS_CODE) { ANS_LOGE("AniSetNotificationEnableSlot error, errorCode: %{public}d", externalCode); AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); return; @@ -100,7 +296,7 @@ void AniSetNotificationEnableSlotWithForce(ani_env *env, NotificationSts::AniBooleanToBool(enable), NotificationSts::AniBooleanToBool(isForceControl)); int externalCode = CJSystemapi::Notification::ErrorToExternal(returncode); - if (externalCode != 0) { + if (externalCode != CJSystemapi::Notification::SUCCESS_CODE) { ANS_LOGE("AniSetNotificationEnableSlotSync error, errorCode: %{public}d", externalCode); AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); } @@ -120,7 +316,7 @@ ani_boolean AniIsNotificationSlotEnabled(ani_env *env, ani_object bundleOption, bool isEnable = false; int returncode = Notification::NotificationHelper::GetEnabledForBundleSlot(option, slotType, isEnable); int externalCode = CJSystemapi::Notification::ErrorToExternal(returncode); - if (externalCode != 0) { + if (externalCode != CJSystemapi::Notification::SUCCESS_CODE) { ANS_LOGE("IsNotificationSlotEnabled -> error, errorCode: %{public}d", externalCode); AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); } @@ -138,7 +334,7 @@ ani_int AniGetSlotFlagsByBundle(ani_env *env, ani_object obj) uint32_t slotFlags = 0; int returncode = Notification::NotificationHelper::GetNotificationSlotFlagsAsBundle(option, slotFlags); int externalCode = CJSystemapi::Notification::ErrorToExternal(returncode); - if (externalCode != 0) { + if (externalCode != CJSystemapi::Notification::SUCCESS_CODE) { ANS_LOGE("AniGetSlotFlagsByBundle -> error, errorCode: %{public}d", externalCode); AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); } @@ -156,7 +352,7 @@ void AniSetSlotFlagsByBundle(ani_env *env, ani_object obj, ani_double slotFlags) int returncode = Notification::NotificationHelper::SetNotificationSlotFlagsAsBundle(option, static_cast(slotFlags)); int externalCode = CJSystemapi::Notification::ErrorToExternal(returncode); - if (externalCode != 0) { + if (externalCode != CJSystemapi::Notification::SUCCESS_CODE) { ANS_LOGE("AniSetSlotFlagsByBundle -> error, errorCode: %{public}d", externalCode); AbilityRuntime::ThrowStsError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); } diff --git a/frameworks/ets/ani/src/sts_common.cpp b/frameworks/ets/ani/src/sts_common.cpp index f8f25b2f7cb0d9f98ea784b296bd24d1fce5431d..253a124d353794022df96c1c836c9e686e117265 100644 --- a/frameworks/ets/ani/src/sts_common.cpp +++ b/frameworks/ets/ani/src/sts_common.cpp @@ -255,6 +255,44 @@ ani_status GetPropertyStringArray(ani_env *env, ani_object param, const char *na return status; } +ani_status GetPropertyNumberArray(ani_env *env, ani_object param, const char *name, + ani_boolean &isUndefined, std::vector &res) +{ + ANS_LOGD("GetPropertyNumberArray enter"); + ani_ref arrayObj = nullptr; + ani_status status; + ani_double length; + if ((status = GetPropertyRef(env, param, name, isUndefined, arrayObj)) != ANI_OK || isUndefined == ANI_TRUE) { + return ANI_INVALID_ARGS; + } + + status = env->Object_GetPropertyByName_Double(static_cast(arrayObj), "length", &length); + if (status != ANI_OK) { + ANS_LOGI("status : %{public}d", status); + return status; + } + + for (int i = 0; i < static_cast(length); i++) { + ani_ref numEntryRef; + status = env->Object_CallMethodByName_Ref(static_cast(arrayObj), + "$_get", "I:Lstd/core/Object;", &numEntryRef, (ani_int)i); + if (status != ANI_OK) { + ANS_LOGI("status : %{public}d, index: %{public}d", status, i); + return status; + } + ani_double doubleValue = 0.0; + status = env->Object_CallMethodByName_Double(static_cast(numEntryRef), "unboxed", + ":D", &doubleValue); + if (status != ANI_OK) { + ANS_LOGI("Object_CallMethodByName_Double uid fail, status: %{public}d", status); + return status; + } + res.push_back(static_cast(doubleValue)); + } + ANS_LOGD("GetPropertyNumberArray leave"); + return status; +} + ani_object GetAniStringArrayByVectorString(ani_env *env, std::vector &strs) { if (env == nullptr || strs.empty()) { diff --git a/frameworks/ets/ani/src/sts_slot.cpp b/frameworks/ets/ani/src/sts_slot.cpp index 69bd7e210ee55608dfbb5fdb9e89b005dc9ad1ee..7202f92ab569bb3220e2236ba9955977a47c2d7d 100644 --- a/frameworks/ets/ani/src/sts_slot.cpp +++ b/frameworks/ets/ani/src/sts_slot.cpp @@ -224,5 +224,120 @@ bool WrapNotificationSlotArray(ani_env *env, const std::vector(doubleValue))); + } + if (GetPropertyDouble(env, notificationSlotObj, "lightColor", isUndefined, doubleValue) == ANI_OK + && isUndefined == ANI_FALSE) { + slot.SetLedLightColor(static_cast(doubleValue)); + } + bool boolValue = true; + if (GetPropertyBool(env, notificationSlotObj, "badgeFlag", isUndefined, boolValue) == ANI_OK + && isUndefined == ANI_FALSE) { + slot.EnableBadge(boolValue); + } + if (GetPropertyBool(env, notificationSlotObj, "bypassDnd", isUndefined, boolValue) == ANI_OK + && isUndefined == ANI_FALSE) { + slot.EnableBypassDnd(boolValue); + } + if (GetPropertyBool(env, notificationSlotObj, "lightEnabled", isUndefined, boolValue) == ANI_OK + && isUndefined == ANI_FALSE) { + slot.SetEnableLight(boolValue); + } + if (GetPropertyBool(env, notificationSlotObj, "vibrationEnabled", isUndefined, boolValue) == ANI_OK + && isUndefined == ANI_FALSE) { + slot.SetEnableVibration(boolValue); + } + return true; +} + +bool UnwrapNotificationSlot(ani_env *env, ani_object notificationSlotObj, NotificationSlot &slot) +{ + ANS_LOGD("UnwrapNotificationSlot enter"); + if (notificationSlotObj == nullptr) { + ANS_LOGE("notificationSlotObj is null"); + return false; + } + ani_status status = ANI_ERROR; + ani_boolean isUndefined = ANI_TRUE; + ani_ref notificationTypeRef = {}; + status = GetPropertyRef(env, notificationSlotObj, "notificationType", isUndefined, notificationTypeRef); + if (status == ANI_OK && isUndefined == ANI_FALSE) { + Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType::OTHER; + if (SlotTypeEtsToC(env, static_cast(notificationTypeRef), slotType)) { + slot.SetType(slotType); + } + } + status = GetPropertyRef(env, notificationSlotObj, "notificationLevel", isUndefined, notificationTypeRef); + if (status == ANI_OK && isUndefined == ANI_FALSE) { + NotificationSlot::NotificationLevel outLevel {NotificationSlot::NotificationLevel::LEVEL_NONE}; + if (SlotLevelEtsToC(env, static_cast(notificationTypeRef), outLevel)) { + slot.SetLevel(outLevel); + } + } + if (!ParseNotificationSlotByBasicType(env, notificationSlotObj, slot)) { + ANS_LOGE("ParseNotificationSlotByBasicType failed"); + return false; + } + std::vector vibrationValues; + if (GetPropertyNumberArray(env, notificationSlotObj, "vibrationValues", isUndefined, vibrationValues) == ANI_OK && + isUndefined == ANI_FALSE) { + slot.SetVibrationStyle(vibrationValues); + } + ANS_LOGD("UnwrapNotificationSlot leave"); + return true; +} + +bool UnwrapNotificationSlotArrayByAniObj(ani_env *env, ani_object notificationSlotArrayObj, + std::vector &slots) +{ + ANS_LOGD("UnwrapNotificationSlotArrayByAniObj enter"); + if (notificationSlotArrayObj == nullptr) { + ANS_LOGE("notificationSlotArrayObj is null"); + return false; + } + ani_double length; + ani_status status = env->Object_GetPropertyByName_Double(notificationSlotArrayObj, "length", &length); + if (status != ANI_OK) { + ANS_LOGE("Object_GetPropertyByName_Double faild. status : %{public}d", status); + return false; + } + for (int i = 0; i < int(length); i++) { + ani_ref notificationSlotEntryRef; + status = env->Object_CallMethodByName_Ref(notificationSlotArrayObj, + "$_get", "I:Lstd/core/Object;", ¬ificationSlotEntryRef, (ani_int)i); + if (status != ANI_OK) { + ANS_LOGE("Object_CallMethodByName_Ref faild. status : %{public}d", status); + } + NotificationSlot slot; + if (!UnwrapNotificationSlot(env, static_cast(notificationSlotEntryRef), slot)) { + ANS_LOGE("UnwrapNotificationSlot faild"); + return false; + } + slots.emplace_back(slot); + } + ANS_LOGD("UnwrapNotificationSlotArrayByAniObj leave"); + return true; +} } // namespace NotificationSts } // OHOS diff --git a/frameworks/ets/ani/src/subscribe/ani_remove.cpp b/frameworks/ets/ani/src/subscribe/ani_remove.cpp index 7f04ae1091d5e27e4efb9353cb2dfcaf6b4df557..be660d9fb26be701c01aac5d7bf653b9429f0e40 100644 --- a/frameworks/ets/ani/src/subscribe/ani_remove.cpp +++ b/frameworks/ets/ani/src/subscribe/ani_remove.cpp @@ -104,7 +104,7 @@ void AniRemoveForHashCodes(ani_env *env, ani_object hashCodes, ani_object reason ANS_LOGD("StsRemoveForHashCodes enter"); std::vector hashCodesStd; int32_t reasonType; - if (ANI_OK != NotificationSts::GetStringArrayByAniObj(env, hashCodes, hashCodesStd)) { + if (!NotificationSts::GetStringArrayByAniObj(env, hashCodes, hashCodesStd)) { ANS_LOGE("hashCodes is valid"); std::string msg = "hashCodes is valid"; OHOS::AbilityRuntime::ThrowStsError(env, ERROR_PARAM_INVALID, msg); diff --git a/frameworks/ets/ets/@ohos.notificationManager.ets b/frameworks/ets/ets/@ohos.notificationManager.ets index 7dcb4589e447c25129cef4777c099a865c6d7054..88c88458fc95a900a6ad9ca37dbad4ca24ff4019 100644 --- a/frameworks/ets/ets/@ohos.notificationManager.ets +++ b/frameworks/ets/ets/@ohos.notificationManager.ets @@ -160,6 +160,45 @@ export default namespace notificationManager { export native function nativeIsNotificationEnabledWithBundleOption(bundleOption: BundleOption): boolean; export native function nativeSetNotificationEnable(bundle: BundleOption, enable: boolean): void; export native function nativeRequestEnableNotification(content: UIAbilityContext): Promise; + + export native function nativeAddSlotByNotificationSlot(slot: NotificationSlot): void; + export native function nativeAddSlotBySlotType(type: SlotType): void; + export native function nativeAddSlots(slots: Array): void; + export native function nativeGetSlot(slotType: SlotType): NotificationSlot; + export native function nativeGetSlots(): Array; + export native function nativeRemoveSlot(slotType: SlotType): void; + export native function nativeRemoveAllSlots(): void; + export native function nativeSetSlotByBundle(bundle: BundleOption, slot: NotificationSlot): void; + export native function nativeGetSlotNumByBundle(bundle: BundleOption): number; + + function isInvalidParameter(slot: NotificationSlot): BusinessError + { + if (slot == null) { + return errorParamInvalid; + } + if (slot.lockscreenVisibility !== undefined && typeof slot.lockscreenVisibility === 'number') { + const num = slot.lockscreenVisibility ?? -1; + if (num < 0) { + return errorParamInvalid; + } + } + if (slot.lightColor !== undefined && typeof slot.lightColor === 'number') { + const num = slot.lightColor ?? -1; + if (num < 0) { + return errorParamInvalid; + } + } + if (slot.desc !== undefined && slot.desc?.trim() === '') { + return errorParamInvalid; + } + if (slot.sound !== undefined && slot.sound?.trim() === '') { + return errorParamInvalid; + } + if (slot.vibrationValues !== undefined && slot.vibrationValues?.length === 0) { + return errorParamInvalid; + } + return successCallbackError; + } export native function nativeOn( type: 'checkNotification', callback: CallbackForCheckInfo, checkRequest?: NotificationCheckRequest): int; @@ -505,6 +544,287 @@ export default namespace notificationManager { } return error; } + + export function getSlotNumByBundle(bundle: BundleOption): Promise { + let error: BusinessError = isInvalidParameter(bundle); + if (error.code !== 0) { + throw error; + } + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): number => { return nativeGetSlotNumByBundle(bundle); }); + p.then((data: NullishType): void => { + let ret : number = data as number; + resolve(ret); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } + + export function getSlotNumByBundle(bundle: BundleOption, callback: AsyncCallback): void { + let error: BusinessError = isInvalidParameter(bundle); + if (error.code !== 0) { + throw error; + } + if (callback == null) { + throw errorParamInvalid; + } + let p = taskpool.execute((): number => { return nativeGetSlotNumByBundle(bundle); }); + p.then((data: NullishType): void => { + let ret : number = data as number; + let err: BusinessError = {code: 0, data: undefined}; + callback(err, ret); + }, (error: Object): void => { + let ret: number = -1; + let err: BusinessError = error as BusinessError; + callback(err, ret); + }); + } + + export function setSlotByBundle(bundle: BundleOption, slot: NotificationSlot, callback: AsyncCallback): void { + let error: BusinessError = isInvalidParameter(bundle); + if (error.code !== 0) { + throw error; + } + let slotError: BusinessError = isInvalidParameter(slot); + if (slotError.code !== 0) { + throw slotError; + } + if (callback == null) { + throw errorParamInvalid; + } + let p = taskpool.execute((): void => { return nativeSetSlotByBundle(bundle, slot); }); + p.then((data: NullishType): void => { + let err: BusinessError = {code: 0, data: undefined}; + callback(err, undefined); + }, (error: Object): void => { + let err: BusinessError = error as BusinessError; + callback(err, undefined); + }); + } + + export function setSlotByBundle(bundle: BundleOption, slot: NotificationSlot): Promise { + let error: BusinessError = isInvalidParameter(bundle); + if (error.code !== 0) { + throw error; + } + let slotError: BusinessError = isInvalidParameter(slot); + if (slotError.code !== 0) { + throw slotError; + } + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): void => { return nativeSetSlotByBundle(bundle, slot); }); + p.then((data: NullishType): void => { + resolve(undefined); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } + + export function removeAllSlots(callback: AsyncCallback): void { + if (callback == null) { + throw errorParamInvalid; + } + let p = taskpool.execute((): void => { return nativeRemoveAllSlots(); }); + p.then((data: NullishType): void => { + let err: BusinessError = {code: 0, data: undefined}; + callback(err, undefined); + }, (error: Object): void => { + let err: BusinessError = error as BusinessError; + callback(err, undefined); + }); + } + + export function removeAllSlots(): Promise { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): void => { return nativeRemoveAllSlots(); }); + p.then((data: NullishType): void => { + resolve(undefined); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } + + export function removeSlot(slotType: SlotType, callback: AsyncCallback): void { + if (callback == null) { + throw errorParamInvalid; + } + let p = taskpool.execute((): void => { return nativeRemoveSlot(slotType); }); + p.then((data: NullishType): void => { + let err: BusinessError = {code: 0, data: undefined}; + callback(err, undefined); + }, (error: Object): void => { + let err: BusinessError = error as BusinessError; + callback(err, undefined); + }); + } + + export function removeSlot(slotType: SlotType): Promise { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): void => { return nativeRemoveSlot(slotType); }); + p.then((data: NullishType): void => { + resolve(undefined); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } + + export function getSlots(): Promise> { + let pPromise = new Promise>((resolve: ResolveCallback>, reject: RejectCallback): void => { + let p = taskpool.execute((): Array => { return nativeGetSlots(); }); + p.then((data: NullishType): void => { + let ret : Array = data as Array; + resolve(ret); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } + + export function getSlots(callback: AsyncCallback>): void { + if (callback == null) { + throw errorParamInvalid; + } + let p = taskpool.execute((): Array => { return nativeGetSlots(); }); + p.then((data: NullishType): void => { + let ret : Array = data as Array; + let err: BusinessError = {code: 0, data: undefined}; + callback(err, ret); + }, (error: Object): void => { + let ret : Array = []; + let err: BusinessError = error as BusinessError; + callback(err, ret); + }); + } + + export function getSlot(slotType: SlotType): Promise { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): NotificationSlot => { return nativeGetSlot(slotType); }); + p.then((data: NullishType): void => { + let ret : NotificationSlot = data as NotificationSlot; + resolve(ret); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } + + export function getSlot(slotType: SlotType, callback: AsyncCallback): void { + if (callback == null) { + throw errorParamInvalid; + } + let p = taskpool.execute((): NotificationSlot => { return nativeGetSlot(slotType); }); + p.then((data: NullishType): void => { + let ret : NotificationSlot = data as NotificationSlot; + let err: BusinessError = {code: 0, data: undefined}; + callback(err, ret); + }, (error: Object): void => { + let ret : NotificationSlot = { enabled: false, reminderMode: 0, authorizedStatus: 0 } as NotificationSlot; + let err: BusinessError = error as BusinessError; + callback(err, ret); + }); + } + + export function addSlots(slots: Array): Promise { + if (!slots || slots.length === 0) { + throw errorParamInvalid; + } + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): void => { return nativeAddSlots(slots); }); + p.then((data: NullishType): void => { + resolve(undefined); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } + + export function addSlots(slots: Array, callback: AsyncCallback): void { + if (!slots || slots.length === 0) { + throw errorParamInvalid; + } + if (callback == null) { + throw errorParamInvalid; + } + let p = taskpool.execute((): void => { return nativeAddSlots(slots); }); + p.then((data: NullishType): void => { + let err: BusinessError = {code: 0, data: undefined}; + callback(err, undefined); + }, (error: Object): void => { + let err: BusinessError = error as BusinessError; + callback(err, undefined); + }); + } + + export function addSlot(slot: NotificationSlot): Promise { + let slotError: BusinessError = isInvalidParameter(slot); + if (slotError.code !== 0) { + throw slotError; + } + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): void => { return nativeAddSlotByNotificationSlot(slot); }); + p.then((data: NullishType): void => { + resolve(undefined); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } + + export function addSlot(slot: NotificationSlot, callback: AsyncCallback): void { + let slotError: BusinessError = isInvalidParameter(slot); + if (slotError.code !== 0) { + throw slotError; + } + if (callback == null) { + throw errorParamInvalid; + } + let p = taskpool.execute((): void => { return nativeAddSlotByNotificationSlot(slot); }); + p.then((data: NullishType): void => { + let err: BusinessError = {code: 0, data: undefined}; + callback(err, undefined); + }, (error: Object): void => { + let err: BusinessError = error as BusinessError; + callback(err, undefined); + }); + } + + export function addSlot(type: SlotType, callback: AsyncCallback): void { + if (callback == null) { + throw errorParamInvalid; + } + let p = taskpool.execute((): void => { return nativeAddSlotBySlotType(type); }); + p.then((data: NullishType): void => { + let err: BusinessError = {code: 0, data: undefined}; + callback(err, undefined); + }, (error: Object): void => { + let err: BusinessError = error as BusinessError; + callback(err, undefined); + }); + } + + export function addSlot(type: SlotType): Promise { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): void => { return nativeAddSlotBySlotType(type); }); + p.then((data: NullishType): void => { + resolve(undefined); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } export function setNotificationEnable(bundle: BundleOption, enable: boolean): Promise { @@ -590,11 +910,7 @@ export default namespace notificationManager { export function isNotificationEnabled(userId: number, callback: AsyncCallback): void { if (callback == null) { - let error: BusinessError = { - code: 410, - message : "callback must be not null" - } - throw error; + throw errorParamInvalid; } let p = taskpool.execute((): boolean => { return nativeIsNotificationEnabledWithId(userId); }); p.then((data: NullishType): void => { @@ -625,11 +941,7 @@ export default namespace notificationManager { export function isNotificationEnabled(callback: AsyncCallback): void { if (callback == null) { - let error: BusinessError = { - code: 410, - message : "callback must be not null" - } - throw error; + throw errorParamInvalid; } let p = taskpool.execute((): boolean => { return nativeIsNotificationEnabled(); }); p.then((data: NullishType): void => { @@ -1192,11 +1504,7 @@ export default namespace notificationManager { throw error; } if (callback == null) { - let error: BusinessError = { - code: 410, - message : "callback must be not null" - } - throw error; + throw errorParamInvalid; } let p = taskpool.execute((): void => { return nativeCancelWithIdLabel(id, label); }); p.then((data: NullishType): void => {