diff --git a/frameworks/ans/native/src/reminder_request_alarm.cpp b/frameworks/ans/native/src/reminder_request_alarm.cpp index 61cd41d68cd407f98405a7803895e5ace274531f..3a3ee1af65fca4354147cbddc34999be8bed0af3 100644 --- a/frameworks/ans/native/src/reminder_request_alarm.cpp +++ b/frameworks/ans/native/src/reminder_request_alarm.cpp @@ -160,12 +160,9 @@ int8_t ReminderRequestAlarm::GetNextAlarm(const time_t now, const time_t target) if (repeatDays_ == 0) { return INVALID_INT_VALUE; } - tm *nowTime = gmtime(&now); - if (nowTime == nullptr) { - ANSR_LOGE("Failed to get next alarm due to gmtime return null."); - return 0; - } - int32_t today = GetActualTime(TimeTransferType::WEEK, nowTime->tm_wday); + struct tm nowTime; + (void)gmtime_r(&now, &nowTime); + int32_t today = GetActualTime(TimeTransferType::WEEK, nowTime.tm_wday); int32_t dayCount = now >= target ? 1 : 0; for (; dayCount <= DAYS_PER_WEEK; dayCount++) { int32_t day = (today + dayCount) % DAYS_PER_WEEK; diff --git a/interfaces/kits/napi/ans/include/reminder/publish.h b/interfaces/kits/napi/ans/include/reminder/publish.h index bb312474615cbf8a898af5f8983d892fa18bf37d..c9a824c963a239240a5ee5f388acdbacbc6f9d4b 100644 --- a/interfaces/kits/napi/ans/include/reminder/publish.h +++ b/interfaces/kits/napi/ans/include/reminder/publish.h @@ -54,6 +54,15 @@ napi_value GetValidReminders(napi_env env, napi_callback_info info); * @return opaque pointer that is used to represent a JavaScript value */ napi_value PublishReminder(napi_env env, napi_callback_info info); + +/** + * @brief Adds a slot type. + * + * @param env Indicates the context. + * @param info Indicates the opaque datatype about the context. + * @return opaque pointer that is used to represent a JavaScript value. + */ +napi_value AddSlot(napi_env env, napi_callback_info info); } // namespace ReminderAgentNapi } // namespace OHOS diff --git a/interfaces/kits/napi/ans/src/reminder/native_module.cpp b/interfaces/kits/napi/ans/src/reminder/native_module.cpp index 23ce01695e03cc84ed5ee988cd7fe53d6411ef3e..613202a83059f0f84c5f040296d759516d5ae8ec 100644 --- a/interfaces/kits/napi/ans/src/reminder/native_module.cpp +++ b/interfaces/kits/napi/ans/src/reminder/native_module.cpp @@ -31,7 +31,7 @@ napi_value ReminderAgentInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("cancelAllReminders", CancelAllReminders), DECLARE_NAPI_FUNCTION("getValidReminders", GetValidReminders), DECLARE_NAPI_FUNCTION("publishReminder", PublishReminder), - DECLARE_NAPI_FUNCTION("addNotificationSlot", NotificationNapi::AddSlot), + DECLARE_NAPI_FUNCTION("addNotificationSlot", AddSlot), DECLARE_NAPI_FUNCTION("removeNotificationSlot", NotificationNapi::RemoveSlot), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); diff --git a/interfaces/kits/napi/ans/src/reminder/publish.cpp b/interfaces/kits/napi/ans/src/reminder/publish.cpp index 7c332d7a99cac34d64590c238c4b8773ddbbce0b..af5008dc45a28888bd7c971423a2f461b1500bcf 100644 --- a/interfaces/kits/napi/ans/src/reminder/publish.cpp +++ b/interfaces/kits/napi/ans/src/reminder/publish.cpp @@ -27,11 +27,14 @@ static const int32_t PUBLISH_PARAM_LEN = 2; static const int32_t CANCEL_PARAM_LEN = 2; static const int32_t CANCEL_ALL_PARAM_LEN = 1; static const int32_t GET_VALID_PARAM_LEN = 1; +static const int32_t ADD_SLOT_PARAM_LEN = 2; struct AsyncCallbackInfo { napi_env env = nullptr; napi_async_work asyncWork = nullptr; int32_t reminderId = -1; + NotificationNapi::NotificationConstant::SlotType inType + = NotificationNapi::NotificationConstant::SlotType::CONTENT_INFORMATION; std::shared_ptr reminder = nullptr; std::vector> validReminders; NotificationNapi::CallbackPromiseInfo info; @@ -40,6 +43,9 @@ struct AsyncCallbackInfo { struct Parameters { int32_t reminderId = -1; + int32_t errCode = ERR_OK; + NotificationNapi::NotificationConstant::SlotType inType + = NotificationNapi::NotificationConstant::SlotType::CONTENT_INFORMATION; std::shared_ptr reminder = nullptr; napi_ref callback = nullptr; }; @@ -94,6 +100,41 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, return NotificationNapi::Common::NapiGetNull(env); } +napi_value ParseSlotParameters(const napi_env &env, const napi_callback_info &info, Parameters ¶ms) +{ + size_t argc = ADD_SLOT_PARAM_LEN; + napi_value argv[ADD_SLOT_PARAM_LEN] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + if ((argc < 1) || (argc > ADD_SLOT_PARAM_LEN)) { + ANSR_LOGW("Wrong number of arguments"); + return nullptr; + } + + // argv[1]: callback + if (argc == ADD_SLOT_PARAM_LEN) { + if (GetCallback(env, argv[1], params) == nullptr) { + ANSR_LOGW("GetCallbak returns nullptr"); + return nullptr; + } + } + + // argv[0] : notificationSlot + // slotType + const char* propertyKey = "type"; + int32_t propertyVal = 0; + if (!ReminderCommon::GetInt32(env, argv[0], propertyKey, propertyVal, false)) { + ANSR_LOGW("Failed to get valid slot type."); + params.errCode = ERR_INVALID_VALUE; + return NotificationNapi::Common::NapiGetNull(env); + } + + if (!NotificationNapi::Common::SlotTypeJSToC(NotificationNapi::SlotType(propertyVal), params.inType)) { + ANSR_LOGW("Failed to get valid slot type"); + return nullptr; + } + return NotificationNapi::Common::NapiGetNull(env); +} + napi_value ParseCanCelParameter(const napi_env &env, const napi_callback_info &info, Parameters ¶ms) { ANSR_LOGI("ParseCanCelParameter"); @@ -650,5 +691,63 @@ napi_value PublishReminder(napi_env env, napi_callback_info info) return promise; } } + +napi_value AddSlot(napi_env env, napi_callback_info info) +{ + ANSR_LOGI("AddSlot"); + + // param + Parameters params; + if (ParseSlotParameters(env, info, params) == nullptr) { + ANSR_LOGW("Parse params error"); + return NotificationNapi::Common::JSParaError(env, params.callback); + } + + // promise + napi_value promise = nullptr; + AsyncCallbackInfo *asynccallbackinfo = SetAsynccallbackinfo(env, params, promise); + if (!asynccallbackinfo) { + return NotificationNapi::Common::JSParaError(env, params.callback); + } + asynccallbackinfo->inType = params.inType; + asynccallbackinfo->info.errorCode = params.errCode; + + // resource name + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "AddSlot", NAPI_AUTO_LENGTH, &resourceName); + + // create and start async work + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANSR_LOGI("AddSlot napi_create_async_work start"); + AsyncCallbackInfo *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo && (asynccallbackinfo->info.errorCode == ERR_OK)) { + asynccallbackinfo->info.errorCode = NotificationHelper::AddSlotByType(asynccallbackinfo->inType); + } + }, + [](napi_env env, napi_status status, void *data) { + AsyncCallbackInfo *asynccallbackinfo = (AsyncCallbackInfo *)data; + NotificationNapi::Common::ReturnCallbackPromise( + env, asynccallbackinfo->info, NotificationNapi::Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + ANSR_LOGD("AddSlot napi_create_async_work complete end"); + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return NotificationNapi::Common::NapiGetNull(env); + } else { + return promise; + } +} } } \ No newline at end of file