From 423e5587ba2cf826695bb1b3eaead6e969f08b74 Mon Sep 17 00:00:00 2001 From: baozeyu Date: Sat, 31 Aug 2024 16:52:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9B=BE=E7=89=87=E6=81=A2?= =?UTF-8?q?=E5=A4=8D=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: baozeyu Change-Id: I94271ae04d3b0f2430a515f9353fd4359c245919 --- frameworks/ans/src/notification_request.cpp | 66 +++++++++++++++++-- frameworks/core/include/ans_image_util.h | 4 +- frameworks/core/src/ans_image_util.cpp | 7 +- interfaces/inner_api/notification_request.h | 8 +++ .../ans/src/advanced_notification_service.cpp | 6 -- 5 files changed, 77 insertions(+), 14 deletions(-) diff --git a/frameworks/ans/src/notification_request.cpp b/frameworks/ans/src/notification_request.cpp index 9dbb6e96b..1c1ad6b26 100644 --- a/frameworks/ans/src/notification_request.cpp +++ b/frameworks/ans/src/notification_request.cpp @@ -300,6 +300,11 @@ int64_t NotificationRequest::GetArchiveDeadLine() const void NotificationRequest::SetLittleIcon(const std::shared_ptr &littleIcon) { littleIcon_ = littleIcon; + if (littleIcon != nullptr) { + Media::ImageInfo outImageInfo; + littleIcon->GetImageInfo(outImageInfo); + littleIconType_ = outImageInfo.encodedFormat; + } } const std::shared_ptr NotificationRequest::GetLittleIcon() const @@ -1224,10 +1229,37 @@ bool NotificationRequest::Marshalling(Parcel &parcel) const } if (valid) { - if (!parcel.WriteParcelable(littleIcon_.get())) { - ANS_LOGE("Failed to write littleIcon"); + if (!parcel.WriteString(littleIconType_)) { + ANS_LOGE("Failed to write littleIconType"); return false; } + + bool isUnPackImage = true; + std::string littleIconString; + + if (!littleIconType_.empty()) { + littleIconString = AnsImageUtil::PackImage(littleIcon_, littleIconType_); + } + + if (isUnPackImage && !littleIconString.empty()) { + isUnPackImage = false; + } + + if (!parcel.WriteBool(isUnPackImage)) { + return false; + } + ANS_LOGD("littleIcon_ : %{public}d, %{public}s", isUnPackImage, littleIconType_.c_str()); + if (isUnPackImage) { + if (!parcel.WriteParcelable(littleIcon_.get())) { + ANS_LOGE("Failed to write littleIcon"); + return false; + } + } else { + if (!parcel.WriteString(littleIconString)) { + ANS_LOGE("Failed to write littleIcon"); + return false; + } + } } valid = bigIcon_ ? true : false; @@ -1567,7 +1599,15 @@ bool NotificationRequest::ReadFromParcel(Parcel &parcel) valid = parcel.ReadBool(); if (valid) { - littleIcon_ = std::shared_ptr(parcel.ReadParcelable()); + littleIconType_ = parcel.ReadString(); + bool isUnPackImage = parcel.ReadBool(); + if (isUnPackImage) { + littleIcon_ = std::shared_ptr(parcel.ReadParcelable()); + } else { + std::string littleIconString = parcel.ReadString(); + littleIcon_ = AnsImageUtil::UnPackImage(littleIconString, littleIconType_); + } + if (!littleIcon_) { ANS_LOGE("Failed to read littleIcon"); return false; @@ -1881,6 +1921,7 @@ void NotificationRequest::CopyOther(const NotificationRequest &other) this->notificationBundleOption_ = other.notificationBundleOption_; this->notificationFlagsOfDevices_ = other.notificationFlagsOfDevices_; this->publishDelayTime_ = other.publishDelayTime_; + this->littleIconType_ = other.littleIconType_; } bool NotificationRequest::ConvertObjectsToJson(nlohmann::json &jsonObject) const @@ -1919,7 +1960,10 @@ bool NotificationRequest::ConvertObjectsToJson(nlohmann::json &jsonObject) const } jsonObject["extraInfo"] = extraInfoStr; - jsonObject["smallIcon"] = AnsImageUtil::PackImage(littleIcon_); + jsonObject["smallIconType"] = littleIconType_; + if (!littleIconType_.empty()) { + jsonObject["smallIcon"] = AnsImageUtil::PackImage(littleIcon_, littleIconType_); + } jsonObject["largeIcon"] = AnsImageUtil::PackImage(bigIcon_); jsonObject["overlayIcon"] = overlayIcon_ ? AnsImageUtil::PackImage(overlayIcon_) : ""; @@ -2165,9 +2209,14 @@ void NotificationRequest::ConvertJsonToPixelMap(NotificationRequest *target, con const auto &jsonEnd = jsonObject.cend(); + if (jsonObject.find("smallIconType") != jsonEnd && jsonObject.at("smallIconType").is_string()) { + std::string littleIconType = jsonObject.at("smallIconType").get(); + target->littleIconType_ = littleIconType; + } + if (jsonObject.find("smallIcon") != jsonEnd && jsonObject.at("smallIcon").is_string()) { - auto littleIconStr = jsonObject.at("smallIcon").get(); - target->littleIcon_ = AnsImageUtil::UnPackImage(littleIconStr); + auto littleIconStr = jsonObject.at("smallIcon").get(); + target->littleIcon_ = AnsImageUtil::UnPackImage(littleIconStr, target->littleIconType_); } if (jsonObject.find("largeIcon") != jsonEnd && jsonObject.at("largeIcon").is_string()) { @@ -2693,5 +2742,10 @@ bool NotificationRequest::IsUpdateByOwnerAllowed() const { return isUpdateByOwnerAllowed_; } + +const std::string NotificationRequest::GetLittleIconType() const +{ + return littleIconType_; +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/include/ans_image_util.h b/frameworks/core/include/ans_image_util.h index d7068e608..5b1bbe86a 100644 --- a/frameworks/core/include/ans_image_util.h +++ b/frameworks/core/include/ans_image_util.h @@ -28,6 +28,7 @@ public: static const uint8_t SHIFT_FOUR; static const uint8_t NUM_TEN; static const size_t TWO_TIMES; + static const uint32_t DEFAULT_SIZE; /** * @brief Packs an image to a string. @@ -45,7 +46,8 @@ public: * @param pixelMapStr Indicates the string of image. * @return Returns an image object. */ - static std::shared_ptr UnPackImage(const std::string &pixelMapStr); + static std::shared_ptr UnPackImage(const std::string &pixelMapStr, + const std::string &format = IMAGE_FORMAT_PNG); /** * @brief Packs an image to a file. diff --git a/frameworks/core/src/ans_image_util.cpp b/frameworks/core/src/ans_image_util.cpp index f2fe05f4e..e3929d1dd 100644 --- a/frameworks/core/src/ans_image_util.cpp +++ b/frameworks/core/src/ans_image_util.cpp @@ -24,6 +24,7 @@ const uint8_t AnsImageUtil::IMAGE_QUALITY {100}; const uint8_t AnsImageUtil::SHIFT_FOUR {4}; const uint8_t AnsImageUtil::NUM_TEN {10}; const size_t AnsImageUtil::TWO_TIMES {2}; +const uint32_t AnsImageUtil::DEFAULT_SIZE {25 * 1024 * 1024}; std::string AnsImageUtil::PackImage(const std::shared_ptr &pixelMap, const std::string &format) { @@ -47,6 +48,9 @@ std::string AnsImageUtil::PackImage(const std::shared_ptr &pixe auto size = static_cast(pixelMap->GetByteCount()); ANS_LOGD("size of pixelMap : %{public}u", size); + if (size < DEFAULT_SIZE) { + size = DEFAULT_SIZE; + } auto pbuf = new (std::nothrow) uint8_t [size]; if (pbuf == nullptr) { ANS_LOGE("create buffer failed"); @@ -66,7 +70,7 @@ std::string AnsImageUtil::PackImage(const std::shared_ptr &pixe return BinToHex(pixelMapStr); } -std::shared_ptr AnsImageUtil::UnPackImage(const std::string &pixelMapStr) +std::shared_ptr AnsImageUtil::UnPackImage(const std::string &pixelMapStr, const std::string &format) { if (pixelMapStr.empty()) { return {}; @@ -76,6 +80,7 @@ std::shared_ptr AnsImageUtil::UnPackImage(const std::string &pi uint32_t errorCode {0}; Media::SourceOptions opts; + opts.formatHint = format; auto imageSource = Media::ImageSource::CreateImageSource( reinterpret_cast(binStr.data()), static_cast(binStr.length()), diff --git a/interfaces/inner_api/notification_request.h b/interfaces/inner_api/notification_request.h index 541f85294..8810bf4d8 100644 --- a/interfaces/inner_api/notification_request.h +++ b/interfaces/inner_api/notification_request.h @@ -507,6 +507,13 @@ public: */ const std::shared_ptr GetLittleIcon() const; + /** + * @brief Obtains the icon type of the notification. + * + * @return Returns the notification icon type + */ + const std::string GetLittleIconType() const; + /** * @brief Sets the large icon of this notification, which is usually displayed on the right of the notification. * @@ -1497,6 +1504,7 @@ private: std::shared_ptr maxScreenWantAgent_ {}; std::shared_ptr additionalParams_ {}; std::shared_ptr littleIcon_ {}; + std::string littleIconType_ {}; std::shared_ptr bigIcon_ {}; std::shared_ptr overlayIcon_ {}; std::shared_ptr notificationContent_ {}; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index a8803cc80..22d46c492 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1194,9 +1194,6 @@ ErrCode AdvancedNotificationService::RemoveFromNotificationList(const sptr