From 9ab9e216056ed91fbc13220b9008554e1036b031 Mon Sep 17 00:00:00 2001 From: markYao Date: Fri, 17 Nov 2023 14:37:56 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9EsubscribeSelf=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: markYao --- frameworks/ans/src/notification_helper.cpp | 5 + .../core/include/ans_manager_interface.h | 8 ++ frameworks/core/include/ans_manager_proxy.h | 9 ++ frameworks/core/include/ans_manager_stub.h | 9 ++ frameworks/core/include/ans_notification.h | 13 +++ ..._notification_service_ipc_interface_code.h | 1 + frameworks/core/src/ans_manager_proxy.cpp | 36 +++++++- frameworks/core/src/ans_manager_stub.cpp | 22 +++++ frameworks/core/src/ans_notification.cpp | 17 ++++ .../ans_notification_branch_test.cpp | 5 + .../napi/include/subscribe/napi_subscribe.h | 1 + .../js/napi/src/subscribe/init_module.cpp | 1 + .../js/napi/src/subscribe/napi_subscribe.cpp | 91 +++++++++++++++++++ interfaces/inner_api/notification_helper.h | 13 +++ .../include/advanced_notification_service.h | 8 ++ .../ans/src/advanced_notification_service.cpp | 39 ++++++++ 16 files changed, 277 insertions(+), 1 deletion(-) diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 2e1c71735..6c4455ec7 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -167,6 +167,11 @@ ErrCode NotificationHelper::SubscribeNotification(const NotificationSubscriber & return DelayedSingleton::GetInstance()->SubscribeNotification(subscriber); } +ErrCode NotificationHelper::SubscribeNotificationSelf(const NotificationSubscriber &subscriber) +{ + return DelayedSingleton::GetInstance()->SubscribeNotificationSelf(subscriber); +} + ErrCode NotificationHelper::SubscribeLocalLiveViewNotification(const NotificationLocalLiveViewSubscriber &subscriber) { return DelayedSingleton::GetInstance()->SubscribeLocalLiveViewNotification(subscriber); diff --git a/frameworks/core/include/ans_manager_interface.h b/frameworks/core/include/ans_manager_interface.h index a58d2237f..34c261fc4 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -400,6 +400,14 @@ public: virtual ErrCode Subscribe(const sptr &subscriber, const sptr &info) = 0; + /** + * @brief Subscribes notifications self. + * + * @param subscriber Indicates the subscriber. + * @return Returns ERR_OK on success, others on failure. + */ + virtual ErrCode SubscribeSelf(const sptr &subscriber) = 0; + /** * @brief Subscribes local live view notifications. * diff --git a/frameworks/core/include/ans_manager_proxy.h b/frameworks/core/include/ans_manager_proxy.h index 5c91814e3..a44909d3a 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -387,6 +387,15 @@ public: ErrCode Subscribe(const sptr &subscriber, const sptr &info) override; + /** + * @brief Subscribes notifications self. + * + * @param subscriber Indicates the subscriber. + * @param info Indicates the NotificationSubscribeInfo object. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode SubscribeSelf(const sptr &subscriber) override; + /** * @brief Subscribes notifications. * diff --git a/frameworks/core/include/ans_manager_stub.h b/frameworks/core/include/ans_manager_stub.h index 4da073244..560c2f60f 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -404,6 +404,14 @@ public: */ virtual ErrCode Subscribe( const sptr &subscriber, const sptr &info) override; + + /** + * @brief Subscribes notifications self. + * + * @param subscriber Indicates the subscriber. + * @return Returns ERR_OK on success, others on failure. + */ + virtual ErrCode SubscribeSelf(const sptr &subscriber) override; virtual ErrCode SubscribeLocalLiveView( const sptr &subscriber, @@ -780,6 +788,7 @@ private: ErrCode HandleUnregisterPushCallback(MessageParcel &data, MessageParcel &reply); ErrCode HandleSubscribeLocalLiveView(MessageParcel &data, MessageParcel &reply); ErrCode HandleTriggerLocalLiveView(MessageParcel &data, MessageParcel &reply); + ErrCode HandleSubscribeSelf(MessageParcel &data, MessageParcel &reply); template bool WriteParcelableVector(const std::vector> &parcelableVector, MessageParcel &reply, ErrCode &result) diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index fff7608ec..bfe213685 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -301,6 +301,19 @@ public: */ ErrCode SubscribeNotification(const NotificationSubscriber &subscriber); + /** + * @brief Subscribes to notifications from the appliaction self. + * @note To subscribe to a notification, inherit the {NotificationSubscriber} class, override its + * callback methods and create a subscriber. The subscriber will be used as a parameter of this method. + * After the notification is published, subscribers that meet the filter criteria can receive the + * notification. + * + * @param subscriber Indicates the {NotificationSubscriber} to receive notifications. + * This parameter must be specified. + * @return Returns subscribe notification result. + */ + ErrCode SubscribeNotificationSelf(const NotificationSubscriber &subscriber); + /** * @brief Subscribes liveView notification. This method can be called only by applications * with required system permissions. diff --git a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h index 78f24ac5d..a49d2127e 100644 --- a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h +++ b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h @@ -120,6 +120,7 @@ namespace Notification { ON_RESPONSE, SUBSCRIBE_LOCAL_LIVE_VIEW_NOTIFICATION, TRIGGER_LOCAL_LIVE_VIEW_NOTIFICATION, + SUBSCRIBE_NOTIFICATION_SELF, }; } } diff --git a/frameworks/core/src/ans_manager_proxy.cpp b/frameworks/core/src/ans_manager_proxy.cpp index 50666b0c8..233a4b7a8 100644 --- a/frameworks/core/src/ans_manager_proxy.cpp +++ b/frameworks/core/src/ans_manager_proxy.cpp @@ -1395,7 +1395,6 @@ ErrCode AnsManagerProxy::Subscribe(const sptr &subscribe return ERR_ANS_PARCELABLE_FAILED; } } - MessageParcel reply; MessageOption option = {MessageOption::TF_SYNC}; ErrCode result = InnerTransact(NotificationInterfaceCode::SUBSCRIBE_NOTIFICATION, option, data, reply); @@ -1412,6 +1411,41 @@ ErrCode AnsManagerProxy::Subscribe(const sptr &subscribe return result; } +ErrCode AnsManagerProxy::SubscribeSelf(const sptr &subscriber) +{ + if (subscriber == nullptr) { + ANS_LOGE("[SubscribeSelf] fail: subscriber is empty."); + return ERR_ANS_INVALID_PARAM; + } + + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("[SubscribeSelf] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + bool ret = data.WriteRemoteObject(subscriber->AsObject()); + if (!ret) { + ANS_LOGE("[SubscribeSelf] fail: write subscriber failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::SUBSCRIBE_NOTIFICATION_SELF, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[SubscribeSelf] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("[SubscribeSelf] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + ErrCode AnsManagerProxy::SubscribeLocalLiveView(const sptr &subscriber, const sptr &info) { diff --git a/frameworks/core/src/ans_manager_stub.cpp b/frameworks/core/src/ans_manager_stub.cpp index 47254fb7c..a7fe77c06 100644 --- a/frameworks/core/src/ans_manager_stub.cpp +++ b/frameworks/core/src/ans_manager_stub.cpp @@ -1146,6 +1146,22 @@ ErrCode AnsManagerStub::HandleSubscribe(MessageParcel &data, MessageParcel &repl return ERR_OK; } +ErrCode AnsManagerStub::HandleSubscribeSelf(MessageParcel &data, MessageParcel &reply) +{ + sptr subscriber = data.ReadRemoteObject(); + if (subscriber == nullptr) { + ANS_LOGE("[HandleSubscribeSelf] fail: read subscriber failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + ErrCode result = SubscribeSelf(iface_cast(subscriber)); + if (!reply.WriteInt32(result)) { + ANS_LOGE("[HandleSubscribeSelf] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + return ERR_OK; +} + ErrCode AnsManagerStub::HandleSubscribeLocalLiveView(MessageParcel &data, MessageParcel &reply) { sptr subscriber = data.ReadRemoteObject(); @@ -2068,6 +2084,12 @@ ErrCode AnsManagerStub::Subscribe(const sptr &subscriber return ERR_INVALID_OPERATION; } +ErrCode AnsManagerStub::SubscribeSelf(const sptr &subscriber) +{ + ANS_LOGE("AnsManagerStub::SubscribeSelf called!"); + return ERR_INVALID_OPERATION; +} + ErrCode AnsManagerStub::SubscribeLocalLiveView(const sptr &subscriber, const sptr &info) { diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index 383756662..6bb04ed4d 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -18,6 +18,7 @@ #include "ans_inner_errors.h" #include "ans_log_wrapper.h" #include "hitrace_meter_adapter.h" +#include "ipc_skeleton.h" #include "iservice_registry.h" #include "notification_button_option.h" #include "notification_local_live_view_subscriber.h" @@ -381,6 +382,22 @@ ErrCode AnsNotification::SubscribeNotification(const NotificationSubscriber &sub return ansManagerProxy_->Subscribe(subscriberSptr, nullptr); } +ErrCode AnsNotification::SubscribeNotificationSelf(const NotificationSubscriber &subscriber) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + sptr subscriberSptr = subscriber.GetImpl(); + if (subscriberSptr == nullptr) { + ANS_LOGE("Failed to subscribeSelf with SubscriberImpl null ptr."); + return ERR_ANS_INVALID_PARAM; + } + return ansManagerProxy_->SubscribeSelf(subscriberSptr); +} + ErrCode AnsNotification::SubscribeLocalLiveViewNotification(const NotificationLocalLiveViewSubscriber &subscriber) { HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index df43fc5f2..c9aa350c6 100755 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -249,6 +249,11 @@ public: return ERR_ANS_INVALID_PARAM; } + ErrCode SubscribeSelf(const sptr &subscriber) override + { + return ERR_ANS_INVALID_PARAM; + } + ErrCode SubscribeLocalLiveView(const sptr &subscriber, const sptr &info) override { diff --git a/frameworks/js/napi/include/subscribe/napi_subscribe.h b/frameworks/js/napi/include/subscribe/napi_subscribe.h index 5bd699e83..046401568 100644 --- a/frameworks/js/napi/include/subscribe/napi_subscribe.h +++ b/frameworks/js/napi/include/subscribe/napi_subscribe.h @@ -22,6 +22,7 @@ namespace NotificationNapi { using namespace OHOS::Notification; napi_value NapiSubscribe(napi_env env, napi_callback_info info); +napi_value NapiSubscribeSelf(napi_env env, napi_callback_info info); napi_value NapiUnsubscribe(napi_env env, napi_callback_info info); } // namespace NotificationNapi } // namespace OHOS diff --git a/frameworks/js/napi/src/subscribe/init_module.cpp b/frameworks/js/napi/src/subscribe/init_module.cpp index 89f17d11e..8a0c21680 100644 --- a/frameworks/js/napi/src/subscribe/init_module.cpp +++ b/frameworks/js/napi/src/subscribe/init_module.cpp @@ -34,6 +34,7 @@ napi_value NotificationSubscribeInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("unsubscribe", NapiUnsubscribe), DECLARE_NAPI_FUNCTION("remove", NapiRemove), DECLARE_NAPI_FUNCTION("removeAll", NapiRemoveAll), + DECLARE_NAPI_FUNCTION("subscribeSelf", NapiSubscribeSelf), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); diff --git a/frameworks/js/napi/src/subscribe/napi_subscribe.cpp b/frameworks/js/napi/src/subscribe/napi_subscribe.cpp index eed5bcde2..6773be6fa 100644 --- a/frameworks/js/napi/src/subscribe/napi_subscribe.cpp +++ b/frameworks/js/napi/src/subscribe/napi_subscribe.cpp @@ -121,6 +121,97 @@ napi_value NapiSubscribe(napi_env env, napi_callback_info info) } } +napi_value NapiSubscribeSelf(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + napi_ref callback = nullptr; + SubscriberInstance *objectInfo = nullptr; + NotificationSubscribeInfo subscriberInfo; + if (ParseParameters(env, info, subscriberInfo, objectInfo, callback) == nullptr) { + if (objectInfo) { + delete objectInfo; + objectInfo = nullptr; + } + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoSubscribe *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoSubscribe { + .env = env, .asyncWork = nullptr, .objectInfo = objectInfo, .subscriberInfo = subscriberInfo + }; + if (!asynccallbackinfo) { + if (objectInfo) { + delete objectInfo; + objectInfo = nullptr; + } + 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, "subscribeNotificationSelf", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("NapiSubscribeSelf work excute."); + if (!data) { + ANS_LOGE("Invalid asynccallbackinfo!"); + return; + } + auto asynccallbackinfo = reinterpret_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::SubscribeNotificationSelf(*(asynccallbackinfo->objectInfo)); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("NapiSubscribeSelf work complete."); + if (!data) { + ANS_LOGE("Invalid asynccallbackinfo!"); + return; + } + auto asynccallbackinfo = reinterpret_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + ANS_LOGD("Delete napiSubscribeSelf callback reference."); + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + ANS_LOGD("NapiSubscribeSelf work complete end."); + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + bool isCallback = asynccallbackinfo->info.isCallback; + napi_status status = napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + if (status != napi_ok) { + ANS_LOGE("Queue napiSubscribeSelf work failed return: %{public}d", status); + asynccallbackinfo->info.errorCode = ERROR_INTERNAL_ERROR; + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + ANS_LOGD("Delete napiSubscribeSelf callback reference."); + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + + if (isCallback) { + ANS_LOGD("NapiSubscribeSelf callback is nullptr."); + return Common::NapiGetNull(env); + } else { + return promise; + } +} + napi_value NapiUnsubscribe(napi_env env, napi_callback_info info) { ANS_LOGI("Unsubscribe start"); diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index e07cd0d17..fe4d71634 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -304,6 +304,19 @@ public: */ static ErrCode SubscribeNotification(const NotificationSubscriber &subscriber); + /** + * @brief Subscribes to notifications from the appliaction self. + * @note To subscribe to a notification, inherit the {NotificationSubscriber} class, override its + * callback methods and create a subscriber. The subscriber will be used as a parameter of this method. + * After the notification is published, subscribers that meet the filter criteria can receive the + * notification. + * + * @param subscriber Indicates the {NotificationSubscriber} to receive notifications. + * This parameter must be specified. + * @return Returns unsubscribe notification result. + */ + static ErrCode SubscribeNotificationSelf(const NotificationSubscriber &subscriber); + /** * @brief Subscribes to all notifications based on the filtering criteria. This method can be called only * by applications with required system permissions. diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index eb54f4259..0706a00c5 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -416,6 +416,14 @@ public: ErrCode Subscribe(const sptr &subscriber, const sptr &info) override; + /** + * @brief Subscribes notifications self. + * + * @param subscriber Indicates the subscriber. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode SubscribeSelf(const sptr &subscriber) override; + /** * @brief Subscribes notifications. * diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 93fae9692..aa6ef0b18 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1531,6 +1531,45 @@ ErrCode AdvancedNotificationService::Subscribe( return errCode; } +ErrCode AdvancedNotificationService::SubscribeSelf(const sptr &subscriber) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + ANS_LOGD("%{public}s", __FUNCTION__); + sptr sptrInfo = new (std::nothrow) NotificationSubscribeInfo(); + ErrCode errCode = ERR_OK; + do { + if (subscriber == nullptr) { + errCode = ERR_ANS_INVALID_PARAM; + break; + } + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGE("Client is not a system app or subsystem"); + errCode = ERR_ANS_NON_SYSTEM_APP; + break; + } + + int32_t uid = IPCSkeleton().GetCallingUid(); + // subscribeSelf doesn't need OHOS_PERMISSION_NOTIFICATION_CONTROLLER permission + std::string bundle; + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + if (bundleManager != nullptr) { + bundle = bundleManager->GetBundleNameByUid(uid); + } + + sptrInfo->AddAppName(bundle); + + errCode = NotificationSubscriberManager::GetInstance()->AddSubscriber(subscriber, sptrInfo); + if (errCode != ERR_OK) { + break; + } + } while (0); + + SendSubscribeHiSysEvent(IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid(), sptrInfo, errCode); + return errCode; +} + ErrCode AdvancedNotificationService::SubscribeLocalLiveView( const sptr &subscriber, const sptr &info) { -- Gitee From 20c4179f4ed9bb48d03fd2fb5ebfdbccf2a00396 Mon Sep 17 00:00:00 2001 From: markYao Date: Wed, 22 Nov 2023 21:56:21 +0800 Subject: [PATCH 2/2] =?UTF-8?q?liveViewContent=E8=BF=94=E5=9B=9E=E5=80=BC?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: markYao --- .../notification_local_live_view_content.cpp | 32 ++++++++++ frameworks/js/napi/src/common.cpp | 64 +++++++++++-------- .../notification_local_live_view_content.h | 24 +++++++ 3 files changed, 95 insertions(+), 25 deletions(-) diff --git a/frameworks/ans/src/notification_local_live_view_content.cpp b/frameworks/ans/src/notification_local_live_view_content.cpp index 12d770e8e..dd5775ebe 100644 --- a/frameworks/ans/src/notification_local_live_view_content.cpp +++ b/frameworks/ans/src/notification_local_live_view_content.cpp @@ -18,6 +18,7 @@ #include #include // for basic_string, operator+ #include // for min +#include #include "ans_log_wrapper.h" #include "nlohmann/json.hpp" // for json, basic_json<>::obje... @@ -83,6 +84,22 @@ NotificationTime NotificationLocalLiveViewContent::GetTime() return time_; } +void NotificationLocalLiveViewContent::addFlag(int32_t flag) +{ + flags_.emplace_back(flag); +} + +bool NotificationLocalLiveViewContent::isFlagExist(int32_t flag) +{ + auto it = std::find(flags_.begin(), flags_.end(), flag); + if (it != flags_.end()) { + return true; + } else { + return false; + } + +} + std::string NotificationLocalLiveViewContent::Dump() { return "NotificationLocalLiveViewContent{ " + NotificationBasicContent::Dump() + @@ -130,6 +147,7 @@ bool NotificationLocalLiveViewContent::ToJson(nlohmann::json &jsonObject) const jsonObject["button"] = buttonObj; jsonObject["progress"] = progressObj; jsonObject["time"] = timeObj; + jsonObject["flags"] = nlohmann::json(flags_); return true; } @@ -194,6 +212,10 @@ NotificationLocalLiveViewContent *NotificationLocalLiveViewContent::FromJson(con } } + if (jsonObject.find("flags") != jsonEnd && jsonObject.at("flags").is_array()) { + pContent->flags_ = jsonObject.at("flags").get>(); + } + return pContent; } @@ -229,6 +251,11 @@ bool NotificationLocalLiveViewContent::Marshalling(Parcel &parcel) const return false; } + if (!parcel.WriteInt32Vector(flags_)) { + ANS_LOGE("Failed to write flags"); + return false; + } + return true; } @@ -291,6 +318,11 @@ bool NotificationLocalLiveViewContent::ReadFromParcel(Parcel &parcel) delete pTime; pTime = nullptr; + if (!parcel.ReadInt32Vector(&flags_)) { + ANS_LOGE("Failed to read flags"); + return false; + } + return true; } } // namespace Notification diff --git a/frameworks/js/napi/src/common.cpp b/frameworks/js/napi/src/common.cpp index f46b609b5..60359e048 100644 --- a/frameworks/js/napi/src/common.cpp +++ b/frameworks/js/napi/src/common.cpp @@ -1019,40 +1019,48 @@ napi_value Common::SetNotificationLocalLiveViewContent( napi_set_named_property(env, result, "typeCode", value); // capsule: NotificationCapsule - napi_value capsule = nullptr; - napi_create_object(env, &capsule); - if (!SetCapsule(env, localLiveViewContent->GetCapsule(), capsule)) { - ANS_LOGE("SetCapsule call failed"); - return NapiGetBoolean(env, false); + if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE)) { + napi_value capsule = nullptr; + napi_create_object(env, &capsule); + if (!SetCapsule(env, localLiveViewContent->GetCapsule(), capsule)) { + ANS_LOGE("SetCapsule call failed"); + return NapiGetBoolean(env, false); + } + napi_set_named_property(env, result, "capsule", capsule); } - napi_set_named_property(env, result, "capsule", capsule); // button: NotificationLocalLiveViewButton - napi_value button = nullptr; - napi_create_object(env, &button); - if (!SetButton(env, localLiveViewContent->GetButton(), button)) { - ANS_LOGE("SetButton call failed"); - return NapiGetBoolean(env, false); + if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON)) { + napi_value button = nullptr; + napi_create_object(env, &button); + if (!SetButton(env, localLiveViewContent->GetButton(), button)) { + ANS_LOGE("SetButton call failed"); + return NapiGetBoolean(env, false); + } + napi_set_named_property(env, result, "button", button); } - napi_set_named_property(env, result, "button", button); // progress: NotificationProgress - napi_value progress = nullptr; - napi_create_object(env, &progress); - if (!SetProgress(env, localLiveViewContent->GetProgress(), progress)) { - ANS_LOGE("SetProgress call failed"); - return NapiGetBoolean(env, false); + if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::PROGRESS)) { + napi_value progress = nullptr; + napi_create_object(env, &progress); + if (!SetProgress(env, localLiveViewContent->GetProgress(), progress)) { + ANS_LOGE("SetProgress call failed"); + return NapiGetBoolean(env, false); + } + napi_set_named_property(env, result, "progress", progress); } - napi_set_named_property(env, result, "progress", progress); - + // time: NotificationTime - napi_value time = nullptr; - napi_create_object(env, &time); - if (!SetTime(env, localLiveViewContent->GetTime(), time)) { - ANS_LOGE("SetMessageUser call failed"); - return NapiGetBoolean(env, false); + if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::TIME)) { + napi_value time = nullptr; + napi_create_object(env, &time); + if (!SetTime(env, localLiveViewContent->GetTime(), time)) { + ANS_LOGE("SetMessageUser call failed"); + return NapiGetBoolean(env, false); + } + napi_set_named_property(env, result, "time", time); } - napi_set_named_property(env, result, "time", time); return NapiGetBoolean(env, true); } @@ -4414,6 +4422,7 @@ napi_value Common::GetNotificationLocalLiveViewCapsule( } content->SetCapsule(capsule); + content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE); return NapiGetNull(env); } @@ -4493,6 +4502,7 @@ napi_value Common::GetNotificationLocalLiveViewButton( } ANS_LOGD("button buttonIcon = %{public}s", str); content->SetButton(button); + content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON); return NapiGetNull(env); } @@ -4558,6 +4568,8 @@ napi_value Common::GetNotificationLocalLiveViewProgress(const napi_env &env, con } content->SetProgress(progress); + content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::PROGRESS); + return NapiGetNull(env); } @@ -4635,6 +4647,8 @@ napi_value Common::GetNotificationLocalLiveViewTime(const napi_env &env, const n } content->SetTime(time); + content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::TIME); + return NapiGetNull(env); } diff --git a/interfaces/inner_api/notification_local_live_view_content.h b/interfaces/inner_api/notification_local_live_view_content.h index 7807c1118..f2f8cb2ed 100644 --- a/interfaces/inner_api/notification_local_live_view_content.h +++ b/interfaces/inner_api/notification_local_live_view_content.h @@ -25,11 +25,19 @@ #include "notification_json_convert.h" #include "notification_time.h" #include "parcel.h" +#include namespace OHOS { namespace Notification { class NotificationLocalLiveViewContent : public NotificationBasicContent { public: + enum LiveViewContentInner { + CAPSULE = 1, + BUTTON, + PROGRESS, + TIME, + }; + NotificationLocalLiveViewContent() = default; ~NotificationLocalLiveViewContent() = default; @@ -100,6 +108,21 @@ public: */ NotificationTime GetTime(); + /* + * @add flag function. + * + * @param flag Indicates the flag to be added. + + */ + void addFlag(int32_t flag); + + /* + * @return is the given flag exist. + * + * @param flag Indicates the flag to be added. + */ + bool isFlagExist(int32_t flag); + /** * @brief Returns a string representation of the object. * @@ -154,6 +177,7 @@ private: NotificationLocalLiveViewButton button_ {}; NotificationProgress progress_ {}; NotificationTime time_ {}; + std::vector flags_ {}; }; } // namespace Notification } // namespace OHOS -- Gitee