diff --git a/frameworks/ans/native/src/notification_action_button.cpp b/frameworks/ans/native/src/notification_action_button.cpp index cc3ba9f0c52089569b25aeb4d137b040fba6ded2..c638c0703bbd38caa4bf6b6aef10e697b9ac4e20 100644 --- a/frameworks/ans/native/src/notification_action_button.cpp +++ b/frameworks/ans/native/src/notification_action_button.cpp @@ -26,7 +26,7 @@ std::shared_ptr NotificationActionButton::Create(const const std::string &title, const std::shared_ptr &wantAgent, const std::shared_ptr &extras, NotificationConstant::SemanticActionButton semanticActionButton, bool autoCreatedReplies, const std::vector> &mimeTypeOnlyInputs, - const std::vector> &userInputs, bool isContextual) + const std::shared_ptr &userInput, bool isContextual) { if (isContextual && (!icon || !wantAgent)) { ANS_LOGE("icon or wantAgent can not be null when isContextual is true"); @@ -42,18 +42,10 @@ std::shared_ptr NotificationActionButton::Create(const } } + std::shared_ptr textInput = userInput; std::vector> onlyInputs = mimeTypeOnlyInputs; - std::vector> textInputs {}; - for (auto &userInput : userInputs) { - if (!userInput) { - continue; - } - - if (userInput->IsMimeTypeOnly()) { - onlyInputs.push_back(userInput); - } else { - textInputs.push_back(userInput); - } + if (userInput && (userInput->IsMimeTypeOnly())) { + onlyInputs.push_back(userInput); } auto pActionButton = new (std::nothrow) NotificationActionButton(icon, @@ -63,7 +55,7 @@ std::shared_ptr NotificationActionButton::Create(const semanticActionButton, autoCreatedReplies, onlyInputs, - textInputs, + textInput, isContextual); if (pActionButton == nullptr) { ANS_LOGE("create NotificationActionButton object failed"); @@ -88,7 +80,7 @@ std::shared_ptr NotificationActionButton::Create( actionButton->GetSemanticActionButton(), actionButton->IsAutoCreatedReplies(), actionButton->GetMimeTypeOnlyUserInputs(), - actionButton->GetUserInputs(), + actionButton->GetUserInput(), actionButton->IsContextDependent()); } @@ -96,7 +88,7 @@ NotificationActionButton::NotificationActionButton(const std::shared_ptr &wantAgent, const std::shared_ptr &extras, NotificationConstant::SemanticActionButton semanticActionButton, bool autoCreatedReplies, const std::vector> &mimeTypeOnlyInputs, - const std::vector> &userInputs, bool isContextual) + const std::shared_ptr &userInput, bool isContextual) : icon_(icon), title_(title), wantAgent_(wantAgent), @@ -104,7 +96,7 @@ NotificationActionButton::NotificationActionButton(const std::shared_ptr> NotificationActionButton::Ge void NotificationActionButton::AddNotificationUserInput(const std::shared_ptr &userInput) { - if (!userInput) { - ANS_LOGE("The userInput is invalid."); - return; - } - - userInputs_.emplace_back(userInput); + userInput_ = userInput; } -std::vector> NotificationActionButton::GetUserInputs() const +const std::shared_ptr NotificationActionButton::GetUserInput() const { - return userInputs_; + return userInput_; } void NotificationActionButton::SetAutoCreatedReplies(bool autoCreatedReplies) @@ -212,14 +199,12 @@ std::string NotificationActionButton::Dump() mimeTypeOnlyUserInputs += ", "; } - std::string userInputs = ""; - for (auto &item : userInputs_) { - if (!item) { - userInputs += "nullptr, "; - continue; - } - userInputs += item->Dump(); - userInputs += ", "; + std::string userInput = ""; + if (userInput_ == nullptr) { + userInput += "nullptr, "; + } else { + userInput += userInput_->Dump(); + userInput += ", "; } return "NotificationActionButton{ " @@ -228,7 +213,7 @@ std::string NotificationActionButton::Dump() ", autoCreatedReplies = " + (autoCreatedReplies_ ? "true" : "false") + ", isContextual = " + (isContextual_ ? "true" : "false") + ", mimeTypeOnlyUserInputs = [" + mimeTypeOnlyUserInputs + "]" + - ", userInputs = [" + userInputs + "]" + + ", userInputs = [" + userInput + "]" + " }"; } @@ -350,24 +335,17 @@ bool NotificationActionButton::Marshalling(Parcel &parcel) const } } - if (!parcel.WriteInt32(static_cast(userInputs_.size()))) { - ANS_LOGE("Failed to write the size of userInputs"); + valid = userInput_ ? true : false; + if (!parcel.WriteBool(valid)) { + ANS_LOGE("Failed to write the flag which indicate whether userInput is null"); return false; } - for (auto it = userInputs_.begin(); it != userInputs_.end(); ++it) { - valid = (*it) ? true : false; - if (!parcel.WriteBool(valid)) { - ANS_LOGE("Failed to write the flag which indicate whether userInput is null"); + if (valid) { + if (!parcel.WriteParcelable(userInput_.get())) { + ANS_LOGE("Failed to write userInput"); return false; } - - if (valid) { - if (!parcel.WriteParcelable(it->get())) { - ANS_LOGE("Failed to write userInput"); - return false; - } - } } return true; @@ -427,19 +405,13 @@ bool NotificationActionButton::ReadFromParcel(Parcel &parcel) } } - auto vsize = parcel.ReadInt32(); - for (auto it = 0; it < vsize; ++it) { - valid = parcel.ReadBool(); - NotificationUserInput *member {nullptr}; - if (valid) { - member = parcel.ReadParcelable(); - if (member == nullptr) { - ANS_LOGE("Failed to read userInput"); - return false; - } + valid = parcel.ReadBool(); + if (valid) { + userInput_ = std::shared_ptr(parcel.ReadParcelable()); + if (!userInput_) { + ANS_LOGE("Failed to read userInput"); + return false; } - - userInputs_.emplace_back(member); } return true; diff --git a/frameworks/ans/native/src/notification_sorting_map.cpp b/frameworks/ans/native/src/notification_sorting_map.cpp index 2bd73d2f504f4b270616547b4da9d50490ba497a..9e87b4e635d4ff3cb3e0e85d582cb7772d5a3b5c 100644 --- a/frameworks/ans/native/src/notification_sorting_map.cpp +++ b/frameworks/ans/native/src/notification_sorting_map.cpp @@ -68,12 +68,19 @@ bool NotificationSortingMap::Marshalling(Parcel &parcel) const ANS_LOGE("Can't write sorting size"); return false; } - for_each(sortings_.begin(), sortings_.end(), [&](std::pair sorting) { + + int count = 0; + for (auto &sorting : sortings_) { if (!parcel.WriteParcelable(&sorting.second)) { ANS_LOGE("Can't write sorting"); ret = false; } - }); + count++; + + if (count == MAX_ACTIVE_NUM) { + break; + } + } return ret; } @@ -84,6 +91,7 @@ NotificationSortingMap *NotificationSortingMap::Unmarshalling(Parcel &parcel) // read sorting num int32_t size = 0; parcel.ReadInt32(size); + size = (size <= MAX_ACTIVE_NUM) ? size : MAX_ACTIVE_NUM; for (int i = 0; i < size; i++) { // read sorting diff --git a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp index 89aa95b853d0cd73f64fef98072b54390b4f12dc..80c11446e13354b9753c7eb9bee404396692b7e7 100644 --- a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp +++ b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp @@ -216,25 +216,23 @@ private: EXPECT_EQ(NotificationConstant::OTHER, notificationRequest.GetSlotType()); std::vector> actionButtons = notificationRequest.GetActionButtons(); for (auto actionButton : actionButtons) { - std::vector> userInputs = actionButton->GetUserInputs(); - for (auto userInput : userInputs) { - EXPECT_EQ(NotificationConstant::FREE_FORM_INPUT, userInput->GetInputsSource(g_want)); - EXPECT_EQ(nullptr, userInput->GetInputsFromWant(g_want)); - std::map> map = userInput->GetMimeInputsFromWant(g_want, ""); - EXPECT_EQ(unsigned(0), map.size()); - EXPECT_EQ("inputKey", userInput->GetInputKey()); - EXPECT_NE(nullptr, userInput->GetAdditionalData()); - EXPECT_EQ(NotificationConstant::InputEditType::EDIT_DISABLED, userInput->GetEditType()); - for (auto option : userInput->GetOptions()) { - EXPECT_EQ("", option); - } - for (auto type : userInput->GetPermitMimeTypes()) { - EXPECT_EQ("mimeType", type); - } - EXPECT_EQ(false, userInput->IsMimeTypeOnly()); - EXPECT_EQ("tag", userInput->GetTag()); - EXPECT_EQ(false, userInput->IsPermitFreeFormInput()); + std::shared_ptr userInput = actionButton->GetUserInput(); + EXPECT_EQ(NotificationConstant::FREE_FORM_INPUT, userInput->GetInputsSource(g_want)); + EXPECT_EQ(nullptr, userInput->GetInputsFromWant(g_want)); + std::map> map = userInput->GetMimeInputsFromWant(g_want, ""); + EXPECT_EQ(unsigned(0), map.size()); + EXPECT_EQ("inputKey", userInput->GetInputKey()); + EXPECT_NE(nullptr, userInput->GetAdditionalData()); + EXPECT_EQ(NotificationConstant::InputEditType::EDIT_DISABLED, userInput->GetEditType()); + for (auto option : userInput->GetOptions()) { + EXPECT_EQ("", option); + } + for (auto type : userInput->GetPermitMimeTypes()) { + EXPECT_EQ("mimeType", type); } + EXPECT_EQ(false, userInput->IsMimeTypeOnly()); + EXPECT_EQ("tag", userInput->GetTag()); + EXPECT_EQ(false, userInput->IsPermitFreeFormInput()); } } @@ -1345,9 +1343,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_08000, Function std::shared_ptr dummyIcon; auto ab1 = NotificationActionButton::Create(dummyIcon, "ab1_title", wAgent2); - auto spUserInput3 = NotificationUserInput::Create("uikey3"); auto spUserInput2 = NotificationUserInput::Create("uikey2"); - ab1->AddNotificationUserInput(spUserInput3); ab1->AddNotificationUserInput(spUserInput2); auto spOnlyUserInput1 = NotificationUserInput::Create("uionlykey1"); spOnlyUserInput1->SetPermitFreeFormInput(false); diff --git a/interfaces/innerkits/ans/native/include/notification_action_button.h b/interfaces/innerkits/ans/native/include/notification_action_button.h index 48dfe2a238c4f6c3c49207c94fbd7d6fd1a42d62..d1a14a2a226143f3b085a58b415bf43cc2b01059 100644 --- a/interfaces/innerkits/ans/native/include/notification_action_button.h +++ b/interfaces/innerkits/ans/native/include/notification_action_button.h @@ -50,7 +50,7 @@ public: NotificationConstant::SemanticActionButton::NONE_ACTION_BUTTON, bool autoCreatedReplies = true, const std::vector> &mimeTypeOnlyInputs = {}, - const std::vector> &userInputs = {}, bool isContextual = false); + const std::shared_ptr &userInput = {}, bool isContextual = false); /** * A static function used to create a NotificationActionButton instance by copying parameters from an existing @@ -130,11 +130,11 @@ public: void AddNotificationUserInput(const std::shared_ptr &userInput); /** - * Obtains the NotificationUserInput objects to be collected from the user when this NotificationActionButton + * Obtains the NotificationUserInput object to be collected from the user when this NotificationActionButton * is sent. - * @return the list of NotificationUserInput objects. + * @return the NotificationUserInput object. */ - std::vector> GetUserInputs() const; + const std::shared_ptr GetUserInput() const; /** * Sets whether to allow the platform to automatically generate possible replies and add them to @@ -222,7 +222,7 @@ private: const std::shared_ptr &extras, NotificationConstant::SemanticActionButton semanticActionButton, bool autoCreatedReplies, const std::vector> &mimeTypeOnlyInputs, - const std::vector> &userInputs, bool isContextual); + const std::shared_ptr &userInput, bool isContextual); /** * Read a NotificationActionButton object from a Parcel. @@ -240,7 +240,7 @@ private: }; bool autoCreatedReplies_ {true}; std::vector> mimeTypeOnlyUserInputs_ {}; - std::vector> userInputs_ {}; + std::shared_ptr userInput_ {}; bool isContextual_ {false}; }; } // namespace Notification diff --git a/interfaces/kits/napi/ans/src/common.cpp b/interfaces/kits/napi/ans/src/common.cpp index da8aa7656a784e1f385615857641ceaf51c9fb40..92155b0486aef76e08631b471312a124226d8c3b 100644 --- a/interfaces/kits/napi/ans/src/common.cpp +++ b/interfaces/kits/napi/ans/src/common.cpp @@ -1204,14 +1204,12 @@ napi_value Common::SetNotificationActionButton( } // userInput?: NotificationUserInput - if (actionButton->GetUserInputs().size() > 0) { - napi_value userInputResult = nullptr; - napi_create_object(env, &userInputResult); - if (!SetNotificationActionButtonByUserInput(env, actionButton->GetUserInputs().front(), userInputResult)) { - return NapiGetBoolean(env, false); - } - napi_set_named_property(env, result, "userInput", userInputResult); + napi_value userInputResult = nullptr; + napi_create_object(env, &userInputResult); + if (!SetNotificationActionButtonByUserInput(env, actionButton->GetUserInput(), userInputResult)) { + return NapiGetBoolean(env, false); } + napi_set_named_property(env, result, "userInput", userInputResult); return NapiGetBoolean(env, true); }