diff --git a/frameworks/ets/ani/include/sts_common.h b/frameworks/ets/ani/include/sts_common.h index 1129ade5a425aa2cd4ed153a493ad8c7415628c6..b9acf718b5f30019cad68445265fca580055ec8d 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); diff --git a/frameworks/ets/ani/include/sts_throw_erro.h b/frameworks/ets/ani/include/sts_throw_erro.h index 8555656dbba97239a676ba236af18b4509e8cc41..2b2d0738e0cdd0028cdbfd3da4588eb7b701700e 100644 --- a/frameworks/ets/ani/include/sts_throw_erro.h +++ b/frameworks/ets/ani/include/sts_throw_erro.h @@ -100,16 +100,16 @@ static const std::vector> ERRORS_CONVERT = { inline int32_t GetExternalCode(uint32_t errCode) { - int32_t ExternalCode = ERR_OK; 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 80e90419cfadd4ec824bb2b04113b801adcc96b6..233bd5d94bfe23711b757cfb27389be261ff2167 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 048f033b529a301c4e84a16a73de3c23e1acac6e..f022a63677da74c6fffd52e8dcf0ebcbc890529e 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; } @@ -499,7 +499,7 @@ ani_object newArrayClass(ani_env *env, int length) 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; } @@ -521,12 +521,12 @@ ani_object newRecordClass(ani_env *env) 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; } @@ -621,20 +621,39 @@ bool CreateDate(ani_env *env, int64_t time, ani_object &outObj) return false; } ani_method ctor; - if (ANI_OK != (status = env->Class_FindMethod( - cls, "", "X{C{escompat.Date}C{std.core.Numeric}C{std.core.String}}:", &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; } @@ -698,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; } @@ -732,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 effc1fa08f775ae5353482012c86cfd8db33c05c..a9d0402ba5addec965361c15259ac5fbc721518d 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; }