diff --git a/frameworks/ans/BUILD.gn b/frameworks/ans/BUILD.gn index 73c3b944784897f0042c2ad3bfd25298564673b2..9fd9eb806e802e85899d47a67088528cfe6ee5f2 100644 --- a/frameworks/ans/BUILD.gn +++ b/frameworks/ans/BUILD.gn @@ -69,6 +69,8 @@ ohos_shared_library("ans_innerkits") { "${frameworks_module_ans_path}/src/notification.cpp", "${frameworks_module_ans_path}/src/notification_action_button.cpp", "${frameworks_module_ans_path}/src/notification_basic_content.cpp", + "${frameworks_module_ans_path}/src/notification_check_info.cpp", + "${frameworks_module_ans_path}/src/notification_check_request.cpp", "${frameworks_module_ans_path}/src/notification_bundle_option.cpp", "${frameworks_module_ans_path}/src/notification_button_option.cpp", "${frameworks_module_ans_path}/src/notification_capsule.cpp", @@ -88,6 +90,7 @@ ohos_shared_library("ans_innerkits") { "${frameworks_module_ans_path}/src/notification_multiline_content.cpp", "${frameworks_module_ans_path}/src/notification_normal_content.cpp", "${frameworks_module_ans_path}/src/notification_picture_content.cpp", + "${frameworks_module_ans_path}/src/notification_live_view_content.cpp", "${frameworks_module_ans_path}/src/notification_progress.cpp", "${frameworks_module_ans_path}/src/notification_request.cpp", "${frameworks_module_ans_path}/src/notification_slot.cpp", diff --git a/frameworks/ans/src/notification_check_info.cpp b/frameworks/ans/src/notification_check_info.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36c4b606fabcb7f039214a198ce3d6c87b23f452 --- /dev/null +++ b/frameworks/ans/src/notification_check_info.cpp @@ -0,0 +1,160 @@ +/* + * Copyright (c) 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. + */ +#include "notification_check_info.h" +#include "ans_log_wrapper.h" +#include "want_params_wrapper.h" +#include "nlohmann/json.hpp" + +namespace OHOS { +namespace Notification { + +NotificationCheckInfo::NotificationCheckInfo(std::string pkgName, int32_t notifyId, int32_t contentType, + int32_t creatorUserId, int32_t slotType, std::shared_ptr extraInfo) : +pkgName_(pkgName), notifyId_(notifyId), contentType_(contentType), + creatorUserId_(creatorUserId), slotType_(slotType), extraInfo_(extraInfo) +{} + +NotificationCheckInfo::~NotificationCheckInfo() +{} + +std::string NotificationCheckInfo::GetPkgName() const +{ + return pkgName_; +} + +void NotificationCheckInfo::SetPkgName(std::string pkgName) +{ + pkgName_ = pkgName; +} + +int32_t NotificationCheckInfo::GetNotifyId() const +{ + return notifyId_; +} + +void NotificationCheckInfo::SetNotifyId(int32_t notifyId) +{ + notifyId_ = notifyId; +} + + +int32_t NotificationCheckInfo::GetContentType() const +{ + return contentType_; +} + +void NotificationCheckInfo::SetContentType(int32_t contentType) +{ + contentType_ = contentType; +} + + +int32_t NotificationCheckInfo::GetCreatorUserId() const +{ + return creatorUserId_; +} + +void NotificationCheckInfo::SetCreatorUserId(int32_t creatorUserId) +{ + creatorUserId_ = creatorUserId; +} + +int32_t NotificationCheckInfo::GetSlotType() const +{ + return slotType_; +} + +void NotificationCheckInfo::SetSlotType(int32_t slotType) +{ + slotType_ = slotType; +} + + +std::shared_ptr NotificationCheckInfo::GetExtraInfo() const +{ + return extraInfo_; +} + +void NotificationCheckInfo::SetExtraInfo(std::shared_ptr extraInfo) +{ + extraInfo_ = extraInfo; +} + +void NotificationCheckInfo::ConvertJsonExtraInfoToValue(nlohmann::json &jsonobj) +{ + const auto &jsonEnd = jsonobj.cend(); + if (jsonobj.find("extraInfo") == jsonEnd) { + return; + } + + if (!jsonobj.at("extraInfo").is_string()) { + ANS_LOGE("Invalid JSON object extraInfo"); + return; + } + auto extraInfoStr = jsonobj.at("extraInfo").get(); + if (!extraInfoStr.empty()) { + AAFwk::WantParams params = AAFwk::WantParamWrapper::ParseWantParams(extraInfoStr); + extraInfo_ = std::make_shared(params); + } +} + +void NotificationCheckInfo::ConvertJsonStringToValue(const std::string ¬ificationData) +{ + nlohmann::json jsonobj = nlohmann::json::parse(notificationData); + if (jsonobj.is_null() || !jsonobj.is_object()) { + ANS_LOGE("Invalid JSON object"); + return; + } + + const auto &jsonEnd = jsonobj.cend(); + if (jsonobj.find("pkgName") != jsonEnd) { + if (!jsonobj.at("pkgName").is_string()) { + ANS_LOGE("Invalid JSON object pkgName"); + return; + } + pkgName_ = jsonobj.at("pkgName").get(); + } + if (jsonobj.find("notifyId") != jsonEnd) { + if (!jsonobj.at("notifyId").is_number()) { + ANS_LOGE("Invalid JSON object notifyId"); + return; + } + notifyId_ = jsonobj.at("notifyId").get(); + } + if (jsonobj.find("contentType") != jsonEnd) { + if (!jsonobj.at("contentType").is_number()) { + ANS_LOGE("Invalid JSON object contentType"); + return; + } + contentType_ = jsonobj.at("contentType").get(); + } + if (jsonobj.find("creatorUserId") != jsonEnd) { + if (!jsonobj.at("creatorUserId").is_number()) { + ANS_LOGE("Invalid JSON object creatorUserId"); + return; + } + creatorUserId_ = jsonobj.at("creatorUserId").get(); + } + if (jsonobj.find("slotType") != jsonEnd) { + if (!jsonobj.at("slotType").is_number()) { + ANS_LOGE("Invalid JSON object slotType"); + return; + } + slotType_ = jsonobj.at("slotType").get(); + } + ConvertJsonExtraInfoToValue(jsonobj); +} +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/ans/src/notification_check_request.cpp b/frameworks/ans/src/notification_check_request.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5accc65c4df3c8075c4438ce1b8b6f0bc0869c66 --- /dev/null +++ b/frameworks/ans/src/notification_check_request.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 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. + */ +#include "notification_check_request.h" +#include "ans_log_wrapper.h" + +namespace OHOS { +namespace Notification { + + +NotificationCheckRequest::NotificationCheckRequest(NotificationContent::Type contentType, + NotificationConstant::SlotType slotType, std::vector extraKeys) + : contentType_(contentType), slotType_(slotType), extraKeys_(extraKeys) +{} + +NotificationCheckRequest::~NotificationCheckRequest() +{} + +void NotificationCheckRequest::SetContentType(NotificationContent::Type contentType) +{ + contentType_ = contentType; +} + +NotificationContent::Type NotificationCheckRequest::GetContentType() const +{ + return contentType_; +} + +void NotificationCheckRequest::SetSlotType(NotificationConstant::SlotType slotType) +{ + slotType_ = slotType; +} + +NotificationConstant::SlotType NotificationCheckRequest::GetSlotType() const +{ + return slotType_; +} + +void NotificationCheckRequest::SetExtraKeys(std::vector extraKeys) +{ + extraKeys_ = extraKeys; +} + +std::vector NotificationCheckRequest::GetExtraKeys() const +{ + return extraKeys_; +} + +void NotificationCheckRequest::SetUid(const int32_t uid) +{ + creatorUid_ = uid; +} + +int32_t NotificationCheckRequest::GetUid() const +{ + return creatorUid_; +} + +bool NotificationCheckRequest::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteInt32(static_cast(contentType_))) { + ANS_LOGE("Failed to write content type"); + return false; + } + if (!parcel.WriteInt32(static_cast(slotType_))) { + ANS_LOGE("Failed to write slot type"); + return false; + } + if (!parcel.WriteStringVector(extraKeys_)) { + ANS_LOGE("Failed to write extra info keys"); + return false; + } + + return true; +} + +NotificationCheckRequest *NotificationCheckRequest::Unmarshalling(Parcel &parcel) +{ + auto objptr = new (std::nothrow) NotificationCheckRequest(); + if ((objptr != nullptr) && !objptr->ReadFromParcel(parcel)) { + delete objptr; + objptr = nullptr; + } + + return objptr; +} + +bool NotificationCheckRequest::ReadFromParcel(Parcel &parcel) +{ + contentType_ = static_cast(parcel.ReadInt32()); + slotType_ = static_cast(parcel.ReadInt32()); + if (!parcel.ReadStringVector(&extraKeys_)) { + ANS_LOGE("Failed to read extra info keys"); + return false; + } + return true; +} + +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/ans/src/notification_content.cpp b/frameworks/ans/src/notification_content.cpp index e00b15f9e45c206b46462a8198cd2a0caa1b5c0b..1556893fe3ffddec8769c19859b37c8fc3d14806 100644 --- a/frameworks/ans/src/notification_content.cpp +++ b/frameworks/ans/src/notification_content.cpp @@ -96,6 +96,17 @@ NotificationContent::NotificationContent(const std::shared_ptr &liveViewContent) +{ + if (!liveViewContent) { + ANS_LOGE("NotificationLiveViewContent can not be null"); + return; + } + + contentType_ = NotificationContent::Type::LIVE_VIEW; + content_ = liveViewContent; +} + NotificationContent::~NotificationContent() {} @@ -118,8 +129,8 @@ std::string NotificationContent::Dump() : (contentType_ == NotificationContent::Type::MULTILINE) ? "MULTILINE" : (contentType_ == NotificationContent::Type::PICTURE) ? "PICTURE" : (contentType_ == NotificationContent::Type::LOCAL_LIVE_VIEW) ? "LOCAL_LIVE_VIEW" - : "NONE"; - + : (contentType_ == NotificationContent::Type::LIVE_VIEW) ? "LIVE_VIEW" + : "NONE"; return "NotificationContent{ " "contentType = " + contentTypeStr + ", content = " + (content_ ? content_->Dump() : "null") + @@ -211,8 +222,7 @@ bool NotificationContent::ReadFromParcel(Parcel &parcel) { contentType_ = static_cast(parcel.ReadInt32()); - auto valid = parcel.ReadBool(); - if (!valid) { + if (!parcel.ReadBool()) { return true; } @@ -247,6 +257,10 @@ bool NotificationContent::ReadFromParcel(Parcel &parcel) std::shared_ptr( parcel.ReadParcelable())); break; + case NotificationContent::Type::LIVE_VIEW: + content_ = std::static_pointer_cast( + std::shared_ptr(parcel.ReadParcelable())); + break; default: ANS_LOGE("Invalid contentType"); return false; @@ -271,8 +285,7 @@ bool NotificationContent::ConvertJsonToContent(NotificationContent *target, cons ANS_LOGE("ContentType is not integer"); return false; } - auto contentTypeValue = contentType.get(); - target->contentType_ = static_cast(contentTypeValue); + target->contentType_ = static_cast(contentType.get()); auto contentObj = jsonObject.at("content"); if (contentObj.is_null()) { @@ -300,6 +313,9 @@ bool NotificationContent::ConvertJsonToContent(NotificationContent *target, cons case NotificationContent::Type::LOCAL_LIVE_VIEW: pBasicContent = NotificationJsonConverter::ConvertFromJson(contentObj); break; + case NotificationContent::Type::LIVE_VIEW: + pBasicContent = NotificationJsonConverter::ConvertFromJson(contentObj); + break; default: ANS_LOGE("Invalid contentType"); break; diff --git a/frameworks/ans/src/notification_live_view_content.cpp b/frameworks/ans/src/notification_live_view_content.cpp new file mode 100644 index 0000000000000000000000000000000000000000..97789ed437f812acc206c39da84948226b536b6e --- /dev/null +++ b/frameworks/ans/src/notification_live_view_content.cpp @@ -0,0 +1,283 @@ +/* + * Copyright (c) 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. + */ + +#include "notification_live_view_content.h" +#include +#include "ans_image_util.h" +#include "ans_log_wrapper.h" +#include "want_params_wrapper.h" + +namespace OHOS { +namespace Notification { +const uint32_t NotificationLiveViewContent::MAX_VERSION {0xffffffff}; +void NotificationLiveViewContent::SetLiveViewStatus(const LiveViewStatus status) +{ + liveViewStatus_ = status; +} + +NotificationLiveViewContent::LiveViewStatus NotificationLiveViewContent::GetLiveViewStatus() const +{ + return liveViewStatus_; +} + +void NotificationLiveViewContent::SetVersion(int32_t version) +{ + version_ = version; +} + +int32_t NotificationLiveViewContent::GetVersion() const +{ + return version_; +} + +void NotificationLiveViewContent::SetExtraInfo(const std::shared_ptr &extras) +{ + extraInfo_ = extras; +} + +std::shared_ptr NotificationLiveViewContent::GetExtraInfo() const +{ + return extraInfo_; +} + +void NotificationLiveViewContent::SetPicture(const PictureMap &pictureMap) +{ + pictureMap_ = pictureMap; +} + +PictureMap NotificationLiveViewContent::GetPicture() const +{ + return pictureMap_; +} + +std::string NotificationLiveViewContent::Dump() +{ + std::string extraStr{"null"}; + if (extraInfo_ != nullptr) { + AAFwk::WantParamWrapper wWrapper(*extraInfo_); + extraStr = wWrapper.ToString(); + } + + std::string pictureStr {", pictureMap = {"}; + for (auto &picture : pictureMap_) { + pictureStr += " { key = " + picture.first + ", value = " + + (picture.second.empty() ? "empty" : "not empty") + " },"; + } + if (pictureStr[pictureStr.length() - 1] == ',') { + pictureStr[pictureStr.length() - 1] = ' '; + } + pictureStr += "}"; + + return "NotificationLiveViewContent{ " + NotificationBasicContent::Dump() + + ", status = " + std::to_string(static_cast(liveViewStatus_)) + ", version = " + + std::to_string(static_cast(version_)) + ", extraInfo = " + extraStr + pictureStr + "}"; +} + +bool NotificationLiveViewContent::PictureToJson(nlohmann::json &jsonObject) const +{ + nlohmann::json pixelMap; + + if (pictureMap_.empty()) { + return true; + } + for (const auto &picture : pictureMap_) { + nlohmann::json pixelRecordArr = nlohmann::json::array(); + for (const auto &pixelMap : picture.second) { + pixelRecordArr.emplace_back(AnsImageUtil::PackImage(pixelMap)); + } + pixelMap[picture.first] = pixelRecordArr; + } + jsonObject["pictureMap"] = pixelMap; + return true; +} + +bool NotificationLiveViewContent::ToJson(nlohmann::json &jsonObject) const +{ + if (!NotificationBasicContent::ToJson(jsonObject)) { + ANS_LOGE("Cannot convert basicContent to JSON"); + return false; + } + + jsonObject["status"] = static_cast(liveViewStatus_); + jsonObject["version"] = version_; + + if (extraInfo_) { + AAFwk::WantParamWrapper wWrapper(*extraInfo_); + jsonObject["extraInfo"] = wWrapper.ToString(); + } + + return PictureToJson(jsonObject); +} + +void NotificationLiveViewContent::ConvertPictureFromJson(const nlohmann::json &jsonObject) +{ + const auto &jsonEnd = jsonObject.cend(); + if ((jsonObject.find("pictureMap") != jsonEnd) && jsonObject.at("pictureMap").is_object()) { + auto pictureMap = jsonObject.at("pictureMap").get(); + for (auto it = pictureMap.begin(); it != pictureMap.end(); it++) { + if (!it.value().is_array()) { + continue; + } + auto pictureArray = it.value().get>(); + pictureMap_[it.key()] = std::vector>(); + for (const auto &picture : pictureArray) { + pictureMap_[it.key()].emplace_back(AnsImageUtil::UnPackImage(picture)); + } + } + } +} + +NotificationLiveViewContent *NotificationLiveViewContent::FromJson(const nlohmann::json &jsonObject) +{ + if (jsonObject.is_null() or !jsonObject.is_object()) { + ANS_LOGE("Invalid JSON object"); + return nullptr; + } + + auto *pContent = new (std::nothrow) NotificationLiveViewContent(); + if (pContent == nullptr) { + ANS_LOGE("Failed to create liveViewContent instance"); + return nullptr; + } + + pContent->ReadFromJson(jsonObject); + + const auto &jsonEnd = jsonObject.cend(); + if (jsonObject.find("status") != jsonEnd && jsonObject.at("status").is_number_integer()) { + auto statusValue = jsonObject.at("status").get(); + pContent->liveViewStatus_ = static_cast(statusValue); + } + + if (jsonObject.find("version") != jsonEnd && jsonObject.at("version").is_number_integer()) { + pContent->version_ = jsonObject.at("version").get(); + } + + if (jsonObject.find("extraInfo") != jsonEnd && jsonObject.at("extraInfo").is_string()) { + std::string extraInfoStr = jsonObject.at("extraInfo").get(); + if (!extraInfoStr.empty()) { + AAFwk::WantParams params = AAFwk::WantParamWrapper::ParseWantParams(extraInfoStr); + pContent->extraInfo_ = std::make_shared(params); + } + } + pContent->ConvertPictureFromJson(jsonObject); + return pContent; +} + +bool NotificationLiveViewContent::Marshalling(Parcel &parcel) const +{ + if (!NotificationBasicContent::Marshalling(parcel)) { + ANS_LOGE("Failed to write basic"); + return false; + } + + if (!parcel.WriteInt32(static_cast(liveViewStatus_))) { + ANS_LOGE("Failed to write liveView status"); + return false; + } + + if (!parcel.WriteUint32(version_)) { + ANS_LOGE("Failed to write version"); + return false; + } + + bool valid{false}; + if (extraInfo_ != nullptr) { + valid = true; + } + if (!parcel.WriteBool(valid)) { + ANS_LOGE("Failed to write the flag which indicate whether extraInfo is null"); + return false; + } + if (valid) { + if (!parcel.WriteParcelable(extraInfo_.get())) { + ANS_LOGE("Failed to write additionalParams"); + return false; + } + } + + if (!parcel.WriteUint64(pictureMap_.size())) { + ANS_LOGE("Failed to write the size of pictureMap."); + return false; + } + + for (const auto &picture : pictureMap_) { + if (!parcel.WriteString(picture.first)) { + ANS_LOGE("Failed to write picture map key %{public}s.", picture.first.c_str()); + return false; + } + std::vector pixelVec; + pixelVec.reserve(picture.second.size()); + for (const auto &pixel : picture.second) { + pixelVec.emplace_back(AnsImageUtil::PackImage(pixel)); + } + if (!parcel.WriteStringVector(pixelVec)) { + ANS_LOGE("Failed to write picture vector."); + return false; + } + } + + return true; +} + +NotificationLiveViewContent *NotificationLiveViewContent::Unmarshalling(Parcel &parcel) +{ + auto *pContent = new (std::nothrow) NotificationLiveViewContent(); + if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) { + delete pContent; + pContent = nullptr; + } + + return pContent; +} + +bool NotificationLiveViewContent::ReadFromParcel(Parcel &parcel) +{ + if (!NotificationBasicContent::ReadFromParcel(parcel)) { + ANS_LOGE("Failed to read basic"); + return false; + } + + liveViewStatus_ = static_cast(parcel.ReadInt32()); + version_ = parcel.ReadUint32(); + + bool valid = parcel.ReadBool(); + if (valid) { + extraInfo_ = std::shared_ptr(parcel.ReadParcelable()); + if (!extraInfo_) { + ANS_LOGE("Failed to read extraInfo."); + return false; + } + } + + uint64_t len = parcel.ReadUint64(); + for (uint64_t i = 0; i < len; i++) { + auto key = parcel.ReadString(); + std::vector strVec; + if (!parcel.ReadStringVector(&strVec)) { + ANS_LOGE("Failed to read extraInfo vector string."); + return false; + } + std::vector> pixelMapVec; + pixelMapVec.reserve(strVec.size()); + for (const auto &str : strVec) { + pixelMapVec.emplace_back(AnsImageUtil::UnPackImage(str)); + } + pictureMap_[key] = pixelMapVec; + } + + return true; +} +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/ans/src/notification_request.cpp b/frameworks/ans/src/notification_request.cpp index c554a697b1829e3d1a7770ce302c11cba642d383..ccfef50a603dc889c0aa0149fa5102dc58318401 100644 --- a/frameworks/ans/src/notification_request.cpp +++ b/frameworks/ans/src/notification_request.cpp @@ -269,6 +269,16 @@ const std::shared_ptr NotificationRequest::GetBigIcon() const return bigIcon_; } +void NotificationRequest::SetOverlayIcon(const std::shared_ptr &overlayIcon) +{ + overlayIcon_ = overlayIcon; +} + +const std::shared_ptr NotificationRequest::GetOverlayIcon() const +{ + return overlayIcon_; +} + void NotificationRequest::SetClassification(const std::string &classification) { classification_ = classification; diff --git a/frameworks/ans/test/unittest/BUILD.gn b/frameworks/ans/test/unittest/BUILD.gn index 80f902f98e0c8e1fcdc57b34cc1bc5b6fe17c732..0c2398ad05cbe64eaae24aab38935769437117a1 100644 --- a/frameworks/ans/test/unittest/BUILD.gn +++ b/frameworks/ans/test/unittest/BUILD.gn @@ -43,6 +43,7 @@ ohos_unittest("ans_reminder_unit_test") { "${frameworks_module_ans_path}/test/unittest/notification_media_content_test.cpp", "${frameworks_module_ans_path}/test/unittest/notification_multiline_content_test.cpp", "${frameworks_module_ans_path}/test/unittest/notification_picture_content_test.cpp", + "${frameworks_module_ans_path}/test/unittest/notification_live_view_content_test.cpp", "${frameworks_module_ans_path}/test/unittest/notification_request_test.cpp", "${frameworks_module_ans_path}/test/unittest/notification_sorting_map_test.cpp", "${frameworks_module_ans_path}/test/unittest/notification_sorting_test.cpp", diff --git a/frameworks/ans/test/unittest/notification_live_view_content_test.cpp b/frameworks/ans/test/unittest/notification_live_view_content_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af71354a313b5b41b1b5bbb0b2afe473207e8175 --- /dev/null +++ b/frameworks/ans/test/unittest/notification_live_view_content_test.cpp @@ -0,0 +1,251 @@ +/* + * Copyright (c) 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. + */ + +#include +#include + +#define private public +#define protected public +#include "notification_live_view_content.h" +#undef private +#undef protected + +using namespace testing::ext; +namespace OHOS { +namespace Notification { +class NotificationLiveViewContentTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() {} +}; + +/** + * @tc.name: SetLiveViewStatus_00001 + * @tc.desc: Test SetLiveViewStatus parameter. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationLiveViewContentTest, SetLiveViewStatus_00001, Function | SmallTest | Level1) +{ + auto rrc = std::make_shared(); + rrc->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_CREATE); + EXPECT_EQ(rrc->GetLiveViewStatus(), NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_CREATE); + rrc->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BUTT); + EXPECT_EQ(rrc->GetLiveViewStatus(), NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BUTT); +} + +/** + * @tc.name: SetVersion_00001 + * @tc.desc: Test SetVersion parameters. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationLiveViewContentTest, SetVersion_00001, Function | SmallTest | Level1) +{ + auto rrc = std::make_shared(); + rrc->SetVersion(NotificationLiveViewContent::MAX_VERSION); + EXPECT_EQ(rrc->GetVersion(), NotificationLiveViewContent::MAX_VERSION); +} + +/** + * @tc.name: SetExtraInfo_00001 + * @tc.desc: Test SetExtraInfo parameters. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationLiveViewContentTest, SetExtraInfo_00001, Function | SmallTest | Level1) +{ + auto extraInfo = std::make_shared(); + auto rrc = std::make_shared(); + rrc->SetExtraInfo(extraInfo); + EXPECT_EQ(rrc->GetExtraInfo(), extraInfo); +} + +/** + * @tc.name: SetPicture_00001 + * @tc.desc: Test SetPicture parameters. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationLiveViewContentTest, SetPicture_00001, Function | SmallTest | Level1) +{ + auto rrc = std::make_shared(); + PictureMap pictureMap{}; + rrc->SetPicture(pictureMap); + EXPECT_EQ(rrc->GetPicture(), pictureMap); +} + +/** + * @tc.name: Dump_00001 + * @tc.desc: Test Dump parameters. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationLiveViewContentTest, Dump_00001, Function | SmallTest | Level1) +{ + auto rrc = std::make_shared(); + std::string ret = "NotificationLiveViewContent{ title = , text = , " + "additionalText = , status = 0, version = 0, extraInfo = null, pictureMap = {}}"; + + EXPECT_EQ(rrc->Dump(), ret); +} + +/** + * @tc.name: Dump_00002 + * @tc.desc: Test Dump parameters when pictureMap isn't empty. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationLiveViewContentTest, Dump_00002, Function | SmallTest | Level1) +{ + PictureMap pictureMap; + std::vector> pixelVec; + pixelVec.push_back(std::make_shared()); + auto picture = std::make_pair(std::string{"test"}, pixelVec); + pictureMap.insert(picture); + + auto rrc = std::make_shared(); + rrc->SetPicture(pictureMap); + rrc->SetTitle("title"); + rrc->SetText("text"); + rrc->SetAdditionalText("addText"); + + std::string ret = "NotificationLiveViewContent{ title = title, text = text, " + "additionalText = addText, status = 0, version = 0, extraInfo = null, " + "pictureMap = { { key = test, value = not empty } }}"; + + EXPECT_EQ(rrc->Dump(), ret); +} + +/** + * @tc.name: JsonConvert_00001 + * @tc.desc: Test JsonConvert parameters. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationLiveViewContentTest, JsonConvert_00001, Function | SmallTest | Level1) +{ + auto rrc = std::make_shared(); + + PictureMap pictureMap; + std::vector> pixelVec; + pixelVec.push_back(std::make_shared()); + auto picture = std::make_pair(std::string{"test"}, pixelVec); + pictureMap.insert(picture); + rrc->SetPicture(pictureMap); + + auto extraInfo = std::make_shared(); + rrc->SetExtraInfo(extraInfo); + + rrc->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BATCH_UPDATE); + rrc->SetVersion(NotificationLiveViewContent::MAX_VERSION); + rrc->SetTitle("title"); + rrc->SetText("text"); + rrc->SetAdditionalText("addText"); + + nlohmann::json jsonObject; + EXPECT_EQ(rrc->ToJson(jsonObject), true); + const auto &jsonEnd = jsonObject.cend(); + EXPECT_NE(jsonObject.find("pictureMap"), jsonEnd); + EXPECT_NE(jsonObject.find("extraInfo"), jsonEnd); + + auto ptr = NotificationLiveViewContent::FromJson(jsonObject); + EXPECT_EQ(jsonObject.is_object(), true); + EXPECT_NE(ptr, nullptr); + EXPECT_EQ(ptr->GetTitle(), std::string("title")); + EXPECT_EQ(ptr->GetText(), std::string("text")); + EXPECT_EQ(ptr->GetAdditionalText(), std::string("addText")); + EXPECT_EQ(ptr->GetLiveViewStatus(), NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BATCH_UPDATE); + EXPECT_EQ(ptr->GetVersion(), NotificationLiveViewContent::MAX_VERSION); + EXPECT_NE(ptr->GetExtraInfo(), nullptr); + EXPECT_EQ(ptr->GetPicture().size(), 1); + delete ptr; +} + +/** + * @tc.name: FromJson_00001 + * @tc.desc: Test FromJson parameters when jsonObject is invalid. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationLiveViewContentTest, FromJson_00001, Function | SmallTest | Level1) +{ + auto rrc = std::make_shared(); + nlohmann::json jsonObject = nlohmann::json{"processName", "process6", "expandedTitle", "arrivedTime1"}; + auto ptr = rrc->FromJson(jsonObject); + EXPECT_EQ(jsonObject.is_object(), false); + EXPECT_EQ(ptr, nullptr); +} + +/** + * @tc.name: MarshallConvert_00001 + * @tc.desc: Test MarshallConvert parameters. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationLiveViewContentTest, MarshallConvert_00001, Function | SmallTest | Level1) +{ + auto rrc = std::make_shared(); + + PictureMap pictureMap; + std::vector> pixelVec; + pixelVec.push_back(std::make_shared()); + auto picture = std::make_pair(std::string{"picture"}, pixelVec); + pictureMap.insert(picture); + auto image = std::make_pair(std::string{"image"}, pixelVec); + pictureMap.insert(image); + rrc->SetPicture(pictureMap); + + auto extraInfo = std::make_shared(); + rrc->SetExtraInfo(extraInfo); + + rrc->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_END); + uint32_t version = NotificationLiveViewContent::MAX_VERSION - 1; + rrc->SetVersion(version); + rrc->SetTitle("title"); + rrc->SetText("text"); + rrc->SetAdditionalText("addText"); + + Parcel parcel; + EXPECT_EQ(rrc->Marshalling(parcel), true); + + auto ptr = NotificationLiveViewContent::Unmarshalling(parcel); + EXPECT_NE(ptr, nullptr); + EXPECT_EQ(ptr->GetTitle(), std::string("title")); + EXPECT_EQ(ptr->GetText(), std::string("text")); + EXPECT_EQ(ptr->GetAdditionalText(), std::string("addText")); + EXPECT_EQ(ptr->GetLiveViewStatus(), NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_END); + EXPECT_EQ(ptr->GetVersion(), version); + EXPECT_NE(ptr->GetExtraInfo(), nullptr); + EXPECT_EQ(ptr->GetPicture().size(), 2); + delete ptr; +} + +/** + * @tc.name: Unmarshalling_00001 + * @tc.desc: Test Unmarshalling parameters. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(NotificationLiveViewContentTest, Unmarshalling_00001, Function | SmallTest | Level1) +{ + Parcel parcel; + auto ptr = NotificationLiveViewContent::Unmarshalling(parcel); + EXPECT_EQ(ptr, nullptr); +} +} +} diff --git a/frameworks/js/napi/include/common.h b/frameworks/js/napi/include/common.h index feb8a40ce4cd95ca24acb23c88e7f0fe26e33900..608996dff97a2896aa5dd26803037ae3c5d77f46 100644 --- a/frameworks/js/napi/include/common.h +++ b/frameworks/js/napi/include/common.h @@ -36,6 +36,7 @@ constexpr uint8_t PARAM0 = 0; constexpr uint8_t PARAM1 = 1; constexpr uint8_t PARAM2 = 2; constexpr uint8_t PARAM3 = 3; +constexpr uint8_t PARAM4 = 4; enum class ContentType { NOTIFICATION_CONTENT_BASIC_TEXT, @@ -43,7 +44,8 @@ enum class ContentType { NOTIFICATION_CONTENT_PICTURE, NOTIFICATION_CONTENT_CONVERSATION, NOTIFICATION_CONTENT_MULTILINE, - NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW + NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW, + NOTIFICATION_CONTENT_LIVE_VIEW }; enum class SlotType { @@ -139,11 +141,6 @@ struct NotificationSubscribeInfo { bool hasSubscribeInfo = false; }; -struct NotificationKey { - int32_t id {}; - std::string label {}; -}; - struct CallbackPromiseInfo { napi_ref callback = nullptr; napi_deferred deferred = nullptr; @@ -532,6 +529,27 @@ public: */ static napi_value SetTime(const napi_env &env, const NotificationTime &time, napi_value &result); + /** + * @brief Sets a js NotificationLiveViewContent object by specified NotificationBasicContent object + * + * @param env Indicates the environment that the API is invoked under + * @param basicContent Indicates a NotificationBasicContent object to be converted + * @param result Indicates a js object to be set + * @return Returns the null object if success, returns the null value otherwise + */ + static napi_value SetNotificationLiveViewContent( + const napi_env &env, NotificationBasicContent *basicContent, napi_value &result); + + /** + * @brief Sets a js liveview picturemap object by specified liveview picturemap + * + * @param env Indicates the environment that the API is invoked under + * @param pictureMap Indicates a picturemap object to be converted + * @return Returns the null object if success, returns the null value otherwise + */ + static napi_value SetLiveViewPictureMap( + const napi_env &env, const std::map>> &pictureMap); + /** * @brief Sets a js object by specified MessageUser object * @@ -1148,6 +1166,17 @@ public: static napi_value GetNotificationLargeIcon( const napi_env &env, const napi_value &value, NotificationRequest &request); + /** + * @brief Gets the overlay icon of NotificationRequest object from specified js object + * + * @param env Indicates the environment that the API is invoked under + * @param value Indicates a js object to be converted + * @param request Indicates a NotificationRequest object from specified js object + * @return Returns the null object if success, returns the null value otherwise + */ + static napi_value GetNotificationOverlayIcon( + const napi_env &env, const napi_value &value, NotificationRequest &request); + /** * @brief Gets the distributed options of NotificationRequest object from specified js object * @@ -1497,6 +1526,50 @@ public: static napi_value GetNotificationMultiLineContentLines(const napi_env &env, const napi_value &result, std::shared_ptr &multiLineContent); + /** + * @brief Gets the liveView content of NotificationRequest object from specified js object + * + * @param env Indicates the environment that the API is invoked under + * @param result Indicates a js object to be converted + * @param request Indicates a NotificationRequest object from specified js object + * @return Returns the null object if success, returns the null value otherwise + */ + static napi_value GetNotificationLiveViewContent( + const napi_env &env, const napi_value &result, NotificationRequest &request); + + /** + * @brief Gets a NotificationLiveViewContent object from specified js object + * + * @param env Indicates the environment that the API is invoked under + * @param contentResult Indicates a js object to be converted + * @param liveViewContent Indicates a NotificationMultiLineContent object from specified js object + * @return Returns the null object if success, returns the null value otherwise + */ + static napi_value GetNotificationLiveViewContentDetailed(const napi_env &env, const napi_value &contentResult, + std::shared_ptr &liveViewContent); + + /** + * @brief Gets a GetLiveViewPictures from specified js object + * + * @param env Indicates the environment that the API is invoked under + * @param picturesObj Indicates a js object to be converted + * @param pictures Indicates pictures object from specified js object + * @return Returns the null object if success, returns the null value otherwise + */ + static napi_value GetLiveViewPictures(const napi_env &env, const napi_value &picturesObj, + std::vector> &pictures); + + /** + * @brief Gets a GetLiveViewPictures from specified js object + * + * @param env Indicates the environment that the API is invoked under + * @param pictureMapObj Indicates a js object to be converted + * @param pictureMap Indicates picturemap from specified js object + * @return Returns the null object if success, returns the null value otherwise + */ + static napi_value GetLiveViewPictureMap(const napi_env &env, const napi_value &pictureMapObj, + std::map>> &pictureMap); + /** * @brief Gets a NotificationBundleOption object from specified js object * @@ -1725,6 +1798,7 @@ private: static const int32_t ONLY_CALLBACK_MIN_PARA = 0; static std::set> wantAgent_; static std::mutex mutex_; + static const char *GetPropertyNameByContentType(ContentType type); }; } // namespace NotificationNapi } // namespace OHOS diff --git a/frameworks/js/napi/include/manager/napi_get_active.h b/frameworks/js/napi/include/manager/napi_get_active.h index ad67dd53d87394c4eb748e4f21a984843cb0f83a..6958fa84a09d835dafc0521de87d5f0c3056be92 100644 --- a/frameworks/js/napi/include/manager/napi_get_active.h +++ b/frameworks/js/napi/include/manager/napi_get_active.h @@ -22,9 +22,17 @@ namespace OHOS { namespace NotificationNapi { using namespace OHOS::Notification; +struct AsyncLiveViewCallBackInfo { + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + LiveViewFilter filter; + sptr notificationRequest = nullptr; +}; + napi_value NapiGetAllActiveNotifications(napi_env env, napi_callback_info info); napi_value NapiGetActiveNotifications(napi_env env, napi_callback_info info); napi_value NapiGetActiveNotificationCount(napi_env env, napi_callback_info info); +napi_value NapiGetActiveNotificationByFilter(napi_env env, napi_callback_info info); } // namespace NotificationNapi } // namespace OHOS diff --git a/frameworks/js/napi/include/manager/napi_push.h b/frameworks/js/napi/include/manager/napi_push.h index a0acc30cdfd0bde5038d81301191e74d7f7069d7..4662cfde5a75aa0fdcfaab151dd6db07a7d32cc4 100644 --- a/frameworks/js/napi/include/manager/napi_push.h +++ b/frameworks/js/napi/include/manager/napi_push.h @@ -35,8 +35,9 @@ private: napi_value OnRegisterPushCallback(napi_env env, const napi_callback_info info); napi_value OnUnregisterPushCallback(napi_env env, const napi_callback_info info); bool CheckCallerIsSystemApp(); + napi_value ParseCheckRequest(const napi_env &env, const napi_value &obj, sptr &checkRequest); }; } // namespace NotificationNapi } // namespace OHOS -#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_PUSH_H \ No newline at end of file +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_PUSH_H diff --git a/frameworks/js/napi/include/manager/napi_push_callback.h b/frameworks/js/napi/include/manager/napi_push_callback.h index e4831302ebd83da2387b19d1a53566a4e2d18e31..16d5d316b63a873fc19a16e2cd7f323dd3cc5474 100644 --- a/frameworks/js/napi/include/manager/napi_push_callback.h +++ b/frameworks/js/napi/include/manager/napi_push_callback.h @@ -23,6 +23,7 @@ #include "native_engine/native_value.h" #include "parcel.h" #include "push_callback_stub.h" +#include "want.h" class NativeReference; diff --git a/frameworks/js/napi/include/manager/napi_slot.h b/frameworks/js/napi/include/manager/napi_slot.h index 0d63119ae4e505560d11bb964c87e3546616cf1e..ed5829329f8b76e21b639d44f53ba633c40cfbc8 100644 --- a/frameworks/js/napi/include/manager/napi_slot.h +++ b/frameworks/js/napi/include/manager/napi_slot.h @@ -130,6 +130,7 @@ struct ParametersInfoEnableSlot { NotificationBundleOption option; NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER; bool enable = false; + bool isForceControl = false; napi_ref callback = nullptr; }; diff --git a/frameworks/js/napi/src/common.cpp b/frameworks/js/napi/src/common.cpp index 6286155892e8a07f988c937c7719b605b22df24f..4a82d11d36473d75c1c7a32fdf820edeeced5a2e 100644 --- a/frameworks/js/napi/src/common.cpp +++ b/frameworks/js/napi/src/common.cpp @@ -15,7 +15,11 @@ #include "common.h" #include "ans_inner_errors.h" +#include "ans_log_wrapper.h" +#include "js_native_api.h" +#include "js_native_api_types.h" #include "napi_common.h" +#include "napi_common_util.h" #include "notification_action_button.h" #include "notification_capsule.h" #include "notification_progress.h" @@ -602,6 +606,21 @@ napi_value Common::SetNotificationRequestByPixelMap( } } + // overlayIcon?: image.PixelMap + std::shared_ptr overlayIcon = request->GetOverlayIcon(); + if (overlayIcon) { + napi_value overlayIconResult = nullptr; + napi_valuetype valuetype = napi_undefined; + overlayIconResult = Media::PixelMapNapi::CreatePixelMap(env, overlayIcon); + NAPI_CALL(env, napi_typeof(env, overlayIconResult, &valuetype)); + if (valuetype == napi_undefined) { + ANS_LOGI("overlayIconResult is undefined"); + napi_set_named_property(env, result, "overlayIcon", NapiGetNull(env)); + } else { + napi_set_named_property(env, result, "overlayIcon", overlayIconResult); + } + } + return NapiGetBoolean(env, true); } @@ -877,6 +896,29 @@ napi_value Common::SetNotificationSlot(const napi_env &env, const NotificationSl return NapiGetBoolean(env, true); } +const char *Common::GetPropertyNameByContentType(ContentType type) +{ + switch (type) { + case ContentType::NOTIFICATION_CONTENT_BASIC_TEXT: // normal?: NotificationBasicContent + return "normal"; + case ContentType::NOTIFICATION_CONTENT_LONG_TEXT: // longText?: NotificationLongTextContent + return "longText"; + case ContentType::NOTIFICATION_CONTENT_PICTURE: // picture?: NotificationPictureContent + return "picture"; + case ContentType::NOTIFICATION_CONTENT_CONVERSATION: // conversation?: NotificationConversationalContent + return "conversation"; + case ContentType::NOTIFICATION_CONTENT_MULTILINE: // multiLine?: NotificationMultiLineContent + return "multiLine"; + case ContentType::NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW: // systemLiveView?: NotificationLocalLiveViewContent + return "systemLiveView"; + case ContentType::NOTIFICATION_CONTENT_LIVE_VIEW: // liveView?: NotificationLiveViewContent + return "liveView"; + default: + ANS_LOGE("ContentType is does not exist"); + return "null"; + } +} + napi_value Common::SetNotificationContentDetailed(const napi_env &env, const ContentType &type, const std::shared_ptr &content, napi_value &result) { @@ -895,45 +937,37 @@ napi_value Common::SetNotificationContentDetailed(const napi_env &env, const Con napi_value contentResult = nullptr; napi_create_object(env, &contentResult); - if (type == ContentType::NOTIFICATION_CONTENT_BASIC_TEXT) { - // normal?: NotificationBasicContent - ret = SetNotificationBasicContent(env, basicContent.get(), contentResult); - if (ret) { - napi_set_named_property(env, result, "normal", contentResult); - } - } else if (type == ContentType::NOTIFICATION_CONTENT_LONG_TEXT) { - // longText?: NotificationLongTextContent - ret = SetNotificationLongTextContent(env, basicContent.get(), contentResult); - if (ret) { - napi_set_named_property(env, result, "longText", contentResult); - } - } else if (type == ContentType::NOTIFICATION_CONTENT_PICTURE) { - // picture?: NotificationPictureContent - ret = SetNotificationPictureContent(env, basicContent.get(), contentResult); - if (ret) { - napi_set_named_property(env, result, "picture", contentResult); - } - } else if (type == ContentType::NOTIFICATION_CONTENT_CONVERSATION) { - // conversation?: NotificationConversationalContent - ret = SetNotificationConversationalContent(env, basicContent.get(), contentResult); - if (ret) { - napi_set_named_property(env, result, "conversation", contentResult); - } - } else if (type == ContentType::NOTIFICATION_CONTENT_MULTILINE) { - // multiLine?: NotificationMultiLineContent - ret = SetNotificationMultiLineContent(env, basicContent.get(), contentResult); - if (ret) { - napi_set_named_property(env, result, "multiLine", contentResult); - } - } else if (type == ContentType::NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW) { - // systemLiveView?: NotificationLocalLiveViewContent - ret = SetNotificationLocalLiveViewContent(env, basicContent.get(), contentResult); - if (ret) { - napi_set_named_property(env, result, "systemLiveView", contentResult); - } - } else { - ANS_LOGE("ContentType is does not exist"); + switch (type) { + case ContentType::NOTIFICATION_CONTENT_BASIC_TEXT: // normal?: NotificationBasicContent + ret = SetNotificationBasicContent(env, basicContent.get(), contentResult); + break; + case ContentType::NOTIFICATION_CONTENT_LONG_TEXT: // longText?: NotificationLongTextContent + ret = SetNotificationLongTextContent(env, basicContent.get(), contentResult); + break; + case ContentType::NOTIFICATION_CONTENT_PICTURE: // picture?: NotificationPictureContent + ret = SetNotificationPictureContent(env, basicContent.get(), contentResult); + break; + case ContentType::NOTIFICATION_CONTENT_CONVERSATION: // conversation?: NotificationConversationalContent + ret = SetNotificationConversationalContent(env, basicContent.get(), contentResult); + break; + case ContentType::NOTIFICATION_CONTENT_MULTILINE: // multiLine?: NotificationMultiLineContent + ret = SetNotificationMultiLineContent(env, basicContent.get(), contentResult); + break; + case ContentType::NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW: // systemLiveView?: NotificationLocalLiveViewContent + ret = SetNotificationLocalLiveViewContent(env, basicContent.get(), contentResult); + break; + case ContentType::NOTIFICATION_CONTENT_LIVE_VIEW: // liveView?: NotificationLiveViewContent + ret = SetNotificationLiveViewContent(env, basicContent.get(), contentResult); + break; + default: + ANS_LOGE("ContentType is does not exist"); + return nullptr; } + if (ret) { + const char *propertyName = GetPropertyNameByContentType(type); + napi_set_named_property(env, result, propertyName, contentResult); + } + return ret; } @@ -1340,6 +1374,80 @@ napi_value Common::SetTime(const napi_env &env, const NotificationTime &time, na return NapiGetBoolean(env, true); } +napi_value Common::SetNotificationLiveViewContent( + const napi_env &env, NotificationBasicContent *basicContent, napi_value &result) +{ + ANS_LOGI("enter"); + napi_value value = nullptr; + if (basicContent == nullptr) { + ANS_LOGE("BasicContent is null"); + return NapiGetBoolean(env, false); + } + + auto liveViewContent = static_cast(basicContent); + if (liveViewContent == nullptr) { + ANS_LOGE("LiveViewContent is null"); + return NapiGetBoolean(env, false); + } + + // status: LiveViewStatus + napi_create_int32(env, (int32_t)(liveViewContent->GetLiveViewStatus()), &value); + napi_set_named_property(env, result, "status", value); + + + // version?: int32_t + napi_create_int32(env, liveViewContent->GetVersion(), &value); + napi_set_named_property(env, result, "version", value); + + // extraInfo?: {[key:string] : any} + std::shared_ptr extraInfoData = liveViewContent->GetExtraInfo(); + if (extraInfoData != nullptr) { + napi_value extraInfo = OHOS::AppExecFwk::WrapWantParams(env, *extraInfoData); + napi_set_named_property(env, result, "extraInfo", extraInfo); + } + + // pictureMap?: {[key, string]: Array} + if (liveViewContent->GetPicture().empty()) { + ANS_LOGI("No pictures in live view."); + return NapiGetBoolean(env, true); + } + + napi_value pictureMapObj = SetLiveViewPictureMap(env, liveViewContent->GetPicture()); + if (pictureMapObj == nullptr) { + ANS_LOGE("Set live view picture map failed."); + return NapiGetBoolean(env, false); + } + napi_set_named_property(env, result, "pictureMap", pictureMapObj); + + return NapiGetBoolean(env, true); +} + +napi_value Common::SetLiveViewPictureMap( + const napi_env &env, const std::map>> &pictureMap) +{ + ANS_LOGI("enter"); + + napi_value pictureMapObj = nullptr; + NAPI_CALL(env, napi_create_object(env, &pictureMapObj)); + + for (auto iter = pictureMap.begin(); iter != pictureMap.end(); iter++) { + int count = 0; + napi_value picturesObj = nullptr; + napi_create_array(env, &picturesObj); + for (auto picture : iter->second) { + napi_value pictureObj = Media::PixelMapNapi::CreatePixelMap(env, picture); + napi_set_element(env, picturesObj, count, pictureObj); + count++; + } + + if (count > 0) { + napi_set_named_property(env, pictureMapObj, iter->first.c_str(), picturesObj); + } + } + + return pictureMapObj; +} + napi_value Common::SetMessageUser(const napi_env &env, const MessageUser &messageUser, napi_value &result) { ANS_LOGI("enter"); @@ -1854,6 +1962,10 @@ napi_value Common::GetNotificationRequestByCustom( if (GetNotificationLargeIcon(env, value, request) == nullptr) { return nullptr; } + // overlayIcon?: image.PixelMap + if (GetNotificationOverlayIcon(env, value, request) == nullptr) { + return nullptr; + } // distributedOption?:DistributedOptions if (GetNotificationRequestDistributedOptions(env, value, request) == nullptr) { return nullptr; @@ -2023,6 +2135,11 @@ napi_value Common::GetNotificationContent(const napi_env &env, const napi_value return nullptr; } break; + case NotificationContent::Type::LIVE_VIEW: + if (GetNotificationLiveViewContent(env, result, request) == nullptr) { + return nullptr; + } + break; default: return nullptr; } @@ -3087,6 +3204,35 @@ napi_value Common::GetNotificationLargeIcon(const napi_env &env, const napi_valu return NapiGetNull(env); } +napi_value Common::GetNotificationOverlayIcon( + const napi_env &env, const napi_value &value, NotificationRequest &request) +{ + ANS_LOGI("enter"); + + napi_valuetype valuetype = napi_undefined; + napi_value result = nullptr; + bool hasProperty = false; + + NAPI_CALL(env, napi_has_named_property(env, value, "overlayIcon", &hasProperty)); + if (hasProperty) { + napi_get_named_property(env, value, "overlayIcon", &result); + NAPI_CALL(env, napi_typeof(env, result, &valuetype)); + if (valuetype != napi_object) { + ANS_LOGE("Wrong argument type. Object expected."); + return nullptr; + } + std::shared_ptr pixelMap = nullptr; + pixelMap = Media::PixelMapNapi::GetPixelMap(env, result); + if (pixelMap == nullptr) { + ANS_LOGE("Invalid object pixelMap"); + return nullptr; + } + request.SetOverlayIcon(pixelMap); + } + + return NapiGetNull(env); +} + napi_value Common::GetNotificationRequestDistributedOptions(const napi_env &env, const napi_value &value, NotificationRequest &request) { @@ -3666,6 +3812,161 @@ napi_value Common::GetNotificationConversationalContent( return NapiGetNull(env); } +napi_value Common::GetNotificationLiveViewContent( + const napi_env &env, const napi_value &result, NotificationRequest &request) +{ + ANS_LOGI("Enter"); + + napi_value contentResult = AppExecFwk::GetPropertyValueByPropertyName(env, result, "liveView", napi_object); + if (contentResult == nullptr) { + ANS_LOGE("Property liveView expected."); + return nullptr; + } + + std::shared_ptr liveViewContent = std::make_shared(); + if (liveViewContent == nullptr) { + ANS_LOGE("LiveViewContent is null"); + return nullptr; + } + + if (GetNotificationLiveViewContentDetailed(env, contentResult, liveViewContent) == nullptr) { + return nullptr; + } + + request.SetContent(std::make_shared(liveViewContent)); + + return NapiGetNull(env); +} + +napi_value Common::GetNotificationLiveViewContentDetailed( + const napi_env &env, const napi_value &contentResult, + std::shared_ptr &liveViewContent) +{ + ANS_LOGI("enter"); + + // status: NotificationLiveViewContent::LiveViewStatus + int32_t status = 0; + if (!AppExecFwk::UnwrapInt32ByPropertyName(env, contentResult, "status", status)) { + ANS_LOGE("Failed to get status from liveView content."); + return nullptr; + } + liveViewContent->SetLiveViewStatus(static_cast(status)); + + // version?: uint32_t + napi_value jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, + "version", napi_number); + if (jsValue != nullptr) { + int32_t version = 0; + NAPI_CALL(env, napi_get_value_int32(env, jsValue, &version)); + liveViewContent->SetVersion(version); + } + + // extraInfo?: {[key:string] : any} + jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "extraInfo", napi_object); + if (jsValue != nullptr) { + AAFwk::WantParams wantParams; + if (!OHOS::AppExecFwk::UnwrapWantParams(env, jsValue, wantParams)) { + return nullptr; + } + std::shared_ptr extras = std::make_shared(wantParams); + liveViewContent->SetExtraInfo(extras); + } + + // pictureMap?: {[key, string]: Array} + jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, contentResult, "pictureMap", napi_object); + if (jsValue == nullptr) { + ANS_LOGI("No picture maps."); + return NapiGetNull(env); + } + + std::map>> pictureMap; + if (GetLiveViewPictureMap(env, jsValue, pictureMap) == nullptr) { + ANS_LOGE("Failed to get picture map from liveView content."); + return nullptr; + } + liveViewContent->SetPicture(pictureMap); + + return NapiGetNull(env); +} + +napi_value Common::GetLiveViewPictures( + const napi_env &env, const napi_value &picturesObj, + std::vector> &pictures) +{ + ANS_LOGI("enter"); + + bool isArray = false; + napi_is_array(env, picturesObj, &isArray); + if (!isArray) { + ANS_LOGE("The picture is not array."); + return nullptr; + } + + uint32_t length = 0; + napi_get_array_length(env, picturesObj, &length); + if (length == 0) { + ANS_LOGE("The array is empty."); + return nullptr; + } + + for (uint32_t i = 0; i < length; ++i) { + napi_value pictureObj = nullptr; + napi_get_element(env, picturesObj, i, &pictureObj); + if (!AppExecFwk::IsTypeForNapiValue(env, pictureObj, napi_object)) { + ANS_LOGE("Wrong argument type. object expected."); + break; + } + + std::shared_ptr pixelMap = Media::PixelMapNapi::GetPixelMap(env, pictureObj); + if (pixelMap == nullptr) { + ANS_LOGE("Invalid pixelMap."); + break; + } + + pictures.emplace_back(pixelMap); + } + + return NapiGetNull(env); +} + +napi_value Common::GetLiveViewPictureMap( + const napi_env &env, const napi_value &pictureMapObj, + std::map>> &pictureMap) +{ + ANS_LOGI("Enter"); + + napi_value pictureNamesObj = nullptr; + uint32_t length = 0; + if (napi_get_property_names(env, pictureMapObj, &pictureNamesObj) != napi_ok) { + ANS_LOGE("Get picture names failed."); + return nullptr; + } + napi_get_array_length(env, pictureNamesObj, &length); + if (length == 0) { + ANS_LOGE("The pictures name is empty."); + return nullptr; + } + + napi_value pictureNameObj = nullptr; + napi_value picturesObj = nullptr; + for (uint32_t index = 0; index < length; index++) { + napi_get_element(env, pictureNamesObj, index, &pictureNameObj); + std::string pictureName = AppExecFwk::UnwrapStringFromJS(env, pictureNameObj); + ANS_LOGD("%{public}s called, get pictures of %{public}s.", __func__, pictureName.c_str()); + napi_get_named_property(env, pictureMapObj, pictureName.c_str(), &picturesObj); + + std::vector> pictures; + if (!GetLiveViewPictures(env, picturesObj, pictures)) { + ANS_LOGE("Get pictures of %{public}s failed.", pictureName.c_str()); + break; + } + + pictureMap[pictureName] = pictures; + } + + return NapiGetNull(env); +} + napi_value Common::GetNotificationConversationalContentByUser( const napi_env &env, const napi_value &contentResult, MessageUser &user) { @@ -5050,6 +5351,9 @@ bool Common::ContentTypeJSToC(const ContentType &inType, NotificationContent::Ty case ContentType::NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW: outType = NotificationContent::Type::LOCAL_LIVE_VIEW; break; + case ContentType::NOTIFICATION_CONTENT_LIVE_VIEW: + outType = NotificationContent::Type::LIVE_VIEW; + break; default: ANS_LOGE("ContentType %{public}d is an invalid value", inType); return false; @@ -5078,6 +5382,9 @@ bool Common::ContentTypeCToJS(const NotificationContent::Type &inType, ContentTy case NotificationContent::Type::LOCAL_LIVE_VIEW: outType = ContentType::NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW; break; + case NotificationContent::Type::LIVE_VIEW: + outType = ContentType::NOTIFICATION_CONTENT_LIVE_VIEW; + break; default: ANS_LOGE("ContentType %{public}d is an invalid value", inType); return false; diff --git a/frameworks/js/napi/src/manager/init_module.cpp b/frameworks/js/napi/src/manager/init_module.cpp index 3e82d3a482369e4488b2b979a4821b745e30dde1..adfe52ea9271521c9626d9ff82928eca005ceb31 100644 --- a/frameworks/js/napi/src/manager/init_module.cpp +++ b/frameworks/js/napi/src/manager/init_module.cpp @@ -86,6 +86,7 @@ napi_value NotificationManagerInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getAllActiveNotifications", NapiGetAllActiveNotifications), DECLARE_NAPI_FUNCTION("getActiveNotifications", NapiGetActiveNotifications), DECLARE_NAPI_FUNCTION("getActiveNotificationCount", NapiGetActiveNotificationCount), + DECLARE_NAPI_FUNCTION("getActiveNotificationByFilter", NapiGetActiveNotificationByFilter), DECLARE_NAPI_FUNCTION("displayBadge", NapiDisplayBadge), DECLARE_NAPI_FUNCTION("isBadgeDisplayed", NapiIsBadgeDisplayed), DECLARE_NAPI_FUNCTION("setBadgeNumber", NapiSetBadgeNumber), diff --git a/frameworks/js/napi/src/manager/napi_get_active.cpp b/frameworks/js/napi/src/manager/napi_get_active.cpp index 01fe2e44606506bc2c5e540b240c07ae6de14264..55228a5f487652576398a5e921d026ecbab31841 100644 --- a/frameworks/js/napi/src/manager/napi_get_active.cpp +++ b/frameworks/js/napi/src/manager/napi_get_active.cpp @@ -17,6 +17,9 @@ #include "ans_inner_errors.h" #include "get_active.h" +#include "napi_common_util.h" +#include "napi_common_want.h" +#include namespace OHOS { namespace NotificationNapi { @@ -324,5 +327,158 @@ napi_value NapiGetActiveNotificationCount(napi_env env, napi_callback_info info) return promise; } } + +napi_value ParseGetLiveViewFilter(const napi_env &env, const napi_value &obj, LiveViewFilter &filter) +{ + napi_value result = AppExecFwk::GetPropertyValueByPropertyName(env, obj, "bundle", napi_object); + if (result == nullptr) { + ANS_LOGW("Failed to get bundle from params."); + return nullptr; + } + auto retValue = Common::GetBundleOption(env, result, filter.bundle); + if (retValue == nullptr) { + ANS_LOGE("GetBundleOption failed"); + return nullptr; + } + + result = AppExecFwk::GetPropertyValueByPropertyName(env, obj, "notificationKeys", napi_object); + if (result == nullptr) { + ANS_LOGW("Failed to get notificationKeys from params."); + return nullptr; + } + retValue = Common::GetNotificationKey(env, result, filter.notificationKey); + if (retValue == nullptr) { + ANS_LOGE("GetNotificationKey failed"); + return nullptr; + } + + if (!AppExecFwk::UnwrapStringArrayByPropertyName(env, obj, "extraInfoKeys", filter.extraInfoKeys)) { + ANS_LOGE("GetExtraInfoKeys failed."); + return nullptr; + } + + return Common::NapiGetNull(env); +} + +napi_value ParseGetLiveViewParams(const napi_env &env, const napi_callback_info &info, + LiveViewFilter &filter, napi_ref &callback) +{ + ANS_LOGI("Enter"); + + size_t argc = ARGS_TWO; + napi_value argv[ARGS_TWO] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); + if (argc < ARGS_ONE) { + ANS_LOGE("Wrong number of arguments"); + return nullptr; + } + + // argv[0] : filter + if (AppExecFwk::IsTypeForNapiValue(env, argv[0], napi_object)) { + ANS_LOGW("Wrong filter type. Object expected."); + return nullptr; + } + if (ParseGetLiveViewFilter(env, argv[0], filter) ==nullptr) { + ANS_LOGW("Parse filter from param failed."); + return nullptr; + } + + // argv[1] : callback + if (argc > ARGS_ONE) { + if (!AppExecFwk::IsTypeForNapiValue(env, argv[1], napi_function)) { + ANS_LOGE("Callback is not function excute promise."); + return Common::NapiGetNull(env); + } + napi_create_reference(env, argv[1], 1, &callback); + } + + ANS_LOGI("End"); + return Common::NapiGetNull(env); +} + +void AsyncGetLiveViewExecute(napi_env env, void *data) +{ + ANS_LOGI("NapiGetActiveNotificationByFilter work excute."); + + auto asyncLiveViewCallBackInfo = static_cast(data); + if (asyncLiveViewCallBackInfo) { + } +} + +void AsyncGetLiveViewComplete(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + + auto asyncCallbackinfo = static_cast(data); + if (asyncCallbackinfo == nullptr) { + ANS_LOGE("Invalid async live view callback data."); + return; + } + + ANS_LOGD("Conversion data is success."); + napi_value result = nullptr; + if (asyncCallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + if (asyncCallbackinfo->notificationRequest == nullptr) { + result = Common::NapiGetNull(env); + } else { + napi_create_object(env, &result); + if (!Common::SetNotificationRequest(env, asyncCallbackinfo->notificationRequest, result)) { + result = Common::NapiGetNull(env); + } + } + } + + Common::CreateReturnValue(env, asyncCallbackinfo->info, result); + if (asyncCallbackinfo->info.callback != nullptr) { + ANS_LOGD("Delete NapiGetActiveNotificationByFilter callback reference."); + napi_delete_reference(env, asyncCallbackinfo->info.callback); + } + napi_delete_async_work(env, asyncCallbackinfo->asyncWork); +} + +napi_value NapiGetActiveNotificationByFilter(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + AsyncLiveViewCallBackInfo asyncLiveViewCallBackInfo; + napi_ref callback = nullptr; + if (ParseGetLiveViewParams(env, info, asyncLiveViewCallBackInfo.filter, callback) == nullptr) { + ANS_LOGD("ParseGetLiveViewParams is nullptr."); + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asyncLiveViewCallBackInfo.info, promise); + asyncLiveViewCallBackInfo.notificationRequest = new (std::nothrow) NotificationRequest(); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getActiveNotificationByFilter", NAPI_AUTO_LENGTH, &resourceName); + napi_create_async_work(env, nullptr, resourceName, + AsyncGetLiveViewExecute, AsyncGetLiveViewComplete, + (void *)&asyncLiveViewCallBackInfo, &asyncLiveViewCallBackInfo.asyncWork); + + bool isCallback = asyncLiveViewCallBackInfo.info.isCallback; + napi_status status = napi_queue_async_work_with_qos( + env, asyncLiveViewCallBackInfo.asyncWork, napi_qos_user_initiated); + if (status != napi_ok) { + ANS_LOGE("GetActiveNotificationByFilter work failed return: %{public}d", status); + asyncLiveViewCallBackInfo.info.errorCode = ERROR_INTERNAL_ERROR; + Common::CreateReturnValue(env, asyncLiveViewCallBackInfo.info, Common::NapiGetNull(env)); + if (asyncLiveViewCallBackInfo.info.callback != nullptr) { + ANS_LOGD("Delete napiGetActiveNotificationByFilter callback reference."); + napi_delete_reference(env, asyncLiveViewCallBackInfo.info.callback); + } + napi_delete_async_work(env, asyncLiveViewCallBackInfo.asyncWork); + } + + if (isCallback) { + ANS_LOGD("NapiGetActiveNotificationByFilter callback is nullptr."); + return Common::NapiGetNull(env); + } + + return promise; +} } // namespace NotificationNapi -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/frameworks/js/napi/src/manager/napi_push.cpp b/frameworks/js/napi/src/manager/napi_push.cpp index 01e433888cfbba92b512c38abc25e46fbecca85d..27c3d66496e195c9078fee9251e609960df82485 100644 --- a/frameworks/js/napi/src/manager/napi_push.cpp +++ b/frameworks/js/napi/src/manager/napi_push.cpp @@ -15,18 +15,22 @@ #include "napi_push.h" #include "ans_inner_errors.h" +#include "common.h" #include "ipc_skeleton.h" #include "js_error_utils.h" #include "js_runtime_utils.h" #include "tokenid_kit.h" +#include "napi_common_util.h" namespace OHOS { namespace NotificationNapi { namespace { constexpr size_t ARGC_ONE = 1; constexpr size_t ARGC_TWO = 2; +constexpr size_t ARGC_THREE = 3; constexpr int32_t INDEX_ZERO = 0; constexpr int32_t INDEX_ONE = 1; +constexpr int32_t INDEX_TWO = 2; } // namespace using namespace OHOS::AbilityRuntime; @@ -54,11 +58,11 @@ napi_value NapiPush::OnRegisterPushCallback(napi_env env, const napi_callback_in napi_value undefined = nullptr; napi_get_undefined(env, &undefined); - size_t argc = ARGC_TWO; - napi_value argv[ARGC_TWO] = {nullptr}; + size_t argc = ARGC_THREE; + napi_value argv[ARGC_THREE] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - if (argc < ARGC_TWO) { + if (argc < ARGC_THREE) { ANS_LOGE("The param is invalid."); ThrowTooFewParametersError(env); return undefined; @@ -81,6 +85,13 @@ napi_value NapiPush::OnRegisterPushCallback(napi_env env, const napi_callback_in return undefined; } + sptr checkRequest = new NotificationCheckRequest(); + if (ParseCheckRequest(env, argv[INDEX_ONE], checkRequest) == nullptr) { + ANS_LOGE("Failed to get check request info from param"); + ThrowError(env, ERROR_PARAM_INVALID); + return undefined; + } + if (!CheckCallerIsSystemApp()) { ThrowError(env, ERROR_NOT_SYSTEM_APP); return undefined; @@ -95,7 +106,7 @@ napi_value NapiPush::OnRegisterPushCallback(napi_env env, const napi_callback_in } } - jsPushCallBack_->SetJsPushCallBackObject(argv[INDEX_ONE]); + jsPushCallBack_->SetJsPushCallBackObject(argv[INDEX_TWO]); NotificationHelper::RegisterPushCallback(jsPushCallBack_->AsObject()); return undefined; } @@ -167,5 +178,40 @@ bool NapiPush::CheckCallerIsSystemApp() } return true; } + +napi_value NapiPush::ParseCheckRequest(const napi_env &env, + const napi_value &obj, sptr &checkRequest) +{ + ANS_LOGI("Enter"); + + if (AppExecFwk::IsTypeForNapiValue(env, obj, napi_object)) { + ANS_LOGW("Wrong argument type. Object expected."); + return nullptr; + } + + int32_t value = 0; + if (!AppExecFwk::UnwrapInt32ByPropertyName(env, obj, "contentType", value)) { + ANS_LOGE("Failed to get contentType from checkRequest."); + return nullptr; + } + checkRequest->SetContentType(static_cast(value)); + + if (!AppExecFwk::UnwrapInt32ByPropertyName(env, obj, "slotType", value)) { + ANS_LOGE("Failed to get slotType from checkRequest."); + return nullptr; + } + checkRequest->SetSlotType(static_cast(value)); + + std::vector extraInfoKeys; + if (!AppExecFwk::UnwrapStringArrayByPropertyName(env, obj, "extraInfoKeys", extraInfoKeys)) { + ANS_LOGE("Failed to get extraInfoKeys from checkRequest."); + return nullptr; + } + checkRequest->SetExtraKeys(extraInfoKeys); + + ANS_LOGI("End"); + return Common::NapiGetNull(env); +} + } // namespace NotificationNapi } // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/manager/napi_push_callback.cpp b/frameworks/js/napi/src/manager/napi_push_callback.cpp index de49a182b05036002d2898e5e22b5ec48c76a7d0..26bf3eea5e5b462b19677e04d9116ac94986e731 100644 --- a/frameworks/js/napi/src/manager/napi_push_callback.cpp +++ b/frameworks/js/napi/src/manager/napi_push_callback.cpp @@ -19,6 +19,8 @@ #include "js_runtime.h" #include "js_runtime_utils.h" #include "nlohmann/json.hpp" +#include "notification_check_info.h" +#include "napi_common_want.h" namespace OHOS { namespace Notification { @@ -201,4 +203,4 @@ bool JSPushCallBack::ConvertFunctionResult(napi_value funcResult) return code == 0; } } // namespace Notification -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/frameworks/js/napi/src/slot.cpp b/frameworks/js/napi/src/slot.cpp index 1005a040c3a6ad76013f5636fbccf7f3aaa2ae35..181a4de6af452e6cdde2f42bc93826a43b001f52 100644 --- a/frameworks/js/napi/src/slot.cpp +++ b/frameworks/js/napi/src/slot.cpp @@ -14,6 +14,8 @@ */ #include "slot.h" +#include "common.h" +#include "napi_common_util.h" namespace OHOS { namespace NotificationNapi { @@ -25,7 +27,8 @@ const int32_t GET_SLOT_NUM_AS_BUNDLE_MAX_PARA = 2; const int32_t GET_SLOTS_AS_BUNDLE_MAX_PARA = 2; const int32_t REMOVE_SLOT_MAX_PARA = 2; const int32_t GET_ENABLE_SLOT_MAX_PARA = 3; -const int32_t SET_ENABLE_SLOT_MAX_PARA = 4; +const int32_t SET_ENABLE_SLOT_MIN_PARA = 5; +const int32_t SET_ENABLE_SLOT_MAX_PARA = 5; struct ParametersInfoAddSlot { NotificationSlot slot; @@ -136,6 +139,7 @@ struct ParametersInfoEnableSlot { NotificationBundleOption option; NotificationConstant::SlotType outType = NotificationConstant::SlotType::OTHER; bool enable = false; + bool isForceControl = false; napi_ref callback = nullptr; }; @@ -1209,6 +1213,23 @@ napi_value RemoveAllSlots(napi_env env, napi_callback_info info) } } +static napi_value ParseEnableSlotCallBackParam( + const napi_env &env, size_t argc, napi_value *argv, ParametersInfoEnableSlot ¶ms) +{ + // argv[4]: callback + if (argc < SET_ENABLE_SLOT_MAX_PARA) { + return Common::NapiGetNull(env); + } + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[PARAM4], &valuetype)); + if (valuetype != napi_function) { + ANS_LOGW("Callback is not function excute promise."); + return Common::NapiGetNull(env);; + } + napi_create_reference(env, argv[PARAM4], 1, ¶ms.callback); + return Common::NapiGetNull(env); +} + napi_value ParseParametersEnableSlot( const napi_env &env, const napi_callback_info &info, ParametersInfoEnableSlot ¶ms) { @@ -1216,17 +1237,15 @@ napi_value ParseParametersEnableSlot( size_t argc = SET_ENABLE_SLOT_MAX_PARA; napi_value argv[SET_ENABLE_SLOT_MAX_PARA] = {nullptr}; - napi_value thisVar = nullptr; - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - if (argc < SET_ENABLE_SLOT_MAX_PARA - 1) { + + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); + if (argc < SET_ENABLE_SLOT_MIN_PARA) { ANS_LOGW("Wrong number of arguments."); return nullptr; } // argv[0]: bundle - NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); - if (valuetype != napi_object) { + if (OHOS::AppExecFwk::IsTypeForNapiValue(env, argv[PARAM0], napi_object)) { ANS_LOGW("Parameter type is error. Object expected."); return nullptr; } @@ -1237,8 +1256,7 @@ napi_value ParseParametersEnableSlot( } // argv[1]: SlotType - NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); - if (valuetype != napi_number) { + if (OHOS::AppExecFwk::IsTypeForNapiValue(env, argv[PARAM1], napi_number)) { ANS_LOGW("Parameter type error. Number expected."); return nullptr; } @@ -1249,24 +1267,30 @@ napi_value ParseParametersEnableSlot( } // argv[2]: enable - NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype)); - if (valuetype != napi_boolean) { + if (OHOS::AppExecFwk::IsTypeForNapiValue(env, argv[PARAM2], napi_boolean)) { ANS_LOGW("Wrong argument type. Bool expected."); return nullptr; } napi_get_value_bool(env, argv[PARAM2], ¶ms.enable); - // argv[3]:callback - if (argc >= SET_ENABLE_SLOT_MAX_PARA) { - NAPI_CALL(env, napi_typeof(env, argv[PARAM3], &valuetype)); - if (valuetype != napi_function) { - ANS_LOGW("Callback is not function excute promise."); - return Common::NapiGetNull(env); - } + if (argc < SET_ENABLE_SLOT_MAX_PARA - 1) { + return Common::NapiGetNull(env); + } + + // argv[3]: maybe isForceControl or callback + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[PARAM3], &valuetype)); + if (valuetype == napi_boolean) { + napi_get_value_bool(env, argv[PARAM3], ¶ms.isForceControl); + } else if (valuetype == napi_function) { napi_create_reference(env, argv[PARAM3], 1, ¶ms.callback); + return Common::NapiGetNull(env); + } else { + ANS_LOGW("Callback is not function excute promise."); + return Common::NapiGetNull(env); } - return Common::NapiGetNull(env); + return ParseEnableSlotCallBackParam(env, argc, argv, params); } napi_value EnableNotificationSlot(napi_env env, napi_callback_info info) diff --git a/interfaces/inner_api/notification_check_info.h b/interfaces/inner_api/notification_check_info.h new file mode 100644 index 0000000000000000000000000000000000000000..276aa5935f593873a5a89045a6b76ad66c608007 --- /dev/null +++ b/interfaces/inner_api/notification_check_info.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 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. + */ +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_CHECK_INFO_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_CHECK_INFO_H + +#include "nlohmann/json.hpp" +#include "want.h" + +namespace OHOS { +namespace Notification { + +class NotificationCheckInfo { +private: + std::string pkgName_; + int32_t notifyId_; + int32_t contentType_; + int32_t creatorUserId_; + int32_t slotType_; + std::shared_ptr extraInfo_ {}; + void ConvertJsonExtraInfoToValue(nlohmann::json &jsonobj); +public: + NotificationCheckInfo() = default; + NotificationCheckInfo(std::string pkgName, int32_t notifyId, int32_t contentType, + int32_t creatorUserId, int32_t slotType, std::shared_ptr extraInfo); + ~NotificationCheckInfo(); + std::string GetPkgName() const; + void SetPkgName(std::string pkgName); + int32_t GetNotifyId() const; + void SetNotifyId(int32_t notifyId); + int32_t GetContentType() const; + void SetContentType(int32_t contentType); + int32_t GetCreatorUserId() const; + void SetCreatorUserId(int32_t creatorUserId); + int32_t GetSlotType() const; + void SetSlotType(int32_t slotType); + std::shared_ptr GetExtraInfo() const; + void SetExtraInfo(std::shared_ptr extraInfo); + void ConvertJsonStringToValue(const std::string ¬ificationData); +}; +} // namespace Notification +} // namespace OHOS + +#endif //BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_CHECK_INFO_H diff --git a/interfaces/inner_api/notification_check_request.h b/interfaces/inner_api/notification_check_request.h new file mode 100644 index 0000000000000000000000000000000000000000..1c3ebc9cf5c6c997757f25749acf544aafbb82bf --- /dev/null +++ b/interfaces/inner_api/notification_check_request.h @@ -0,0 +1,130 @@ +/* + * Copyright (c) 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. + */ +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_CHECK_REQUEST_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_CHECK_REQUEST_H + +#include "parcel.h" +#include "notification_content.h" +#include "notification_constant.h" + +namespace OHOS { +namespace Notification { +class NotificationCheckRequest : public Parcelable { +public: + NotificationCheckRequest() = default; + + /** + * @brief A constructor used to create a NotificationCheckRequest instance based on the filter condition. + * + * @param contentType Indicates type of notification content. + * @param SlotType Indicates type of slot. + * @param extraInfoKeys Indicates keys of extra info that need to be filtered. + */ + NotificationCheckRequest(NotificationContent::Type contentType, NotificationConstant::SlotType slotType, + std::vector extraInfoKeys); + + ~NotificationCheckRequest(); + + /** + * @brief Returns a string representation of the object. + * + * @return Returns a string representation of the object. + */ + std::string Dump(); + + /** + * @brief Sets the content type of a notification check request. + * + * @param contentType Indicates the content type of the notification check request. + */ + void SetContentType(NotificationContent::Type contentType); + + /** + * @brief Obtains the content type of a notification check request. + * + * @return Returns the content type of a notification check request. + */ + NotificationContent::Type GetContentType() const; + + /** + * @brief Sets the slot type of a notification check request. + * + * @param slotType Indicates the slot type of the notification check request. + */ + void SetSlotType(NotificationConstant::SlotType slotType); + + /** + * @brief Obtains the slot type of a notification check request. + * + * @return Returns the slot type of a notification check request. + */ + NotificationConstant::SlotType GetSlotType() const; + + /** + * @brief Sets the extra info keys of a notification check request. + * + * @param extraKeys Indicates the extra info keys of the notification check request. + */ + void SetExtraKeys(std::vector extraKeys); + + /** + * @brief Obtains the extra info keys of a notification check request. + * + * @return Returns the extra info keys of a notification check request. + */ + std::vector GetExtraKeys() const; + + /** + * @brief Sets the creator uid. + * + * @param uid Indicates the creator uid. + */ + void SetUid(const int32_t uid); + + /** + * @brief Obtains the creator uid. + * + * @return Returns the creator uid. + */ + int32_t GetUid() const; + + /** + * @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 NotificationBundleOption + */ + static NotificationCheckRequest *Unmarshalling(Parcel &parcel); + + bool ReadFromParcel(Parcel &parcel); + +private: + NotificationContent::Type contentType_ {}; + NotificationConstant::SlotType slotType_ {}; + std::vector extraKeys_ {}; + int32_t creatorUid_ {}; +}; +} // namespace Notification +} // namespace OHOS + +#endif //BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_CHECK_REQUEST_H diff --git a/interfaces/inner_api/notification_content.h b/interfaces/inner_api/notification_content.h index 29b2697f4c973a46c20c77ec85d44bc91073018e..6469aeb4da13bf0a4463813bbf8ac1de21d34a48 100644 --- a/interfaces/inner_api/notification_content.h +++ b/interfaces/inner_api/notification_content.h @@ -24,6 +24,7 @@ #include "notification_multiline_content.h" #include "notification_normal_content.h" #include "notification_picture_content.h" +#include "notification_live_view_content.h" #include "notification_local_live_view_content.h" #include "parcel.h" @@ -69,7 +70,12 @@ public: * Indicates notifications that include local live view. * Such notifications are created using NotificationLocalLiveViewContent. */ - LOCAL_LIVE_VIEW + LOCAL_LIVE_VIEW, + /** + * Indicates notifications that include a live view. + * Such notifications are created using NotificationLiveViewContent. + */ + LIVE_VIEW }; /** @@ -135,6 +141,14 @@ public: */ explicit NotificationContent(const std::shared_ptr &localLiveViewContent); + /** + * @brief A constructor used to create a NotificationLiveViewContent instance (obtained by calling + * GetNotificationContent()) and set the content type to NotificationContent::Type::LIVE_VIEW (obtained by calling + * GetContentType()). + * + * @param liveViewContent Indicates the NotificationMediaContent object. + */ + explicit NotificationContent(const std::shared_ptr &liveViewContent); virtual ~NotificationContent(); /** @@ -145,8 +159,10 @@ public: * NotificationContent::Type::LONG_TEXT, * NotificationContent::Type::PICTURE, * NotificationContent::Type::CONVERSATION, - * NotificationContent::Type::MULTILINE, or - * NotificationContent::Type::MEDIA. + * NotificationContent::Type::MULTILINE, + * NotificationContent::Type::MEDIA, + * NotificationContent::Type::LIVE_VIEW, or + * NotificationContent::Type::LOCAL_LIVE_VIEW */ NotificationContent::Type GetContentType() const; diff --git a/interfaces/inner_api/notification_live_view_content.h b/interfaces/inner_api/notification_live_view_content.h new file mode 100644 index 0000000000000000000000000000000000000000..e375f811138d5bb70f1eed7d01f893ac315c5f38 --- /dev/null +++ b/interfaces/inner_api/notification_live_view_content.h @@ -0,0 +1,160 @@ +/* + * Copyright (c) 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. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_LIVE_VIEW_CONTENT_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_LIVE_VIEW_CONTENT_H + +#include "notification_basic_content.h" +#include "parcel.h" +#include "pixel_map.h" +#include "want_params.h" + +namespace OHOS { +namespace Notification { +using PictureMap = std::map>>; +class NotificationLiveViewContent : public NotificationBasicContent { +public: + static const uint32_t MAX_VERSION; + enum class LiveViewStatus { + LIVE_VIEW_CREATE, + LIVE_VIEW_BATCH_UPDATE, + LIVE_VIEW_END, + LIVE_VIEW_FULL_UPDATE, + LIVE_VIEW_BUTT + }; + + NotificationLiveViewContent() = default; + ~NotificationLiveViewContent() override = default; + /** + * @brief Set the status of the liveView notification. + * + * @param status Indicates the status of liveView notification. + */ + void SetLiveViewStatus(const LiveViewStatus status); + + /** + * @brief Obtains the status of the liveView notification. + * + * @return Returns the status that attached to this notification. + */ + LiveViewStatus GetLiveViewStatus() const; + + /** + * @brief Set the version of the liveView notification. + * + * @param status Indicates the version of liveView notification. + */ + void SetVersion(int32_t version); + + /** + * @brief Obtains the version of the liveView notification. + * + * @return Returns the version that attached to this notification. + */ + int32_t GetVersion() const; + + /** + * @brief Sets extra parameters that are stored as key-value pairs for the notification content. + * + * @param extras Indicates the WantParams object containing the extra parameters in key-value pair format. + */ + void SetExtraInfo(const std::shared_ptr &extras); + + /** + * @brief Obtains the WantParams object set in the notification content. + * + * @return Returns the WantParams object. + */ + std::shared_ptr GetExtraInfo() const; + + /** + * @brief Sets extra picture parameters that are stored as key-value pairs for the notification content. + * + * @param picture Indicates the picture object containing the extra picture parameters in key-value pair format. + */ + void SetPicture(const PictureMap &pictureMap); + + /** + * @brief Obtains the picture object map in the notification content. + * + * @return Returns the picture map object. + */ + PictureMap GetPicture() const; + + /** + * @brief Returns a string representation of the object. + * + * @return Returns a string representation of the object. + */ + std::string Dump() override; + + /** + * @brief Converts a NotificationLiveViewContent 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 NotificationLiveViewContent object from a Json. + * + * @param jsonObject Indicates the Json object. + * @return Returns the NotificationLiveViewContent object. + */ + static NotificationLiveViewContent *FromJson(const nlohmann::json &jsonObject); + + /** + * @brief Creates a picture object from a Json. + * + * @param jsonObject Indicates the Json object. + */ + void ConvertPictureFromJson(const nlohmann::json &jsonObject); + + /** + * @brief Marshal a object into a Parcel. + * @param parcel the object into the parcel. + * @return Returns true if succeed; returns false otherwise. + */ + bool Marshalling(Parcel &parcel) const override; + + /** + * @brief Unmarshal object from a Parcel. + * + * @param parcel Indicates the parcel object. + * @return Returns the NotificationLiveViewContent object. + */ + static NotificationLiveViewContent *Unmarshalling(Parcel &parcel); + +protected: + /** + * @brief Read a NotificationLiveViewContent object from a Parcel. + * + * @param parcel Indicates the parcel object. + * @return Returns true if succeed; returns false otherwise. + */ + bool ReadFromParcel(Parcel &parcel) override; + +private: + bool PictureToJson(nlohmann::json &jsonObject) const; + LiveViewStatus liveViewStatus_ {}; + int32_t version_ {-1}; + std::shared_ptr extraInfo_ {}; + PictureMap pictureMap_ {}; +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_LIVE_VIEW_CONTENT_H diff --git a/interfaces/inner_api/notification_request.h b/interfaces/inner_api/notification_request.h index d080a7d91e29bac4403b4845183bb376ba0b8162..baa5a7b46dcdc38cda5ce3f35cf4788daa341146 100644 --- a/interfaces/inner_api/notification_request.h +++ b/interfaces/inner_api/notification_request.h @@ -28,9 +28,22 @@ #include "pixel_map.h" #include "want_agent.h" #include "want_params.h" +#include "notification_check_request.h" +#include "notification_bundle_option.h" namespace OHOS { namespace Notification { +struct NotificationKey { + int32_t id {}; + std::string label {}; +}; + +struct LiveViewFilter { + NotificationBundleOption bundle; + NotificationKey notificationKey; + std::vector extraInfoKeys; +}; + class NotificationRequest : public Parcelable, public NotificationJsonConvertionBase { public: enum class BadgeStyle { @@ -440,6 +453,20 @@ public: */ const std::shared_ptr GetBigIcon() const; + /** + * @brief Sets the overlay icon of this notification. + * + * @param overlayIcon Indicates the overlay icon of the notification. + */ + void SetOverlayIcon(const std::shared_ptr &overlayIcon); + + /** + * @brief Obtains the overlay icon of this notification. + * + * @return Returns the overlay icon of this notification. + */ + const std::shared_ptr GetOverlayIcon() const; + /** * @brief Sets the classification of this notification, which describes the purpose of this notification. * Notification classifications are used to filter and sort notifications. @@ -517,7 +544,8 @@ public: * NotificationContent::Type::PICTURE, * NotificationContent::Type::CONVERSATION, * NotificationContent::Type::MULTILINE, - * or NotificationContent::Type::MEDIA. + * NotificationContent::Type::MEDIA, + * or NotificationContent::Type::LIVE_VIEW */ NotificationContent::Type GetNotificationType() const; @@ -1181,6 +1209,7 @@ private: std::shared_ptr additionalParams_ {}; std::shared_ptr littleIcon_ {}; std::shared_ptr bigIcon_ {}; + std::shared_ptr overlayIcon_ {}; std::shared_ptr notificationContent_ {}; std::vector> actionButtons_ {}; diff --git a/interfaces/inner_api/notification_slot.h b/interfaces/inner_api/notification_slot.h index 354938d857cdbf762fda984813b256a04a0a9409..9471ab149d0c3353202067f4776a611e84a1dbea 100644 --- a/interfaces/inner_api/notification_slot.h +++ b/interfaces/inner_api/notification_slot.h @@ -278,6 +278,21 @@ public: */ bool GetEnable() const; + /** + * @brief Set whether the application slot is force control. + * @note If the slot is force control, the notification ability is not affected by the bundle. + * + * @param isForceControl Specifies whether is force control. + */ + void SetForceControl(bool isForceControl); + + /** + * @brief Obtains whether the application slot is force control. + * + * @return Returns true if the slot is force control; returns false otherwise. + */ + bool GetForceControl() const; + /** * @brief Dumps a string representation of the object. * @@ -352,6 +367,7 @@ private: Uri sound_; std::vector vibrationValues_ {}; bool enabled_ {true}; + bool isForceControl_ {false}; // no object in parcel static constexpr int32_t VALUE_NULL = -1; @@ -361,4 +377,4 @@ private: } // namespace Notification } // namespace OHOS -#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SLOT_H \ No newline at end of file +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SLOT_H