From 122aeae9c07b5dd34a760c0b32a57fe91bffa1d7 Mon Sep 17 00:00:00 2001 From: markYao Date: Thu, 19 Oct 2023 23:08:57 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=AE=9E=E6=97=B6=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: markYao --- .../ans/src/notification_button_option.cpp | 103 ++++++++ frameworks/ans/src/notification_capsule.cpp | 166 +++++++++++++ .../notification_local_activity_button.cpp | 221 ++++++++++++++++++ .../notification_local_activity_content.cpp | 213 +++++++++++++++++ ...notification_local_activity_subscriber.cpp | 106 +++++++++ 5 files changed, 809 insertions(+) create mode 100644 frameworks/ans/src/notification_button_option.cpp create mode 100644 frameworks/ans/src/notification_capsule.cpp create mode 100644 frameworks/ans/src/notification_local_activity_button.cpp create mode 100644 frameworks/ans/src/notification_local_activity_content.cpp create mode 100644 frameworks/ans/src/notification_local_activity_subscriber.cpp diff --git a/frameworks/ans/src/notification_button_option.cpp b/frameworks/ans/src/notification_button_option.cpp new file mode 100644 index 000000000..677e07082 --- /dev/null +++ b/frameworks/ans/src/notification_button_option.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2021 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_button_option.h" + +#include // for basic_string, operator+, basic_string<>... +#include // for shared_ptr, shared_ptr<>::element_type + + +#include "ans_image_util.h" +#include "ans_log_wrapper.h" +#include "nlohmann/json.hpp" // for json, basic_json<>::object_t, basic_json +#include "parcel.h" // for Parcel + +namespace OHOS { +namespace Notification { + +void NotificationButtonOption::SetButtonName(const std::string &buttonName) +{ + buttonName_ = buttonName; +} + +std::string NotificationButtonOption::GetButtonName() const +{ + return buttonName_; +} + +std::string NotificationButtonOption::Dump() +{ + return "NotificationButtonOption{ " + "buttonName = " + buttonName_ + + " }"; +} + +bool NotificationButtonOption::ToJson(nlohmann::json &jsonObject) const +{ + jsonObject["buttonName"] = buttonName_; + return true; +} + +NotificationButtonOption *NotificationButtonOption::FromJson(const nlohmann::json &jsonObject) +{ + if (jsonObject.is_null() or !jsonObject.is_object()) { + ANS_LOGE("Invalid JSON object"); + return nullptr; + } + + NotificationButtonOption *button = new (std::nothrow) NotificationButtonOption(); + if (button == nullptr) { + ANS_LOGE("Failed to create button option instance"); + return nullptr; + } + + const auto &jsonEnd = jsonObject.cend(); + if (jsonObject.find("buttonName") != jsonEnd && jsonObject.at("buttonName").is_string()) { + button->buttonName_ = jsonObject.at("buttonName").get(); + } + + return button; +} + +bool NotificationButtonOption::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteString(buttonName_)) { + ANS_LOGE("Failed to write title"); + return false; + } + + return true; +} + +bool NotificationButtonOption::ReadFromParcel(Parcel &parcel) +{ + buttonName_ = parcel.ReadString(); + + return true; +} + +NotificationButtonOption *NotificationButtonOption::Unmarshalling(Parcel &parcel) +{ + NotificationButtonOption *button = new (std::nothrow) NotificationButtonOption(); + + if (button && !button->ReadFromParcel(parcel)) { + delete button; + button = nullptr; + } + + return button; +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ans/src/notification_capsule.cpp b/frameworks/ans/src/notification_capsule.cpp new file mode 100644 index 000000000..f2cafc693 --- /dev/null +++ b/frameworks/ans/src/notification_capsule.cpp @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2021 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_capsule.h" + +#include // for basic_string, operator+, basic_string<>... +#include // for shared_ptr, shared_ptr<>::element_type + + +#include "ans_image_util.h" +#include "ans_log_wrapper.h" +#include "nlohmann/json.hpp" // for json, basic_json<>::object_t, basic_json +#include "parcel.h" // for Parcel +#include "pixel_map.h" // for PixelMap + +namespace OHOS { +namespace Notification { + +void NotificationCapsule::SetTitle(const std::string &title) +{ + title_ = title; +} + +std::string NotificationCapsule::GetTitle() const +{ + return title_; +} + +void NotificationCapsule::SetBackgroundColor(const std::string &color) +{ + backgroundColor_ = color; +} + +std::string NotificationCapsule::GetBackgroundColor() const +{ + return backgroundColor_; +} + +void NotificationCapsule::SetIcon(const std::shared_ptr &pixelMap) +{ + icon_ = pixelMap; +} + +const std::shared_ptr NotificationCapsule::GetIcon() const +{ + return icon_; +} + +std::string NotificationCapsule::Dump() +{ + return "Capsule{ " + "title = " + title_ + + ", backgroundColor = " + backgroundColor_ + + ", icon = " + (icon_ ? "not null" : "null") + + " }"; +} + +bool NotificationCapsule::ToJson(nlohmann::json &jsonObject) const +{ + jsonObject["title"] = title_; + jsonObject["backgroundColor"] = backgroundColor_; + jsonObject["icon"] = AnsImageUtil::PackImage(icon_); + + return true; +} + +NotificationCapsule *NotificationCapsule::FromJson(const nlohmann::json &jsonObject) +{ + if (jsonObject.is_null() or !jsonObject.is_object()) { + ANS_LOGE("Invalid JSON object"); + return nullptr; + } + + NotificationCapsule *capsule = new (std::nothrow) NotificationCapsule(); + if (capsule == nullptr) { + ANS_LOGE("Failed to create capsule instance"); + return nullptr; + } + + const auto &jsonEnd = jsonObject.cend(); + if (jsonObject.find("title") != jsonEnd && jsonObject.at("title").is_string()) { + capsule->title_ = jsonObject.at("title").get(); + } + + if (jsonObject.find("backgroundColor") != jsonEnd && jsonObject.at("backgroundColor").is_string()) { + capsule->backgroundColor_ = jsonObject.at("backgroundColor").get(); + } + + if (jsonObject.find("icon") != jsonEnd && jsonObject.at("icon").is_string()) { + auto pmStr = jsonObject.at("icon").get(); + capsule->icon_ = AnsImageUtil::UnPackImage(pmStr); + } + + return capsule; +} + +bool NotificationCapsule::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteString(title_)) { + ANS_LOGE("Failed to write title"); + return false; + } + + if (!parcel.WriteString(backgroundColor_)) { + ANS_LOGE("Failed to write backgroundColor"); + return false; + } + + bool valid = icon_ ? true : false; + if (!parcel.WriteBool(valid)) { + ANS_LOGE("Failed to write the flag which indicate whether icon pixelMap is null"); + return false; + } + + if (valid) { + if (!parcel.WriteParcelable(icon_.get())) { + ANS_LOGE("Failed to write icon"); + return false; + } + } + + return true; +} + +bool NotificationCapsule::ReadFromParcel(Parcel &parcel) +{ + title_ = parcel.ReadString(); + backgroundColor_ = parcel.ReadString(); + + bool valid = parcel.ReadBool(); + if (valid) { + icon_ = std::shared_ptr(parcel.ReadParcelable()); + if (!icon_) { + ANS_LOGE("Failed to read icon pixelMap"); + return false; + } + } + + return true; +} + +NotificationCapsule *NotificationCapsule::Unmarshalling(Parcel &parcel) +{ + NotificationCapsule *capsule = new (std::nothrow) NotificationCapsule(); + + if (capsule && !capsule->ReadFromParcel(parcel)) { + delete capsule; + capsule = nullptr; + } + + return capsule; +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ans/src/notification_local_activity_button.cpp b/frameworks/ans/src/notification_local_activity_button.cpp new file mode 100644 index 000000000..6894d4300 --- /dev/null +++ b/frameworks/ans/src/notification_local_activity_button.cpp @@ -0,0 +1,221 @@ +/* + * Copyright (c) 2021 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_local_activity_button.h" + +#include +#include // for basic_string, operator+, basic_string<>... +#include // for shared_ptr, shared_ptr<>::element_type +#include + + +#include "ans_image_util.h" +#include "ans_log_wrapper.h" +#include "nlohmann/json.hpp" // for json, basic_json<>::object_t, basic_json +#include "notification_button_option.h" +#include "notification_json_convert.h" +#include "parcel.h" // for Parcel +#include "pixel_map.h" // for PixelMap + +namespace OHOS { +namespace Notification { + +std::vector> NotificationLocalActivityButton::GetAllButtonOption() const +{ + return buttonOptions_; +} + +void NotificationLocalActivityButton::addSingleButtonOption(std::shared_ptr &buttonOption) +{ + if (buttonOptions_.size() >= 3) { + ANS_LOGW("already added 3 buttonOption"); + return; + } + + buttonOptions_.emplace_back(buttonOption); +} + +std::vector> NotificationLocalActivityButton::GetAllButtonIcon() const +{ + return buttonIcons_; +} + +void NotificationLocalActivityButton::addSingleButtonIcon(std::shared_ptr &icon) +{ + if (buttonIcons_.size() >= 3) { + ANS_LOGW("already added 3 buttonIcon"); + return; + } + + buttonIcons_.emplace_back(icon); +} + +std::string NotificationLocalActivityButton::Dump() +{ + // return "LocalActivityButton{ " + // "buttonOptions = " + title_ + + // ", buttonIcons = " + backgroundColor_ + + // ", icon = " + (icon_ ? "not null" : "null") + + // " }"; + return ""; +} + +bool NotificationLocalActivityButton::ToJson(nlohmann::json &jsonObject) const +{ + nlohmann::json buttonsArr = nlohmann::json::array(); + for (auto &btn : buttonOptions_) { + if (!btn) { + continue; + } + + nlohmann::json btnObj; + if (!NotificationJsonConverter::ConvertToJson(btn.get(), btnObj)) { + ANS_LOGE("Cannot convert actionButton to JSON"); + return false; + } + + buttonsArr.emplace_back(btnObj); + } + jsonObject["buttonOptions"] = buttonsArr; + + nlohmann::json iconsArr = nlohmann::json::array(); + for (auto &btn : buttonIcons_) { + if (!btn) { + continue; + } + nlohmann::json btnObj = AnsImageUtil::PackImage(btn); + iconsArr.emplace_back(btnObj); + } + jsonObject["buttonIcons"] = iconsArr; + + return true; +} + +NotificationLocalActivityButton *NotificationLocalActivityButton::FromJson(const nlohmann::json &jsonObject) +{ + if (jsonObject.is_null() or !jsonObject.is_object()) { + ANS_LOGE("Invalid JSON object"); + return nullptr; + } + + NotificationLocalActivityButton *button = new (std::nothrow) NotificationLocalActivityButton(); + if (button == nullptr) { + ANS_LOGE("Failed to create capsule instance"); + return nullptr; + } + + const auto &jsonEnd = jsonObject.cend(); + + if (jsonObject.find("buttonOptions") != jsonEnd) { + auto optionArr = jsonObject.at("buttonOptions"); + for (auto &optObj : optionArr) { + auto pOpt = NotificationJsonConverter::ConvertFromJson(optObj); + if (pOpt == nullptr) { + ANS_LOGE("Failed to parse button option"); + return nullptr; + } + button->buttonOptions_.emplace_back(pOpt); + } + } + + if (jsonObject.find("buttonIcons") != jsonEnd) { + auto iconArr = jsonObject.at("buttonIcons"); + for (auto &iconObj : iconArr) { + auto pIcon = AnsImageUtil::UnPackImage(iconObj.get()); + + if (pIcon == nullptr) { + ANS_LOGE("Failed to parse button icon"); + return nullptr; + } + button->buttonIcons_.emplace_back(pIcon); + } + } + + return button; +} + +bool NotificationLocalActivityButton::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteUint64(buttonOptions_.size())) { + ANS_LOGE("Failed to write the size of buttonOptions"); + return false; + } + + for (auto it = buttonOptions_.begin(); it != buttonOptions_.end(); ++it) { + if (!parcel.WriteParcelable(it->get())) { + ANS_LOGE("Failed to write buttonOptions"); + return false; + } + } + + if (!parcel.WriteUint64(buttonIcons_.size())) { + ANS_LOGE("Failed to write the size of buttonIcons"); + return false; + } + + for (auto it = buttonIcons_.begin(); it != buttonIcons_.end(); ++it) { + if (!parcel.WriteParcelable(it->get())) { + ANS_LOGE("Failed to write buttonIcons"); + return false; + } + } + + return true; +} + +bool NotificationLocalActivityButton::ReadFromParcel(Parcel &parcel) +{ + auto vsize = parcel.ReadUint64(); + vsize = (vsize < 3) ? vsize : 3; + for (uint64_t it = 0; it < vsize; ++it) { + auto member = parcel.ReadParcelable(); + if (member == nullptr) { + buttonOptions_.clear(); + ANS_LOGE("Failed to read actionButton"); + return false; + } + + buttonOptions_.emplace_back(member); + } + + vsize = parcel.ReadUint64(); + vsize = (vsize < 3) ? vsize : 3; + for (uint64_t it = 0; it < vsize; ++it) { + auto member = parcel.ReadParcelable(); + if (member == nullptr) { + buttonIcons_.clear(); + ANS_LOGE("Failed to read actionButton"); + return false; + } + + buttonIcons_.emplace_back(member); + } + + return true; +} + +NotificationLocalActivityButton *NotificationLocalActivityButton::Unmarshalling(Parcel &parcel) +{ + NotificationLocalActivityButton *button = new (std::nothrow) NotificationLocalActivityButton(); + + if (button && !button->ReadFromParcel(parcel)) { + delete button; + button = nullptr; + } + + return button; +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ans/src/notification_local_activity_content.cpp b/frameworks/ans/src/notification_local_activity_content.cpp new file mode 100644 index 000000000..cd45c124b --- /dev/null +++ b/frameworks/ans/src/notification_local_activity_content.cpp @@ -0,0 +1,213 @@ +/* + * Copyright (c) 2021-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_local_activity_content.h" + +#include +#include // for basic_string, operator+ +#include // for min + +#include "ans_log_wrapper.h" +#include "nlohmann/json.hpp" // for json, basic_json<>::obje... +#include "notification_action_button.h" +#include "notification_basic_content.h" // for NotificationBasicContent +#include "notification_capsule.h" +#include "notification_json_convert.h" +#include "notification_progress.h" +#include "notification_local_activity_button.h" +#include "parcel.h" // for Parcel + +namespace OHOS { +namespace Notification { +std::string NotificationLocalActivityContent::Dump() +{ + return "NotificationLocalActivityContent{ " + NotificationBasicContent::Dump() + + ", type = " + std::to_string(type_) + + ", capsule = " + capsule_.Dump() + + ", button = " + button_.Dump() + + ", progress = " + progress_.Dump() + + " }"; +} + +bool NotificationLocalActivityContent::ToJson(nlohmann::json &jsonObject) const +{ + if (!NotificationBasicContent::ToJson(jsonObject)) { + ANS_LOGE("Cannot convert basicContent to JSON"); + return false; + } + + nlohmann::json capsuleObj; + if (!NotificationJsonConverter::ConvertToJson(&capsule_, capsuleObj)) { + ANS_LOGE("Cannot convert capsule to JSON"); + return false; + } + + nlohmann::json buttonObj; + if (!NotificationJsonConverter::ConvertToJson(&button_, buttonObj)) { + ANS_LOGE("Cannot convert button to JSON"); + return false; + } + + nlohmann::json progressObj; + if (!NotificationJsonConverter::ConvertToJson(&progress_, progressObj)) { + ANS_LOGE("Cannot convert progress to JSON"); + return false; + } + + jsonObject["type"] = type_; + jsonObject["capsule"] = capsuleObj; + jsonObject["button"] = buttonObj; + jsonObject["progress"] = progressObj; + + return true; +} + +NotificationLocalActivityContent *NotificationLocalActivityContent::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) NotificationLocalActivityContent(); + if (pContent == nullptr) { + ANS_LOGE("Failed to create localActivityContent instance"); + return nullptr; + } + + pContent->ReadFromJson(jsonObject); + + const auto &jsonEnd = jsonObject.cend(); + if (jsonObject.find("type") != jsonEnd ) { + pContent->type_ = jsonObject.at("type").get(); + } + + if (jsonObject.find("capsule") != jsonEnd ) { + auto capsuleObj = jsonObject.at("capsule"); + auto pCapsule = NotificationJsonConverter::ConvertFromJson(capsuleObj); + if (pCapsule != nullptr) { + pContent->capsule_ = *pCapsule; + delete pCapsule; + pCapsule = nullptr; + } + } + + if (jsonObject.find("button") != jsonEnd ) { + auto buttonObj = jsonObject.at("button"); + auto pButton = NotificationJsonConverter::ConvertFromJson(buttonObj); + if (pButton != nullptr) { + pContent->button_ = *pButton; + delete pButton; + pButton = nullptr; + } + } + + if (jsonObject.find("progress") != jsonEnd ) { + auto progressObj = jsonObject.at("progress"); + auto pProgress = NotificationJsonConverter::ConvertFromJson(progressObj); + if (pProgress != nullptr) { + pContent->progress_ = *pProgress; + delete pProgress; + pProgress = nullptr; + } + } + + return pContent; +} + +bool NotificationLocalActivityContent::Marshalling(Parcel &parcel) const +{ + if (!NotificationBasicContent::Marshalling(parcel)) { + ANS_LOGE("Failed to write basic"); + return false; + } + + if (!parcel.WriteInt32(type_)) { + ANS_LOGE("Write type fail."); + return false; + } + + if (!parcel.WriteParcelable(&capsule_)) { + ANS_LOGE("Failed to write capsule"); + return false; + } + + if (!parcel.WriteParcelable(&button_)) { + ANS_LOGE("Failed to write button"); + return false; + } + + if (!parcel.WriteParcelable(&progress_)) { + ANS_LOGE("Failed to write progress"); + return false; + } + + return true; +} + +NotificationLocalActivityContent *NotificationLocalActivityContent::Unmarshalling(Parcel &parcel) +{ + auto pContent = new (std::nothrow) NotificationLocalActivityContent(); + if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) { + delete pContent; + pContent = nullptr; + } + + return pContent; +} + +bool NotificationLocalActivityContent::ReadFromParcel(Parcel &parcel) +{ + if (!NotificationBasicContent::ReadFromParcel(parcel)) { + ANS_LOGE("Failed to read basic"); + return false; + } + + if (!parcel.ReadInt32(type_)) { + ANS_LOGE("Read type failed."); + return false; + } + + auto pCapsule = parcel.ReadParcelable(); + if (pCapsule == nullptr) { + ANS_LOGE("Failed to read capsule"); + return false; + } + capsule_ = *pCapsule; + delete pCapsule; + pCapsule = nullptr; + + auto pButton = parcel.ReadParcelable(); + if (pButton == nullptr) { + ANS_LOGE("Failed to read button"); + return false; + } + button_ = *pButton; + delete pButton; + pButton = nullptr; + + auto pProgress = parcel.ReadParcelable(); + if (pProgress == nullptr) { + ANS_LOGE("Failed to read capsule"); + return false; + } + progress_ = *pProgress; + delete pProgress; + pProgress = nullptr; + + return true; +} +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/ans/src/notification_local_activity_subscriber.cpp b/frameworks/ans/src/notification_local_activity_subscriber.cpp new file mode 100644 index 000000000..d094e0ea9 --- /dev/null +++ b/frameworks/ans/src/notification_local_activity_subscriber.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "notification_local_activity_subscriber.h" + +#include "hitrace_meter.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace Notification { +NotificationLocalActivitySubscriber::NotificationLocalActivitySubscriber() +{ + impl_ = new (std::nothrow) SubscriberLocalActivityImpl(*this); +}; + +NotificationLocalActivitySubscriber::~NotificationLocalActivitySubscriber() +{} + +const sptr NotificationLocalActivitySubscriber::GetImpl() const +{ + return impl_; +} + +NotificationLocalActivitySubscriber::SubscriberLocalActivityImpl::SubscriberLocalActivityImpl(NotificationLocalActivitySubscriber &subscriber) : subscriber_(subscriber) +{ + recipient_ = new (std::nothrow) DeathRecipient(*this); +}; + +void NotificationLocalActivitySubscriber::SubscriberLocalActivityImpl::OnConnected() +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + if (GetAnsManagerProxy()) { + proxy_->AsObject()->AddDeathRecipient(recipient_); + ANS_LOGD("%s, Add death recipient.", __func__); + } + subscriber_.OnConnected(); +} + +void NotificationLocalActivitySubscriber::SubscriberLocalActivityImpl::OnDisconnected() +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + if (GetAnsManagerProxy()) { + proxy_->AsObject()->RemoveDeathRecipient(recipient_); + ANS_LOGD("%s, Remove death recipient.", __func__); + } + subscriber_.OnDisconnected(); +} + +void NotificationLocalActivitySubscriber::SubscriberLocalActivityImpl::OnResponse(int32_t notificationId, const sptr &buttonOption) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + subscriber_.OnResponse(notificationId, buttonOption); +} + +bool NotificationLocalActivitySubscriber::SubscriberLocalActivityImpl::GetAnsManagerProxy() +{ + if (proxy_ == nullptr) { + std::lock_guard lock(mutex_); + if (proxy_ == nullptr) { + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + return false; + } + + sptr remoteObject = + systemAbilityManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID); + if (!remoteObject) { + return false; + } + + proxy_ = iface_cast(remoteObject); + if ((proxy_ == nullptr) || (proxy_->AsObject() == nullptr)) { + return false; + } + } + } + + return true; +} + +NotificationLocalActivitySubscriber::SubscriberLocalActivityImpl::DeathRecipient::DeathRecipient(SubscriberLocalActivityImpl &subscriberImpl) + : subscriberImpl_(subscriberImpl) {}; + +NotificationLocalActivitySubscriber::SubscriberLocalActivityImpl::DeathRecipient::~DeathRecipient() {}; + +void NotificationLocalActivitySubscriber::SubscriberLocalActivityImpl::DeathRecipient::OnRemoteDied(const wptr &object) +{ + subscriberImpl_.proxy_ = nullptr; + subscriberImpl_.subscriber_.OnDied(); +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file -- Gitee From 6f13b0f0ddec3048f4e61530ab18f9cc1be2eb37 Mon Sep 17 00:00:00 2001 From: markYao Date: Sat, 21 Oct 2023 16:47:43 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9build.gn=20Signed-off-by:?= =?UTF-8?q?=20markYao=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frameworks/ans/BUILD.gn | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/frameworks/ans/BUILD.gn b/frameworks/ans/BUILD.gn index 92524efdd..7b7c84406 100644 --- a/frameworks/ans/BUILD.gn +++ b/frameworks/ans/BUILD.gn @@ -63,6 +63,8 @@ ohos_shared_library("ans_innerkits") { "${frameworks_module_ans_path}/src/notification_action_button.cpp", "${frameworks_module_ans_path}/src/notification_basic_content.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", "${frameworks_module_ans_path}/src/notification_constant.cpp", "${frameworks_module_ans_path}/src/notification_content.cpp", "${frameworks_module_ans_path}/src/notification_conversational_content.cpp", @@ -71,6 +73,9 @@ ohos_shared_library("ans_innerkits") { "${frameworks_module_ans_path}/src/notification_do_not_disturb_date.cpp", "${frameworks_module_ans_path}/src/notification_flags.cpp", "${frameworks_module_ans_path}/src/notification_helper.cpp", + "${frameworks_module_ans_path}/src/notification_local_activity_button.cpp", + "${frameworks_module_ans_path}/src/notification_local_activity_content.cpp", + "${frameworks_module_ans_path}/src/notification_local_activity_subscriber.cpp", "${frameworks_module_ans_path}/src/notification_long_text_content.cpp", "${frameworks_module_ans_path}/src/notification_media_content.cpp", "${frameworks_module_ans_path}/src/notification_multiline_content.cpp", -- Gitee From 46327d5c3c04f9850b645e2185fa519d9aae0af5 Mon Sep 17 00:00:00 2001 From: markYao Date: Sat, 21 Oct 2023 21:55:43 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frameworks/ans/src/notification_button_option.cpp | 5 ++--- frameworks/ans/src/notification_capsule.cpp | 11 +++++------ .../ans/src/notification_local_activity_button.cpp | 7 +------ 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/frameworks/ans/src/notification_button_option.cpp b/frameworks/ans/src/notification_button_option.cpp index 677e07082..e92dea1db 100644 --- a/frameworks/ans/src/notification_button_option.cpp +++ b/frameworks/ans/src/notification_button_option.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 @@ -26,7 +26,6 @@ namespace OHOS { namespace Notification { - void NotificationButtonOption::SetButtonName(const std::string &buttonName) { buttonName_ = buttonName; @@ -46,7 +45,7 @@ std::string NotificationButtonOption::Dump() bool NotificationButtonOption::ToJson(nlohmann::json &jsonObject) const { - jsonObject["buttonName"] = buttonName_; + jsonObject["buttonName"] = buttonName_; return true; } diff --git a/frameworks/ans/src/notification_capsule.cpp b/frameworks/ans/src/notification_capsule.cpp index f2cafc693..486c276bf 100644 --- a/frameworks/ans/src/notification_capsule.cpp +++ b/frameworks/ans/src/notification_capsule.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 @@ -27,7 +27,6 @@ namespace OHOS { namespace Notification { - void NotificationCapsule::SetTitle(const std::string &title) { title_ = title; @@ -69,9 +68,9 @@ std::string NotificationCapsule::Dump() bool NotificationCapsule::ToJson(nlohmann::json &jsonObject) const { - jsonObject["title"] = title_; - jsonObject["backgroundColor"] = backgroundColor_; - jsonObject["icon"] = AnsImageUtil::PackImage(icon_); + jsonObject["title"] = title_; + jsonObject["backgroundColor"] = backgroundColor_; + jsonObject["icon"] = AnsImageUtil::PackImage(icon_); return true; } @@ -99,7 +98,7 @@ NotificationCapsule *NotificationCapsule::FromJson(const nlohmann::json &jsonObj } if (jsonObject.find("icon") != jsonEnd && jsonObject.at("icon").is_string()) { - auto pmStr = jsonObject.at("icon").get(); + auto pmStr = jsonObject.at("icon").get(); capsule->icon_ = AnsImageUtil::UnPackImage(pmStr); } diff --git a/frameworks/ans/src/notification_local_activity_button.cpp b/frameworks/ans/src/notification_local_activity_button.cpp index 6894d4300..889c7949c 100644 --- a/frameworks/ans/src/notification_local_activity_button.cpp +++ b/frameworks/ans/src/notification_local_activity_button.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 @@ -64,11 +64,6 @@ void NotificationLocalActivityButton::addSingleButtonIcon(std::shared_ptr