diff --git a/frameworks/ans/BUILD.gn b/frameworks/ans/BUILD.gn index 92524efdd91bf18d073632e82d23ec4e3ff55ade..a7015aa2e1d5413918a02b858f80d50697a2ca2f 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,11 +73,15 @@ 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_live_view_button.cpp", + "${frameworks_module_ans_path}/src/notification_local_live_view_content.cpp", + "${frameworks_module_ans_path}/src/notification_local_live_view_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", "${frameworks_module_ans_path}/src/notification_normal_content.cpp", "${frameworks_module_ans_path}/src/notification_picture_content.cpp", + "${frameworks_module_ans_path}/src/notification_progress.cpp", "${frameworks_module_ans_path}/src/notification_request.cpp", "${frameworks_module_ans_path}/src/notification_slot.cpp", "${frameworks_module_ans_path}/src/notification_sorting.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..677e070820f3470f2e8c924846c44e19ac76b751 --- /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 0000000000000000000000000000000000000000..f2cafc693af012f8f3c0866aa8e3f6f0cbff3448 --- /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_content.cpp b/frameworks/ans/src/notification_content.cpp index 244982c2d5b12a41fe94764765430ab2bc8a20a4..886e58863be15a2dc0151ffc1e16477f7f9441e8 100644 --- a/frameworks/ans/src/notification_content.cpp +++ b/frameworks/ans/src/notification_content.cpp @@ -15,6 +15,7 @@ #include "notification_content.h" #include "ans_log_wrapper.h" +#include "notification_local_live_view_content.h" namespace OHOS { namespace Notification { @@ -84,6 +85,17 @@ NotificationContent::NotificationContent(const std::shared_ptr &localLiveViewContent) +{ + if (!localLiveViewContent) { + ANS_LOGE("NotificationLocalLiveViewContent can not be null"); + return; + } + + contentType_ = NotificationContent::Type::LOCAL_LIVE_VIEW; + content_ = localLiveViewContent; +} + NotificationContent::~NotificationContent() {} @@ -99,12 +111,13 @@ std::shared_ptr NotificationContent::GetNotificationCo std::string NotificationContent::Dump() { - std::string contentTypeStr = (contentType_ == NotificationContent::Type::BASIC_TEXT) ? "BASIC_TEXT" - : (contentType_ == NotificationContent::Type::CONVERSATION) ? "CONVERSATION" - : (contentType_ == NotificationContent::Type::LONG_TEXT) ? "LONG_TEXT" - : (contentType_ == NotificationContent::Type::MEDIA) ? "MEDIA" - : (contentType_ == NotificationContent::Type::MULTILINE) ? "MULTILINE" - : (contentType_ == NotificationContent::Type::PICTURE) ? "PICTURE" : "NONE"; + std::string contentTypeStr = (contentType_ == NotificationContent::Type::BASIC_TEXT) ? "BASIC_TEXT" + : (contentType_ == NotificationContent::Type::CONVERSATION) ? "CONVERSATION" + : (contentType_ == NotificationContent::Type::LONG_TEXT) ? "LONG_TEXT" + : (contentType_ == NotificationContent::Type::MEDIA) ? "MEDIA" + : (contentType_ == NotificationContent::Type::MULTILINE) ? "MULTILINE" + : (contentType_ == NotificationContent::Type::PICTURE) ? "PICTURE" + : (contentType_ == NotificationContent::Type::LOCAL_LIVE_VIEW) ? "LOCAL_LIVE_VIEW" : "NONE"; return "NotificationContent{ " "contentType = " + contentTypeStr + @@ -227,6 +240,10 @@ bool NotificationContent::ReadFromParcel(Parcel &parcel) content_ = std::static_pointer_cast( std::shared_ptr(parcel.ReadParcelable())); break; + case NotificationContent::Type::LOCAL_LIVE_VIEW: + content_ = std::static_pointer_cast( + std::shared_ptr(parcel.ReadParcelable())); + break; default: ANS_LOGE("Invalid contentType"); return false; @@ -277,6 +294,9 @@ bool NotificationContent::ConvertJsonToContent(NotificationContent *target, cons case NotificationContent::Type::PICTURE: pBasicContent = NotificationJsonConverter::ConvertFromJson(contentObj); break; + case NotificationContent::Type::LOCAL_LIVE_VIEW: + pBasicContent = NotificationJsonConverter::ConvertFromJson(contentObj); + break; default: ANS_LOGE("Invalid contentType"); break; diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 2eac9228496e18e20a3a27c9e226c302b9f3c183..81a84f5b0258f2d17e8ed4abd56d08c50ecb65b9 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -164,6 +164,11 @@ ErrCode NotificationHelper::SubscribeNotification(const NotificationSubscriber & return DelayedSingleton::GetInstance()->SubscribeNotification(subscriber); } +ErrCode NotificationHelper::SubscribeLocalLiveViewNotification(const NotificationLocalLiveViewSubscriber &subscriber) +{ + return DelayedSingleton::GetInstance()->SubscribeLocalLiveViewNotification(subscriber); +} + ErrCode NotificationHelper::SubscribeNotification( const NotificationSubscriber &subscriber, const NotificationSubscribeInfo &subscribeInfo) { @@ -181,6 +186,12 @@ ErrCode NotificationHelper::UnSubscribeNotification( return DelayedSingleton::GetInstance()->UnSubscribeNotification(subscriber, subscribeInfo); } +ErrCode NotificationHelper::TriggerLocalLiveView(const NotificationBundleOption &bundleOption, + const int32_t notificationId, const NotificationButtonOption &buttonOption) +{ + return DelayedSingleton::GetInstance()->TriggerLocalLiveView(bundleOption, notificationId, buttonOption); +} + ErrCode NotificationHelper::RemoveNotification(const std::string &key, int32_t removeReason) { return DelayedSingleton::GetInstance()->RemoveNotification(key, removeReason); 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 0000000000000000000000000000000000000000..1bf090ed4ef3e2bf1707e9cc22d6df2e558dc576 --- /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 { + +std::vector NotificationLocalLiveViewButton::GetAllButtonNames() const +{ + return buttonNames_; +} + +void NotificationLocalLiveViewButton::addSingleButtonName(const std::string &buttonName) +{ + if (buttonNames_.size() >= 3) { + 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() >= 3) { + 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 < 3) ? vsize : 3; + 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 \ No newline at end of file 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 0000000000000000000000000000000000000000..854d9d43dcdec67e5c1e58c7bba84196ced5ce38 --- /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 0000000000000000000000000000000000000000..5db0e166625ad4fa4503f1f451d409ede776f921 --- /dev/null +++ b/frameworks/ans/src/notification_local_live_view_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_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 \ No newline at end of file diff --git a/frameworks/ans/src/notification_progress.cpp b/frameworks/ans/src/notification_progress.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b051c05ea028df52ebc74127a74b8453379db9a --- /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 \ No newline at end of file