diff --git a/frameworks/ans/src/notification_live_view_content.cpp b/frameworks/ans/src/notification_live_view_content.cpp index a8523142832752377507a47916527f91f9e32c5c..0630c98ebe7dd1ae69346f5e888d210eebf1d327 100644 --- a/frameworks/ans/src/notification_live_view_content.cpp +++ b/frameworks/ans/src/notification_live_view_content.cpp @@ -19,6 +19,7 @@ #include "ans_ipc_common_utils.h" #include "ans_log_wrapper.h" #include "want_params_wrapper.h" +#include "want_agent_helper.h" #include "ans_const_define.h" namespace OHOS { @@ -74,6 +75,27 @@ bool NotificationLiveViewContent::GetIsOnlyLocalUpdate() const return isOnlyLocalUpdate_; } +void NotificationLiveViewContent::SetExtensionWantAgent( + const std::shared_ptr &wantAgent) +{ + extensionWantAgent_ = wantAgent; +} + +const std::shared_ptr NotificationLiveViewContent::GetExtensionWantAgent() const +{ + return extensionWantAgent_; +} + +void NotificationLiveViewContent::SetUid(const int32_t uid) +{ + uid_ = uid; +} + +int32_t NotificationLiveViewContent::GetUid() const +{ + return uid_; +} + std::string NotificationLiveViewContent::Dump() { std::string extraStr{"null"}; @@ -95,7 +117,8 @@ std::string NotificationLiveViewContent::Dump() return "NotificationLiveViewContent{ " + NotificationBasicContent::Dump() + ", status = " + std::to_string(static_cast(liveViewStatus_)) + ", version = " + std::to_string(static_cast(version_)) + ", extraInfo = " + extraStr + - ", isOnlyLocalUpdate_ = " + (GetIsOnlyLocalUpdate()?"true":"false") + pictureStr + "}"; + ", isOnlyLocalUpdate_ = " + (GetIsOnlyLocalUpdate()?"true":"false") + pictureStr + + ", extensionWantAgent_ = " + (extensionWantAgent_ ? "not null" : "null") + "}"; } bool NotificationLiveViewContent::PictureToJson(nlohmann::json &jsonObject) const @@ -132,6 +155,10 @@ bool NotificationLiveViewContent::ToJson(nlohmann::json &jsonObject) const } jsonObject["isLocalUpdateOnly"] = isOnlyLocalUpdate_; + if (extensionWantAgent_ != nullptr) { + jsonObject["extensionWantAgent"] = AbilityRuntime::WantAgent::WantAgentHelper::ToString(extensionWantAgent_); + jsonObject["uid"] = uid_; + } return PictureToJson(jsonObject); } @@ -190,6 +217,18 @@ NotificationLiveViewContent *NotificationLiveViewContent::FromJson(const nlohman pContent->isOnlyLocalUpdate_ = jsonObject.at("isOnlyLocalUpdate").get(); } pContent->ConvertPictureFromJson(jsonObject); + + if (jsonObject.find("uid") != jsonEnd && jsonObject.at("uid").is_number_integer()) { + pContent->uid_ =jsonObject.at("uid").get(); + } + + if (jsonObject.find("extensionWantAgent") != jsonEnd && jsonObject.at("extensionWantAgent").is_string()) { + auto extensionWantAgentString = jsonObject.at("extensionWantAgent").get(); + pContent->extensionWantAgent_ = AbilityRuntime::WantAgent::WantAgentHelper::FromString( + extensionWantAgentString, pContent->uid_); + } else { + ANS_LOGW("no want"); + } return pContent; } @@ -233,7 +272,30 @@ bool NotificationLiveViewContent::Marshalling(Parcel &parcel) const return false; } - return MarshallingPictureMap(parcel); + bool res = MarshallingPictureMap(parcel); + if (!res) { + return res; + } + return MarshallingExtensionWantAgent(parcel); +} + +bool NotificationLiveViewContent::MarshallingExtensionWantAgent(Parcel &parcel) const +{ + bool valid{false}; + + valid = extensionWantAgent_ ? true : false; + if (!parcel.WriteBool(valid)) { + ANS_LOGE("Failed to write the flag which indicate whether wantAgent is null"); + return false; + } + + if (valid) { + if (!parcel.WriteParcelable(extensionWantAgent_.get())) { + ANS_LOGE("Failed to write wantAgent"); + return false; + } + } + return true; } NotificationLiveViewContent *NotificationLiveViewContent::Unmarshalling(Parcel &parcel) @@ -283,6 +345,15 @@ bool NotificationLiveViewContent::ReadFromParcel(Parcel &parcel) pictureMap_[key] = pixelMapVec; } + valid = parcel.ReadBool(); + if (valid) { + extensionWantAgent_ = std::shared_ptr( + parcel.ReadParcelable()); + if (!extensionWantAgent_) { + ANS_LOGE("null wantAgent"); + return false; + } + } return true; } diff --git a/frameworks/ans/test/unittest/notification_live_view_content_test.cpp b/frameworks/ans/test/unittest/notification_live_view_content_test.cpp index 913e0358943d9a9354a625493bcf414a47a2308e..649bc2de95eccf1099ee4614c117ac137046b907 100644 --- a/frameworks/ans/test/unittest/notification_live_view_content_test.cpp +++ b/frameworks/ans/test/unittest/notification_live_view_content_test.cpp @@ -21,6 +21,8 @@ #include "notification_live_view_content.h" #undef private #undef protected +#include "want_params_wrapper.h" +#include "want_agent_helper.h" using namespace testing::ext; namespace OHOS { @@ -114,7 +116,7 @@ HWTEST_F(NotificationLiveViewContentTest, Dump_00001, Function | SmallTest | Lev auto rrc = std::make_shared(); std::string ret = "NotificationLiveViewContent{ title = , text = , " "additionalText = , lockScreenPicture = null, status = 0, version = -1, extraInfo = null, " - "isOnlyLocalUpdate_ = false, pictureMap = {}}"; + "isOnlyLocalUpdate_ = false, pictureMap = {}, extensionWantAgent_ = null}"; EXPECT_EQ(rrc->Dump(), ret); } @@ -141,7 +143,8 @@ HWTEST_F(NotificationLiveViewContentTest, Dump_00002, Function | SmallTest | Lev std::string ret = "NotificationLiveViewContent{ title = title, text = text, " "additionalText = addText, lockScreenPicture = null, status = 0, version = -1, extraInfo = null, " - "isOnlyLocalUpdate_ = false, pictureMap = { { key = test, value = not empty } }}"; + "isOnlyLocalUpdate_ = false, pictureMap = { { key = test, value = not empty } }, " + "extensionWantAgent_ = null}"; EXPECT_EQ(rrc->Dump(), ret); } @@ -327,5 +330,37 @@ HWTEST_F(NotificationLiveViewContentTest, MarshallingPictureMap_00003, Function bool isSuccess = liveViewContent->MarshallingPictureMap(parcel); EXPECT_EQ(isSuccess, true); } + +/** + * @tc.name: GetUid_00001 + * @tc.desc: Test uid + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationLiveViewContentTest, GetUid_00001, Function | SmallTest | Level1) +{ + NotificationLiveViewContent liveViewContent; + liveViewContent.SetUid(100); + auto uid = liveViewContent.GetUid(); + ASSERT_EQ(uid, 100); +} + +/** + * @tc.name: GetExtensionWantAgent_00001 + * @tc.desc: Test extensionWantAgent_ + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationLiveViewContentTest, GetExtensionWantAgent_00001, Function | SmallTest | Level1) +{ + AbilityRuntime::WantAgent::WantAgentInfo paramsInfo; + std::shared_ptr wantAgent = + std::make_shared(); + ASSERT_NE(wantAgent, nullptr); + NotificationLiveViewContent liveViewContent; + liveViewContent.SetExtensionWantAgent(wantAgent); + auto want = liveViewContent.GetExtensionWantAgent(); + ASSERT_NE(want, nullptr); +} } } diff --git a/frameworks/js/napi/include/common.h b/frameworks/js/napi/include/common.h index d383bd5279d9dde6e0d9867e288a5148806e483b..c8d497c5d26d5578c707f46b5b54e5b7d7c84315 100644 --- a/frameworks/js/napi/include/common.h +++ b/frameworks/js/napi/include/common.h @@ -1669,6 +1669,8 @@ public: static napi_value GetHashCodes(const napi_env &env, const napi_value &value, std::vector &hashCodes); + static napi_value GetLiveViewWantAgent(const napi_env &env, const napi_value &value, + std::shared_ptr &liveViewContent); /** * @brief Gets a NotificationKey object from specified js object * diff --git a/frameworks/js/napi/src/common_convert_liveview.cpp b/frameworks/js/napi/src/common_convert_liveview.cpp index e974bceac38394a40ffc73a7f2f3fa4029289a9a..aa1a946652244f50a91a32b832cd43debd925417 100644 --- a/frameworks/js/napi/src/common_convert_liveview.cpp +++ b/frameworks/js/napi/src/common_convert_liveview.cpp @@ -437,6 +437,14 @@ napi_value Common::SetNotificationLiveViewContent( } napi_set_named_property(env, result, "pictureInfo", pictureMapObj); + std::shared_ptr agent = liveViewContent->GetExtensionWantAgent(); + if (agent) { + napi_value wantAgent = nullptr; + wantAgent = CreateWantAgentByJS(env, agent); + napi_set_named_property(env, result, "extensionWantAgent", wantAgent); + } else { + napi_set_named_property(env, result, "extensionWantAgent", NapiGetNull(env)); + } return NapiGetBoolean(env, true); } @@ -1272,6 +1280,46 @@ napi_value Common::GetNotificationLiveViewContentDetailed( } liveViewContent->SetPicture(pictureMap); + if (GetLiveViewWantAgent(env, contentResult, liveViewContent) == nullptr) { + ANS_LOGD("no live view wantAgent"); + return nullptr; + } + + return NapiGetNull(env); +} + +napi_value Common::GetLiveViewWantAgent(const napi_env &env, const napi_value &value, + std::shared_ptr &liveViewContent) +{ + if (liveViewContent == nullptr) { + ANS_LOGE("null liveViewContent"); + return nullptr; + } + + bool hasProperty = false; + AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr; + napi_value result = nullptr; + napi_valuetype valuetype = napi_undefined; + + NAPI_CALL(env, napi_has_named_property(env, value, "extensionWantAgent", &hasProperty)); + if (hasProperty) { + napi_get_named_property(env, value, "extensionWantAgent", &result); + NAPI_CALL(env, napi_typeof(env, result, &valuetype)); + if (valuetype != napi_object) { + ANS_LOGE("Wrong argument type. Object expected."); + std::string msg = "Incorrect parameter types. The type of wantAgent must be object."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + napi_unwrap(env, result, (void **)&wantAgent); + if (wantAgent == nullptr) { + ANS_LOGE("null wantAgent"); + return nullptr; + } + std::shared_ptr sWantAgent = + std::make_shared(*wantAgent); + liveViewContent->SetExtensionWantAgent(sWantAgent); + } return NapiGetNull(env); } diff --git a/interfaces/inner_api/notification_live_view_content.h b/interfaces/inner_api/notification_live_view_content.h index 86baef5ad55570af729aec3b90f4f888bbc07685..f752869dc255e32c5303181aa1432774943881bf 100644 --- a/interfaces/inner_api/notification_live_view_content.h +++ b/interfaces/inner_api/notification_live_view_content.h @@ -20,6 +20,7 @@ #include "parcel.h" #include "pixel_map.h" #include "want_params.h" +#include "want_agent.h" namespace OHOS { namespace Notification { @@ -147,6 +148,16 @@ public: bool GetIsOnlyLocalUpdate() const; + void SetExtensionWantAgent(const std::shared_ptr &wantAgent); + + const std::shared_ptr GetExtensionWantAgent() const; + + void SetUid(const int32_t uid); + + int32_t GetUid() const; + + bool MarshallingExtensionWantAgent(Parcel &parcel) const; + protected: /** * @brief Read a NotificationLiveViewContent object from a Parcel. @@ -163,6 +174,8 @@ private: std::shared_ptr extraInfo_ {}; PictureMap pictureMap_ {}; bool isOnlyLocalUpdate_ = false; + int32_t uid_ = -1; + std::shared_ptr extensionWantAgent_ {}; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/advanced_notification_live_view_service.cpp b/services/ans/src/advanced_notification_live_view_service.cpp index b9345c323dcc42a9289b5418032d6323aaf5596a..e7ef7046f9934925ff5184b7df2fe192614fd56e 100644 --- a/services/ans/src/advanced_notification_live_view_service.cpp +++ b/services/ans/src/advanced_notification_live_view_service.cpp @@ -237,6 +237,9 @@ int32_t AdvancedNotificationService::SetNotificationRequestToDb(const Notificati ANS_LOGI("Enter."); auto content = std::static_pointer_cast( request->GetContent()->GetNotificationContent()); + if (request->GetOwnerUid() != DEFAULT_UID) { + content->SetUid(request->GetOwnerUid()); + } if (content->GetLiveViewStatus() == NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_END && request->GetAutoDeletedTime() == NotificationConstant::NO_DELAY_DELETE_TIME) { ANS_LOGI("Don't need set to db when liveview is in end status and no delay delete time."); diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index def3aaa26d949eb5e652f3a115a040022e1081db..48880e37b3442f642e5dcc8dbcd244ccf6782dfc 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1306,6 +1306,25 @@ void AdvancedNotificationService::CancelWantAgent(const sptr ¬i } } } + + if (!notification->GetNotificationRequestPoint()->IsCommonLiveView()) { + return; + } + if (content == nullptr) { + return; + } + auto notificationContent = content->GetNotificationContent(); + if (notificationContent == nullptr) { + return; + } + auto liveViewContent = std::static_pointer_cast(notificationContent); + if (liveViewContent == nullptr) { + return; + } + auto want = liveViewContent->GetExtensionWantAgent(); + if (want != nullptr) { + CancelOnceWantAgent(want); + } } ErrCode AdvancedNotificationService::RemoveFromNotificationList(const sptr &bundleOption,