diff --git a/frameworks/js/napi/include/manager/napi_display_badge.h b/frameworks/js/napi/include/manager/napi_display_badge.h new file mode 100644 index 0000000000000000000000000000000000000000..2e31c2ed891b067c8aa50b378a39ef2a4070b442 --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_display_badge.h @@ -0,0 +1,28 @@ +/* + * 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_DISPLAY_BADGE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISPLAY_BADGE_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value NapiDisplayBadge(napi_env env, napi_callback_info info); +napi_value NapiIsBadgeDisplayed(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_DISPLAY_BADGE_H \ No newline at end of file diff --git a/frameworks/js/napi/include/manager/napi_enable_notification.h b/frameworks/js/napi/include/manager/napi_enable_notification.h new file mode 100644 index 0000000000000000000000000000000000000000..a75cce5d55263116962fd3c06df3722acbd060ef --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_enable_notification.h @@ -0,0 +1,30 @@ +/* + * 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_ENABLE_NOTIFICATION_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_ENABLE_NOTIFICATION_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value NapiEnableNotification(napi_env env, napi_callback_info info); +napi_value NapiIsNotificationEnabled(napi_env env, napi_callback_info info); +napi_value NapiIsNotificationEnabledSelf(napi_env env, napi_callback_info info); +napi_value NapiRequestEnableNotification(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_ENABLE_NOTIFICATION_H \ No newline at end of file diff --git a/frameworks/js/napi/include/manager/napi_get_active.h b/frameworks/js/napi/include/manager/napi_get_active.h new file mode 100644 index 0000000000000000000000000000000000000000..ad67dd53d87394c4eb748e4f21a984843cb0f83a --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_get_active.h @@ -0,0 +1,31 @@ +/* + * 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_GET_ACTIVE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_GET_ACTIVE_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value NapiGetAllActiveNotifications(napi_env env, napi_callback_info info); +napi_value NapiGetActiveNotifications(napi_env env, napi_callback_info info); +napi_value NapiGetActiveNotificationCount(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_GET_ACTIVE_H \ No newline at end of file diff --git a/frameworks/js/napi/include/manager/napi_template.h b/frameworks/js/napi/include/manager/napi_template.h new file mode 100644 index 0000000000000000000000000000000000000000..6ccc8a174c75cb465ae087ec53790540adf5c873 --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_template.h @@ -0,0 +1,29 @@ +/* + * 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_TEMPLATE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_TEMPLATE_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value NapiIsSupportTemplate(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_TEMPLATE_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 5babc88c77866852a5201c41c8a7c06cafd7b7c9..71e9a3b7d4b107dd368254ba02e397785516bd5a 100644 --- a/frameworks/js/napi/src/manager/BUILD.gn +++ b/frameworks/js/napi/src/manager/BUILD.gn @@ -50,10 +50,13 @@ ohos_shared_library("notificationmanager") { "../get_active.cpp", "../publish.cpp", "../slot.cpp", - "init_module.cpp", "napi_cancel.cpp", + "napi_display_badge.cpp", + "napi_enable_notification.cpp", + "napi_get_active.cpp", "napi_publish.cpp", "napi_slot.cpp", + "napi_template.cpp", ] deps = [ diff --git a/frameworks/js/napi/src/manager/init_module.cpp b/frameworks/js/napi/src/manager/init_module.cpp index 41d8c3230d520c148ba6d91c5919103706c27ec9..19c26b38b281e016bbba5d096011d72c353c774d 100644 --- a/frameworks/js/napi/src/manager/init_module.cpp +++ b/frameworks/js/napi/src/manager/init_module.cpp @@ -16,8 +16,12 @@ #include "init_module.h" #include "constant.h" #include "napi_cancel.h" +#include "napi_display_badge.h" +#include "napi_enable_notification.h" +#include "napi_get_active.h" #include "napi_publish.h" #include "napi_slot.h" +#include "napi_template.h" #include "pixel_map_napi.h" namespace OHOS { @@ -49,6 +53,15 @@ napi_value NotificationManagerInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("removeAllSlots", NapiRemoveAllSlots), DECLARE_NAPI_FUNCTION("setNotificationEnableSlot", NapiEnableNotificationSlot), DECLARE_NAPI_FUNCTION("isNotificationSlotEnabled", NapiIsEnableNotificationSlot), + DECLARE_NAPI_FUNCTION("setNotificationEnable", NapiEnableNotification), + DECLARE_NAPI_FUNCTION("isNotificationEnabled", NapiIsNotificationEnabled), + DECLARE_NAPI_FUNCTION("requestEnableNotification", NapiRequestEnableNotification), + DECLARE_NAPI_FUNCTION("getAllActiveNotifications", NapiGetAllActiveNotifications), + DECLARE_NAPI_FUNCTION("getActiveNotifications", NapiGetActiveNotifications), + DECLARE_NAPI_FUNCTION("getActiveNotificationCount", NapiGetActiveNotificationCount), + DECLARE_NAPI_FUNCTION("displayBadge", NapiDisplayBadge), + DECLARE_NAPI_FUNCTION("isBadgeDisplayed", NapiIsBadgeDisplayed), + DECLARE_NAPI_FUNCTION("isSupportTemplate", NapiIsSupportTemplate), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); diff --git a/frameworks/js/napi/src/manager/napi_display_badge.cpp b/frameworks/js/napi/src/manager/napi_display_badge.cpp new file mode 100644 index 0000000000000000000000000000000000000000..990f70b58c0803fa9440808993c39b1247cccbe6 --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_display_badge.cpp @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2021-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_display_badge.h" + +#include "ans_inner_errors.h" +#include "display_badge.h" + +namespace OHOS { +namespace NotificationNapi { +napi_value NapiDisplayBadge(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + EnableBadgeParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoEnableBadge *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoEnableBadge {.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, "DisplayBadge", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("DisplayBadge napi_create_async_work start"); + AsyncCallbackInfoEnableBadge *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + ANS_LOGI("option.bundle = %{public}s option.uid = %{public}d enable = %{public}d", + asynccallbackinfo->params.option.GetBundleName().c_str(), + asynccallbackinfo->params.option.GetUid(), + asynccallbackinfo->params.enable); + asynccallbackinfo->info.errorCode = NotificationHelper::SetShowBadgeEnabledForBundle( + asynccallbackinfo->params.option, asynccallbackinfo->params.enable); + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("DisplayBadge napi_create_async_work end"); + AsyncCallbackInfoEnableBadge *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 AsyncCompleteCallbackNapiIsBadgeDisplayed(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackInfoIsDisplayBadge *asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + napi_get_boolean(env, asynccallbackinfo->enabled, &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 NapiIsBadgeDisplayed(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + IsDisplayBadgeParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + ANS_LOGE("Failed to parse params!"); + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoIsDisplayBadge *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoIsDisplayBadge {.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, "IsBadgeDisplayed", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsBadgeDisplayed napi_create_async_work start"); + AsyncCallbackInfoIsDisplayBadge *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + if (asynccallbackinfo->params.hasBundleOption) { + ANS_LOGI("option.bundle = %{public}s option.uid = %{public}d", + asynccallbackinfo->params.option.GetBundleName().c_str(), + asynccallbackinfo->params.option.GetUid()); + asynccallbackinfo->info.errorCode = NotificationHelper::GetShowBadgeEnabledForBundle( + asynccallbackinfo->params.option, asynccallbackinfo->enabled); + } else { + asynccallbackinfo->info.errorCode = NotificationHelper::GetShowBadgeEnabled( + asynccallbackinfo->enabled); + } + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d, enabled = %{public}d", + asynccallbackinfo->info.errorCode, asynccallbackinfo->enabled); + } + }, + AsyncCompleteCallbackNapiIsBadgeDisplayed, + (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 diff --git a/frameworks/js/napi/src/manager/napi_enable_notification.cpp b/frameworks/js/napi/src/manager/napi_enable_notification.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36de4d82744b3842d4a383d3f3c86610ee5601c7 --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_enable_notification.cpp @@ -0,0 +1,247 @@ +/* + * 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_enable_notification.h" + +#include "ans_inner_errors.h" +#include "enable_notification.h" + +namespace OHOS { +namespace NotificationNapi { +void AsyncCompleteCallbackNapiEnableNotification(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackInfoEnable *asynccallbackinfo = static_cast(data); + 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 NapiEnableNotification(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + EnableParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoEnable *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoEnable {.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, "enableNotification", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("EnableNotification napi_create_async_work start"); + AsyncCallbackInfoEnable *asynccallbackinfo = static_cast(data); + std::string deviceId {""}; + asynccallbackinfo->info.errorCode = NotificationHelper::SetNotificationsEnabledForSpecifiedBundle( + asynccallbackinfo->params.option, deviceId, asynccallbackinfo->params.enable); + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode); + }, + AsyncCompleteCallbackNapiEnableNotification, + (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 AsyncCompleteCallbackNapiIsNotificationEnabled(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + napi_get_boolean(env, asynccallbackinfo->allowed, &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 NapiIsNotificationEnabled(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + IsEnableParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoIsEnable *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoIsEnable {.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, "isNotificationEnabled", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsNotificationEnabled napi_create_async_work start"); + AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); + + if (asynccallbackinfo->params.hasBundleOption) { + ANS_LOGI("option.bundle = %{public}s option.uid = %{public}d", + asynccallbackinfo->params.option.GetBundleName().c_str(), + asynccallbackinfo->params.option.GetUid()); + asynccallbackinfo->info.errorCode = + NotificationHelper::IsAllowedNotify(asynccallbackinfo->params.option, asynccallbackinfo->allowed); + } else if (asynccallbackinfo->params.hasUserId) { + ANS_LOGI("userId = %{public}d", asynccallbackinfo->params.userId); + asynccallbackinfo->info.errorCode = + NotificationHelper::IsAllowedNotify(asynccallbackinfo->params.userId, asynccallbackinfo->allowed); + } else { + asynccallbackinfo->info.errorCode = NotificationHelper::IsAllowedNotify(asynccallbackinfo->allowed); + } + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d, allowed = %{public}d", + asynccallbackinfo->info.errorCode, asynccallbackinfo->allowed); + }, + AsyncCompleteCallbackNapiIsNotificationEnabled, + (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 NapiIsNotificationEnabledSelf(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + IsEnableParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoIsEnable *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoIsEnable {.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, "IsNotificationEnabledSelf", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsNotificationEnabledSelf napi_create_async_work start"); + AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); + + if (asynccallbackinfo->params.hasBundleOption) { + ANS_LOGE("Not allowed to query another application"); + } else { + asynccallbackinfo->info.errorCode = NotificationHelper::IsAllowedNotifySelf(asynccallbackinfo->allowed); + } + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d, allowed = %{public}d", + asynccallbackinfo->info.errorCode, asynccallbackinfo->allowed); + }, + AsyncCompleteCallbackNapiIsNotificationEnabled, + (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 NapiRequestEnableNotification(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + IsEnableParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoIsEnable *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoIsEnable {.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, "RequestEnableNotification", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("RequestEnableNotification napi_create_async_work start"); + AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); + + std::string deviceId {""}; + asynccallbackinfo->info.errorCode = NotificationHelper::RequestEnableNotification(deviceId); + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode); + }, + AsyncCompleteCallbackNapiIsNotificationEnabled, + (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 diff --git a/frameworks/js/napi/src/manager/napi_get_active.cpp b/frameworks/js/napi/src/manager/napi_get_active.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65b2fe66a9b43102b7f7d431d60af8f445ddaf2c --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_get_active.cpp @@ -0,0 +1,265 @@ +/* + * 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_get_active.h" + +#include "ans_inner_errors.h" +#include "get_active.h" + +namespace OHOS { +namespace NotificationNapi { +void AsyncCompleteCallbackNapiGetAllActiveNotifications(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("GetAllActiveNotifications callback"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + + auto asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_value arr = nullptr; + int32_t count = 0; + napi_create_array(env, &arr); + for (auto vec : asynccallbackinfo->notifications) { + if (!vec) { + ANS_LOGW("Invalid Notification object ptr"); + continue; + } + napi_value notificationResult = nullptr; + napi_create_object(env, ¬ificationResult); + if (!Common::SetNotification(env, vec.GetRefPtr(), notificationResult)) { + ANS_LOGW("Set Notification object failed"); + continue; + } + napi_set_element(env, arr, count, notificationResult); + count++; + } + ANS_LOGI("GetAllActiveNotifications count = %{public}d", count); + result = arr; + if ((count == 0) && (asynccallbackinfo->notifications.size() > 0)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + } + 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 NapiGetAllActiveNotifications(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) AsyncCallbackInfoActive {.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, "getAllActiveNotifications", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetAllActiveNotifications napi_create_async_work start"); + auto asynccallbackinfo = static_cast(data); + + asynccallbackinfo->info.errorCode = + NotificationHelper::GetAllActiveNotifications(asynccallbackinfo->notifications); + }, + AsyncCompleteCallbackNapiGetAllActiveNotifications, + (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 AsyncCompleteCallbackNapiGetActiveNotifications(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("GetActiveNotifications callback"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + + auto asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_value arr = nullptr; + int32_t count = 0; + napi_create_array(env, &arr); + for (auto vec : asynccallbackinfo->requests) { + if (!vec) { + ANS_LOGW("Invalid NotificationRequest object ptr"); + continue; + } + napi_value requestResult = nullptr; + napi_create_object(env, &requestResult); + if (!Common::SetNotificationRequest(env, vec.GetRefPtr(), requestResult)) { + ANS_LOGW("Set NotificationRequest object failed"); + continue; + } + napi_set_element(env, arr, count, requestResult); + count++; + } + ANS_LOGI("GetActiveNotifications count = %{public}d", count); + result = arr; + if ((count == 0) && (asynccallbackinfo->requests.size() > 0)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + } + 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 NapiGetActiveNotifications(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) AsyncCallbackInfoActive {.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, "getActiveNotifications", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetActiveNotifications napi_create_async_work start"); + auto asynccallbackinfo = static_cast(data); + + asynccallbackinfo->info.errorCode = + NotificationHelper::GetActiveNotifications(asynccallbackinfo->requests); + }, + AsyncCompleteCallbackNapiGetActiveNotifications, + (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 AsyncCompleteCallbackNapiGetActiveNotificationCount(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("GetActiveNotificationCount callback"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + + auto asynccallbackinfo = static_cast(data); + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_create_uint32(env, asynccallbackinfo->num, &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 NapiGetActiveNotificationCount(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) AsyncCallbackInfoActive {.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, "getActiveNotificationCount", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetActiveNotificationCount napi_create_async_work start"); + auto asynccallbackinfo = static_cast(data); + + asynccallbackinfo->info.errorCode = NotificationHelper::GetActiveNotificationNums(asynccallbackinfo->num); + ANS_LOGI("GetActiveNotificationCount count = %{public}" PRIu64 "", asynccallbackinfo->num); + }, + AsyncCompleteCallbackNapiGetActiveNotificationCount, + (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 diff --git a/frameworks/js/napi/src/manager/napi_template.cpp b/frameworks/js/napi/src/manager/napi_template.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3297da6ccc7d88216052827fb6aeebbabf38d73 --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_template.cpp @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2021-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_template.h" + +#include "ans_inner_errors.h" +#include "ans_template.h" + +namespace OHOS { +namespace NotificationNapi { +napi_value NapiIsSupportTemplate(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + TemplateName params; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoTemplate *asyncCallbackinfo = new (std::nothrow) + AsyncCallbackInfoTemplate {.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, "IsSupportTemplate", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsSupportTemplate napi_create_async_work start"); + AsyncCallbackInfoTemplate *asyncCallbackinfo = static_cast(data); + + if (asyncCallbackinfo) { + asyncCallbackinfo->info.errorCode = NotificationHelper::IsSupportTemplate( + asyncCallbackinfo->params.templateName, asyncCallbackinfo->params.support); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("IsSupportTemplate napi_create_async_work end"); + AsyncCallbackInfoTemplate *asyncCallbackinfo = static_cast(data); + if (asyncCallbackinfo) { + napi_value result = nullptr; + napi_get_boolean(env, asyncCallbackinfo->params.support, &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