diff --git a/frameworks/ans/BUILD.gn b/frameworks/ans/BUILD.gn index 92524efdd91bf18d073632e82d23ec4e3ff55ade..7b7c84406e7d327d93125ca1a79c440cef2afe5e 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", diff --git a/frameworks/ans/src/notification_button_option.cpp b/frameworks/ans/src/notification_button_option.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e92dea1db129d64d5e3ad6994f2b5c0164b15c0c --- /dev/null +++ b/frameworks/ans/src/notification_button_option.cpp @@ -0,0 +1,102 @@ +/* + * 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_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 0000000000000000000000000000000000000000..486c276bf4a7620bce00b4d8c52eedaaca36cfd8 --- /dev/null +++ b/frameworks/ans/src/notification_capsule.cpp @@ -0,0 +1,165 @@ +/* + * 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_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 0000000000000000000000000000000000000000..889c7949c049555449f8462cf27a1b7d5d2675d7 --- /dev/null +++ b/frameworks/ans/src/notification_local_activity_button.cpp @@ -0,0 +1,216 @@ +/* + * 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_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 ""; +} + +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 0000000000000000000000000000000000000000..cd45c124b0b525b11eede9e336d6f97d03635bc3 --- /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 0000000000000000000000000000000000000000..d094e0ea9da9efa26a9a37416d681aacca5b45a1 --- /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