From 37815fe98e26c0b04d3e9cc96944f74fb247068c Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Mon, 14 Jul 2025 21:37:45 +0800 Subject: [PATCH 1/3] bug fix for ErrorCode Signed-off-by: Michael Wang --- frameworks/ets/ani/include/sts_throw_erro.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/frameworks/ets/ani/include/sts_throw_erro.h b/frameworks/ets/ani/include/sts_throw_erro.h index 8555656db..cd72e4c74 100644 --- a/frameworks/ets/ani/include/sts_throw_erro.h +++ b/frameworks/ets/ani/include/sts_throw_erro.h @@ -100,8 +100,11 @@ static const std::vector> ERRORS_CONVERT = { inline int32_t GetExternalCode(uint32_t errCode) { - int32_t ExternalCode = ERR_OK; + int32_t ExternalCode = ERROR_INTERNAL_ERROR; for (const auto &errorConvert : ERRORS_CONVERT) { + if (errCode == errorConvert.second) { + return errCode; + } if (errCode == errorConvert.first) { ExternalCode = errorConvert.second; break; -- Gitee From 30450a1cc23e70b40ddb6746cafdbd2f3fbc1ae4 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Tue, 15 Jul 2025 20:29:59 +0800 Subject: [PATCH 2/3] fixed bugs of GetDoNotDisturbDate Signed-off-by: Michael Wang --- frameworks/ets/ani/include/sts_throw_erro.h | 9 +-- .../src/manager/ani_do_not_disturb_date.cpp | 19 ++---- frameworks/ets/ani/src/sts_common.cpp | 60 ++++++++++++------- .../ets/ani/src/sts_notification_manager.cpp | 22 +++++-- 4 files changed, 64 insertions(+), 46 deletions(-) diff --git a/frameworks/ets/ani/include/sts_throw_erro.h b/frameworks/ets/ani/include/sts_throw_erro.h index cd72e4c74..2b2d0738e 100644 --- a/frameworks/ets/ani/include/sts_throw_erro.h +++ b/frameworks/ets/ani/include/sts_throw_erro.h @@ -100,19 +100,16 @@ static const std::vector> ERRORS_CONVERT = { inline int32_t GetExternalCode(uint32_t errCode) { - int32_t ExternalCode = ERROR_INTERNAL_ERROR; for (const auto &errorConvert : ERRORS_CONVERT) { if (errCode == errorConvert.second) { return errCode; } if (errCode == errorConvert.first) { - ExternalCode = errorConvert.second; - break; + return errorConvert.second; } } - ANS_LOGI("internal errorCode[%{public}u] to external errorCode[%{public}d]", errCode, ExternalCode); - return ExternalCode; -}; + return ERROR_INTERNAL_ERROR; +} inline std::string FindAnsErrMsg(const int32_t errCode) { diff --git a/frameworks/ets/ani/src/manager/ani_do_not_disturb_date.cpp b/frameworks/ets/ani/src/manager/ani_do_not_disturb_date.cpp index 80e90419c..233bd5d94 100644 --- a/frameworks/ets/ani/src/manager/ani_do_not_disturb_date.cpp +++ b/frameworks/ets/ani/src/manager/ani_do_not_disturb_date.cpp @@ -28,7 +28,7 @@ void AniSetDoNotDisturbDate(ani_env *env, ani_object date) { ANS_LOGD("AniSetDoNotDisturbDate enter"); Notification::NotificationDoNotDisturbDate doNotDisturbDate; - if (NotificationSts::UnWarpNotificationDoNotDisturbDate(env, date, doNotDisturbDate)) { + if (!NotificationSts::UnWarpNotificationDoNotDisturbDate(env, date, doNotDisturbDate)) { ANS_LOGE("AniSetDoNotDisturbDate UnWarpNotificationDoNotDisturbDate ERROR_INTERNAL_ERROR"); NotificationSts::ThrowStsErroWithMsg(env, "UnWarpNotificationDoNotDisturbDate ERROR_INTERNAL_ERROR"); return; @@ -48,12 +48,11 @@ void AniSetDoNotDisturbDateWithId(ani_env *env, ani_object date, ani_double user { ANS_LOGD("AniSetDoNotDisturbDateWithId enter"); Notification::NotificationDoNotDisturbDate doNotDisturbDate; - if (NotificationSts::UnWarpNotificationDoNotDisturbDate(env, date, doNotDisturbDate)) { + if (!NotificationSts::UnWarpNotificationDoNotDisturbDate(env, date, doNotDisturbDate)) { ANS_LOGE("AniSetDoNotDisturbDateWithId UnWarpNotificationDoNotDisturbDate ERROR_INTERNAL_ERROR"); NotificationSts::ThrowStsErroWithMsg(env, "UnWarpNotificationDoNotDisturbDate ERROR_INTERNAL_ERROR"); return; } - const int32_t id = static_cast(userId); int returncode = Notification::NotificationHelper::SetDoNotDisturbDate(id, doNotDisturbDate); if (returncode != ERR_OK) { @@ -62,7 +61,6 @@ void AniSetDoNotDisturbDateWithId(ani_env *env, ani_object date, ani_double user returncode, externalCode); OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); } - ANS_LOGD("AniSetDoNotDisturbDateWithId end"); } @@ -70,7 +68,6 @@ ani_object AniGetDoNotDisturbDate(ani_env *env) { ani_object data = nullptr; Notification::NotificationDoNotDisturbDate doNotDisturbDate; - ANS_LOGD("AniGetDoNotDisturbDate enter"); int returncode = Notification::NotificationHelper::GetDoNotDisturbDate(doNotDisturbDate); if (returncode != ERR_OK) { @@ -80,13 +77,11 @@ ani_object AniGetDoNotDisturbDate(ani_env *env) OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); return data; } - auto datePtr = std::make_shared(doNotDisturbDate); - if (NotificationSts::WarpNotificationDoNotDisturbDate(env, datePtr, data) == false) { + if (!NotificationSts::WarpNotificationDoNotDisturbDate(env, datePtr, data) == false) { ANS_LOGE("WarpNotificationDoNotDisturbDate faild"); NotificationSts::ThrowStsErroWithMsg(env, "AniGetDoNotDisturbDate ERROR_INTERNAL_ERROR"); } - ANS_LOGD("AniGetDoNotDisturbDate end"); return data; } @@ -95,9 +90,7 @@ ani_object AniGetDoNotDisturbDateWithId(ani_env *env, ani_double userId) { ani_object data = nullptr; Notification::NotificationDoNotDisturbDate doNotDisturbDate; - ANS_LOGD("AniGetDoNotDisturbDateWithId enter"); - const int32_t id = static_cast(userId); int returncode = Notification::NotificationHelper::GetDoNotDisturbDate(id, doNotDisturbDate); if (returncode != ERR_OK) { @@ -107,12 +100,11 @@ ani_object AniGetDoNotDisturbDateWithId(ani_env *env, ani_double userId) OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); return data; } - auto datePtr = std::make_shared(doNotDisturbDate); - if (NotificationSts::WarpNotificationDoNotDisturbDate(env, datePtr, data) == false) { + if (!NotificationSts::WarpNotificationDoNotDisturbDate(env, datePtr, data) == false) { ANS_LOGE("AniGetDoNotDisturbDateWithId WarpNotificationDoNotDisturbDate faild"); + NotificationSts::ThrowStsErroWithMsg(env, "AniGetDoNotDisturbDate ERROR_INTERNAL_ERROR"); } - ANS_LOGD("AniGetDoNotDisturbDateWithId end"); return data; } @@ -129,7 +121,6 @@ ani_boolean AniIsSupportDoNotDisturbMode(ani_env *env) OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); return ANI_FALSE; } - ANS_LOGD("DoesSupportDoNotDisturbMode returncode: %{public}d", supportDoNotDisturbMode); ANS_LOGD("AniIsSupportDoNotDisturbMode end"); return NotificationSts::BoolToAniBoolean(supportDoNotDisturbMode); } diff --git a/frameworks/ets/ani/src/sts_common.cpp b/frameworks/ets/ani/src/sts_common.cpp index f0002ca40..13860f729 100644 --- a/frameworks/ets/ani/src/sts_common.cpp +++ b/frameworks/ets/ani/src/sts_common.cpp @@ -34,7 +34,7 @@ bool IsUndefine(ani_env *env, const ani_object &obj) return true; } ani_boolean isUndefined; - if (ANI_OK != env->Reference_IsUndefined(obj, &isUndefined)) { + if (env->Reference_IsUndefined(obj, &isUndefined) != ANI_OK) { ANS_LOGE("Reference_IsUndefined faild"); return true; } @@ -98,7 +98,7 @@ bool GetStringArrayByAniObj(ani_env *env, const ani_object ani_obj, std::vector< return false; } std::string std_string; - if (ANI_OK != (status = GetStringByAniString(env, static_cast(stringEntryRef), std_string))) { + if ((status = GetStringByAniString(env, static_cast(stringEntryRef), std_string)) != ANI_OK) { ANS_LOGE("GetStdString faild. status %{public}d", status); return false; } @@ -489,17 +489,17 @@ ani_object newArrayClass(ani_env *env, int length) return nullptr; } ani_class arrayCls = nullptr; - if (ANI_OK != env->FindClass("Lescompat/Array;", &arrayCls)) { + if (env->FindClass("Lescompat/Array;", &arrayCls) != ANI_OK) { ANS_LOGE("FindClass Lescompat/Array; Failed"); return nullptr; } ani_method arrayCtor; - if (ANI_OK != env->Class_FindMethod(arrayCls, "", "I:V", &arrayCtor)) { + if (env->Class_FindMethod(arrayCls, "", "I:V", &arrayCtor) != ANI_OK) { ANS_LOGE("Class_FindMethod Failed"); return nullptr; } ani_object arrayObj = nullptr; - if (ANI_OK != env->Object_New(arrayCls, arrayCtor, &arrayObj, length)) { + if (env->Object_New(arrayCls, arrayCtor, &arrayObj, length) != ANI_OK) { ANS_LOGE("Object_New Array Faild"); return nullptr; } @@ -516,17 +516,17 @@ ani_object newRecordClass(ani_env *env) } ani_status status = ANI_ERROR; ani_class recordCls; - if (ANI_OK != (status = env->FindClass("Lescompat/Record;", &recordCls))) { + if ((status = env->FindClass("Lescompat/Record;", &recordCls)) != ANI_OK) { ANS_LOGE("newRecordClass fail, FindClass status = %{public}d", status); return nullptr; } ani_method ctor; - if (ANI_OK != (status = env->Class_FindMethod(recordCls, "", nullptr, &ctor))) { + if ((status = env->Class_FindMethod(recordCls, "", nullptr, &ctor)) != ANI_OK) { ANS_LOGE("newRecordClass fail, Class_FindMethod status = %{public}d", status); return nullptr; } ani_object recordObj = {}; - if (ANI_OK != (status = env->Object_New(recordCls, ctor, &recordObj))) { + if ((status = env->Object_New(recordCls, ctor, &recordObj)) != ANI_OK) { ANS_LOGE("newRecordClass fail, Object_New status = %{public}d", status); return nullptr; } @@ -591,17 +591,17 @@ bool CreateClassObjByClassName(ani_env *env, const char *className, ani_class &c ANS_LOGE("CreateClassObjByClassName fail, has nullptr"); return false; } - if (ANI_OK != env->FindClass(className, &cls)) { + if (env->FindClass(className, &cls) != ANI_OK) { ANS_LOGE("FindClass fail"); return false; } ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", nullptr, &ctor)) { + if (env->Class_FindMethod(cls, "", nullptr, &ctor) != ANI_OK) { ANS_LOGE("FindMethod fail"); return false; } outAniObj = {}; - if (ANI_OK != env->Object_New(cls, ctor, &outAniObj)) { + if (env->Object_New(cls, ctor, &outAniObj) != ANI_OK) { ANS_LOGE("Object_New fail"); return false; } @@ -616,24 +616,44 @@ bool CreateDate(ani_env *env, int64_t time, ani_object &outObj) } ani_class cls; ani_status status; - if (ANI_OK != (status = env->FindClass("Lescompat/Date;", &cls))) { + if ((status = env->FindClass("Lescompat/Date;", &cls)) != ANI_OK) { ANS_LOGD("error. not find class name 'Lescompat/Date;'. status %{public}d", status); return false; } ani_method ctor; - if (ANI_OK != (status = env->Class_FindMethod(cls, "", "Lstd/core/Object;:V", &ctor))) { + if ((status = env->Class_FindMethod(cls, "", ":V", &ctor)) != ANI_OK) { ANS_LOGD("error. not find method name ''. status %{public}d", status); return false; } - ani_object timeObj = CreateDouble(env, static_cast(time)); - if (timeObj == nullptr) { - ANS_LOGD("createDouble faild"); + if ((status = env->Object_New(cls, ctor, &outObj)) != ANI_OK) { + ANS_LOGD("Object_New faild. status %{public}d", status); return false; } - if (ANI_OK != (status = env->Object_New(cls, ctor, &outObj, timeObj))) { - ANS_LOGD("Object_New faild. status %{public}d", status); + ani_double msObj = 0; + if ((status = env->Object_CallMethodByName_Double(outObj, "setTime", "D:D", &msObj, static_cast(time))) + != ANI_OK) { + ANS_LOGD("Object_CallMethodByName_Double setDate faild. status %{public}d", status); + return false; + } + return true; +} + +bool GetDateByObject(ani_env *env, ani_object timeObj, int64_t &time) +{ + ANS_LOGD("GetDateByObject call"); + if (env == nullptr || timeObj == nullptr) { + ANS_LOGE("GetDateByObject fail, env or timeObj is nullptr"); + return false; + } + + ani_status status; + ani_double timeMsObj = 0; + if ((status = env->Object_CallMethodByName_Double(timeObj, "getTime", ":D", &timeMsObj)) != ANI_OK) { + ANS_LOGD("Object_CallMethodByName_Double faild. status %{public}d", status); return false; } + time = static_cast(timeMsObj); + ANS_LOGD("GetDateByObject end"); return true; } @@ -697,7 +717,7 @@ bool SetPropertyOptionalByString(ani_env *env, ani_object &object, const char *n } ani_string stringObj; ani_status status = ANI_OK; - if (ANI_OK != (status = GetAniStringByString(env, value, stringObj))) { + if ((status = GetAniStringByString(env, value, stringObj)) != ANI_OK) { ANS_LOGE("GetAniStringByString faild. status %{public}d", status); return false; } @@ -731,7 +751,7 @@ bool SetPropertyByRef(ani_env *env, ani_object &object, const char *name, ani_re ANS_LOGE("The parameter is invalid."); return false; } - if (ANI_OK != (status = env->Object_SetPropertyByName_Ref(object, name, value))) { + if ((status = env->Object_SetPropertyByName_Ref(object, name, value)) != ANI_OK) { ANS_LOGE("set '%{public}s' faild. status %{public}d", name, status); return false; } diff --git a/frameworks/ets/ani/src/sts_notification_manager.cpp b/frameworks/ets/ani/src/sts_notification_manager.cpp index ab01030aa..d3734ef3e 100644 --- a/frameworks/ets/ani/src/sts_notification_manager.cpp +++ b/frameworks/ets/ani/src/sts_notification_manager.cpp @@ -744,25 +744,35 @@ bool UnWarpNotificationDoNotDisturbDate( NotificationDoNotDisturbDate& doNotDisturbDate) { ani_boolean isUndefined = false; - ani_double mDouble = 0.0; + ani_ref mDate = nullptr; if (env == nullptr) { ANS_LOGE("UnWarpNotificationDoNotDisturbDate: Invalid input parameters"); return false; } GetDoNotDisturbDateByDoNotDisturbType(env, doNotDisturbDateObj, doNotDisturbDate); - if (ANI_OK == GetPropertyDouble(env, doNotDisturbDateObj, "begin", isUndefined, mDouble) + int64_t beginTime = 0; + if (ANI_OK == GetPropertyRef(env, doNotDisturbDateObj, "begin", isUndefined, mDate) && isUndefined == ANI_FALSE) { - doNotDisturbDate.SetBeginDate(static_cast(mDouble)); + if (mDate == nullptr || !GetDateByObject(env, static_cast(mDate), beginTime)) { + ANS_LOGE("get begin time failed"); + return false; + } } - if (ANI_OK == GetPropertyDouble(env, doNotDisturbDateObj, "end", isUndefined, mDouble) + int64_t endTime = 0; + if (ANI_OK == GetPropertyRef(env, doNotDisturbDateObj, "end", isUndefined, mDate) && isUndefined == ANI_FALSE) { - doNotDisturbDate.SetEndDate(static_cast(mDouble)); + if (mDate == nullptr || !GetDateByObject(env, static_cast(mDate), endTime)) { + ANS_LOGE("get end time failed"); + return false; + } } - if (doNotDisturbDate.GetBeginDate() >= doNotDisturbDate.GetEndDate()) { + if (beginTime >= endTime) { ANS_LOGE("Invalid time range"); return false; } + doNotDisturbDate.SetBeginDate(static_cast(beginTime)); + doNotDisturbDate.SetEndDate(static_cast(endTime)); ANS_LOGD("Successfully parsed DoNotDisturbDate"); return true; } -- Gitee From 884d2caeb4a64192dd9524ef135629bb40ecc854 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Tue, 15 Jul 2025 21:07:42 +0800 Subject: [PATCH 3/3] add Signed-off-by: Michael Wang --- frameworks/ets/ani/include/sts_common.h | 1 + 1 file changed, 1 insertion(+) diff --git a/frameworks/ets/ani/include/sts_common.h b/frameworks/ets/ani/include/sts_common.h index 1129ade5a..b9acf718b 100644 --- a/frameworks/ets/ani/include/sts_common.h +++ b/frameworks/ets/ani/include/sts_common.h @@ -35,6 +35,7 @@ ani_object CreateBoolean(ani_env *env, bool value); ani_object CreateDouble(ani_env *env, ani_double value); ani_object CreateInt(ani_env *env, int32_t value); bool CreateDate(ani_env *env, int64_t time, ani_object &outObj); +bool GetDateByObject(ani_env *env, ani_object timeObj, int64_t &time); ani_status GetAniStringByString(ani_env* env, const std::string str, ani_string &aniStr); ani_status GetStringByAniString(ani_env *env, ani_string str, std::string &res); bool GetStringArrayByAniObj(ani_env *env, const ani_object ani_obj, std::vector &stdVString); -- Gitee