diff --git a/frameworks/js/napi/include/manager/napi_distributed.h b/frameworks/js/napi/include/manager/napi_distributed.h new file mode 100644 index 0000000000000000000000000000000000000000..a6bd53639932d9dec6deccc6b2135216366b7303 --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_distributed.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISTRIBUTED_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISTRIBUTED_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value NapiIsDistributedEnabled(napi_env env, napi_callback_info info); +napi_value NapiEnableDistributed(napi_env env, napi_callback_info info); +napi_value NapiEnableDistributedByBundle(napi_env env, napi_callback_info info); +napi_value NapiEnableDistributedSelf(napi_env env, napi_callback_info info); +napi_value NapiIsDistributedEnableByBundle(napi_env env, napi_callback_info info); +napi_value NapiGetDeviceRemindType(napi_env env, napi_callback_info info); +napi_value NapiSetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info); +napi_value NapiGetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISTRIBUTED_H \ No newline at end of file diff --git a/frameworks/js/napi/include/manager/napi_disturb_mode.h b/frameworks/js/napi/include/manager/napi_disturb_mode.h new file mode 100644 index 0000000000000000000000000000000000000000..4cbf88d3ef34d91de8ac136f4106bc44afa3a542 --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_disturb_mode.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISTURB_MODE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISTURB_MODE_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value NapiSetDoNotDisturbDate(napi_env env, napi_callback_info info); +napi_value NapiGetDoNotDisturbDate(napi_env env, napi_callback_info info); +napi_value NapiSupportDoNotDisturbMode(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISTURB_MODE_H \ No newline at end of file diff --git a/frameworks/js/napi/src/manager/BUILD.gn b/frameworks/js/napi/src/manager/BUILD.gn index 71e9a3b7d4b107dd368254ba02e397785516bd5a..119263940b32d7084e12c71d2761cb2df808828d 100644 --- a/frameworks/js/napi/src/manager/BUILD.gn +++ b/frameworks/js/napi/src/manager/BUILD.gn @@ -52,11 +52,14 @@ ohos_shared_library("notificationmanager") { "../slot.cpp", "napi_cancel.cpp", "napi_display_badge.cpp", + "napi_distributed.cpp", + "napi_disturb_mode.cpp", "napi_enable_notification.cpp", "napi_get_active.cpp", "napi_publish.cpp", "napi_slot.cpp", "napi_template.cpp", + "init_module.cpp", ] deps = [ diff --git a/frameworks/js/napi/src/manager/init_module.cpp b/frameworks/js/napi/src/manager/init_module.cpp index 19c26b38b281e016bbba5d096011d72c353c774d..7b1fb3a714467a07fbb8811d235f359f774d579b 100644 --- a/frameworks/js/napi/src/manager/init_module.cpp +++ b/frameworks/js/napi/src/manager/init_module.cpp @@ -17,6 +17,8 @@ #include "constant.h" #include "napi_cancel.h" #include "napi_display_badge.h" +#include "napi_distributed.h" +#include "napi_disturb_mode.h" #include "napi_enable_notification.h" #include "napi_get_active.h" #include "napi_publish.h" @@ -62,6 +64,17 @@ napi_value NotificationManagerInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("displayBadge", NapiDisplayBadge), DECLARE_NAPI_FUNCTION("isBadgeDisplayed", NapiIsBadgeDisplayed), DECLARE_NAPI_FUNCTION("isSupportTemplate", NapiIsSupportTemplate), + DECLARE_NAPI_FUNCTION("setDoNotDisturbDate", NapiSetDoNotDisturbDate), + DECLARE_NAPI_FUNCTION("getDoNotDisturbDate", NapiGetDoNotDisturbDate), + DECLARE_NAPI_FUNCTION("supportDoNotDisturbMode", NapiSupportDoNotDisturbMode), + DECLARE_NAPI_FUNCTION("isDistributedEnabled", NapiIsDistributedEnabled), + DECLARE_NAPI_FUNCTION("setDistributedEnable", NapiEnableDistributed), + DECLARE_NAPI_FUNCTION("setDistributedEnableByBundle", NapiEnableDistributedByBundle), + DECLARE_NAPI_FUNCTION("enableDistributedSelf", NapiEnableDistributedSelf), + DECLARE_NAPI_FUNCTION("isDistributedEnableByBundle", NapiIsDistributedEnableByBundle), + DECLARE_NAPI_FUNCTION("getDeviceRemindType", NapiGetDeviceRemindType), + DECLARE_NAPI_FUNCTION("setSyncNotificationEnabledWithoutApp", NapiSetSyncNotificationEnabledWithoutApp), + DECLARE_NAPI_FUNCTION("getSyncNotificationEnabledWithoutApp", NapiGetSyncNotificationEnabledWithoutApp), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); diff --git a/frameworks/js/napi/src/manager/napi_distributed.cpp b/frameworks/js/napi/src/manager/napi_distributed.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b4968f368209c5fca30a5e372a51b137d3c45c4 --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_distributed.cpp @@ -0,0 +1,545 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi_distributed.h" + +#include "ans_inner_errors.h" +#include "distributed.h" + +namespace OHOS { +namespace NotificationNapi { +void AsyncCompleteCallbackNapiIsDistributedEnabled(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + ANS_LOGI("IsDistributedEnabled napi_create_async_work end"); + AsyncCallbackInfoIsEnabled *asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_get_boolean(env, asynccallbackinfo->enable, &result); + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value NapiIsDistributedEnabled(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + napi_ref callback = nullptr; + if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoIsEnabled {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "isDistributedEnabled", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsDistributedEnabled napi_create_async_work start"); + AsyncCallbackInfoIsEnabled *asynccallbackinfo = static_cast(data); + + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::IsDistributedEnabled(asynccallbackinfo->enable); + ANS_LOGI("IsDistributedEnabled enable = %{public}d", asynccallbackinfo->enable); + } + }, + AsyncCompleteCallbackNapiIsDistributedEnabled, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value NapiEnableDistributed(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + EnabledParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoEnabled *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoEnabled {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "enableDistributed", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("EnableDistributed napi_create_async_work start"); + AsyncCallbackInfoEnabled *asynccallbackinfo = static_cast(data); + + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::EnableDistributed(asynccallbackinfo->params.enable); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("EnableDistributed napi_create_async_work end"); + AsyncCallbackInfoEnabled *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, 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; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value NapiEnableDistributedByBundle(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + EnabledByBundleParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoEnabledByBundle *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoEnabledByBundle {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "enableDistributedByBundle", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("EnableDistributedByBundle napi_create_async_work start"); + AsyncCallbackInfoEnabledByBundle *asynccallbackinfo = static_cast(data); + + asynccallbackinfo->info.errorCode = NotificationHelper::EnableDistributedByBundle( + asynccallbackinfo->params.option, asynccallbackinfo->params.enable); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("EnableDistributedByBundle napi_create_async_work end"); + AsyncCallbackInfoEnabledByBundle *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, 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; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value NapiEnableDistributedSelf(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + EnabledParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoEnabled *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoEnabled {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "enableDistributedSelf", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("EnableDistributedSelf napi_create_async_work start"); + auto asynccallbackinfo = reinterpret_cast(data); + asynccallbackinfo->info.errorCode = + NotificationHelper::EnableDistributedSelf(asynccallbackinfo->params.enable); + ANS_LOGI("EnableDistributedSelf enable = %{public}d", asynccallbackinfo->params.enable); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("EnableDistributedSelf napi_create_async_work end"); + auto asynccallbackinfo = reinterpret_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, 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; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +void AsyncCompleteCallbackNapiIsDistributedEnableByBundle(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + ANS_LOGI("IsDistributedEnableByBundle napi_create_async_work end"); + AsyncCallbackInfoIsEnabledByBundle *asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_get_boolean(env, asynccallbackinfo->enable, &result); + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value NapiIsDistributedEnableByBundle(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + IsEnabledByBundleParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoIsEnabledByBundle *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoIsEnabledByBundle {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "isDistributedEnableByBundle", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsDistributedEnableByBundle napi_create_async_work start"); + auto asynccallbackinfo = reinterpret_cast(data); + + asynccallbackinfo->info.errorCode = NotificationHelper::IsDistributedEnableByBundle( + asynccallbackinfo->params.option, asynccallbackinfo->enable); + }, + AsyncCompleteCallbackNapiIsDistributedEnableByBundle, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +void AsyncCompleteCallbackNapiGetDeviceRemindType(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + ANS_LOGI("GetDeviceRemindType napi_create_async_work end"); + AsyncCallbackInfoGetRemindType *asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + DeviceRemindType outType = DeviceRemindType::IDLE_DONOT_REMIND; + if (!Common::DeviceRemindTypeCToJS(asynccallbackinfo->remindType, outType)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + napi_create_int32(env, (int32_t)outType, &result); + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; +} + +napi_value NapiGetDeviceRemindType(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + napi_ref callback = nullptr; + if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + auto asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoGetRemindType {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getDeviceRemindType", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetDeviceRemindType napi_create_async_work start"); + auto asynccallbackinfo = reinterpret_cast(data); + asynccallbackinfo->info.errorCode = + NotificationHelper::GetDeviceRemindType(asynccallbackinfo->remindType); + }, + AsyncCompleteCallbackNapiGetDeviceRemindType, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value NapiSetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + EnabledWithoutAppParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::JSParaError(env, params.callback); + } + + AsyncCallbackInfoEnabledWithoutApp *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoEnabledWithoutApp {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "SetSyncNotificationEnabledWithoutApp", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("SetSyncNotificationEnabledWithoutApp napi_create_async_work start"); + AsyncCallbackInfoEnabledWithoutApp *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::SetSyncNotificationEnabledWithoutApp( + asynccallbackinfo->params.userId, asynccallbackinfo->params.enable); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("SetSyncNotificationEnabledWithoutApp napi_create_async_work end"); + AsyncCallbackInfoEnabledWithoutApp *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, 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; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + if (napi_queue_async_work(env, asynccallbackinfo->asyncWork) != napi_ok) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + return Common::JSParaError(env, params.callback); + } + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } + return promise; +} + +napi_value NapiGetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + GetEnabledWithoutAppParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::JSParaError(env, params.callback); + } + + AsyncCallbackInfoGetEnabledWithoutApp *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoGetEnabledWithoutApp { + .env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "GetSyncNotificationEnabledWithoutApp", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetSyncNotificationEnabledWithoutApp napi_create_async_work start"); + AsyncCallbackInfoGetEnabledWithoutApp *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::GetSyncNotificationEnabledWithoutApp( + asynccallbackinfo->params.userId, asynccallbackinfo->enable); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("GetSyncNotificationEnabledWithoutApp napi_create_async_work end"); + AsyncCallbackInfoGetEnabledWithoutApp *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_get_boolean(env, asynccallbackinfo->enable, &result); + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + if (napi_queue_async_work(env, asynccallbackinfo->asyncWork) != napi_ok) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + return Common::JSParaError(env, params.callback); + } + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } + return promise; +} +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/manager/napi_disturb_mode.cpp b/frameworks/js/napi/src/manager/napi_disturb_mode.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed4f9e6d10e5398dc6afff7dd0ef8084298b47d9 --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_disturb_mode.cpp @@ -0,0 +1,221 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "napi_disturb_mode.h" + +#include "ans_inner_errors.h" +#include "disturb_mode.h" + +namespace OHOS { +namespace NotificationNapi { +napi_value NapiSetDoNotDisturbDate(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + SetDoNotDisturbDateParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoSetDoNotDisturb {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "setDoNotDisturbDate", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, resourceName, [](napi_env env, void *data) { + ANS_LOGI("SetDoNotDisturbDate napi_create_async_work start"); + AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo->params.hasUserId) { + asynccallbackinfo->info.errorCode = NotificationHelper::SetDoNotDisturbDate( + asynccallbackinfo->params.userId, asynccallbackinfo->params.date); + } else { + asynccallbackinfo->info.errorCode = NotificationHelper::SetDoNotDisturbDate( + asynccallbackinfo->params.date); + } + + ANS_LOGI("SetDoNotDisturbDate date=%{public}s errorCode=%{public}d, hasUserId=%{public}d", + asynccallbackinfo->params.date.Dump().c_str(), asynccallbackinfo->info.errorCode, + asynccallbackinfo->params.hasUserId); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("SetDoNotDisturbDate napi_create_async_work end"); + AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, 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; + } + }, + (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +void AsyncCompleteCallbackNapiGetDoNotDisturbDate(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo = static_cast(data); + napi_value result = Common::NapiGetNull(env); + if (asynccallbackinfo->info.errorCode == ERR_OK) { + napi_create_object(env, &result); + if (!Common::SetDoNotDisturbDate(env, asynccallbackinfo->date, result)) { + asynccallbackinfo->info.errorCode = ERROR; + } + } + Common::CreateReturnValue(env, asynccallbackinfo->info, 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; +} + +napi_value NapiGetDoNotDisturbDate(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + GetDoNotDisturbDateParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoGetDoNotDisturb {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getDoNotDisturbDate", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetDoNotDisturbDate napi_create_async_work start"); + AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo->params.hasUserId) { + asynccallbackinfo->info.errorCode = NotificationHelper::GetDoNotDisturbDate( + asynccallbackinfo->params.userId, asynccallbackinfo->date); + } else { + asynccallbackinfo->info.errorCode = NotificationHelper::GetDoNotDisturbDate(asynccallbackinfo->date); + } + + ANS_LOGI("GetDoNotDisturbDate errorCode=%{public}d date=%{public}s, hasUserId=%{public}d", + asynccallbackinfo->info.errorCode, asynccallbackinfo->date.Dump().c_str(), + asynccallbackinfo->params.hasUserId); + }, + AsyncCompleteCallbackNapiGetDoNotDisturbDate, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +napi_value NapiSupportDoNotDisturbMode(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + napi_ref callback = nullptr; + if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoSupportDoNotDisturb { + .env = env, .asyncWork = nullptr, .callback = callback}; + + if (!asynccallbackinfo) { + return Common::JSParaError(env, callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "supportDoNotDisturbMode", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("SupportDoNotDisturbMode napi_create_async_work start"); + AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo = + static_cast(data); + asynccallbackinfo->info.errorCode = + NotificationHelper::DoesSupportDoNotDisturbMode(asynccallbackinfo->isSupported); + ANS_LOGI("SupportDoNotDisturbMode errorCode=%{public}d isSupported=%{public}d", + asynccallbackinfo->info.errorCode, asynccallbackinfo->isSupported); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("SupportDoNotDisturbMode napi_create_async_work end"); + AsyncCallbackInfoSupportDoNotDisturb *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + napi_value result = nullptr; + napi_get_boolean(env, asynccallbackinfo->isSupported, &result); + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file