diff --git a/frameworks/ans/core/include/ans_subscriber_interface.h b/frameworks/ans/core/include/ans_subscriber_interface.h index 539f22a6005d18cf0ff2d8b6e18fb512ea91e182..e49cb26eaaa61460056f5a45332581ba68b3f867 100644 --- a/frameworks/ans/core/include/ans_subscriber_interface.h +++ b/frameworks/ans/core/include/ans_subscriber_interface.h @@ -34,8 +34,8 @@ public: DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Notification.IAnsSubscriber"); - virtual void OnSubscribeResult(NotificationConstant::SubscribeResult result) = 0; - virtual void OnUnsubscribeResult(NotificationConstant::SubscribeResult result) = 0; + virtual void OnConnected() = 0; + virtual void OnDisconnected() = 0; virtual void OnConsumed(const sptr ¬ification) = 0; virtual void OnConsumed( const sptr ¬ification, const sptr ¬ificationMap) = 0; @@ -47,8 +47,8 @@ public: protected: enum TransactId : uint32_t { - ON_SUBSCRIBE = FIRST_CALL_TRANSACTION, - ON_UNSUBSCRIBE, + ON_CONNECTED = FIRST_CALL_TRANSACTION, + ON_DISCONNECTED, ON_CONSUMED, ON_CONSUMED_MAP, ON_CANCELED, diff --git a/frameworks/ans/core/include/ans_subscriber_proxy.h b/frameworks/ans/core/include/ans_subscriber_proxy.h index 1774567c236f3946efefca6d0b92785c3a9281e1..f3101c52f73396dcd6e9dd5b5b4c54fac52a126b 100644 --- a/frameworks/ans/core/include/ans_subscriber_proxy.h +++ b/frameworks/ans/core/include/ans_subscriber_proxy.h @@ -29,8 +29,8 @@ public: ~AnsSubscriberProxy() override; DISALLOW_COPY_AND_MOVE(AnsSubscriberProxy); - void OnSubscribeResult(NotificationConstant::SubscribeResult result) override; - void OnUnsubscribeResult(NotificationConstant::SubscribeResult result) override; + void OnConnected() override; + void OnDisconnected() override; void OnConsumed(const sptr ¬ification) override; void OnConsumed( const sptr ¬ification, const sptr ¬ificationMap) override; diff --git a/frameworks/ans/core/include/ans_subscriber_stub.h b/frameworks/ans/core/include/ans_subscriber_stub.h index 4d60ccfc00c3c37bd30c4b992bb05eff68ce3035..b8c8b82de0bc7bddf0764584308e8ce5359f3a93 100644 --- a/frameworks/ans/core/include/ans_subscriber_stub.h +++ b/frameworks/ans/core/include/ans_subscriber_stub.h @@ -31,8 +31,8 @@ public: virtual int OnRemoteRequest( uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; - void OnSubscribeResult(NotificationConstant::SubscribeResult result) override; - void OnUnsubscribeResult(NotificationConstant::SubscribeResult result) override; + void OnConnected() override; + void OnDisconnected() override; void OnConsumed(const sptr ¬ification) override; void OnConsumed( const sptr ¬ification, const sptr ¬ificationMap) override; @@ -45,8 +45,8 @@ public: private: std::map> interfaces_; - ErrCode HandleOnSubscribe(MessageParcel &data, MessageParcel &reply); - ErrCode HandleOnUnsubscribe(MessageParcel &data, MessageParcel &reply); + ErrCode HandleOnConnected(MessageParcel &data, MessageParcel &reply); + ErrCode HandleOnDisconnected(MessageParcel &data, MessageParcel &reply); ErrCode HandleOnConsumed(MessageParcel &data, MessageParcel &reply); ErrCode HandleOnConsumedMap(MessageParcel &data, MessageParcel &reply); ErrCode HandleOnCanceled(MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/ans/core/src/ans_notification.cpp b/frameworks/ans/core/src/ans_notification.cpp index da55aed922b6b05cde374cecffb495014c3ce17e..503b764d0c9b72ee12900434f0e4958636d6b16b 100644 --- a/frameworks/ans/core/src/ans_notification.cpp +++ b/frameworks/ans/core/src/ans_notification.cpp @@ -165,7 +165,7 @@ ErrCode AnsNotification::GetNotificationSlotNumAsBundle(const NotificationBundle return ERR_ANS_SERVICE_NOT_CONNECTED; } - sptr bo(new NotificationBundleOption(bundleOption)); + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); return ansManagerProxy_->GetSlotNumAsBundle(bo, num); } @@ -519,7 +519,7 @@ ErrCode AnsNotification::RemoveNotification( return ERR_ANS_SERVICE_NOT_CONNECTED; } - sptr bo(new NotificationBundleOption(bundleOption)); + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); return ansManagerProxy_->RemoveNotification(bo, notificationId, label); } @@ -530,7 +530,7 @@ ErrCode AnsNotification::RemoveAllNotifications(const NotificationBundleOption & return ERR_ANS_SERVICE_NOT_CONNECTED; } - sptr bo(new NotificationBundleOption(bundleOption)); + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); return ansManagerProxy_->RemoveAllNotifications(bo); } @@ -541,7 +541,7 @@ ErrCode AnsNotification::RemoveNotificationsByBundle(const NotificationBundleOpt return ERR_ANS_SERVICE_NOT_CONNECTED; } - sptr bo(new NotificationBundleOption(bundleOption)); + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); return ansManagerProxy_->DeleteByBundle(bo); } @@ -567,7 +567,7 @@ ErrCode AnsNotification::GetNotificationSlotsForBundle( return ERR_ANS_SERVICE_NOT_CONNECTED; } - sptr bo(new NotificationBundleOption(bundleOption)); + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); return ansManagerProxy_->GetSlotsByBundle(bo, slots); } @@ -579,7 +579,7 @@ ErrCode AnsNotification::UpdateNotificationSlots( return ERR_ANS_SERVICE_NOT_CONNECTED; } - sptr bo(new NotificationBundleOption(bundleOption)); + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); return ansManagerProxy_->UpdateSlots(bo, slots); } @@ -591,7 +591,7 @@ ErrCode AnsNotification::UpdateNotificationSlotGroups( return ERR_ANS_SERVICE_NOT_CONNECTED; } - sptr bo(new NotificationBundleOption(bundleOption)); + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); return ansManagerProxy_->UpdateSlotGroups(bo, groups); } @@ -626,7 +626,7 @@ ErrCode AnsNotification::IsAllowedNotify(const NotificationBundleOption &bundleO return ERR_ANS_SERVICE_NOT_CONNECTED; } - sptr bo(new NotificationBundleOption(bundleOption)); + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); return ansManagerProxy_->IsSpecialBundleAllowedNotify(bo, allowed); } @@ -656,7 +656,7 @@ ErrCode AnsNotification::SetNotificationsEnabledForSpecifiedBundle( return ERR_ANS_SERVICE_NOT_CONNECTED; } - sptr bo(new NotificationBundleOption(bundleOption)); + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); return ansManagerProxy_->SetNotificationsEnabledForSpecialBundle(deviceId, bo, enabled); } @@ -667,7 +667,7 @@ ErrCode AnsNotification::SetShowBadgeEnabledForBundle(const NotificationBundleOp return ERR_ANS_SERVICE_NOT_CONNECTED; } - sptr bo(new NotificationBundleOption(bundleOption)); + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); return ansManagerProxy_->SetShowBadgeEnabledForBundle(bo, enabled); } @@ -678,7 +678,7 @@ ErrCode AnsNotification::GetShowBadgeEnabledForBundle(const NotificationBundleOp return ERR_ANS_SERVICE_NOT_CONNECTED; } - sptr bo(new NotificationBundleOption(bundleOption)); + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); return ansManagerProxy_->GetShowBadgeEnabledForBundle(bo, enabled); } @@ -754,7 +754,7 @@ bool AnsNotification::GetAnsManagerProxy() return false; } - recipient_ = new AnsManagerDeathRecipient(); + recipient_ = new (std::nothrow) AnsManagerDeathRecipient(); if (!recipient_) { ANS_LOGE("Failed to create death recipient"); return false; @@ -884,4 +884,4 @@ ErrCode AnsNotification::CheckImageSize(const NotificationRequest &request) return ERR_OK; } } // namespace Notification -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/frameworks/ans/core/src/ans_subscriber_proxy.cpp b/frameworks/ans/core/src/ans_subscriber_proxy.cpp index f42f9a8c310e7d13efd89ee8a418a2b587e24df0..7cef65d7d12e16be9ccabc0f8d0409fc4205f603 100644 --- a/frameworks/ans/core/src/ans_subscriber_proxy.cpp +++ b/frameworks/ans/core/src/ans_subscriber_proxy.cpp @@ -53,46 +53,36 @@ ErrCode AnsSubscriberProxy::InnerTransact( } } -void AnsSubscriberProxy::OnSubscribeResult(NotificationConstant::SubscribeResult result) +void AnsSubscriberProxy::OnConnected() { MessageParcel data; if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) { - ANS_LOGW("[OnSubscribeResult] fail: write interface token failed."); - return; - } - - if (!data.WriteUint32(result)) { - ANS_LOGW("[OnSubscribeResult] fail: write result failed"); + ANS_LOGW("[OnConnected] fail: write interface token failed."); return; } MessageParcel reply; MessageOption option = {MessageOption::TF_ASYNC}; - ErrCode transactResult = InnerTransact(ON_SUBSCRIBE, option, data, reply); - if (transactResult != ERR_OK) { - ANS_LOGW("[OnSubscribeResult] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); + ErrCode result = InnerTransact(ON_CONNECTED, option, data, reply); + if (result != ERR_OK) { + ANS_LOGW("[OnConnected] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); return; } } -void AnsSubscriberProxy::OnUnsubscribeResult(NotificationConstant::SubscribeResult result) +void AnsSubscriberProxy::OnDisconnected() { MessageParcel data; if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) { - ANS_LOGW("[OnUnsubscribeResult] fail: write interface token failed."); - return; - } - - if (!data.WriteUint32(result)) { - ANS_LOGW("[OnUnsubscribeResult] fail: write result failed"); + ANS_LOGW("[OnDisconnected] fail: write interface token failed."); return; } MessageParcel reply; MessageOption option = {MessageOption::TF_ASYNC}; - ErrCode transactResult = InnerTransact(ON_UNSUBSCRIBE, option, data, reply); - if (transactResult != ERR_OK) { - ANS_LOGW("[OnUnsubscribeResult] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); + ErrCode result = InnerTransact(ON_DISCONNECTED, option, data, reply); + if (result != ERR_OK) { + ANS_LOGW("[OnDisconnected] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); return; } } diff --git a/frameworks/ans/core/src/ans_subscriber_stub.cpp b/frameworks/ans/core/src/ans_subscriber_stub.cpp index 8ecb5721982416f3ddfd6cf698aeb97b4528c003..716affd1e0692fae6a31b48c1f36fd5956efc7f4 100644 --- a/frameworks/ans/core/src/ans_subscriber_stub.cpp +++ b/frameworks/ans/core/src/ans_subscriber_stub.cpp @@ -28,10 +28,10 @@ namespace OHOS { namespace Notification { AnsSubscriberStub::AnsSubscriberStub() { - interfaces_.emplace(ON_SUBSCRIBE, - std::bind(&AnsSubscriberStub::HandleOnSubscribe, this, std::placeholders::_1, std::placeholders::_2)); - interfaces_.emplace(ON_UNSUBSCRIBE, - std::bind(&AnsSubscriberStub::HandleOnUnsubscribe, this, std::placeholders::_1, std::placeholders::_2)); + interfaces_.emplace(ON_CONNECTED, + std::bind(&AnsSubscriberStub::HandleOnConnected, this, std::placeholders::_1, std::placeholders::_2)); + interfaces_.emplace(ON_DISCONNECTED, + std::bind(&AnsSubscriberStub::HandleOnDisconnected, this, std::placeholders::_1, std::placeholders::_2)); interfaces_.emplace(ON_CONSUMED, std::bind(&AnsSubscriberStub::HandleOnConsumed, this, std::placeholders::_1, std::placeholders::_2)); interfaces_.emplace(ON_CONSUMED_MAP, @@ -75,19 +75,15 @@ int32_t AnsSubscriberStub::OnRemoteRequest( return NO_ERROR; } -ErrCode AnsSubscriberStub::HandleOnSubscribe(MessageParcel &data, MessageParcel &reply) +ErrCode AnsSubscriberStub::HandleOnConnected(MessageParcel &data, MessageParcel &reply) { - NotificationConstant::SubscribeResult result = - static_cast(data.ReadUint32()); - OnSubscribeResult(result); + OnConnected(); return ERR_OK; } -ErrCode AnsSubscriberStub::HandleOnUnsubscribe(MessageParcel &data, MessageParcel &reply) +ErrCode AnsSubscriberStub::HandleOnDisconnected(MessageParcel &data, MessageParcel &reply) { - NotificationConstant::SubscribeResult result = - static_cast(data.ReadUint32()); - OnUnsubscribeResult(result); + OnDisconnected(); return ERR_OK; } @@ -194,10 +190,10 @@ ErrCode AnsSubscriberStub::HandleOnDisturbModeChanged(MessageParcel &data, Messa return ERR_OK; } -void AnsSubscriberStub::OnSubscribeResult(NotificationConstant::SubscribeResult result) +void AnsSubscriberStub::OnConnected() {} -void AnsSubscriberStub::OnUnsubscribeResult(NotificationConstant::SubscribeResult result) +void AnsSubscriberStub::OnDisconnected() {} void AnsSubscriberStub::OnConsumed(const sptr ¬ification) diff --git a/frameworks/ans/native/src/notification_subscriber.cpp b/frameworks/ans/native/src/notification_subscriber.cpp index 4220ea0ec500180a2ec75ba27dd12ed3f56cccc9..aae2544d8c4ba1b6ef44b23d099dba6b0a0eb104 100644 --- a/frameworks/ans/native/src/notification_subscriber.cpp +++ b/frameworks/ans/native/src/notification_subscriber.cpp @@ -39,22 +39,22 @@ NotificationSubscriber::SubscriberImpl::SubscriberImpl(NotificationSubscriber &s recipient_ = new DeathRecipient(*this); }; -void NotificationSubscriber::SubscriberImpl::OnSubscribeResult(NotificationConstant::SubscribeResult result) +void NotificationSubscriber::SubscriberImpl::OnConnected() { - if (result == 0 && GetAnsManagerProxy()) { + if (GetAnsManagerProxy()) { proxy_->AsObject()->AddDeathRecipient(recipient_); ANS_LOGD("%s, Add death recipient.", __func__); } - subscriber_.OnSubscribeResult(result); + subscriber_.OnConnected(); } -void NotificationSubscriber::SubscriberImpl::OnUnsubscribeResult(NotificationConstant::SubscribeResult result) +void NotificationSubscriber::SubscriberImpl::OnDisconnected() { - if (result == 0 && GetAnsManagerProxy()) { + if (GetAnsManagerProxy()) { proxy_->AsObject()->RemoveDeathRecipient(recipient_); ANS_LOGD("%s, Remove death recipient.", __func__); } - subscriber_.OnUnsubscribeResult(result); + subscriber_.OnDisconnected(); } void NotificationSubscriber::SubscriberImpl::OnConsumed(const sptr ¬ification) @@ -131,4 +131,4 @@ void NotificationSubscriber::SubscriberImpl::DeathRecipient::OnRemoteDied(const } } // namespace Notification -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp index d851d969aabf4732c097031d3ff293afdb00b0c2..b906561729a2fe441872272ae99bf05083427939 100644 --- a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp +++ b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp @@ -74,34 +74,20 @@ class OnSubscribeResultEvent : public SubscriberEvent { public: ~OnSubscribeResultEvent() override {} - OnSubscribeResultEvent(NotificationConstant::SubscribeResult result) - : SubscriberEvent(SubscriberEventType::ON_SUBSCRIBERESULT), subscribeResult_(result) - {} - NotificationConstant::SubscribeResult GetSubscribeResult() - { - return subscribeResult_; - } -private: - NotificationConstant::SubscribeResult subscribeResult_; + OnSubscribeResultEvent() + : SubscriberEvent(SubscriberEventType::ON_SUBSCRIBERESULT) + {} }; class OnUnSubscribeResultEvent : public SubscriberEvent { public: - OnUnSubscribeResultEvent(NotificationConstant::SubscribeResult result) - : SubscriberEvent(SubscriberEventType::ON_UNSUBSCRIBERESULT), unSubscribeResult_(result) + OnUnSubscribeResultEvent() + : SubscriberEvent(SubscriberEventType::ON_UNSUBSCRIBERESULT) {} ~OnUnSubscribeResultEvent() override {} - - NotificationConstant::SubscribeResult GetUnSubscribeResult() - { - return unSubscribeResult_; - } - -private: - NotificationConstant::SubscribeResult unSubscribeResult_; }; class OnDiedEvent : public SubscriberEvent { @@ -246,15 +232,15 @@ private: class TestAnsSubscriber : public NotificationSubscriber { public: - void OnSubscribeResult(NotificationConstant::SubscribeResult result) override + void OnConnected() override { - std::shared_ptr event = std::make_shared(result); + std::shared_ptr event = std::make_shared(); std::unique_lock lck(mtx_); events_.push_back(event); } - void OnUnsubscribeResult(NotificationConstant::SubscribeResult result) override + void OnDisconnected() override { - std::shared_ptr event = std::make_shared(result); + std::shared_ptr event = std::make_shared(); std::unique_lock lck(mtx_); events_.push_back(event); } @@ -357,8 +343,7 @@ public: { for (auto event : events) { if (event->GetType() == SubscriberEventType::ON_SUBSCRIBERESULT) { - std::shared_ptr ev = std::static_pointer_cast(event); - waitOnSubscriber_ = ev->GetSubscribeResult(); + waitOnSubscriber_ = true; } else if (event->GetType() == SubscriberEventType::ON_CONSUMED) { std::shared_ptr ev = std::static_pointer_cast(event); waitOnConsumed_ = true; @@ -381,9 +366,7 @@ public: onCanceledWithSortingMapSor_.push_back(ev->GetSortingMap()); onCanceledWithSortingMapDelRea_.push_back(ev->GetDeleteReason()); } else if (event->GetType() == SubscriberEventType::ON_UNSUBSCRIBERESULT) { - std::shared_ptr ev = - std::static_pointer_cast(event); - waitOnUnSubscriber_ = ev->GetUnSubscribeResult(); + waitOnUnSubscriber_ = true; } } } @@ -408,12 +391,12 @@ public: waitOnUnSubscriber_ = NotificationConstant::SubscribeResult::RESOURCES_FAIL; } - uint32_t getWaitOnSubscriber() + bool getWaitOnSubscriber() { return waitOnSubscriber_; } - uint32_t getWaitOnUnSubscriber() + bool getWaitOnUnSubscriber() { return waitOnUnSubscriber_; } @@ -474,8 +457,8 @@ public: } private: - uint32_t waitOnSubscriber_; - uint32_t waitOnUnSubscriber_; + bool waitOnSubscriber_ = false; + bool waitOnUnSubscriber_ = false; bool waitOnConsumed_ = false; std::vector> onConsumedReq_; std::vector> onConsumedWithSortingMapReq_; @@ -572,8 +555,8 @@ HWTEST_F(AnsFWModuleTest, ANS_FW_MT_FlowControl_00100, Function | MediumTest | L } EXPECT_EQ((uint32_t)eventParser.getOnConsumedReq().size(), MAX_ACTIVE_NUM_PERSECOND); EXPECT_EQ((uint32_t)eventParser.getOnConsumedWithSortingMapReq().size(), MAX_ACTIVE_NUM_PERSECOND); - EXPECT_EQ(eventParser.getWaitOnSubscriber(), NotificationConstant::SubscribeResult::SUCCESS); - EXPECT_EQ(eventParser.getWaitOnUnSubscriber(), NotificationConstant::SubscribeResult::SUCCESS); + EXPECT_TRUE(eventParser.getWaitOnSubscriber()); + EXPECT_TRUE(eventParser.getWaitOnUnSubscriber()); subscriber.ClearEvents(); SleepForFC(); } @@ -759,19 +742,17 @@ HWTEST_F(AnsFWModuleTest, ANS_FW_MT_UnSubscriber_00100, Function | MediumTest | EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), ERR_OK); SleepForFC(); std::list> events = subscriber.GetEvents(); - uint32_t waitOnSubscriber_; - uint32_t waitOnUnSubscriber_; + bool waitOnSubscriber = false; + bool waitOnUnSubscriber = false; for (auto event : events) { if (event->GetType() == SubscriberEventType::ON_SUBSCRIBERESULT) { - std::shared_ptr ev = std::static_pointer_cast(event); - waitOnSubscriber_ = ev->GetSubscribeResult(); + waitOnSubscriber = true; } else if (event->GetType() == SubscriberEventType::ON_UNSUBSCRIBERESULT) { - std::shared_ptr ev = std::static_pointer_cast(event); - waitOnUnSubscriber_ = ev->GetUnSubscribeResult(); + waitOnUnSubscriber = true; } } - EXPECT_EQ(waitOnSubscriber_, NotificationConstant::SubscribeResult::SUCCESS); + EXPECT_TRUE(waitOnSubscriber); subscriber.ClearEvents(); SleepForFC(); @@ -794,28 +775,29 @@ HWTEST_F(AnsFWModuleTest, ANS_FW_MT_UnSubscriber_00200, Function | MediumTest | EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), (int)ERR_OK); SleepForFC(); std::list> events = subscriber.GetEvents(); - uint32_t waitOnSubscriber_; - uint32_t waitOnUnSubscriber_; + bool waitOnSubscriber = false; + bool waitOnUnSubscriber = false; for (auto event : events) { if (event->GetType() == SubscriberEventType::ON_SUBSCRIBERESULT) { - std::shared_ptr ev = std::static_pointer_cast(event); - waitOnSubscriber_ = ev->GetSubscribeResult(); + waitOnSubscriber = true; } else if (event->GetType() == SubscriberEventType::ON_UNSUBSCRIBERESULT) { - std::shared_ptr ev = std::static_pointer_cast(event); - waitOnUnSubscriber_ = ev->GetUnSubscribeResult(); + waitOnUnSubscriber = true; } } - EXPECT_EQ(waitOnSubscriber_, NotificationConstant::SubscribeResult::SUCCESS); - waitOnSubscriber_ = NotificationConstant::SubscribeResult::PREMISSION_FAIL; - EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber, info), (int)ERR_OK); + EXPECT_TRUE(waitOnSubscriber); + EXPECT_TRUE(waitOnUnSubscriber); + + waitOnSubscriber = false; + waitOnUnSubscriber = false; + subscriber.ClearEvents(); + EXPECT_NE(NotificationHelper::UnSubscribeNotification(subscriber, info), (int)ERR_OK); events = subscriber.GetEvents(); for (auto event : events) { if (event->GetType() == SubscriberEventType::ON_UNSUBSCRIBERESULT) { - std::shared_ptr ev = std::static_pointer_cast(event); - waitOnUnSubscriber_ = ev->GetUnSubscribeResult(); + waitOnUnSubscriber = true; } } - EXPECT_EQ(waitOnUnSubscriber_, NotificationConstant::SubscribeResult::SUCCESS); + EXPECT_FALSE(waitOnUnSubscriber); subscriber.ClearEvents(); SleepForFC(); } @@ -833,18 +815,16 @@ HWTEST_F(AnsFWModuleTest, ANS_FW_MT_Subscriber_00100, Function | MediumTest | Le EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber, info), ERR_OK); SleepForFC(); std::list> events = subscriber.GetEvents(); - uint32_t waitOnSubscriber_; - uint32_t waitOnUnSubscriber_; + bool waitOnSubscriber = false; + bool waitOnUnSubscriber = false; for (auto event : events) { if (event->GetType() == SubscriberEventType::ON_SUBSCRIBERESULT) { - std::shared_ptr ev = std::static_pointer_cast(event); - waitOnSubscriber_ = ev->GetSubscribeResult(); + waitOnSubscriber = true; } else if (event->GetType() == SubscriberEventType::ON_UNSUBSCRIBERESULT) { - std::shared_ptr ev = std::static_pointer_cast(event); - waitOnUnSubscriber_ = ev->GetUnSubscribeResult(); + waitOnUnSubscriber = true; } } - EXPECT_EQ(waitOnSubscriber_, NotificationConstant::SubscribeResult::SUCCESS); + EXPECT_TRUE(waitOnSubscriber); subscriber.ClearEvents(); SleepForFC(); } 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 a1ba734cdf57aff72cc350ba28e1de89c3c47787..666e8d3a8191f487d87c1b9c37b26a2a0cff58c4 100644 --- a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp +++ b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp @@ -72,12 +72,12 @@ const std::string CLASSIFICATION_ALARM{"alarm"}; class TestAnsSubscriber : public NotificationSubscriber { public: - virtual void OnSubscribeResult(NotificationConstant::SubscribeResult result) override + virtual void OnConnected() override { g_subscribe_mtx.unlock(); } - virtual void OnUnsubscribeResult(NotificationConstant::SubscribeResult result) override + virtual void OnDisconnected() override { g_unsubscribe_mtx.unlock(); } @@ -93,6 +93,7 @@ public: virtual void OnCanceled(const std::shared_ptr &request) override { + GTEST_LOG_(INFO) << "ANS_Interface_MT::OnCanceled request : " << request->GetNotificationRequest().Dump(); OnCanceledReceived = true; } @@ -1204,4 +1205,4 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_GetActiveNotifications_ OnCanceledReceived = false; } } // namespace Notification -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/interfaces/innerkits/ans/native/include/notification_helper.h b/interfaces/innerkits/ans/native/include/notification_helper.h index b6b5fbf65936495fce013a40afe962384a32b5bf..83c8717cebe425a36571179ce4b7553bc41e0d58 100644 --- a/interfaces/innerkits/ans/native/include/notification_helper.h +++ b/interfaces/innerkits/ans/native/include/notification_helper.h @@ -588,8 +588,7 @@ public: */ static ErrCode GetDisturbMode(NotificationConstant::DisturbMode &disturbMode); }; - } // namespace Notification } // namespace OHOS -#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_HELPER_H \ No newline at end of file +#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_HELPER_H diff --git a/interfaces/innerkits/ans/native/include/notification_request.h b/interfaces/innerkits/ans/native/include/notification_request.h index 1536f675bc07ebc95ed98951705f4a5def950195..46261862f94072abe9a1615b1dcfcf3b0bb38804 100644 --- a/interfaces/innerkits/ans/native/include/notification_request.h +++ b/interfaces/innerkits/ans/native/include/notification_request.h @@ -325,7 +325,7 @@ public: /** * Adds a NotificationActionButton to this notification. - * An operation button is usually placed next to the notification content by the system. + * An operation button is usually placed next to the notification content by the system. * Each action button must contain an icon, a title, and an WantAgent. When a notification is expanded, * a maximum of three action buttons can be displayed from left to right in the order they were added. * When the notification is collapsed, no action buttons will be displayed. diff --git a/interfaces/innerkits/ans/native/include/notification_subscriber.h b/interfaces/innerkits/ans/native/include/notification_subscriber.h index 6cbdecc3e3d64e0801dd1ca3f028fd451a4bca61..2e60fdbf2aa88195f38588dfd26a061ecbdebe82 100644 --- a/interfaces/innerkits/ans/native/include/notification_subscriber.h +++ b/interfaces/innerkits/ans/native/include/notification_subscriber.h @@ -59,11 +59,9 @@ public: const std::shared_ptr &sortingMap, int deleteReason) = 0; /** - * @brief Called back when the subscriber is connected to the Advanced Notification Service (ANS). - * - * @param result Indicates subscribe result. + * Called back when the subscriber is connected to the Advanced Notification Service (ANS). **/ - virtual void OnSubscribeResult(NotificationConstant::SubscribeResult result) = 0; + virtual void OnConnected() = 0; /** * @brief Called back when the subscriber receives a new notification. @@ -83,11 +81,9 @@ public: const std::shared_ptr &request, const std::shared_ptr &sortingMap) = 0; /** - * @brief Called back when the subscriber is disconnected from the ANS. - * - * @param result Indicates unsubscribe result. + * Called back when the subscriber is disconnected from the ANS. **/ - virtual void OnUnsubscribeResult(NotificationConstant::SubscribeResult result) = 0; + virtual void OnDisconnected() = 0; /** * @brief Called back when the ranking information about the current notification changes. @@ -127,9 +123,9 @@ private: SubscriberImpl(NotificationSubscriber &subscriber); ~SubscriberImpl(){}; - void OnSubscribeResult(NotificationConstant::SubscribeResult result) override; + void OnConnected() override; - void OnUnsubscribeResult(NotificationConstant::SubscribeResult result) override; + void OnDisconnected() override; void OnConsumed(const sptr ¬ification) override; @@ -165,4 +161,4 @@ private: } // namespace Notification } // namespace OHOS -#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_SUBSCRIBER_H \ No newline at end of file +#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_SUBSCRIBER_H diff --git a/interfaces/kits/napi/ans/include/common.h b/interfaces/kits/napi/ans/include/common.h index 8cd32ef3aa549a898d1ddc18cc1e553604ba6f27..2482eeeb7128e557ebe796fbe8c92d0fcfcb891e 100644 --- a/interfaces/kits/napi/ans/include/common.h +++ b/interfaces/kits/napi/ans/include/common.h @@ -132,6 +132,9 @@ public: static void SetCallback( const napi_env &env, const napi_ref &callbackIn, const int &errorCode, const napi_value &result); + static void SetCallback( + const napi_env &env, const napi_ref &callbackIn, const napi_value &result); + static void SetPromise( const napi_env &env, const napi_deferred &deferred, const int &errorCode, const napi_value &result); @@ -214,6 +217,7 @@ public: static napi_value CreateWantAgentByJS(const napi_env &env, const std::shared_ptr &agent); private: + static const int ARGS_ONE = 1; static const int ARGS_TWO = 2; static const int PARAM0 = 0; static const int PARAM1 = 1; @@ -223,4 +227,4 @@ private: } // namespace NotificationNapi } // namespace OHOS -#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NAPI_INCLUDE_COMMON_H \ No newline at end of file +#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NAPI_INCLUDE_COMMON_H diff --git a/interfaces/kits/napi/ans/include/subscribe.h b/interfaces/kits/napi/ans/include/subscribe.h index 5046928a0aec27bf2f03e92eac04b0e508a5608c..f02c8545ad167231c043a8c4f5842420b74c0938 100644 --- a/interfaces/kits/napi/ans/include/subscribe.h +++ b/interfaces/kits/napi/ans/include/subscribe.h @@ -39,9 +39,9 @@ public: virtual void OnUpdate(const std::shared_ptr &sortingMap) override; - virtual void OnSubscribeResult(NotificationConstant::SubscribeResult result) override; + virtual void OnConnected() override; - virtual void OnUnsubscribeResult(NotificationConstant::SubscribeResult result) override; + virtual void OnDisconnected() override; virtual void OnDied() override; @@ -95,4 +95,4 @@ napi_value Subscribe(napi_env env, napi_callback_info info); } // namespace NotificationNapi } // namespace OHOS -#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NAPI_INCLUDE_SUBSCRIBE_H \ No newline at end of file +#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NAPI_INCLUDE_SUBSCRIBE_H diff --git a/interfaces/kits/napi/ans/src/cancel.cpp b/interfaces/kits/napi/ans/src/cancel.cpp index 71ea420be9f92fd8b9f260852d8a32438d8626bf..4df187ab00c5fc4a1ccda0acc2d0baffca6d6d75 100644 --- a/interfaces/kits/napi/ans/src/cancel.cpp +++ b/interfaces/kits/napi/ans/src/cancel.cpp @@ -77,7 +77,7 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, return Common::NapiGetNull(env); } -napi_value ParseParametersByCancelAll(const napi_env &env, const napi_callback_info &info, napi_ref &callback) +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, napi_ref &callback) { ANS_LOGI("enter"); @@ -159,7 +159,7 @@ napi_value CancelAll(napi_env env, napi_callback_info info) ANS_LOGI("enter"); napi_ref callback = nullptr; - if (ParseParametersByCancelAll(env, info, callback) == nullptr) { + if (ParseParameters(env, info, callback) == nullptr) { return Common::NapiGetUndefined(env); } @@ -211,4 +211,4 @@ napi_value CancelAll(napi_env env, napi_callback_info info) } } // namespace NotificationNapi -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/interfaces/kits/napi/ans/src/common.cpp b/interfaces/kits/napi/ans/src/common.cpp index 650e0935ea1ba36fca49c9a41a71473d03abed49..f27f0f31de63c25da792b9c4bd647c16c047cc56 100644 --- a/interfaces/kits/napi/ans/src/common.cpp +++ b/interfaces/kits/napi/ans/src/common.cpp @@ -160,6 +160,20 @@ void Common::SetCallback( ANS_LOGI("end"); } +void Common::SetCallback( + const napi_env &env, const napi_ref &callbackIn, const napi_value &result) +{ + ANS_LOGI("enter"); + napi_value undefined = nullptr; + napi_get_undefined(env, &undefined); + + napi_value callback = nullptr; + napi_value resultout = nullptr; + napi_get_reference_value(env, callbackIn, &callback); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_ONE, &result, &resultout)); + ANS_LOGI("end"); +} + void Common::SetPromise( const napi_env &env, const napi_deferred &deferred, const int &errorCode, const napi_value &result) { diff --git a/interfaces/kits/napi/ans/src/remove.cpp b/interfaces/kits/napi/ans/src/remove.cpp index 90c71f2f2cb521612b05eb5fb290c2cdfc75d9c5..692d0305bbb830089173af918bfbbe2ced9e3a90 100644 --- a/interfaces/kits/napi/ans/src/remove.cpp +++ b/interfaces/kits/napi/ans/src/remove.cpp @@ -274,6 +274,5 @@ napi_value RemoveAll(napi_env env, napi_callback_info info) return promise; } } - } // namespace NotificationNapi } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/ans/src/subscribe.cpp b/interfaces/kits/napi/ans/src/subscribe.cpp index 4edb90d995aef25473cef75ca58a3dbe77b345e4..db783a02149a9d01f9b91759f61d268bc063703d 100644 --- a/interfaces/kits/napi/ans/src/subscribe.cpp +++ b/interfaces/kits/napi/ans/src/subscribe.cpp @@ -141,6 +141,38 @@ SubscriberInstance::~SubscriberInstance() void SubscriberInstance::OnCanceled(const std::shared_ptr &request) {} +void UvQueueWorkOnCanceled(uv_work_t *work, int status) +{ + ANS_LOGI("OnCanceled uv_work_t start"); + + if (work == nullptr) { + ANS_LOGE("work is null"); + return; + } + + NotificationReceiveDataWorker *dataWorkerData = (NotificationReceiveDataWorker *)work->data; + if (dataWorkerData == nullptr) { + ANS_LOGE("dataWorkerData is null"); + return; + } + napi_value result = nullptr; + napi_create_object(dataWorkerData->env, &result); + if (!SetSubscribeCallbackData(dataWorkerData->env, + dataWorkerData->request, + dataWorkerData->sortingMap, + dataWorkerData->deleteReason, + result)) { + ANS_LOGE("Failed to convert data to JS"); + } else { + Common::SetCallback(dataWorkerData->env, dataWorkerData->ref, result); + } + + delete dataWorkerData; + dataWorkerData = nullptr; + delete work; + work = nullptr; +} + void SubscriberInstance::OnCanceled(const std::shared_ptr &request, const std::shared_ptr &sortingMap, int deleteReason) { @@ -194,44 +226,44 @@ void SubscriberInstance::OnCanceled(const std::shared_ptrdata; - if (dataWorkerData == nullptr) { - ANS_LOGE("dataWorkerData is null"); - return; - } - napi_value result = nullptr; - napi_create_object(dataWorkerData->env, &result); - int error = 0; - if (!SetSubscribeCallbackData(dataWorkerData->env, - dataWorkerData->request, - dataWorkerData->sortingMap, - dataWorkerData->deleteReason, - result)) { - result = Common::NapiGetNull(dataWorkerData->env); - error = ERROR; - } else { - error = NO_ERROR; - } - Common::SetCallback(dataWorkerData->env, dataWorkerData->ref, error, result); - - delete dataWorkerData; - dataWorkerData = nullptr; - delete work; - work = nullptr; - }); + UvQueueWorkOnCanceled); } void SubscriberInstance::OnConsumed(const std::shared_ptr &request) {} +void UvQueueWorkOnConsumed(uv_work_t *work, int status) +{ + ANS_LOGI("OnConsumed uv_work_t start"); + + if (work == nullptr) { + ANS_LOGE("work is null"); + return; + } + + NotificationReceiveDataWorker *dataWorkerData = (NotificationReceiveDataWorker *)work->data; + if (dataWorkerData == nullptr) { + ANS_LOGE("dataWorkerData is null"); + return; + } + napi_value result = nullptr; + napi_create_object(dataWorkerData->env, &result); + if (!SetSubscribeCallbackData(dataWorkerData->env, + dataWorkerData->request, + dataWorkerData->sortingMap, + NO_DELETE_REASON, + result)) { + ANS_LOGE("Failed to convert data to JS"); + } else { + Common::SetCallback(dataWorkerData->env, dataWorkerData->ref, result); + } + + delete dataWorkerData; + dataWorkerData = nullptr; + delete work; + work = nullptr; +} + void SubscriberInstance::OnConsumed(const std::shared_ptr &request, const std::shared_ptr &sortingMap) { @@ -283,39 +315,35 @@ void SubscriberInstance::OnConsumed(const std::shared_ptrdata; - if (dataWorkerData == nullptr) { - ANS_LOGE("dataWorkerData is null"); - return; - } - napi_value result = nullptr; - napi_create_object(dataWorkerData->env, &result); - int error = 0; - if (!SetSubscribeCallbackData(dataWorkerData->env, - dataWorkerData->request, - dataWorkerData->sortingMap, - NO_DELETE_REASON, - result)) { - result = Common::NapiGetNull(dataWorkerData->env); - error = ERROR; - } else { - error = NO_ERROR; - } - Common::SetCallback(dataWorkerData->env, dataWorkerData->ref, error, result); + if (work == nullptr) { + ANS_LOGE("work is null"); + return; + } - delete dataWorkerData; - dataWorkerData = nullptr; - delete work; - work = nullptr; - }); + NotificationReceiveDataWorker *dataWorkerData = (NotificationReceiveDataWorker *)work->data; + if (dataWorkerData == nullptr) { + ANS_LOGE("dataWorkerData is null"); + return; + } + napi_value result = nullptr; + napi_create_object(dataWorkerData->env, &result); + if (!Common::SetNotificationSortingMap(dataWorkerData->env, dataWorkerData->sortingMap, result)) { + ANS_LOGE("Failed to convert data to JS"); + } else { + Common::SetCallback(dataWorkerData->env, dataWorkerData->ref, result); + } + + delete dataWorkerData; + dataWorkerData = nullptr; + delete work; + work = nullptr; } void SubscriberInstance::OnUpdate(const std::shared_ptr &sortingMap) @@ -361,38 +389,10 @@ void SubscriberInstance::OnUpdate(const std::shared_ptr uv_queue_work(loop, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - ANS_LOGI("OnUpdate uv_work_t start"); - - if (work == nullptr) { - ANS_LOGE("work is null"); - return; - } - - NotificationReceiveDataWorker *dataWorkerData = (NotificationReceiveDataWorker *)work->data; - if (dataWorkerData == nullptr) { - ANS_LOGE("dataWorkerData is null"); - return; - } - napi_value result = nullptr; - napi_create_object(dataWorkerData->env, &result); - int error = 0; - if (!Common::SetNotificationSortingMap(dataWorkerData->env, dataWorkerData->sortingMap, result)) { - result = Common::NapiGetNull(dataWorkerData->env); - error = ERROR; - } else { - error = NO_ERROR; - } - Common::SetCallback(dataWorkerData->env, dataWorkerData->ref, error, result); - - delete dataWorkerData; - dataWorkerData = nullptr; - delete work; - work = nullptr; - }); + UvQueueWorkOnUpdate); } -void SubscriberInstance::OnSubscribeResult(NotificationConstant::SubscribeResult result) +void SubscriberInstance::OnConnected() { ANS_LOGI("enter"); @@ -400,7 +400,6 @@ void SubscriberInstance::OnSubscribeResult(NotificationConstant::SubscribeResult ANS_LOGI("subscribe callback unset"); return; } - ANS_LOGI("OnSubscribeResult result = %{public}d", result); uv_loop_s *loop = nullptr; napi_get_uv_event_loop(subscribeCallbackInfo_.env, &loop); @@ -415,7 +414,6 @@ void SubscriberInstance::OnSubscribeResult(NotificationConstant::SubscribeResult return; } - dataWorker->result = (int)result; dataWorker->env = subscribeCallbackInfo_.env; dataWorker->ref = subscribeCallbackInfo_.ref; @@ -443,11 +441,8 @@ void SubscriberInstance::OnSubscribeResult(NotificationConstant::SubscribeResult ANS_LOGE("dataWorkerData is null"); return; } - // result: number - napi_value result = nullptr; - napi_create_int32(dataWorkerData->env, dataWorkerData->result, &result); - Common::SetCallback(dataWorkerData->env, dataWorkerData->ref, NO_ERROR, result); + Common::SetCallback(dataWorkerData->env, dataWorkerData->ref, Common::NapiGetNull(dataWorkerData->env)); delete dataWorkerData; dataWorkerData = nullptr; @@ -456,7 +451,7 @@ void SubscriberInstance::OnSubscribeResult(NotificationConstant::SubscribeResult }); } -void SubscriberInstance::OnUnsubscribeResult(NotificationConstant::SubscribeResult result) +void SubscriberInstance::OnDisconnected() { ANS_LOGI("enter"); @@ -464,7 +459,6 @@ void SubscriberInstance::OnUnsubscribeResult(NotificationConstant::SubscribeResu ANS_LOGI("unsubscribe callback unset"); return; } - ANS_LOGI("OnUnsubscribeResult result = %{public}d", result); uv_loop_s *loop = nullptr; napi_get_uv_event_loop(unsubscribeCallbackInfo_.env, &loop); @@ -479,7 +473,6 @@ void SubscriberInstance::OnUnsubscribeResult(NotificationConstant::SubscribeResu return; } - dataWorker->result = (int)result; dataWorker->env = unsubscribeCallbackInfo_.env; dataWorker->ref = unsubscribeCallbackInfo_.ref; dataWorker->subscriber = this; @@ -508,11 +501,8 @@ void SubscriberInstance::OnUnsubscribeResult(NotificationConstant::SubscribeResu ANS_LOGE("dataWorkerData is null"); return; } - // result: number - napi_value result = nullptr; - napi_create_int32(dataWorkerData->env, dataWorkerData->result, &result); - Common::SetCallback(dataWorkerData->env, dataWorkerData->ref, NO_ERROR, result); + Common::SetCallback(dataWorkerData->env, dataWorkerData->ref, Common::NapiGetNull(dataWorkerData->env)); DelSubscriberInstancesInfo(dataWorkerData->env, dataWorkerData->subscriber); @@ -574,7 +564,7 @@ void SubscriberInstance::OnDied() } Common::SetCallback( - dataWorkerData->env, dataWorkerData->ref, NO_ERROR, Common::NapiGetNull(dataWorkerData->env)); + dataWorkerData->env, dataWorkerData->ref, Common::NapiGetNull(dataWorkerData->env)); delete dataWorkerData; dataWorkerData = nullptr; @@ -584,68 +574,7 @@ void SubscriberInstance::OnDied() } void SubscriberInstance::OnDisturbModeChanged(int disturbMode) -{ - ANS_LOGI("enter"); - - if (disturbModeCallbackInfo_.ref == nullptr) { - ANS_LOGI("disturbModeChange callback unset"); - return; - } - ANS_LOGI("OnDisturbModeChanged disturbMode = %{public}d", disturbMode); - - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(disturbModeCallbackInfo_.env, &loop); - if (loop == nullptr) { - ANS_LOGE("loop instance is nullptr"); - return; - } - - NotificationReceiveDataWorker *dataWorker = new (std::nothrow) NotificationReceiveDataWorker(); - if (dataWorker == nullptr) { - ANS_LOGE("new dataWorker failed"); - return; - } - - dataWorker->disturbMode = disturbMode; - dataWorker->env = disturbModeCallbackInfo_.env; - dataWorker->ref = disturbModeCallbackInfo_.ref; - - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - ANS_LOGE("new work failed"); - return; - } - - work->data = (void *)dataWorker; - - uv_queue_work(loop, - work, - [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - ANS_LOGI("OnDisturbModeChanged uv_work_t start"); - - if (work == nullptr) { - ANS_LOGE("work is null"); - return; - } - - NotificationReceiveDataWorker *dataWorkerData = (NotificationReceiveDataWorker *)work->data; - if (dataWorkerData == nullptr) { - ANS_LOGE("dataWorkerData is null"); - return; - } - // disturbMode: number - napi_value result = nullptr; - napi_create_int32(dataWorkerData->env, dataWorkerData->disturbMode, &result); - - Common::SetCallback(dataWorkerData->env, dataWorkerData->ref, NO_ERROR, result); - - delete dataWorkerData; - dataWorkerData = nullptr; - delete work; - work = nullptr; - }); -} +{} void SubscriberInstance::SetCancelCallbackInfo(const napi_env &env, const napi_ref &ref) { @@ -989,4 +918,4 @@ napi_value Subscribe(napi_env env, napi_callback_info info) } } // namespace NotificationNapi -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/ans/include/notification_subscriber_manager.h b/services/ans/include/notification_subscriber_manager.h index 0e1ff2c115d90f61715a707760247871abc5e259..4377c1f4187963067a65e97c635aec15753a0b53 100644 --- a/services/ans/include/notification_subscriber_manager.h +++ b/services/ans/include/notification_subscriber_manager.h @@ -57,9 +57,9 @@ private: void AddRecordInfo(std::shared_ptr &record, const sptr &subscribeInfo); void RemoveRecordInfo( std::shared_ptr &record, const sptr &subscribeInfo); - void AddSubscriberInner( + ErrCode AddSubscriberInner( const sptr &subscriber, const sptr &subscribeInfo); - void RemoveSubscriberInner( + ErrCode RemoveSubscriberInner( const sptr &subscriber, const sptr &subscribeInfo); void NotifyConsumedInner( @@ -83,4 +83,4 @@ private: } // namespace Notification } // namespace OHOS -#endif // BASE_NOTIFICATION_ANS_STANDARD_SERVICES_NOTIFICATIONMGR_INCLUDE_NOTIFICATION_SUBSCRIBER_MANAGER_H \ No newline at end of file +#endif // BASE_NOTIFICATION_ANS_STANDARD_SERVICES_NOTIFICATIONMGR_INCLUDE_NOTIFICATION_SUBSCRIBER_MANAGER_H diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 3d8eb02a93fee674458a9dbcc7de388d9e6f4e3b..87ef67aa3794f043909f20e92b59ef7a10bbda23 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -961,8 +961,13 @@ ErrCode AdvancedNotificationService::GetShowBadgeEnabledForBundle( } ErrCode result = ERR_OK; - handler_->PostSyncTask( - std::bind([&]() { result = NotificationPreferences::GetInstance().IsShowBadge(bundle, enabled); })); + handler_->PostSyncTask(std::bind([&]() { + result = NotificationPreferences::GetInstance().IsShowBadge(bundle, enabled); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + enabled = false; + } + })); return result; } @@ -1045,11 +1050,11 @@ ErrCode AdvancedNotificationService::Unsubscribe( ANS_LOGD("%{public}s", __FUNCTION__); if (subscriber == nullptr) { - ANS_LOGE("Client is not a system app"); return ERR_ANS_INVALID_PARAM; } if (!IsSystemApp()) { + ANS_LOGE("Client is not a system app"); return ERR_ANS_NON_SYSTEM_APP; } diff --git a/services/ans/src/notification_subscriber_manager.cpp b/services/ans/src/notification_subscriber_manager.cpp index fedc523f82bbe35a6a536534d1cbe9e7bcea6811..5f0eadd34e98b6cbfad655ae5207fabfbb3202c2 100644 --- a/services/ans/src/notification_subscriber_manager.cpp +++ b/services/ans/src/notification_subscriber_manager.cpp @@ -53,13 +53,12 @@ ErrCode NotificationSubscriberManager::AddSubscriber( return ERR_ANS_INVALID_PARAM; } - AppExecFwk::EventHandler::Callback addSubscriberFunc = - std::bind(&NotificationSubscriberManager::AddSubscriberInner, this, subscriber, subscribeInfo); - - if (!handler_->PostTask(addSubscriberFunc, AppExecFwk::EventQueue::Priority::HIGH)) { - return ERR_ANS_TASK_ERR; - } - return ERR_OK; + ErrCode result = ERR_ANS_TASK_ERR; + handler_->PostSyncTask(std::bind([this, &subscriber, &subscribeInfo, &result]() { + result = this->AddSubscriberInner(subscriber, subscribeInfo); + }), + AppExecFwk::EventQueue::Priority::HIGH); + return result; } ErrCode NotificationSubscriberManager::RemoveSubscriber( @@ -70,13 +69,12 @@ ErrCode NotificationSubscriberManager::RemoveSubscriber( return ERR_ANS_INVALID_PARAM; } - AppExecFwk::EventHandler::Callback removeSubscriberFunc = - std::bind(&NotificationSubscriberManager::RemoveSubscriberInner, this, subscriber, subscribeInfo); - - if (!handler_->PostTask(removeSubscriberFunc, AppExecFwk::EventQueue::Priority::HIGH)) { - return ERR_ANS_TASK_ERR; - } - return ERR_OK; + ErrCode result = ERR_ANS_TASK_ERR; + handler_->PostSyncTask(std::bind([this, &subscriber, &subscribeInfo, &result]() { + result = this->RemoveSubscriberInner(subscriber, subscribeInfo); + }), + AppExecFwk::EventQueue::Priority::HIGH); + return result; } void NotificationSubscriberManager::NotifyConsumed( @@ -214,7 +212,7 @@ void NotificationSubscriberManager::RemoveRecordInfo( } } -void NotificationSubscriberManager::AddSubscriberInner( +ErrCode NotificationSubscriberManager::AddSubscriberInner( const sptr &subscriber, const sptr &subscribeInfo) { std::shared_ptr record = FindSubscriberRecord(subscriber); @@ -223,31 +221,29 @@ void NotificationSubscriberManager::AddSubscriberInner( record = CreateSubscriberRecord(subscriber); if (record == nullptr) { ANS_LOGE("CreateSubscriberRecord failed."); - subscriber->OnSubscribeResult(NotificationConstant::RESOURCES_FAIL); - return; + return ERR_ANS_NO_MEMORY; } subscriberRecordList_.push_back(record); record->subscriber->AsObject()->AddDeathRecipient(recipient_); - record->subscriber->OnSubscribeResult(NotificationConstant::SUCCESS); + record->subscriber->OnConnected(); ANS_LOGI("subscriber is connected."); } AddRecordInfo(record, subscribeInfo); - return; + return ERR_OK; } -void NotificationSubscriberManager::RemoveSubscriberInner( +ErrCode NotificationSubscriberManager::RemoveSubscriberInner( const sptr &subscriber, const sptr &subscribeInfo) { std::shared_ptr record = FindSubscriberRecord(subscriber); if (record == nullptr) { ANS_LOGE("subscriber not found."); - subscriber->OnUnsubscribeResult(NotificationConstant::SUCCESS); - return; + return ERR_ANS_INVALID_PARAM; } RemoveRecordInfo(record, subscribeInfo); @@ -257,11 +253,11 @@ void NotificationSubscriberManager::RemoveSubscriberInner( subscriberRecordList_.remove(record); - record->subscriber->OnUnsubscribeResult(NotificationConstant::SUCCESS); + record->subscriber->OnDisconnected(); ANS_LOGI("subscriber is disconnected."); } - return; + return ERR_OK; } void NotificationSubscriberManager::NotifyConsumedInner( @@ -302,10 +298,8 @@ void NotificationSubscriberManager::NotifyUpdatedInner(const sptrsubscriber->OnDisturbModeChanged(mode); } } - } // namespace Notification } // namespace OHOS diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index b54d4b57f5205b39505d2ce2827c520cd2020e6e..e2514e0e0d10e8f7d9b3352c75a777c70c4a6207 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -80,9 +80,9 @@ inline void SleepForFC() class TestAnsSubscriber : public NotificationSubscriber { public: - void OnSubscribeResult(NotificationConstant::SubscribeResult result) override + void OnConnected() override {} - void OnUnsubscribeResult(NotificationConstant::SubscribeResult result) override + void OnDisconnected() override {} void OnDied() override {} @@ -618,14 +618,18 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_02700, /** * @tc.number : AdvancedNotificationServiceTest_02800 * @tc.name : AMS_ANS_GetShowBadgeEnabledForBundle_0100 - * @tc.desc : Test GetShowBadgeEnabledForBundle function when no bundle + * @tc.desc : Test GetShowBadgeEnabledForBundle function */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_02800, Function | SmallTest | Level1) { + EXPECT_EQ(advancedNotificationService_->SetShowBadgeEnabledForBundle( + new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID), true), + (int)ERR_OK); bool allow = false; EXPECT_EQ((int)advancedNotificationService_->GetShowBadgeEnabledForBundle( new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID), allow), - (int)ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST); + (int)ERR_OK); + EXPECT_TRUE(allow); } /** @@ -1068,7 +1072,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_06900, bool allow = false; EXPECT_EQ((int)advancedNotificationService_->GetShowBadgeEnabledForBundle( new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID), allow), - (int)ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST); + (int)ERR_OK); } /** diff --git a/services/ans/test/unittest/notification_subscriber_manager_test.cpp b/services/ans/test/unittest/notification_subscriber_manager_test.cpp index 2c62f96f8bb643e9d1c1268b4736992d4db5c8d6..0428f0f357fa4b4111033c2f04324183fd422f6e 100644 --- a/services/ans/test/unittest/notification_subscriber_manager_test.cpp +++ b/services/ans/test/unittest/notification_subscriber_manager_test.cpp @@ -36,9 +36,9 @@ public: private: class TestAnsSubscriber : public NotificationSubscriber { public: - void OnSubscribeResult(NotificationConstant::SubscribeResult result) override + void OnConnected() override {} - void OnUnsubscribeResult(NotificationConstant::SubscribeResult result) override + void OnDisconnected() override {} void OnDied() override {} @@ -189,4 +189,4 @@ HWTEST_F(NotificationSubscriberManagerTest, NotificationSubscriberManagerTest_00 EXPECT_EQ(notificationSubscriberManager_->RemoveSubscriber(nullptr, info), (int)ERR_ANS_INVALID_PARAM); } } // namespace Notification -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/test/moduletest/ans_module_test.cpp b/services/test/moduletest/ans_module_test.cpp index 52d333eb065fdc448089b30c0b68af9398b64d14..1a76cf30e5f5cd9db2054f54084ed3fbffcb7bda 100644 --- a/services/test/moduletest/ans_module_test.cpp +++ b/services/test/moduletest/ans_module_test.cpp @@ -35,16 +35,16 @@ class TestAnsSubscriber : public NotificationSubscriber { public: ~TestAnsSubscriber(){}; - void OnSubscribeResult(NotificationConstant::SubscribeResult result) override + void OnConnected() override { if (subscriberCb_ != nullptr) { - subscriberCb_(result); + subscriberCb_(); } } - void OnUnsubscribeResult(NotificationConstant::SubscribeResult result) override + void OnDisconnected() override { if (unSubscriberCb_ != nullptr) { - unSubscriberCb_(result); + unSubscriberCb_(); } } void OnDied() override @@ -74,8 +74,8 @@ public: ConsumedFunc consumedCb_ = nullptr; CanceledFunc canceledCb_ = nullptr; - std::function unSubscriberCb_ = nullptr; - std::function subscriberCb_ = nullptr; + std::function unSubscriberCb_ = nullptr; + std::function subscriberCb_ = nullptr; }; class AnsModuleTest : public testing::Test { @@ -1465,7 +1465,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0064, Function | SmallTest | Level1) sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); - subscriber->unSubscriberCb_ = [](NotificationConstant::SubscribeResult) { passed = true; }; + subscriber->unSubscriberCb_ = []() { passed = true; }; g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), subscriberInfo); EXPECT_TRUE(passed); } @@ -1480,7 +1480,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0065, Function | SmallTest | Level1) // subscriber auto subscriber = new TestAnsSubscriber(); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), nullptr); - subscriber->unSubscriberCb_ = [](NotificationConstant::SubscribeResult) { passed = true; }; + subscriber->unSubscriberCb_ = []() { passed = true; }; g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), nullptr); EXPECT_TRUE(passed); } @@ -1848,7 +1848,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0110, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); - subscriber->unSubscriberCb_ = [](NotificationConstant::SubscribeResult) { passed = true; }; + subscriber->unSubscriberCb_ = []() { passed = true; }; g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); // unsubscriber @@ -1867,7 +1867,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0111, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); - subscriber->subscriberCb_ = [](NotificationConstant::SubscribeResult) { passed = true; }; + subscriber->subscriberCb_ = []() { passed = true; }; g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), subscriberInfo); EXPECT_EQ(passed, true);