From e70c9b365d8e3ffaf286160d81685017081c3127 Mon Sep 17 00:00:00 2001 From: markYao Date: Sun, 22 Oct 2023 18:20:47 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=9E=E5=86=B5=E7=AA=97=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dongqingran --- .../ans/src/notification_button_option.cpp | 103 ++++++++ frameworks/ans/src/notification_capsule.cpp | 166 +++++++++++++ .../notification_local_live_view_button.cpp | 181 ++++++++++++++ .../notification_local_live_view_content.cpp | 234 ++++++++++++++++++ ...otification_local_live_view_subscriber.cpp | 109 ++++++++ frameworks/ans/src/notification_progress.cpp | 148 +++++++++++ .../ans_subscriber_local_live_view_proxy.cpp | 119 +++++++++ .../ans_subscriber_local_live_view_stub.cpp | 134 ++++++++++ 8 files changed, 1194 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_live_view_button.cpp create mode 100644 frameworks/ans/src/notification_local_live_view_content.cpp create mode 100644 frameworks/ans/src/notification_local_live_view_subscriber.cpp create mode 100644 frameworks/ans/src/notification_progress.cpp create mode 100644 frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp create mode 100644 frameworks/core/src/ans_subscriber_local_live_view_stub.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_live_view_button.cpp b/frameworks/ans/src/notification_local_live_view_button.cpp new file mode 100644 index 000000000..4b098fd1a --- /dev/null +++ b/frameworks/ans/src/notification_local_live_view_button.cpp @@ -0,0 +1,181 @@ +/* + * 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_live_view_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 { +const uint32_t BUTTON_MAX_SIZE = 3; + +std::vector NotificationLocalLiveViewButton::GetAllButtonNames() const +{ + return buttonNames_; +} + +void NotificationLocalLiveViewButton::addSingleButtonName(const std::string &buttonName) +{ + if (buttonNames_.size() >= BUTTON_MAX_SIZE) { + ANS_LOGW("already added 3 buttonOption"); + return; + } + + buttonNames_.emplace_back(buttonName); +} + +std::vector> NotificationLocalLiveViewButton::GetAllButtonIcon() const +{ + return buttonIcons_; +} + +void NotificationLocalLiveViewButton::addSingleButtonIcon(std::shared_ptr &icon) +{ + if (buttonIcons_.size() >= BUTTON_MAX_SIZE) { + ANS_LOGW("already added 3 buttonIcon"); + return; + } + + buttonIcons_.emplace_back(icon); +} + +std::string NotificationLocalLiveViewButton::Dump() +{ + return ""; +} + +bool NotificationLocalLiveViewButton::ToJson(nlohmann::json &jsonObject) const +{ + nlohmann::json buttonsArr = nlohmann::json::array(); + + jsonObject["buttonNames"] = nlohmann::json(buttonNames_); + + 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; +} + +NotificationLocalLiveViewButton *NotificationLocalLiveViewButton::FromJson(const nlohmann::json &jsonObject) +{ + if (jsonObject.is_null() or !jsonObject.is_object()) { + ANS_LOGE("Invalid JSON object"); + return nullptr; + } + + NotificationLocalLiveViewButton *button = new (std::nothrow) NotificationLocalLiveViewButton(); + if (button == nullptr) { + ANS_LOGE("Failed to create capsule instance"); + return nullptr; + } + + const auto &jsonEnd = jsonObject.cend(); + + if (jsonObject.find("buttonNames") != jsonEnd && jsonObject.at("buttonNames").is_array()) { + button->buttonNames_ = jsonObject.at("buttonNames").get>(); + } + + 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 NotificationLocalLiveViewButton::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteStringVector(buttonNames_)) { + ANS_LOGE("Failed to write buttonNames"); + 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 NotificationLocalLiveViewButton::ReadFromParcel(Parcel &parcel) +{ + if (!parcel.ReadStringVector(&buttonNames_)) { + ANS_LOGE("Failed to read button names"); + return false; + } + + auto vsize = parcel.ReadUint64(); + vsize = (vsize < BUTTON_MAX_SIZE) ? vsize : BUTTON_MAX_SIZE; + for (uint64_t it = 0; it < vsize; ++it) { + auto member = std::shared_ptr(parcel.ReadParcelable()); + if (member == nullptr) { + buttonIcons_.clear(); + ANS_LOGE("Failed to read LocalLiveViewButton %llu, %llu", it, vsize); + return false; + } + + buttonIcons_.emplace_back(member); + } + + return true; +} + +NotificationLocalLiveViewButton *NotificationLocalLiveViewButton::Unmarshalling(Parcel &parcel) +{ + NotificationLocalLiveViewButton *button = new (std::nothrow) NotificationLocalLiveViewButton(); + + if (button && !button->ReadFromParcel(parcel)) { + delete button; + button = nullptr; + } + + return button; +} +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/ans/src/notification_local_live_view_content.cpp b/frameworks/ans/src/notification_local_live_view_content.cpp new file mode 100644 index 000000000..6a64e31c9 --- /dev/null +++ b/frameworks/ans/src/notification_local_live_view_content.cpp @@ -0,0 +1,234 @@ +/* + * 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_live_view_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_live_view_button.h" +#include "parcel.h" // for Parcel + +namespace OHOS { +namespace Notification { + +void NotificationLocalLiveViewContent::SetType(int32_t type) +{ + type_ = type; +} + +void NotificationLocalLiveViewContent::SetCapsule(NotificationCapsule capsule) +{ + capsule_ = capsule; +} + +void NotificationLocalLiveViewContent::SetButton(NotificationLocalLiveViewButton button) +{ + button_ = button; +} + +void NotificationLocalLiveViewContent::SetProgress(NotificationProgress progress) +{ + progress_ = progress; +} + +std::string NotificationLocalLiveViewContent::Dump() +{ + return "NotificationLocalLiveViewContent{ " + NotificationBasicContent::Dump() + + ", type = " + std::to_string(type_) + + ", capsule = " + capsule_.Dump() + + ", button = " + button_.Dump() + + ", progress = " + progress_.Dump() + + " }"; +} + +bool NotificationLocalLiveViewContent::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; +} + +NotificationLocalLiveViewContent *NotificationLocalLiveViewContent::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) NotificationLocalLiveViewContent(); + if (pContent == nullptr) { + ANS_LOGE("Failed to create localLiveViewContent 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 NotificationLocalLiveViewContent::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; +} + +NotificationLocalLiveViewContent *NotificationLocalLiveViewContent::Unmarshalling(Parcel &parcel) +{ + auto pContent = new (std::nothrow) NotificationLocalLiveViewContent(); + if ((pContent != nullptr) && !pContent->ReadFromParcel(parcel)) { + delete pContent; + pContent = nullptr; + } + + return pContent; +} + +bool NotificationLocalLiveViewContent::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_live_view_subscriber.cpp b/frameworks/ans/src/notification_local_live_view_subscriber.cpp new file mode 100644 index 000000000..dacbf3761 --- /dev/null +++ b/frameworks/ans/src/notification_local_live_view_subscriber.cpp @@ -0,0 +1,109 @@ +/* + * 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_live_view_subscriber.h" + +#include "hitrace_meter.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace Notification { +NotificationLocalLiveViewSubscriber::NotificationLocalLiveViewSubscriber() +{ + impl_ = new (std::nothrow) SubscriberLocalLiveViewImpl(*this); +}; + +NotificationLocalLiveViewSubscriber::~NotificationLocalLiveViewSubscriber() +{} + +const sptr NotificationLocalLiveViewSubscriber::GetImpl() const +{ + return impl_; +} + +NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::SubscriberLocalLiveViewImpl( + NotificationLocalLiveViewSubscriber &subscriber) : subscriber_(subscriber) +{ + recipient_ = new (std::nothrow) DeathRecipient(*this); +}; + +void NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::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 NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::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 NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::OnResponse(int32_t notificationId, + sptr buttonOption) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + subscriber_.OnResponse(notificationId, buttonOption); +} + +bool NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::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; +} + +NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::DeathRecipient::DeathRecipient( + SubscriberLocalLiveViewImpl &subscriberImpl) : subscriberImpl_(subscriberImpl) {}; + +NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::DeathRecipient::~DeathRecipient() {}; + +void NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl::DeathRecipient::OnRemoteDied( + const wptr &object) +{ + subscriberImpl_.proxy_ = nullptr; + subscriberImpl_.subscriber_.OnDied(); +} +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/ans/src/notification_progress.cpp b/frameworks/ans/src/notification_progress.cpp new file mode 100644 index 000000000..3a2e0cba6 --- /dev/null +++ b/frameworks/ans/src/notification_progress.cpp @@ -0,0 +1,148 @@ +/* + * 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_progress.h" + +#include +#include // for basic_string, operator+, basic_string<>... +#include // for shared_ptr, shared_ptr<>::element_type + + +#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 { + +int32_t NotificationProgress::GetMaxValue() const +{ + return maxValue_; +} + +void NotificationProgress::SetMaxValue(int32_t maxValue) +{ + maxValue_ = maxValue; +} + +int32_t NotificationProgress::GetCurrentValue() const +{ + return currentValue_; +} + +void NotificationProgress::SetCurrentValue(int32_t curValue) +{ + currentValue_ = curValue; +} + +int32_t NotificationProgress::GetIsPercentage() const +{ + return isPercentage_; +} + +void NotificationProgress::SetIsPercentage(int32_t isPercentage) +{ + isPercentage_ = isPercentage; +} + + +std::string NotificationProgress::Dump() +{ + return "Progress{ " + "maxValue = " + std::to_string(maxValue_) + + ", currentValue = " + std::to_string(currentValue_) + + ", isPercentage = " + std::to_string(isPercentage_) + + " }"; +} + +bool NotificationProgress::ToJson(nlohmann::json &jsonObject) const +{ + jsonObject["maxValue"] = maxValue_; + jsonObject["currentValue"] = currentValue_; + jsonObject["isPercentage"] = isPercentage_; + + return true; +} + +NotificationProgress *NotificationProgress::FromJson(const nlohmann::json &jsonObject) +{ + if (jsonObject.is_null() or !jsonObject.is_object()) { + ANS_LOGE("Invalid JSON object"); + return nullptr; + } + + NotificationProgress *progress = new (std::nothrow) NotificationProgress(); + if (progress == nullptr) { + ANS_LOGE("Failed to create capsule instance"); + return nullptr; + } + + const auto &jsonEnd = jsonObject.cend(); + if (jsonObject.find("maxValue") != jsonEnd && jsonObject.at("maxValue").is_number_integer()) { + progress->maxValue_ = jsonObject.at("maxValue").get(); + } + + if (jsonObject.find("currentValue") != jsonEnd && jsonObject.at("currentValue").is_number_integer()) { + progress->currentValue_ = jsonObject.at("currentValue").get(); + } + + if (jsonObject.find("isPercentage") != jsonEnd && jsonObject.at("isPercentage").is_number_integer()) { + progress->isPercentage_ = jsonObject.at("isPercentage_").get(); + } + + return progress; +} + +bool NotificationProgress::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteInt32(maxValue_)) { + ANS_LOGE("Failed to write maxValue"); + return false; + } + + if (!parcel.WriteInt32(currentValue_)) { + ANS_LOGE("Failed to write currentValue"); + return false; + } + + if (!parcel.WriteInt32(isPercentage_)) { + ANS_LOGE("Failed to write isPercentage"); + return false; + } + return true; +} + +bool NotificationProgress::ReadFromParcel(Parcel &parcel) +{ + maxValue_ = parcel.ReadInt32(); + currentValue_ = parcel.ReadInt32(); + isPercentage_ = parcel.ReadInt32(); + + return true; +} + +NotificationProgress *NotificationProgress::Unmarshalling(Parcel &parcel) +{ + NotificationProgress *progress = new (std::nothrow) NotificationProgress(); + + if (progress && !progress->ReadFromParcel(parcel)) { + delete progress; + progress = nullptr; + } + + return progress; +} +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp b/frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp new file mode 100644 index 000000000..28a092a4a --- /dev/null +++ b/frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp @@ -0,0 +1,119 @@ +/* + * 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 "ans_subscriber_local_live_view_proxy.h" + +#include "ans_inner_errors.h" +#include "ans_log_wrapper.h" +#include "message_option.h" +#include "message_parcel.h" + +namespace OHOS { +namespace Notification { +AnsSubscriberLocalLiveViewProxy::AnsSubscriberLocalLiveViewProxy( + const sptr &impl) : IRemoteProxy(impl) +{} + +AnsSubscriberLocalLiveViewProxy::~AnsSubscriberLocalLiveViewProxy() +{} + +ErrCode AnsSubscriberLocalLiveViewProxy::InnerTransact( + NotificationInterfaceCode code, MessageOption &flags, MessageParcel &data, MessageParcel &reply) +{ + auto remote = Remote(); + if (remote == nullptr) { + ANS_LOGE("[InnerTransact] fail: get Remote fail code %{public}u", code); + return ERR_DEAD_OBJECT; + } + + int32_t err = remote->SendRequest(static_cast(code), data, reply, flags); + switch (err) { + case NO_ERROR: { + return ERR_OK; + } + case DEAD_OBJECT: { + ANS_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code); + return ERR_DEAD_OBJECT; + } + default: { + ANS_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code); + return ERR_ANS_TRANSACT_FAILED; + } + } +} + +void AnsSubscriberLocalLiveViewProxy::OnConnected() +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsSubscriberLocalLiveViewProxy::GetDescriptor())) { + ANS_LOGE("[OnConnected] fail: write interface token failed."); + return; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_ASYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::ON_CONNECTED, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[OnConnected] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); + return; + } +} + +void AnsSubscriberLocalLiveViewProxy::OnDisconnected() +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsSubscriberLocalLiveViewProxy::GetDescriptor())) { + ANS_LOGE("[OnDisconnected] fail: write interface token failed."); + return; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_ASYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::ON_DISCONNECTED, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[OnDisconnected] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); + return; + } +} + + +void AnsSubscriberLocalLiveViewProxy::OnResponse(int32_t notificationId, sptr buttonOption) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsSubscriberLocalLiveViewProxy::GetDescriptor())) { + ANS_LOGE("[OnResponse] fail: write interface token failed."); + return; + } + + if (!data.WriteInt32(notificationId)) { + ANS_LOGE("[OnResponse] fail: write notificationId failed"); + return; + } + + if (!data.WriteParcelable(buttonOption)) { + ANS_LOGE("[OnResponse] fail: write buttonName failed"); + return; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_ASYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::ON_RESPONSE, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[OnResponse] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); + return; + } +} +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/core/src/ans_subscriber_local_live_view_stub.cpp b/frameworks/core/src/ans_subscriber_local_live_view_stub.cpp new file mode 100644 index 000000000..ac26eff5f --- /dev/null +++ b/frameworks/core/src/ans_subscriber_local_live_view_stub.cpp @@ -0,0 +1,134 @@ +/* + * 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 "ans_subscriber_local_live_view_stub.h" + +#include "ans_const_define.h" +#include "ans_inner_errors.h" +#include "ans_log_wrapper.h" +#include "message_option.h" +#include "message_parcel.h" +#include "notification_bundle_option.h" +#include "notification_button_option.h" +#include "parcel.h" +#include "refbase.h" +#include + +namespace OHOS { +namespace Notification { +AnsSubscriberLocalLiveViewStub::AnsSubscriberLocalLiveViewStub() +{ + interfaces_.emplace(NotificationInterfaceCode::ON_CONNECTED, + std::bind(&AnsSubscriberLocalLiveViewStub::HandleOnConnected, + this, std::placeholders::_1, std::placeholders::_2)); + interfaces_.emplace(NotificationInterfaceCode::ON_DISCONNECTED, + std::bind(&AnsSubscriberLocalLiveViewStub::HandleOnDisconnected, + this, std::placeholders::_1, std::placeholders::_2)); + interfaces_.emplace(NotificationInterfaceCode::ON_RESPONSE, + std::bind(&AnsSubscriberLocalLiveViewStub::HandleOnResponse, + this, std::placeholders::_1, std::placeholders::_2)); +} + +AnsSubscriberLocalLiveViewStub::~AnsSubscriberLocalLiveViewStub() +{} + +int32_t AnsSubscriberLocalLiveViewStub::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &flags) +{ + std::u16string descriptor = AnsSubscriberLocalLiveViewStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + ANS_LOGW("[OnRemoteRequest] fail: invalid interface token!"); + return OBJECT_NULL; + } + + auto it = interfaces_.find(static_cast(code)); + if (it == interfaces_.end()) { + ANS_LOGW("[OnRemoteRequest] fail: unknown code!"); + return IPCObjectStub::OnRemoteRequest(code, data, reply, flags); + } + + auto fun = it->second; + if (fun == nullptr) { + ANS_LOGW("[OnRemoteRequest] fail: not find function!"); + return IPCObjectStub::OnRemoteRequest(code, data, reply, flags); + } + + fun(data, reply); + return NO_ERROR; +} + +ErrCode AnsSubscriberLocalLiveViewStub::HandleOnConnected(MessageParcel &data, MessageParcel &reply) +{ + OnConnected(); + return ERR_OK; +} + +ErrCode AnsSubscriberLocalLiveViewStub::HandleOnDisconnected(MessageParcel &data, MessageParcel &reply) +{ + OnDisconnected(); + return ERR_OK; +} + +template +bool AnsSubscriberLocalLiveViewStub::ReadParcelableVector(std::vector> &parcelableInfos, MessageParcel &data) +{ + int32_t infoSize = 0; + if (!data.ReadInt32(infoSize)) { + ANS_LOGE("read Parcelable size failed."); + return false; + } + + parcelableInfos.clear(); + infoSize = (infoSize < MAX_PARCELABLE_VECTOR_NUM) ? infoSize : MAX_PARCELABLE_VECTOR_NUM; + for (int32_t index = 0; index < infoSize; index++) { + sptr info = data.ReadStrongParcelable(); + if (info == nullptr) { + ANS_LOGE("read Parcelable infos failed."); + return false; + } + parcelableInfos.emplace_back(info); + } + + return true; +} + +ErrCode AnsSubscriberLocalLiveViewStub::HandleOnResponse(MessageParcel &data, MessageParcel &reply) +{ + int32_t notificationId = 0; + if (!data.ReadInt32(notificationId)) { + ANS_LOGE("[HandleOnResponse] fail : read notificationId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + sptr buttonOption = nullptr; + buttonOption = data.ReadParcelable(); + if (buttonOption == nullptr) { + ANS_LOGE("[HandleOnResponse] fail : read buttonOption failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + OnResponse(notificationId, buttonOption); + return ERR_OK; +} + +void AnsSubscriberLocalLiveViewStub::OnConnected() +{} + +void AnsSubscriberLocalLiveViewStub::OnDisconnected() +{} + +void AnsSubscriberLocalLiveViewStub::OnResponse(int32_t notificationId, sptr buttonOption) +{} +} // namespace Notification +} // namespace OHOS -- Gitee From b8b13b43bb1bfb9e0415055e05051cb1483de46f Mon Sep 17 00:00:00 2001 From: markYao Date: Sun, 22 Oct 2023 18:20:47 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=9E=E5=86=B5=E7=AA=97=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: markYao --- frameworks/ans/src/notification_button_option.cpp | 4 ++-- frameworks/ans/src/notification_capsule.cpp | 8 ++++---- .../ans/src/notification_local_live_view_button.cpp | 3 +-- .../ans/src/notification_local_live_view_content.cpp | 6 +++--- .../ans/src/notification_local_live_view_subscriber.cpp | 2 +- .../core/src/ans_subscriber_local_live_view_proxy.cpp | 2 +- .../core/src/ans_subscriber_local_live_view_stub.cpp | 2 +- 7 files changed, 13 insertions(+), 14 deletions(-) diff --git a/frameworks/ans/src/notification_button_option.cpp b/frameworks/ans/src/notification_button_option.cpp index 677e07082..f125e02cb 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 @@ -46,7 +46,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..14e8000c8 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 @@ -69,9 +69,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; } diff --git a/frameworks/ans/src/notification_local_live_view_button.cpp b/frameworks/ans/src/notification_local_live_view_button.cpp index 4b098fd1a..d6d8e99aa 100644 --- a/frameworks/ans/src/notification_local_live_view_button.cpp +++ b/frameworks/ans/src/notification_local_live_view_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 @@ -20,7 +20,6 @@ #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 diff --git a/frameworks/ans/src/notification_local_live_view_content.cpp b/frameworks/ans/src/notification_local_live_view_content.cpp index 6a64e31c9..373e96e0a 100644 --- a/frameworks/ans/src/notification_local_live_view_content.cpp +++ b/frameworks/ans/src/notification_local_live_view_content.cpp @@ -87,9 +87,9 @@ bool NotificationLocalLiveViewContent::ToJson(nlohmann::json &jsonObject) const return false; } - jsonObject["type"] = type_; - jsonObject["capsule"] = capsuleObj; - jsonObject["button"] = buttonObj; + jsonObject["type"] = type_; + jsonObject["capsule"] = capsuleObj; + jsonObject["button"] = buttonObj; jsonObject["progress"] = progressObj; return true; diff --git a/frameworks/ans/src/notification_local_live_view_subscriber.cpp b/frameworks/ans/src/notification_local_live_view_subscriber.cpp index dacbf3761..545c5f200 100644 --- a/frameworks/ans/src/notification_local_live_view_subscriber.cpp +++ b/frameworks/ans/src/notification_local_live_view_subscriber.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * 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 diff --git a/frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp b/frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp index 28a092a4a..27e935805 100644 --- a/frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp +++ b/frameworks/core/src/ans_subscriber_local_live_view_proxy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 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 diff --git a/frameworks/core/src/ans_subscriber_local_live_view_stub.cpp b/frameworks/core/src/ans_subscriber_local_live_view_stub.cpp index ac26eff5f..0f73b11c3 100644 --- a/frameworks/core/src/ans_subscriber_local_live_view_stub.cpp +++ b/frameworks/core/src/ans_subscriber_local_live_view_stub.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 -- Gitee