diff --git a/bundle.json b/bundle.json index 9a6f3ee2e6062989551b44a2b0a7c4540ccd73ad..7c191896c4d94a5d467de3c4e68a5e73876c390a 100644 --- a/bundle.json +++ b/bundle.json @@ -130,6 +130,7 @@ ], "fwk_group": [ "//base/notification/distributed_notification_service/frameworks/ans:ans_client", + "//base/notification/distributed_notification_service/frameworks/js/napi:napi_extension_subscription", "//base/notification/distributed_notification_service/frameworks/js/napi:napi_notification", "//base/notification/distributed_notification_service/frameworks/js/napi:napi_reminder", "//base/notification/distributed_notification_service/interfaces/ndk:ohnotification", diff --git a/frameworks/ans/BUILD.gn b/frameworks/ans/BUILD.gn index c0b17fc2ecd1cbaff7fb247203d1836b3397a912..289fa1eb16d8023b5e2835af30b5bdf278624a34 100644 --- a/frameworks/ans/BUILD.gn +++ b/frameworks/ans/BUILD.gn @@ -101,6 +101,7 @@ ohos_shared_library("ans_innerkits") { "${frameworks_module_ans_path}/src/notification_distributed_options.cpp", "${frameworks_module_ans_path}/src/notification_do_not_disturb_date.cpp", "${frameworks_module_ans_path}/src/notification_do_not_disturb_profile.cpp", + "${frameworks_module_ans_path}/src/notification_extension_subscription_info.cpp", "${frameworks_module_ans_path}/src/notification_flags.cpp", "${frameworks_module_ans_path}/src/notification_helper.cpp", "${frameworks_module_ans_path}/src/notification_icon_button.cpp", diff --git a/frameworks/ans/IAnsManager.idl b/frameworks/ans/IAnsManager.idl index 48812d68095b3f0bf836dc6d943efc5ffc2edbf5..1c52e99ad832537cc1c6c0422b7834dfc8ab2641 100644 --- a/frameworks/ans/IAnsManager.idl +++ b/frameworks/ans/IAnsManager.idl @@ -23,6 +23,7 @@ sequenceable OHOS.Notification.DistributedBundleOption; sequenceable OHOS.Notification.NotificationDisable; sequenceable OHOS.Notification.NotificationDoNotDisturbDate; sequenceable OHOS.Notification.NotificationDoNotDisturbProfile; +sequenceable OHOS.Notification.NotificationExtensionSubscriptionInfo; sequenceable OHOS.Notification.NotificationRequest; sequenceable OHOS.Notification.NotificationSlot; sequenceable OHOS.Notification.NotificationSubscribeInfo; @@ -331,5 +332,23 @@ interface OHOS.Notification.IAnsManager { void GetTargetDeviceBundleList([in] String deviceType, [in] String deviceId, [out] String[] bundleList, [out] String[] labelList); + void NotificationExtensionSubscribe([in] sptr[] infos); + + void NotificationExtensionUnsubscribe(); + + void GetSubscribeInfo([out] sptr[] infos); + + void GetAllSubscriptionBundles([out] sptr[] bundles); + + void IsUserGranted([out] boolean enabled); + + void GetUserGrantedState([in] sptr targetBundle, [out] boolean enabled); + + void SetUserGrantedState([in] sptr targetBundle, [in] boolean enabled); + + void GetUserGrantedEnabledBundles([in] sptr targetBundle, [out] sptr[] enabledBundles); + + void SetUserGrantedBundleState([in] sptr targetBundle, [in] sptr[] enabledBundles, [in] boolean enabled); + [macrodef NOTIFICATION_SMART_REMINDER_SUPPORTED] void RegisterSwingCallback([in] IRemoteObject swingCallback); } diff --git a/frameworks/ans/src/notification_extension_subscription_info.cpp b/frameworks/ans/src/notification_extension_subscription_info.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da6fe5e68849e8a8fa50326c451e4f6cf20a0900 --- /dev/null +++ b/frameworks/ans/src/notification_extension_subscription_info.cpp @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2025 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 +#include "ans_log_wrapper.h" +#include "notification_extension_subscription_info.h" +#include "parcel.h" + +namespace OHOS { +namespace Notification { +NotificationExtensionSubscriptionInfo::NotificationExtensionSubscriptionInfo( + const std::string& addr, const NotificationConstant::SubscribeType type) + : addr_(addr), type_(type) +{} + +NotificationExtensionSubscriptionInfo::~NotificationExtensionSubscriptionInfo() +{} + +std::string NotificationExtensionSubscriptionInfo::GetAddr() const +{ + return addr_; +} + +void NotificationExtensionSubscriptionInfo::SetAddr(const std::string& addr) +{ + addr_ = addr; +} + +bool NotificationExtensionSubscriptionInfo::IsHfp() const +{ + return isHfp_; +} + +void NotificationExtensionSubscriptionInfo::SetHfp(const bool& hfp) +{ + isHfp_ = hfp; +} + +NotificationConstant::SubscribeType NotificationExtensionSubscriptionInfo::GetType() const +{ + return type_; +} + +void NotificationExtensionSubscriptionInfo::SetType(const NotificationConstant::SubscribeType type) +{ + type_ = type; +} + +std::string NotificationExtensionSubscriptionInfo::Dump() +{ + return "NotificationExtensionSubscriptionInfo{ " + "addr = " + addr_ + + ", type = " + std::to_string(static_cast(type_)) + + " }"; +} + +bool NotificationExtensionSubscriptionInfo::Marshalling(Parcel& parcel) const +{ + if (!parcel.WriteString(addr_)) { + ANS_LOGE("Failed to write address"); + return false; + } + + if (!parcel.WriteInt32(static_cast(type_))) { + ANS_LOGE("Failed to write type"); + return false; + } + + return true; +} + +NotificationExtensionSubscriptionInfo *NotificationExtensionSubscriptionInfo::Unmarshalling(Parcel& parcel) +{ + auto pNotificationExtensionSubscriptionInfo = new (std::nothrow) NotificationExtensionSubscriptionInfo(); + if (pNotificationExtensionSubscriptionInfo && !pNotificationExtensionSubscriptionInfo->ReadFromParcel(parcel)) { + delete pNotificationExtensionSubscriptionInfo; + pNotificationExtensionSubscriptionInfo = nullptr; + } + + return pNotificationExtensionSubscriptionInfo; +} + +bool NotificationExtensionSubscriptionInfo::ReadFromParcel(Parcel& parcel) +{ + if (!parcel.ReadString(addr_)) { + ANS_LOGE("Failed to read address"); + return false; + } + + type_ = static_cast(parcel.ReadInt32()); + + return true; +} + +bool NotificationExtensionSubscriptionInfo::ToJson(nlohmann::json& jsonObject) const +{ + jsonObject["addr"] = addr_; + jsonObject["isHfp"] = isHfp_; + jsonObject["type"] = static_cast(type_); + + return true; +} + +NotificationExtensionSubscriptionInfo* NotificationExtensionSubscriptionInfo::FromJson(const nlohmann::json& jsonObject) +{ + if (jsonObject.is_null() or !jsonObject.is_object()) { + ANS_LOGE("Invalid JSON object"); + return nullptr; + } + + auto *pDistributedBundleOption = new (std::nothrow) NotificationExtensionSubscriptionInfo(); + if (pDistributedBundleOption == nullptr) { + ANS_LOGE("null pDistributedBundleOption"); + return nullptr; + } + + const auto& jsonEnd = jsonObject.cend(); + + if (jsonObject.find("addr") != jsonEnd && jsonObject.at("addr").is_string()) { + pDistributedBundleOption->addr_ = jsonObject.at("addr").get(); + } + + if (jsonObject.find("isHfp") != jsonEnd && jsonObject.at("isHfp").is_boolean()) { + pDistributedBundleOption->isHfp_ = jsonObject.at("isHfp").get(); + } + + if (jsonObject.find("type") != jsonEnd && jsonObject.at("type").is_number_integer()) { + auto typeValue = jsonObject.at("type").get(); + pDistributedBundleOption->type_ = static_cast(typeValue); + } + + return pDistributedBundleOption; +} + +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index bcdcab6c6449a23b80d78973e50c96d1ef1967f7..3326d5b6eb0797cd35765112037c8199b981b496 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -789,5 +789,54 @@ ErrCode NotificationHelper::GetLiveViewConfig(const std::vector& bu { return DelayedSingleton::GetInstance()->GetLiveViewConfig(bundleList); } + +ErrCode NotificationHelper::NotificationExtensionSubscribe( + const std::vector>& infos) +{ + return DelayedSingleton::GetInstance()->NotificationExtensionSubscribe(infos); +} + +ErrCode NotificationHelper::NotificationExtensionUnsubscribe() +{ + return DelayedSingleton::GetInstance()->NotificationExtensionUnsubscribe(); +} + +ErrCode NotificationHelper::GetSubscribeInfo(std::vector>& infos) +{ + return DelayedSingleton::GetInstance()->GetSubscribeInfo(infos); +} + +ErrCode NotificationHelper::GetAllSubscriptionBundles(std::vector>& bundles) +{ + return DelayedSingleton::GetInstance()->GetAllSubscriptionBundles(bundles); +} + +ErrCode NotificationHelper::IsUserGranted(bool& enabled) +{ + return DelayedSingleton::GetInstance()->IsUserGranted(enabled); +} + +ErrCode NotificationHelper::GetUserGrantedState(const NotificationBundleOption& targetBundle, bool& enabled) +{ + return DelayedSingleton::GetInstance()->GetUserGrantedState(targetBundle, enabled); +} + +ErrCode NotificationHelper::SetUserGrantedState(const NotificationBundleOption& targetBundle, bool enabled) +{ + return DelayedSingleton::GetInstance()->SetUserGrantedState(targetBundle, enabled); +} + +ErrCode NotificationHelper::GetUserGrantedEnabledBundles( + const NotificationBundleOption& targetBundle, std::vector>& enabledBundles) +{ + return DelayedSingleton::GetInstance()->GetUserGrantedEnabledBundles(targetBundle, enabledBundles); +} + +ErrCode NotificationHelper::SetUserGrantedBundleState(const NotificationBundleOption& targetBundle, + const std::vector>& enabledBundles, bool enabled) +{ + return DelayedSingleton::GetInstance()->SetUserGrantedBundleState( + targetBundle, enabledBundles, enabled); +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/common/include/ans_convert_enum.h b/frameworks/core/common/include/ans_convert_enum.h index b8ec30e6608dc26a5fa587e982622afa2f27510d..b6570f5f38fde5728007fe13250af4aeaafc00bb 100644 --- a/frameworks/core/common/include/ans_convert_enum.h +++ b/frameworks/core/common/include/ans_convert_enum.h @@ -143,6 +143,10 @@ enum class SwitchState { SYSTEM_DEFAULT_ON }; +enum class SubscribeType { + BLUETOOTH = 0 +}; + class AnsEnumUtil { public: /** @@ -297,6 +301,24 @@ public: * @return Returns true if success, returns false otherwise */ static bool LiveViewTypesCToJS(const NotificationLocalLiveViewContent::LiveViewTypes &in, LiveViewTypes &out); + + /** + * @brief Converts subscribe types from js to native + * + * @param inType Indicates a js subscribe type object + * @param outType Indicates a subscribe type object + * @return Returns true if success, returns false otherwise + */ + static bool SubscribeTypeJSToC(const SubscribeType &inType, NotificationConstant::SubscribeType &outType); + + /** + * @brief Converts subscribe status type from native to js + * + * @param inType Indicates a native subscribe status object + * @param outType Indicates a js subscribe status object + * @return Returns true if success, returns false otherwise + */ + static bool SubscribeTypeCToJS(const NotificationConstant::SubscribeType &inType, SubscribeType &outType); }; } } diff --git a/frameworks/core/common/src/ans_convert_enum.cpp b/frameworks/core/common/src/ans_convert_enum.cpp index 90dc89a253dcd60d572b19775ebdaecc7633b085..79d536a2f27b28f76e10b069f35a09724b8ff28b 100644 --- a/frameworks/core/common/src/ans_convert_enum.cpp +++ b/frameworks/core/common/src/ans_convert_enum.cpp @@ -517,5 +517,32 @@ bool AnsEnumUtil::LiveViewTypesCToJS(const NotificationLocalLiveViewContent::Liv return true; } + +bool AnsEnumUtil::SubscribeTypeJSToC(const SubscribeType &inType, NotificationConstant::SubscribeType &outType) +{ + switch (inType) { + case SubscribeType::BLUETOOTH: + outType = NotificationConstant::SubscribeType::BLUETOOTH; + break; + default: + ANS_LOGE("SubscribeType %{public}d", inType); + return false; + } + return true; +} + +bool AnsEnumUtil::SubscribeTypeCToJS(const NotificationConstant::SubscribeType &inType, SubscribeType &outType) +{ + switch (inType) { + case NotificationConstant::SubscribeType::BLUETOOTH: + outType = SubscribeType::BLUETOOTH; + break; + default: + ANS_LOGD("SubscribeType %{public}d", inType); + return false; + } + + return true; +} } } diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index 0f54553b2288ee5249d82c801e21970c6a3a55c8..928ca1d9ca8aababc6d45396ee5946752d33c14a 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -22,6 +22,7 @@ #include "ans_dialog_host_client.h" #include "ans_subscriber_listener.h" #include "ians_manager.h" +#include "notification_extension_subscription_info.h" #include "notification_local_live_view_subscriber.h" #include "notification_subscriber.h" #include "want_params.h" @@ -1435,6 +1436,77 @@ public: */ ErrCode GetLiveViewConfig(const std::vector& bundleList); + /** + * @brief Subscribe the notification when the bluetooth addr is connected. + * + * @param infos The info to be subscribe. + * @return Returns subscribe result. + */ + ErrCode NotificationExtensionSubscribe(const std::vector>& infos); + + /** + * @brief Unsubscribe the notification. + * + * @return Returns unsubscribe result. + */ + ErrCode NotificationExtensionUnsubscribe(); + + /** + * @brief Obtains the subscribe info for app. + * @param infos The returned subscribe info. + * @return Returns get result. + */ + ErrCode GetSubscribeInfo(std::vector>& infos); + + /** + * @brief Obtains the list of bundleOption which granted by user. + * @param bundles The returned list. + * @return Returns get result. + */ + ErrCode GetAllSubscriptionBundles(std::vector>& bundles); + + /** + * @brief Obtains whether the notification extension subscription is enabled. + * @param enabled True if the subscription is enabled. + * @return Returns get result. + */ + ErrCode IsUserGranted(bool& enabled); + + /** + * @brief Obtains whether the notification extension subscription is enabled. + * @param targetBundle The bundle option to be queried. + * @param enabled True if the subscription is enabled. + * @return Returns get result. + */ + ErrCode GetUserGrantedState(const NotificationBundleOption& targetBundle, bool& enabled); + + /** + * @brief Set the notification extension subscription state. + * @param targetBundle The bundle option to be set. + * @param enabled True if the subscription is enabled. + * @return Returns set result. + */ + ErrCode SetUserGrantedState(const NotificationBundleOption& targetBundle, bool enabled); + + /** + * @brief Obtains the list of bundleOption which subscribed by targetBundle + * @param targetBundle The bundle option to be queried. + * @param enabledBundles the list of subscribed bundle options. + * @return Returns get result. + */ + ErrCode GetUserGrantedEnabledBundles(const NotificationBundleOption& targetBundle, + std::vector>& enabledBundles); + + /** + * @brief Set the bundleOptions of the extensionAbility to be subscribed or unsubscribed. + * @param targetBundle The bundle option to be set. + * @param enabledBundles The bundle option list to be configured. + * @param enabled Set enabled or not. + * @return Returns set result. + */ + ErrCode SetUserGrantedBundleState(const NotificationBundleOption& targetBundle, + const std::vector>& enabledBundles, bool enabled); + private: /** * @brief Gets Ans Manager proxy. diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index 704f8734e61a35b6af51aac1bda88417cb34f280..1ae9b90f8ec4722cbb36bf2960d7ab776a155650 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -2593,5 +2593,169 @@ ErrCode AnsNotification::GetDistributedDevicelist(std::vector &devi } return proxy->GetDistributedDevicelist(deviceTypes); } + +ErrCode AnsNotification::NotificationExtensionSubscribe( + const std::vector>& infos) +{ + ANS_LOGD("called"); + if (infos.empty()) { + ANS_LOGE("Invalid infos."); + return ERR_ANS_INVALID_PARAM; + } + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("Get ans manager proxy fail"); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return proxy->NotificationExtensionSubscribe(infos); +} + +ErrCode AnsNotification::NotificationExtensionUnsubscribe() +{ + ANS_LOGD("called"); + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("Get ans manager proxy fail"); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return proxy->NotificationExtensionUnsubscribe(); +} + +ErrCode AnsNotification::GetSubscribeInfo(std::vector>& infos) +{ + ANS_LOGD("called"); + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("Get ans manager proxy fail"); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return proxy->GetSubscribeInfo(infos); +} + +ErrCode AnsNotification::GetAllSubscriptionBundles(std::vector>& bundles) +{ + ANS_LOGD("called"); + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("Get ans manager proxy fail"); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return proxy->GetAllSubscriptionBundles(bundles); +} + +ErrCode AnsNotification::IsUserGranted(bool& enabled) +{ + ANS_LOGD("called"); + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("Get ans manager proxy fail"); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return proxy->IsUserGranted(enabled); +} + +ErrCode AnsNotification::GetUserGrantedState(const NotificationBundleOption& targetBundle, bool& enabled) +{ + ANS_LOGD("called"); + if (targetBundle.GetBundleName().empty()) { + ANS_LOGE("Invalid bundle name."); + return ERR_ANS_INVALID_PARAM; + } + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("Get ans manager proxy fail"); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + sptr bo(new (std::nothrow) NotificationBundleOption(targetBundle)); + if (bo == nullptr) { + ANS_LOGE("null bundleOption"); + return ERR_ANS_INVALID_PARAM; + } + return proxy->GetUserGrantedState(bo, enabled); +} + +ErrCode AnsNotification::SetUserGrantedState(const NotificationBundleOption& targetBundle, bool enabled) +{ + ANS_LOGD("called"); + if (targetBundle.GetBundleName().empty()) { + ANS_LOGE("Invalid bundle name."); + return ERR_ANS_INVALID_PARAM; + } + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("Get ans manager proxy fail"); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + sptr bo(new (std::nothrow) NotificationBundleOption(targetBundle)); + if (bo == nullptr) { + ANS_LOGE("null bundleOption"); + return ERR_ANS_INVALID_PARAM; + } + return proxy->SetUserGrantedState(bo, enabled); +} + +ErrCode AnsNotification::GetUserGrantedEnabledBundles( + const NotificationBundleOption& targetBundle, std::vector>& enabledBundles) +{ + ANS_LOGD("called"); + if (targetBundle.GetBundleName().empty()) { + ANS_LOGE("Invalid bundle name."); + return ERR_ANS_INVALID_PARAM; + } + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("Get ans manager proxy fail"); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + sptr bo(new (std::nothrow) NotificationBundleOption(targetBundle)); + if (bo == nullptr) { + ANS_LOGE("null bundleOption"); + return ERR_ANS_INVALID_PARAM; + } + return proxy->GetUserGrantedEnabledBundles(bo, enabledBundles); +} + +ErrCode AnsNotification::SetUserGrantedBundleState(const NotificationBundleOption& targetBundle, + const std::vector>& enabledBundles, bool enabled) +{ + ANS_LOGD("called"); + if (targetBundle.GetBundleName().empty()) { + ANS_LOGE("Invalid bundle name."); + return ERR_ANS_INVALID_PARAM; + } + if (enabledBundles.empty()) { + ANS_LOGE("Invalid enabledBundles."); + return ERR_ANS_INVALID_PARAM; + } + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("Get ans manager proxy fail"); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + sptr bo(new (std::nothrow) NotificationBundleOption(targetBundle)); + if (bo == nullptr) { + ANS_LOGE("null bundleOption"); + return ERR_ANS_INVALID_PARAM; + } + return proxy->SetUserGrantedBundleState(bo, enabledBundles, enabled); +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index ac0505b7841a38f8f784120ab7559e7bc100501f..f2817eda6f67038bcc643777f3d6f30474992fdd 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -29,6 +29,12 @@ group("napi_notification") { ] } +group("napi_extension_subscription") { + deps = [ + "src/extension_subscription:notificationextensionsubscription", + ] +} + cflags = [ "-fno-math-errno", "-fno-unroll-loops", diff --git a/frameworks/js/napi/include/constant.h b/frameworks/js/napi/include/constant.h index 54d921ca89c08c70c2952ab17b105d7d06f63dd3..84e2cebe23e8cc5344d674af8834680114137089 100644 --- a/frameworks/js/napi/include/constant.h +++ b/frameworks/js/napi/include/constant.h @@ -32,6 +32,7 @@ napi_value DeviceRemindTypeInit(napi_env env, napi_value exports); napi_value ConstantInit(napi_env env, napi_value exports); napi_value LiveViewStatusInit(napi_env env, napi_value exports); napi_value SwitchStateInit(napi_env env, napi_value exports); +napi_value SubscribeTypeInit(napi_env env, napi_value exports); } // namespace NotificationNapi } // namespace OHOS diff --git a/frameworks/js/napi/include/extension_subscription/init_module.h b/frameworks/js/napi/include/extension_subscription/init_module.h new file mode 100644 index 0000000000000000000000000000000000000000..fecde4b5095424e7c0e88e2ac365907265f4fbca --- /dev/null +++ b/frameworks/js/napi/include/extension_subscription/init_module.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 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_INCLUDE_EXTENSION_SUBSCRIPTION_INIT_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_EXTENSION_SUBSCRIPTION_INIT_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace NotificationNapi { +#ifdef __cplusplus +extern "C" { +#endif + +__attribute__((constructor)) void RegisterModule(void); +napi_value NotificationSubscribeInit(napi_env env, napi_value exports); +static napi_value Init(napi_env env, napi_value exports); + +#ifdef __cplusplus +} +#endif + +/* + * Module define + */ +napi_module _module_subscribe = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "notificationExtensionSubscription", + .nm_priv = ((void *)0), + .reserved = {0} + +}; +} // namespace NotificationNapi +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_EXTENSION_SUBSCRIPTION_INIT_H diff --git a/frameworks/js/napi/include/extension_subscription/napi_notification_extension.h b/frameworks/js/napi/include/extension_subscription/napi_notification_extension.h new file mode 100644 index 0000000000000000000000000000000000000000..be27fcf73cd05e024574e461c69f05de6428d715 --- /dev/null +++ b/frameworks/js/napi/include/extension_subscription/napi_notification_extension.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 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_NOTIFICATION_EXTENSION_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_NOTIFICATION_EXTENSION_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +struct AsyncCallbackInfoNotificationExtensionSubscription { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + std::vector> subscriptionInfo; +}; + +struct NotificationExtensionUserGrantedParams { + NotificationBundleOption targetBundle; + bool enabled = false; + std::vector> bundles; +}; + +struct AsyncCallbackInfoNotificationExtensionUserGranted { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + NotificationExtensionUserGrantedParams params; +}; + +napi_value NapiNotificationExtensionOpenSettings(napi_env env, napi_callback_info info); +napi_value NapiNotificationExtensionSubscribe(napi_env env, napi_callback_info info); +napi_value NapiNotificationExtensionUnsubscribe(napi_env env, napi_callback_info info); +napi_value NapiGetSubscribeInfo(napi_env env, napi_callback_info info); +napi_value NapiGetAllSubscriptionBundles(napi_env env, napi_callback_info info); +napi_value NapiIsUserGranted(napi_env env, napi_callback_info info); +napi_value NapiGetUserGrantedState(napi_env env, napi_callback_info info); +napi_value NapiSetUserGrantedState(napi_env env, napi_callback_info info); +napi_value NapiGetUserGrantedEnableBundles(napi_env env, napi_callback_info info); +napi_value NapiSetUserGrantedBundleState(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_NOTIFICATION_EXTENSION_H \ No newline at end of file diff --git a/frameworks/js/napi/src/constant.cpp b/frameworks/js/napi/src/constant.cpp index 629674270769d3f7a61a701a598824d8a139d44b..5c17308cb2b156106b6f6f1db07e5d9649b0ca50 100644 --- a/frameworks/js/napi/src/constant.cpp +++ b/frameworks/js/napi/src/constant.cpp @@ -366,6 +366,23 @@ napi_value SwitchStateInit(napi_env env, napi_value exports) return exports; } +napi_value SubscribeTypeInit(napi_env env, napi_value exports) +{ + ANS_LOGD("%{public}s, called", __func__); + + napi_value obj = nullptr; + napi_create_object(env, &obj); + + SetNamedPropertyByInteger(env, obj, (int32_t)SubscribeType::BLUETOOTH, "BLUETOOTH"); + + napi_property_descriptor exportFuncs[] = { + DECLARE_NAPI_PROPERTY("SubscribeType", obj), + }; + + napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs); + return exports; +} + napi_value ConstantInit(napi_env env, napi_value exports) { RemoveReasonInit(env, exports); @@ -383,6 +400,7 @@ napi_value ConstantInit(napi_env env, napi_value exports) NotificationFlagTypeInit(env, exports); LiveViewStatusInit(env, exports); SwitchStateInit(env, exports); + SubscribeTypeInit(env, exports); return exports; } } // namespace NotificationNapi diff --git a/frameworks/js/napi/src/extension_subscription/BUILD.gn b/frameworks/js/napi/src/extension_subscription/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..8d3c4c015224c34919a9c8daf27c538028346025 --- /dev/null +++ b/frameworks/js/napi/src/extension_subscription/BUILD.gn @@ -0,0 +1,94 @@ +# Copyright (c) 2022-2023 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. + +import("//base/notification/distributed_notification_service/notification.gni") +import("//build/ohos.gni") + +cflags = [ + "-fno-math-errno", + "-fno-unroll-loops", + "-fmerge-all-constants", + "-fno-ident", + "-Oz", + "-flto", + "-ffunction-sections", + "-fdata-sections", +] + +config("native_module_config") { + visibility = [ ":*" ] + + include_dirs = [] + + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } +} + +ohos_shared_library("notificationextensionsubscription") { + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + branch_protector_ret = "pac_ret" + + include_dirs = [ + "../../include", + "../../include/extension_subscription", + ] + + configs = [ ":native_module_config" ] + + sources = [ + "../common.cpp", + "../common_utils.cpp", + "../constant.cpp", + "init_module.cpp", + "napi_notification_extension.cpp", + ] + + deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_context_native", + "ability_runtime:ability_manager", + "ability_runtime:abilitykit_native", + "ability_runtime:app_context", + "ability_runtime:napi_base_context", + "ability_runtime:napi_common", + "ability_runtime:napi_wantagent_common", + "ability_runtime:runtime", + "ability_runtime:ui_extension", + "ability_runtime:wantagent_innerkits", + "access_token:libtokenid_sdk", + "ace_engine:ace_uicontent", + "c_utils:utils", + "ffrt:libffrt", + "hilog:libhilog", + "hitrace:libhitracechain", + "image_framework:image", + "ipc:ipc_single", + "napi:ace_napi", + "relational_store:native_rdb", + ] + + relative_install_dir = "module" + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} diff --git a/frameworks/js/napi/src/extension_subscription/init_module.cpp b/frameworks/js/napi/src/extension_subscription/init_module.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4100c49dd652bedef7d9bdd929d714128d88f032 --- /dev/null +++ b/frameworks/js/napi/src/extension_subscription/init_module.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2025 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 "constant.h" +#include "init_module.h" +#include "napi_notification_extension.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +EXTERN_C_START + +napi_value NotificationSubscribeInit(napi_env env, napi_value exports) +{ + ANS_LOGD("called"); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("openSettings", NapiNotificationExtensionOpenSettings), + DECLARE_NAPI_FUNCTION("subscribe", NapiNotificationExtensionSubscribe), + DECLARE_NAPI_FUNCTION("unsubscribe", NapiNotificationExtensionUnsubscribe), + DECLARE_NAPI_FUNCTION("getSubscribeInfo", NapiGetSubscribeInfo), + DECLARE_NAPI_FUNCTION("getAllSubscriptionBundles", NapiGetAllSubscriptionBundles), + DECLARE_NAPI_FUNCTION("isUserGranted", NapiIsUserGranted), + DECLARE_NAPI_FUNCTION("getUserGrantedState", NapiGetUserGrantedState), + DECLARE_NAPI_FUNCTION("setUserGrantedState", NapiSetUserGrantedState), + DECLARE_NAPI_FUNCTION("getUserGrantedEnableBundles", NapiGetUserGrantedEnableBundles), + DECLARE_NAPI_FUNCTION("setUserGrantedBundleState", NapiSetUserGrantedBundleState), + }; + + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + + return exports; +} +/* + * Module export function + */ +static napi_value Init(napi_env env, napi_value exports) +{ + /* + * Propertise define + */ + NotificationSubscribeInit(env, exports); + ConstantInit(env, exports); + + return exports; +} + +/* + * Module register function + */ +__attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&_module_subscribe); +} +EXTERN_C_END +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/extension_subscription/napi_notification_extension.cpp b/frameworks/js/napi/src/extension_subscription/napi_notification_extension.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6150c64fb49bb660745604a934c0de28c581bf75 --- /dev/null +++ b/frameworks/js/napi/src/extension_subscription/napi_notification_extension.cpp @@ -0,0 +1,794 @@ +/* + * Copyright (c) 2025 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_notification_extension.h" + +#include "ans_inner_errors.h" +#include "js_native_api.h" +#include "js_native_api_types.h" + +namespace OHOS { +namespace NotificationNapi { +namespace { +const int SUBSCRIBE_MAX_PARA = 1; +const int USER_GRANTED_MAX_PARA = 3; +const int NAPI_GET_USER_GRANTED_STATE_MAX_PARA = 1; +const int NAPI_SET_USER_GRANTED_STATE_MAX_PARA = 2; +const int NAPI_GET_USER_GRANTED_ENABLE_BUNDLES_MAX_PARA = 1; +const int NAPI_SET_USER_GRANTED_BUNDLE_STATE_MAX_PARA = 3; +} + +napi_value GetNotificationExtensionSubscriptionInfo( + const napi_env& env, const napi_value& value, sptr& info) +{ + ANS_LOGD("called"); + + if (info == nullptr) { + ANS_LOGW("Invalid NotificationExtensionSubscriptionInfo object ptr."); + return nullptr; + } + + bool hasProperty {false}; + napi_valuetype valuetype = napi_undefined; + napi_value result = nullptr; + + // addr: string + char str[STR_MAX_SIZE] = {0}; + size_t strLen = 0; + napi_get_named_property(env, value, "addr", &result); + NAPI_CALL(env, napi_typeof(env, result, &valuetype)); + if (valuetype != napi_string) { + ANS_LOGE("Wrong argument type. string expected."); + std::string msg = "Incorrect parameter addr. The type of addr must be string."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen)); + info->SetAddr(str); + + // type: SubscribeType + int32_t type = 0; + napi_get_named_property(env, value, "type", &result); + NAPI_CALL(env, napi_typeof(env, result, &valuetype)); + if (valuetype != napi_number) { + ANS_LOGE("Wrong argument type. number expected."); + std::string msg = "Incorrect parameter uid. The type of uid must be number."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + napi_get_value_int32(env, result, &type); + NotificationConstant::SubscribeType outType = NotificationConstant::SubscribeType::BLUETOOTH; + if (!AnsEnumUtil::SubscribeTypeJSToC(SubscribeType(type), outType)) { + std::string msg = "Incorrect parameter types. SubscribeType name must be in enum."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + info->SetType(outType); + + return Common::NapiGetNull(env); +} + +napi_value SetNotificationExtensionSubscriptionInfo( + const napi_env& env, const sptr& info, napi_value& result) +{ + if (info == nullptr) { + ANS_LOGW("Invalid NotificationExtensionSubscriptionInfo object ptr."); + return nullptr; + } + + napi_value value = nullptr; + + // addr: string + napi_create_string_utf8(env, info->GetAddr().c_str(), NAPI_AUTO_LENGTH, &value); + napi_set_named_property(env, result, "addr", value); + + // type: SubscribeType + SubscribeType outType = SubscribeType::BLUETOOTH; + if (!AnsEnumUtil::SubscribeTypeCToJS(info->GetType(), outType)) { + return nullptr; + } + napi_create_int32(env, static_cast(outType), &value); + napi_set_named_property(env, result, "type", value); + + return result; +} + +napi_value ParseParameters(const napi_env& env, const napi_callback_info& info, + std::vector>& subscriptionInfo) +{ + ANS_LOGD("called"); + + size_t argc = SUBSCRIBE_MAX_PARA; + napi_value argv[SUBSCRIBE_MAX_PARA] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + if (argc < SUBSCRIBE_MAX_PARA) { + ANS_LOGE("Wrong number of arguments."); + Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED); + return nullptr; + } + + // argv[0]: info + napi_valuetype valuetype = napi_undefined; + bool isArray = false; + napi_is_array(env, argv[PARAM0], &isArray); + if (!isArray) { + ANS_LOGE("Wrong argument type. Array expected."); + std::string msg = "Incorrect parameter types.The type of param must be array."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + uint32_t length = 0; + napi_get_array_length(env, argv[PARAM0], &length); + if (length == 0) { + ANS_LOGD("The array is empty."); + std::string msg = "Mandatory parameters are left unspecified. The array is empty."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + for (size_t index = 0; index < length; index++) { + napi_value nNotificationExtensionSubscriptionInfo = nullptr; + napi_get_element(env, argv[PARAM0], index, &nNotificationExtensionSubscriptionInfo); + NAPI_CALL_BASE(env, napi_typeof(env, nNotificationExtensionSubscriptionInfo, &valuetype), nullptr); + if (valuetype != napi_object) { + ANS_LOGE("Wrong argument type. Object expected."); + std::string msg = "Incorrect parameter types.The type of param must be object."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + sptr item = + new (std::nothrow) NotificationExtensionSubscriptionInfo(); + if (item == nullptr) { + ANS_LOGE("Failed to create NotificationExtensionSubscriptionInfo."); + std::string msg = + "Parameter verification failed. Failed to create NotificationExtensionSubscriptionInfo ptr"; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + auto retValue = GetNotificationExtensionSubscriptionInfo( + env, nNotificationExtensionSubscriptionInfo, item); + if (retValue == nullptr) { + ANS_LOGE("null retValue"); + Common::NapiThrow(env, ERROR_PARAM_INVALID, PARAMETER_VERIFICATION_FAILED); + return nullptr; + } + subscriptionInfo.emplace_back(item); + } + + return Common::NapiGetNull(env); +} + +napi_value ParseParameters(const napi_env& env, const napi_callback_info& info, + NotificationExtensionUserGrantedParams& params, const size_t expectedArgc) +{ + ANS_LOGD("called"); + + if (expectedArgc < PARAM1 || expectedArgc > USER_GRANTED_MAX_PARA) { + ANS_LOGE("Wrong number of expected argument count."); + std::string msg = "Incorrect expected argument count."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + + size_t argc = USER_GRANTED_MAX_PARA; + napi_value argv[USER_GRANTED_MAX_PARA] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + if (argc < expectedArgc) { + ANS_LOGE("Wrong number of arguments."); + Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED); + return nullptr; + } + + napi_valuetype valuetype = napi_undefined; + // argv[0]: targetBundle + NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); + if (valuetype != napi_object) { + ANS_LOGE("Argument type is incorrect. Object expected."); + std::string msg = "Incorrect parameter types.The type of param must be object."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + auto retValue = Common::GetBundleOption(env, argv[PARAM0], params.targetBundle); + if (retValue == nullptr) { + ANS_LOGE("GetBundleOption failed."); + Common::NapiThrow(env, ERROR_PARAM_INVALID, PARAMETER_VERIFICATION_FAILED); + return nullptr; + } + + if (expectedArgc == PARAM1) { + return Common::NapiGetNull(env); + } + + auto enabledArgIndex = expectedArgc > PARAM2? PARAM2: PARAM1; + // argv[1] or argv[2]: enabled + NAPI_CALL(env, napi_typeof(env, argv[enabledArgIndex], &valuetype)); + if (valuetype != napi_boolean) { + ANS_LOGE("Wrong argument type. Bool expected."); + std::string msg = "Incorrect parameter types.The type of param must be boolean."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + napi_get_value_bool(env, argv[enabledArgIndex], ¶ms.enabled); + + if (expectedArgc == PARAM2) { + return Common::NapiGetNull(env); + } + + // argv[1]: enabledBundles + bool isArray = false; + napi_is_array(env, argv[PARAM1], &isArray); + if (!isArray) { + ANS_LOGE("Wrong argument type. Array expected."); + std::string msg = "Incorrect parameter types.The type of param must be array."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + uint32_t length = 0; + napi_get_array_length(env, argv[PARAM1], &length); + if (length == 0) { + ANS_LOGD("The array is empty."); + std::string msg = "Mandatory parameters are left unspecified. The array is empty."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + for (size_t index = 0; index < length; index++) { + napi_value bundle = nullptr; + napi_get_element(env, argv[PARAM1], index, &bundle); + NAPI_CALL_BASE(env, napi_typeof(env, bundle, &valuetype), nullptr); + if (valuetype != napi_object) { + ANS_LOGE("Wrong argument type. Object expected."); + std::string msg = "Incorrect parameter types.The type of param must be object."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + sptr item = new (std::nothrow) NotificationBundleOption(); + if (item == nullptr) { + ANS_LOGE("Failed to create NotificationBundleOption."); + std::string msg = "Parameter verification failed. Failed to create NotificationBundleOption ptr"; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + auto retValue = Common::GetBundleOption(env, bundle, *item); + if (retValue == nullptr) { + ANS_LOGE("null retValue"); + Common::NapiThrow(env, ERROR_PARAM_INVALID, PARAMETER_VERIFICATION_FAILED); + return nullptr; + } + params.bundles.emplace_back(item); + } + + return Common::NapiGetNull(env); +} + +void AsyncCompleteCallbackReturnVoid(napi_env env, napi_status status, void *data) +{ + ANS_LOGD("called"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackInfoNotificationExtensionSubscription* 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 AsyncCompleteCallbackReturnSubscribeInfoArray(napi_env env, napi_status status, void *data) +{ + ANS_LOGD("called"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackInfoNotificationExtensionSubscription* asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + 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 item : asynccallbackinfo->subscriptionInfo) { + if (item == nullptr) { + ANS_LOGW("Invalid NotificationExtensionSubscriptionInfo object ptr."); + continue; + } + napi_value notificationExtensionSubscriptionInfo = nullptr; + napi_create_object(env, ¬ificationExtensionSubscriptionInfo); + if (!SetNotificationExtensionSubscriptionInfo(env, item, notificationExtensionSubscriptionInfo)) { + ANS_LOGW("Set NotificationExtensionSubscriptionInfo object failed."); + continue; + } + napi_set_element(env, arr, count, notificationExtensionSubscriptionInfo); + ++count; + } + ANS_LOGI("count = %{public}d", count); + result = arr; + if ((count == 0) && (asynccallbackinfo->subscriptionInfo.size() > 0)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + } + Common::ReturnCallbackPromise(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 AsyncCompleteCallbackRetrunBundleOptionArray(napi_env env, napi_status status, void *data) +{ + ANS_LOGD("called"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackInfoNotificationExtensionUserGranted* asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + 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 item : asynccallbackinfo->params.bundles) { + if (item == nullptr) { + ANS_LOGW("Invalid NotificationBundleOption object ptr."); + continue; + } + napi_value bundleOption = nullptr; + napi_create_object(env, &bundleOption); + if (!Common::SetBundleOption(env, *item, bundleOption)) { + ANS_LOGW("Set NotificationBundleOption object failed."); + continue; + } + napi_set_element(env, arr, count, bundleOption); + ++count; + } + ANS_LOGI("count = %{public}d", count); + result = arr; + if ((count == 0) && (asynccallbackinfo->params.bundles.size() > 0)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + } + Common::ReturnCallbackPromise(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 AsyncCompleteCallbackReturnBoolean(napi_env env, napi_status status, void *data) +{ + ANS_LOGD("called"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackInfoNotificationExtensionUserGranted* asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + napi_value result = nullptr; + napi_get_boolean(env, asynccallbackinfo->params.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 NapiNotificationExtensionOpenSettings(napi_env env, napi_callback_info info) +{ + return nullptr; +} + +napi_value NapiNotificationExtensionSubscribe(napi_env env, napi_callback_info info) +{ + ANS_LOGD("called"); + + AsyncCallbackInfoNotificationExtensionSubscription* asynccallbackinfo = new (std::nothrow) + AsyncCallbackInfoNotificationExtensionSubscription { .env = env, .asyncWork = nullptr }; + if (!asynccallbackinfo) { + Common::NapiThrow(env, ERROR_INTERNAL_ERROR); + return Common::JSParaError(env, nullptr); + } + + if (ParseParameters(env, info, asynccallbackinfo->subscriptionInfo) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "notificationExtensionSubscribe", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGD("notificationExtensionSubscribe work excute."); + AsyncCallbackInfoNotificationExtensionSubscription *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::NotificationExtensionSubscribe(asynccallbackinfo->subscriptionInfo); + ANS_LOGI("errorCode = %{public}d", asynccallbackinfo->info.errorCode); + } + }, + AsyncCompleteCallbackReturnVoid, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + + return promise; +} + +napi_value NapiNotificationExtensionUnsubscribe(napi_env env, napi_callback_info info) +{ + ANS_LOGD("called"); + + AsyncCallbackInfoNotificationExtensionSubscription* asynccallbackinfo = new (std::nothrow) + AsyncCallbackInfoNotificationExtensionSubscription { .env = env, .asyncWork = nullptr }; + if (!asynccallbackinfo) { + Common::NapiThrow(env, ERROR_INTERNAL_ERROR); + return Common::JSParaError(env, nullptr); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "notificationExtensionUnsubscribe", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGD("notificationExtensionUnsubscribe work excute."); + AsyncCallbackInfoNotificationExtensionSubscription *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::NotificationExtensionUnsubscribe(); + ANS_LOGI("errorCode = %{public}d", asynccallbackinfo->info.errorCode); + } + }, + AsyncCompleteCallbackReturnVoid, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + + return promise; +} + +napi_value NapiGetSubscribeInfo(napi_env env, napi_callback_info info) +{ + ANS_LOGD("called"); + + AsyncCallbackInfoNotificationExtensionSubscription* asynccallbackinfo = new (std::nothrow) + AsyncCallbackInfoNotificationExtensionSubscription { .env = env, .asyncWork = nullptr }; + if (!asynccallbackinfo) { + Common::NapiThrow(env, ERROR_INTERNAL_ERROR); + return Common::JSParaError(env, nullptr); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getSubscribeInfo", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGD("getSubscribeInfo work excute."); + AsyncCallbackInfoNotificationExtensionSubscription *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::GetSubscribeInfo(asynccallbackinfo->subscriptionInfo); + ANS_LOGI("errorCode = %{public}d", asynccallbackinfo->info.errorCode); + } + }, + AsyncCompleteCallbackReturnSubscribeInfoArray, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + + return promise; +} + +napi_value NapiGetAllSubscriptionBundles(napi_env env, napi_callback_info info) +{ + ANS_LOGD("called"); + + AsyncCallbackInfoNotificationExtensionUserGranted* asynccallbackinfo = new (std::nothrow) + AsyncCallbackInfoNotificationExtensionUserGranted { .env = env, .asyncWork = nullptr }; + if (!asynccallbackinfo) { + Common::NapiThrow(env, ERROR_INTERNAL_ERROR); + return Common::JSParaError(env, nullptr); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getAllSubscriptionBundles", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGD("getAllSubscriptionBundles work excute."); + AsyncCallbackInfoNotificationExtensionUserGranted *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::GetAllSubscriptionBundles(asynccallbackinfo->params.bundles); + ANS_LOGI("errorCode = %{public}d", asynccallbackinfo->info.errorCode); + } + }, + AsyncCompleteCallbackRetrunBundleOptionArray, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + + return promise; +} + +napi_value NapiIsUserGranted(napi_env env, napi_callback_info info) +{ + ANS_LOGD("called"); + + AsyncCallbackInfoNotificationExtensionUserGranted* asynccallbackinfo = new (std::nothrow) + AsyncCallbackInfoNotificationExtensionUserGranted { .env = env, .asyncWork = nullptr }; + if (!asynccallbackinfo) { + Common::NapiThrow(env, ERROR_INTERNAL_ERROR); + return Common::JSParaError(env, nullptr); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "isUserGranted", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGD("isUserGranted work excute."); + AsyncCallbackInfoNotificationExtensionUserGranted *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::IsUserGranted(asynccallbackinfo->params.enabled); + ANS_LOGI("errorCode = %{public}d", asynccallbackinfo->info.errorCode); + } + }, + AsyncCompleteCallbackReturnBoolean, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + + return promise; +} + +napi_value NapiGetUserGrantedState(napi_env env, napi_callback_info info) +{ + ANS_LOGD("called"); + + AsyncCallbackInfoNotificationExtensionUserGranted* asynccallbackinfo = new (std::nothrow) + AsyncCallbackInfoNotificationExtensionUserGranted { .env = env, .asyncWork = nullptr }; + if (!asynccallbackinfo) { + Common::NapiThrow(env, ERROR_INTERNAL_ERROR); + return Common::JSParaError(env, nullptr); + } + + if (ParseParameters(env, info, asynccallbackinfo->params, NAPI_GET_USER_GRANTED_STATE_MAX_PARA) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getUserGrantedState", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGD("getUserGrantedState work excute."); + AsyncCallbackInfoNotificationExtensionUserGranted *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::GetUserGrantedState( + asynccallbackinfo->params.targetBundle, asynccallbackinfo->params.enabled); + ANS_LOGI("errorCode = %{public}d", asynccallbackinfo->info.errorCode); + } + }, + AsyncCompleteCallbackReturnBoolean, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + + return promise; +} + +napi_value NapiSetUserGrantedState(napi_env env, napi_callback_info info) +{ + ANS_LOGD("called"); + + AsyncCallbackInfoNotificationExtensionUserGranted* asynccallbackinfo = new (std::nothrow) + AsyncCallbackInfoNotificationExtensionUserGranted { .env = env, .asyncWork = nullptr }; + if (!asynccallbackinfo) { + Common::NapiThrow(env, ERROR_INTERNAL_ERROR); + return Common::JSParaError(env, nullptr); + } + + if (ParseParameters(env, info, asynccallbackinfo->params, NAPI_SET_USER_GRANTED_STATE_MAX_PARA) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "setUserGrantedState", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGD("setUserGrantedState work excute."); + AsyncCallbackInfoNotificationExtensionUserGranted *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::SetUserGrantedState( + asynccallbackinfo->params.targetBundle, asynccallbackinfo->params.enabled); + ANS_LOGI("errorCode = %{public}d", asynccallbackinfo->info.errorCode); + } + }, + AsyncCompleteCallbackReturnVoid, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + + return promise; +} + +napi_value NapiGetUserGrantedEnableBundles(napi_env env, napi_callback_info info) +{ + ANS_LOGD("called"); + + AsyncCallbackInfoNotificationExtensionUserGranted* asynccallbackinfo = new (std::nothrow) + AsyncCallbackInfoNotificationExtensionUserGranted { .env = env, .asyncWork = nullptr }; + if (!asynccallbackinfo) { + Common::NapiThrow(env, ERROR_INTERNAL_ERROR); + return Common::JSParaError(env, nullptr); + } + + if (ParseParameters(env, info, asynccallbackinfo->params, NAPI_GET_USER_GRANTED_ENABLE_BUNDLES_MAX_PARA) == + nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getUserGrantedEnableBundles", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGD("getUserGrantedEnableBundles work excute."); + AsyncCallbackInfoNotificationExtensionUserGranted *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::GetUserGrantedEnabledBundles( + asynccallbackinfo->params.targetBundle, asynccallbackinfo->params.bundles); + ANS_LOGI("errorCode = %{public}d", asynccallbackinfo->info.errorCode); + } + }, + AsyncCompleteCallbackRetrunBundleOptionArray, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + + return promise; +} + +napi_value NapiSetUserGrantedBundleState(napi_env env, napi_callback_info info) +{ + ANS_LOGD("called"); + + AsyncCallbackInfoNotificationExtensionUserGranted* asynccallbackinfo = new (std::nothrow) + AsyncCallbackInfoNotificationExtensionUserGranted { .env = env, .asyncWork = nullptr }; + if (!asynccallbackinfo) { + Common::NapiThrow(env, ERROR_INTERNAL_ERROR); + return Common::JSParaError(env, nullptr); + } + + if (ParseParameters(env, info, asynccallbackinfo->params, NAPI_SET_USER_GRANTED_BUNDLE_STATE_MAX_PARA) == + nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "setUserGrantedBundleState", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGD("setUserGrantedBundleState work excute."); + AsyncCallbackInfoNotificationExtensionUserGranted *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::SetUserGrantedBundleState(asynccallbackinfo->params.targetBundle, + asynccallbackinfo->params.bundles, asynccallbackinfo->params.enabled); + ANS_LOGI("errorCode = %{public}d", asynccallbackinfo->info.errorCode); + } + }, + AsyncCompleteCallbackReturnVoid, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + + return promise; +} +} // namespace NotificationNapi +} // namespace OHOS diff --git a/interfaces/inner_api/notification_constant.h b/interfaces/inner_api/notification_constant.h index 4734ac39977f555d4db050aa14766a74656dcc8e..64c7bcb5fcaafcb1a35526954330524ee81c83bf 100644 --- a/interfaces/inner_api/notification_constant.h +++ b/interfaces/inner_api/notification_constant.h @@ -187,6 +187,10 @@ public: SUPPORT }; + enum class SubscribeType { + BLUETOOTH = 0 + }; + static const int32_t DEFAULT_REASON_DELETE = 0; /** diff --git a/interfaces/inner_api/notification_extension_subscription_info.h b/interfaces/inner_api/notification_extension_subscription_info.h new file mode 100644 index 0000000000000000000000000000000000000000..343043907927b775a8edc7af6fafca14f2c42afa --- /dev/null +++ b/interfaces/inner_api/notification_extension_subscription_info.h @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2025 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_INTERFACES_INNER_API_EXTENSION_SUBSCRIPTION_INFO_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_EXTENSION_SUBSCRIPTION_INFO_H + +#include "notification_constant.h" +#include "notification_json_convert.h" +#include "parcel.h" + +namespace OHOS { +namespace Notification { +class NotificationExtensionSubscriptionInfo : public Parcelable, public NotificationJsonConvertionBase { +public: + NotificationExtensionSubscriptionInfo() = default; + + /** + * @brief A constructor used to create a NotificationExtensionSubscriptionInfo instance based on the address and + * type. + * + * @param addr Indicates the address. + * @param type Indicates the type. + */ + NotificationExtensionSubscriptionInfo(const std::string& addr, const NotificationConstant::SubscribeType type); + + virtual ~NotificationExtensionSubscriptionInfo(); + + /** + * @brief Get addr. + * + * @return Returns addr. + */ + std::string GetAddr() const; + + /** + * @brief Sets addr. + * + * @param addr addr info. + */ + void SetAddr(const std::string& addr); + + /** + * @brief Get hfp. + * + * @return Returns hfp value. + */ + bool IsHfp() const; + + /** + * @brief Sets hfp. + * + * @param hfp hfp status. + */ + void SetHfp(const bool& hfp); + + /** + * @brief Get type. + * + * @return Returns type value. + */ + NotificationConstant::SubscribeType GetType() const; + + /** + * @brief Sets type. + * + * @param type type value. + */ + void SetType(const NotificationConstant::SubscribeType type); + + /** + * @brief Returns a string representation of the object. + * + * @return Returns a string representation of the object. + */ + std::string Dump(); + + /** + * @brief Marshal a object into a Parcel. + * + * @param parcel Indicates the object into the parcel + * @return Returns true if succeed; returns false otherwise. + */ + virtual bool Marshalling(Parcel& parcel) const override; + + /** + * @brief Unmarshal object from a Parcel. + * + * @param parcel Indicates the parcel object. + * @return Returns the NotificationExtensionSubscriptionInfo + */ + static NotificationExtensionSubscriptionInfo *Unmarshalling(Parcel& parcel); + + /** + * @brief Converts a notification extension subscription info object into a Json. + * + * @param jsonObject Indicates the Json object. + * @return Returns true if succeed; returns false otherwise. + */ + bool ToJson(nlohmann::json& jsonObject) const override; + + /** + * @brief Creates a notification extension subscription info object from a Json. + * + * @param jsonObject Indicates the Json object. + * @return Returns the NotificationExtensionSubscriptionInfo. + */ + static NotificationExtensionSubscriptionInfo *FromJson(const nlohmann::json& jsonObject); + +private: + /** + * @brief Read data from a Parcel. + * + * @param parcel Indicates the parcel object. + * @return Returns true if read success; returns false otherwise. + */ + bool ReadFromParcel(Parcel& parcel); + +private: + std::string addr_ {}; + bool isHfp_ = false; + NotificationConstant::SubscribeType type_ = NotificationConstant::SubscribeType::BLUETOOTH; +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_EXTENSION_SUBSCRIPTION_INFO_H \ No newline at end of file diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 9344b108c445723b809bda1036d3ab30feb8193e..5d12cbc940e7f6d8458d1f3da9e513c355180462 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -23,6 +23,7 @@ #include "notification_do_not_disturb_date.h" #include "notification_do_not_disturb_profile.h" #include "enabled_notification_callback_data.h" +#include "notification_extension_subscription_info.h" #include "notification_request.h" #include "notification_slot.h" #include "notification_sorting_map.h" @@ -1435,6 +1436,77 @@ public: * @return Returns set result. */ static ErrCode GetLiveViewConfig(const std::vector& bundleList); + + /** + * @brief Subscribe the notification when the bluetooth addr is connected. + * + * @param infos The info to be subscribe. + * @return Returns subscribe result. + */ + static ErrCode NotificationExtensionSubscribe( + const std::vector>& infos); + + /** + * @brief Unsubscribe the notification. + * @return Returns unsubscribe result. + */ + static ErrCode NotificationExtensionUnsubscribe(); + + /** + * @brief Obtains the subscribe info for app. + * @param infos The returned subscribe info. + * @return Returns get result. + */ + static ErrCode GetSubscribeInfo(std::vector>& infos); + + /** + * @brief Obtains the list of bundleOption which granted by user. + * @param bundles The returned list. + * @return Returns get result. + */ + static ErrCode GetAllSubscriptionBundles(std::vector>& bundles); + + /** + * @brief Obtains whether the notification extension subscription is enabled. + * @param enabled True if the subscription is enabled. + * @return Returns get result. + */ + static ErrCode IsUserGranted(bool& enabled); + + /** + * @brief Obtains whether the notification extension subscription is enabled. + * @param targetBundle The bundle option to be queried. + * @param enabled True if the subscription is enabled. + * @return Returns get result. + */ + static ErrCode GetUserGrantedState(const NotificationBundleOption& targetBundle, bool& enabled); + + /** + * @brief Set the notification extension subscription state. + * @param targetBundle The bundle option to be set. + * @param enabled True if the subscription is enabled. + * @return Returns set result. + */ + static ErrCode SetUserGrantedState(const NotificationBundleOption& targetBundle, bool enabled); + + /** + * @brief Obtains the list of bundleOption which subscribed by targetBundle + * @param targetBundle The bundle option to be queried. + * @param enabledBundles the list of subscribed bundle options. + * @return Returns get result. + */ + static ErrCode GetUserGrantedEnabledBundles(const NotificationBundleOption& targetBundle, + std::vector>& enabledBundles); + + /** + * @brief Set the bundleOptions of the extensionAbility to be subscribed or unsubscribed. + * @param targetBundle The bundle option to be set. + * @param enabledBundles The bundle option list to be configured. + * @param enabled Set enabled or not. + * @return Returns set result. + */ + static ErrCode SetUserGrantedBundleState(const NotificationBundleOption& targetBundle, + const std::vector>& enabledBundles, bool enabled); }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index 754e060d5b45459f872f372982d8463f8994e351..f44b4974a2e0eeea19697d38ccb2d6ce7280a6af 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -68,6 +68,7 @@ ohos_source_set("ans_service_sources") { "src/advanced_notification_manager/advanced_notification_cancel.cpp", "src/advanced_notification_clone_service.cpp", "src/advanced_notification_event_service.cpp", + "src/advanced_notification_extension_subscription.cpp", "src/advanced_notification_flow_control_service.cpp", "src/advanced_notification_inline.cpp", "src/advanced_notification_live_view_service.cpp", diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index ed0e690f262838a045adb77f25d7c771df1e9bf7..be9f8143f90a12e87ee07ed71cf9c5f9a30b7744 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1446,6 +1446,28 @@ public: const std::string& value) override; ErrCode GetLiveViewConfig(const std::vector& bundleList) override; + + ErrCode NotificationExtensionSubscribe( + const std::vector>& infos) override; + + ErrCode NotificationExtensionUnsubscribe() override; + + ErrCode GetSubscribeInfo(std::vector>& infos) override; + + ErrCode GetAllSubscriptionBundles(std::vector>& bundles) override; + + ErrCode IsUserGranted(bool& isEnabled) override; + + ErrCode GetUserGrantedState(const sptr& targetBundle, bool& enabled) override; + + ErrCode SetUserGrantedState(const sptr& targetBundle, bool enabled) override; + + ErrCode GetUserGrantedEnabledBundles(const sptr& targetBundle, + std::vector>& enabledBundles) override; + + ErrCode SetUserGrantedBundleState(const sptr& targetBundle, + const std::vector>& enabledBundles, bool enabled) override; + protected: /** * @brief Query whether there is a agent relationship between the two apps. diff --git a/services/ans/src/advanced_notification_extension_subscription.cpp b/services/ans/src/advanced_notification_extension_subscription.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e446804f78422cf5e172d750decb832d5e155b18 --- /dev/null +++ b/services/ans/src/advanced_notification_extension_subscription.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2025 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 "advanced_notification_service.h" + +namespace OHOS { +namespace Notification { + +ErrCode AdvancedNotificationService::NotificationExtensionSubscribe( + const std::vector>& infos) +{ + return ERR_OK; +} + +ErrCode AdvancedNotificationService::NotificationExtensionUnsubscribe() +{ + return ERR_OK; +} + +ErrCode AdvancedNotificationService::GetSubscribeInfo(std::vector>& infos) +{ + return ERR_OK; +} + +ErrCode AdvancedNotificationService::GetAllSubscriptionBundles(std::vector>& bundles) +{ + return ERR_OK; +} + +ErrCode AdvancedNotificationService::IsUserGranted(bool& isEnabled) +{ + return ERR_OK; +} + +ErrCode AdvancedNotificationService::GetUserGrantedState( + const sptr& targetBundle, bool& enabled) +{ + return ERR_OK; +} + +ErrCode AdvancedNotificationService::SetUserGrantedState( + const sptr& targetBundle, bool enabled) +{ + return ERR_OK; +} + +ErrCode AdvancedNotificationService::GetUserGrantedEnabledBundles( + const sptr& targetBundle, std::vector>& enabledBundles) +{ + return ERR_OK; +} + +ErrCode AdvancedNotificationService::SetUserGrantedBundleState( + const sptr& targetBundle, + const std::vector>& enabledBundles, bool enabled) +{ + return ERR_OK; +} +} // namespace Notification +} // namespace OHOS