From 7b705e49961468677a76e98620acd656826ecd6a Mon Sep 17 00:00:00 2001 From: njupthan Date: Tue, 18 Jan 2022 16:44:26 +0800 Subject: [PATCH 1/2] Distributed reminder policy Signed-off-by: njupthan --- .../core/common/include/ans_const_define.h | 5 + .../ans/core/include/ans_manager_interface.h | 2 + .../ans/core/include/ans_manager_proxy.h | 1 + .../ans/core/include/ans_manager_stub.h | 2 + .../ans/core/include/ans_notification.h | 9 + frameworks/ans/core/src/ans_manager_proxy.cpp | 34 +++ frameworks/ans/core/src/ans_manager_stub.cpp | 26 ++ frameworks/ans/core/src/ans_notification.cpp | 10 + frameworks/ans/native/BUILD.gn | 6 + frameworks/ans/native/src/notification.cpp | 20 ++ .../ans/native/src/notification_content.cpp | 1 - .../ans/native/src/notification_helper.cpp | 5 + .../test/moduletest/ans_fw_module_test.cpp | 248 ++++++++++++++++++ .../ans_innerkits_module_publish_test.cpp | 1 - .../ans/native/include/notification.h | 8 + .../native/include/notification_constant.h | 14 + .../ans/native/include/notification_helper.h | 9 + interfaces/kits/napi/ans/include/common.h | 9 + interfaces/kits/napi/ans/include/constant.h | 1 + .../kits/napi/ans/include/distributed.h | 2 + interfaces/kits/napi/ans/src/common.cpp | 30 +++ interfaces/kits/napi/ans/src/constant.cpp | 21 ++ interfaces/kits/napi/ans/src/distributed.cpp | 83 ++++++ interfaces/kits/napi/ans/src/init.cpp | 1 + .../include/advanced_notification_service.h | 7 + services/ans/include/interface_system_event.h | 4 + .../ans/src/advanced_notification_service.cpp | 85 ++++++ services/ans/src/system_event_observer.cpp | 14 + services/distributed/BUILD.gn | 1 + .../distributed_screen_status_manager.h | 64 +++++ .../src/distributed_screen_status_manager.cpp | 237 +++++++++++++++++ services/distributed/test/unittest/BUILD.gn | 2 + ...distributed_screen_status_manager_test.cpp | 70 +++++ .../unittest/mock/mock_changeNotification.cpp | 57 ---- .../test/unittest/mock/mock_event_runner.cpp | 2 +- 35 files changed, 1031 insertions(+), 60 deletions(-) create mode 100644 services/distributed/include/distributed_screen_status_manager.h create mode 100644 services/distributed/src/distributed_screen_status_manager.cpp create mode 100644 services/distributed/test/unittest/distributed_screen_status_manager_test.cpp delete mode 100644 services/distributed/test/unittest/mock/mock_changeNotification.cpp diff --git a/frameworks/ans/core/common/include/ans_const_define.h b/frameworks/ans/core/common/include/ans_const_define.h index a38245ac7..7d474f2d5 100644 --- a/frameworks/ans/core/common/include/ans_const_define.h +++ b/frameworks/ans/core/common/include/ans_const_define.h @@ -46,6 +46,11 @@ const static std::vector DEFAULT_NOTIFICATION_VIBRATION = {200}; // Default path for template const static std::string DEFAULT_TEMPLATE_PATH("/system/etc/external.json"); + +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED +constexpr NotificationConstant::DistributedReminderPolicy DEFAULT_DISTRIBUTED_REMINDER_POLICY = + NotificationConstant::DistributedReminderPolicy::DEFAULT; +#endif } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/core/include/ans_manager_interface.h b/frameworks/ans/core/include/ans_manager_interface.h index 76a9ebc97..f2e65ab62 100644 --- a/frameworks/ans/core/include/ans_manager_interface.h +++ b/frameworks/ans/core/include/ans_manager_interface.h @@ -116,6 +116,7 @@ public: virtual ErrCode EnableDistributedByBundle(const sptr &bundleOption, bool enabled) = 0; virtual ErrCode EnableDistributedSelf(bool enabled) = 0; virtual ErrCode IsDistributedEnableByBundle(const sptr &bundleOption, bool &enabled) = 0; + virtual ErrCode GetDeviceRemindType(NotificationConstant::RemindType &remindType) = 0; virtual ErrCode ShellDump(const std::string &dumpOption, std::vector &dumpInfo) = 0; virtual ErrCode PublishContinuousTaskNotification(const sptr &request) = 0; @@ -186,6 +187,7 @@ protected: ENABLE_DISTRIBUTED_BY_BUNDLE, ENABLE_DISTRIBUTED_SELF, IS_DISTRIBUTED_ENABLED_BY_BUNDLE, + GET_DEVICE_REMIND_TYPE, SHELL_DUMP, PUBLISH_CONTINUOUS_TASK_NOTIFICATION, CANCEL_CONTINUOUS_TASK_NOTIFICATION, diff --git a/frameworks/ans/core/include/ans_manager_proxy.h b/frameworks/ans/core/include/ans_manager_proxy.h index 0f590eb9f..feed3ff2a 100644 --- a/frameworks/ans/core/include/ans_manager_proxy.h +++ b/frameworks/ans/core/include/ans_manager_proxy.h @@ -103,6 +103,7 @@ public: ErrCode EnableDistributedByBundle(const sptr &bundleOption, bool enabled) override; ErrCode EnableDistributedSelf(bool enabled) override; ErrCode IsDistributedEnableByBundle(const sptr &bundleOption, bool &enabled) override; + ErrCode GetDeviceRemindType(NotificationConstant::RemindType &remindType) override; ErrCode ShellDump(const std::string &dumpOption, std::vector &dumpInfo) override; ErrCode PublishContinuousTaskNotification(const sptr &request) override; diff --git a/frameworks/ans/core/include/ans_manager_stub.h b/frameworks/ans/core/include/ans_manager_stub.h index ce61b644d..f65b42c3c 100644 --- a/frameworks/ans/core/include/ans_manager_stub.h +++ b/frameworks/ans/core/include/ans_manager_stub.h @@ -110,6 +110,7 @@ public: virtual ErrCode EnableDistributedSelf(bool enabled) override; virtual ErrCode IsDistributedEnableByBundle( const sptr &bundleOption, bool &enabled) override; + virtual ErrCode GetDeviceRemindType(NotificationConstant::RemindType &remindType) override; virtual ErrCode ShellDump(const std::string &dumpOption, std::vector &dumpInfo) override; virtual ErrCode PublishContinuousTaskNotification(const sptr &request) override; @@ -183,6 +184,7 @@ private: ErrCode HandleSetDoNotDisturbDate(MessageParcel &data, MessageParcel &reply); ErrCode HandleGetDoNotDisturbDate(MessageParcel &data, MessageParcel &reply); ErrCode HandleDoesSupportDoNotDisturbMode(MessageParcel &data, MessageParcel &reply); + ErrCode HandleGetDeviceRemindType(MessageParcel &data, MessageParcel &reply); ErrCode HandlePublishContinuousTaskNotification(MessageParcel &data, MessageParcel &reply); ErrCode HandleCancelContinuousTaskNotification(MessageParcel &data, MessageParcel &reply); ErrCode HandlePublishReminder(MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/ans/core/include/ans_notification.h b/frameworks/ans/core/include/ans_notification.h index 0aaa4d4ab..a780fd5aa 100644 --- a/frameworks/ans/core/include/ans_notification.h +++ b/frameworks/ans/core/include/ans_notification.h @@ -662,6 +662,15 @@ public: */ ErrCode IsDistributedEnableByBundle(const NotificationBundleOption &bundleOption, bool &enabled); + /** + * Obtains the device remind type. + * @note Your application must have system signature to call this method. + * + * @param remindType Indicates the device remind type to get. + * @return Returns get device reminder type result. + */ + ErrCode GetDeviceRemindType(NotificationConstant::RemindType &remindType); + /** * Publishes a continuous task notification. * @param request Indicates the NotificationRequest object for setting the notification content. diff --git a/frameworks/ans/core/src/ans_manager_proxy.cpp b/frameworks/ans/core/src/ans_manager_proxy.cpp index 9090effc1..b0ab15db8 100644 --- a/frameworks/ans/core/src/ans_manager_proxy.cpp +++ b/frameworks/ans/core/src/ans_manager_proxy.cpp @@ -2007,6 +2007,40 @@ ErrCode AnsManagerProxy::IsDistributedEnableByBundle(const sptr(rType); + } + + return result; +} + ErrCode AnsManagerProxy::ShellDump(const std::string &dumpOption, std::vector &dumpInfo) { MessageParcel data; diff --git a/frameworks/ans/core/src/ans_manager_stub.cpp b/frameworks/ans/core/src/ans_manager_stub.cpp index 2c37310aa..9dee804e9 100644 --- a/frameworks/ans/core/src/ans_manager_stub.cpp +++ b/frameworks/ans/core/src/ans_manager_stub.cpp @@ -201,6 +201,9 @@ const std::map(rType))) { + ANS_LOGW("[HandleGetDeviceRemindType] fail: write remind type failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return ERR_OK; +} + ErrCode AnsManagerStub::HandleShellDump(MessageParcel &data, MessageParcel &reply) { std::string dumpOption; @@ -1885,6 +1905,12 @@ ErrCode AnsManagerStub::IsDistributedEnableByBundle(const sptr &dumpInfo) { ANS_LOGW("AnsManagerStub::ShellDump called!"); diff --git a/frameworks/ans/core/src/ans_notification.cpp b/frameworks/ans/core/src/ans_notification.cpp index 0e32f0589..a9249491d 100644 --- a/frameworks/ans/core/src/ans_notification.cpp +++ b/frameworks/ans/core/src/ans_notification.cpp @@ -929,6 +929,16 @@ ErrCode AnsNotification::IsDistributedEnableByBundle(const NotificationBundleOpt return ansManagerProxy_->IsDistributedEnableByBundle(bo, enabled); } +ErrCode AnsNotification::GetDeviceRemindType(NotificationConstant::RemindType &remindType) +{ + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return ansManagerProxy_->GetDeviceRemindType(remindType); +} + void AnsNotification::ResetAnsManagerProxy() { ANS_LOGI("enter"); diff --git a/frameworks/ans/native/BUILD.gn b/frameworks/ans/native/BUILD.gn index 6be024c76..04783c8e3 100644 --- a/frameworks/ans/native/BUILD.gn +++ b/frameworks/ans/native/BUILD.gn @@ -25,6 +25,12 @@ config("ans_innerkits_public_config") { "${interfaces_path}/ans/native/include", "${frameworks_path}/ans/core/common/include", "${frameworks_path}/ans/core/include", + "//foundation/aafwk/standard/frameworks/kits/content/cpp/src/ohos/aafwk/content/", + "//foundation/aafwk/standard/interfaces/innerkits/want/include/ohos/aafwk/content", + "//foundation/multimedia/image_standard/interfaces/innerkits/include", + "//third_party/json/single_include", + "//third_party/jsoncpp/include", + "//utils/system/safwk/native/include", ] configs = [ "${frameworks_path}/wantagent:wantagent_innerkits_public_config" ] } diff --git a/frameworks/ans/native/src/notification.cpp b/frameworks/ans/native/src/notification.cpp index c146e5491..e7744cede 100644 --- a/frameworks/ans/native/src/notification.cpp +++ b/frameworks/ans/native/src/notification.cpp @@ -43,6 +43,7 @@ Notification::Notification(const Notification &other) key_ = other.key_; ledLightColor_ = other.ledLightColor_; lockscreenVisibleness_ = other.lockscreenVisibleness_; + remindType_ = other.remindType_; request_ = other.request_; postTime_ = other.postTime_; sound_ = other.sound_; @@ -180,6 +181,11 @@ bool Notification::IsFloatingIcon() const return request_->IsFloatingIcon(); } +NotificationConstant::RemindType Notification::GetRemindType() const +{ + return remindType_; +} + bool Notification::IsRemoveAllowed() const { return isRemoveAllowed_; @@ -262,6 +268,11 @@ bool Notification::MarshallingInt32(Parcel &parcel) const return false; } + if (!parcel.WriteInt32(static_cast(remindType_))) { + ANS_LOGE("Can't write remindType"); + return false; + } + if (!parcel.WriteInt32(static_cast(sourceType_))) { ANS_LOGE("Can't write sourceType"); return false; @@ -353,6 +364,9 @@ void Notification::ReadFromParcelInt32(Parcel &parcel) // Read lockscreenVisibleness_ lockscreenVisibleness_ = static_cast(parcel.ReadInt32()); + // Read remindType_ + remindType_ = static_cast(parcel.ReadInt32()); + // Read sourceType_ sourceType_ = static_cast(parcel.ReadInt32()); } @@ -434,6 +448,11 @@ void Notification::SetVibrationStyle(const std::vector &style) vibrationStyle_ = style; } +void Notification::SetRemindType(const NotificationConstant::RemindType &reminType) +{ + remindType_ = reminType; +} + std::string Notification::GenerateNotificationKey( const std::string &deviceId, int32_t uid, const std::string &label, int32_t id) { @@ -466,6 +485,7 @@ std::string Notification::Dump() const "key = " + key_ + ", ledLightColor = " + std::to_string(ledLightColor_) + ", lockscreenVisbleness = " + std::to_string(static_cast(lockscreenVisibleness_)) + + ", remindType = " + std::to_string(static_cast(remindType_)) + ", isRemoveAllowed = " + (isRemoveAllowed_ ? "true" : "false") + ", sourceType = " + std::to_string(static_cast(sourceType_)) + ", deviceId = " + deviceId_ + diff --git a/frameworks/ans/native/src/notification_content.cpp b/frameworks/ans/native/src/notification_content.cpp index 4ece6f602..dbd390189 100644 --- a/frameworks/ans/native/src/notification_content.cpp +++ b/frameworks/ans/native/src/notification_content.cpp @@ -137,7 +137,6 @@ NotificationContent *NotificationContent::FromJson(const nlohmann::json &jsonObj ANS_LOGE("Invalid JSON object"); return nullptr; } - const auto &jsonEnd = jsonObject.cend(); if ((jsonObject.find("contentType") == jsonEnd) || (jsonObject.find("content") == jsonEnd)) { ANS_LOGE("Incomplete NotificationContent json object. Cannot convert content from JSON."); diff --git a/frameworks/ans/native/src/notification_helper.cpp b/frameworks/ans/native/src/notification_helper.cpp index 4cf4afaa1..9452c6a5c 100644 --- a/frameworks/ans/native/src/notification_helper.cpp +++ b/frameworks/ans/native/src/notification_helper.cpp @@ -347,6 +347,11 @@ ErrCode NotificationHelper::IsDistributedEnableByBundle(const NotificationBundle return DelayedSingleton::GetInstance()->IsDistributedEnableByBundle(bundleOption, enabled); } +ErrCode NotificationHelper::GetDeviceRemindType(NotificationConstant::RemindType &remindType) +{ + return DelayedSingleton::GetInstance()->GetDeviceRemindType(remindType); +} + ErrCode NotificationHelper::PublishContinuousTaskNotification(const NotificationRequest &request) { return DelayedSingleton::GetInstance()->PublishContinuousTaskNotification(request); diff --git a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp index 34930d206..0e9ef6fd9 100644 --- a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp +++ b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp @@ -1994,6 +1994,254 @@ HWTEST_F(AnsFWModuleTest, DistributedNotification_Subscribe_00200, Function | Me ASSERT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK); SleepForFC(); } + +/** + * + * @tc.number : ANS_Interface_MT_GetDeviceRemindType_00100 + * @tc.name : GetDeviceRemindType_00100 + * @tc.desc : Get device remind type. + */ +HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_GetDeviceRemindType_00100, Function | MediumTest | Level1) +{ + auto rType{NotificationConstant::RemindType::NONE}; + EXPECT_EQ(NotificationHelper::GetDeviceRemindType(rType), ERR_OK); + ANS_LOGI("ANS_Interface_MT_GetDeviceRemindType_00100:: rType : %{public}d", static_cast(rType)); + + EXPECT_NE(rType, NotificationConstant::RemindType::NONE); +} + +/** + * + * @tc.number : ANS_FW_MT_DistributedNotification_ScreenStatusChange_00100 + * @tc.name : ScreenStatusChange_00100 + * @tc.desc : Receive local screen status from common event to kvstore. + */ +HWTEST_F(AnsFWModuleTest, ScreenStatusChange_00100, Function | MediumTest | Level1) +{ + ANS_LOGI("%{public}s", test_info_->name()); + std::shared_ptr pointer = + DistributedKv::AnsTestSingleKvStore::GetMockKvStorePointer({KVSTORE_APP_ID}, {KVSTORE_SCREEN_STATUS_STORE_ID}); + DistributedKv::Key key(""); + std::vector entries; + + PublishCommonEventScreenStatus(false); + EXPECT_EQ(pointer->GetEntries(key, entries), DistributedKv::Status::SUCCESS); + EXPECT_EQ(entries.size(), std::size_t(1)); + EXPECT_EQ(entries[0].value.ToString(), "off"); + entries.clear(); + + PublishCommonEventScreenStatus(true); + EXPECT_EQ(pointer->GetEntries(key, entries), DistributedKv::Status::SUCCESS); + EXPECT_EQ(entries.size(), std::size_t(1)); + EXPECT_EQ(entries[0].value.ToString(), "on"); + entries.clear(); +} + +/** + * + * @tc.number : ANS_FW_MT_DistributedNotification_DefaultRemindPolicy_00100 + * @tc.name : DefaultRemindPolicy_00100 + * @tc.desc : Publish a notification when local screen on and remote screen off. + */ +HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00100, Function | MediumTest | Level1) +{ + ANS_LOGI("%{public}s", test_info_->name()); + NotificationRequest request = CreateDistributedRequest(test_info_->name()); + + PublishCommonEventScreenStatus(true); + SetDistributedScreenStatus(false); + + TestAnsSubscriber subscriber; + EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + + EXPECT_EQ(NotificationHelper::PublishNotification(request), ERR_OK); + + EventParser parser; + parser.Parse(subscriber.GetEvents()); + auto notificationList = parser.GetOnConsumedReq(); + std::shared_ptr outNotification; + EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true); + EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND); + + subscriber.ClearEvents(); + EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK); + SleepForFC(); +} + +/** + * + * @tc.number : ANS_FW_MT_DistributedNotification_DefaultRemindPolicy_00200 + * @tc.name : DefaultRemindPolicy_00200 + * @tc.desc : Publish a notification when local screen on and remote screen on. + */ +HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00200, Function | MediumTest | Level1) +{ + ANS_LOGI("%{public}s", test_info_->name()); + NotificationRequest request = CreateDistributedRequest(test_info_->name()); + + PublishCommonEventScreenStatus(true); + SetDistributedScreenStatus(true); + + TestAnsSubscriber subscriber; + EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + + EXPECT_EQ(NotificationHelper::PublishNotification(request), ERR_OK); + + EventParser parser; + parser.Parse(subscriber.GetEvents()); + auto notificationList = parser.GetOnConsumedReq(); + std::shared_ptr outNotification; + EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true); + EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND); + + subscriber.ClearEvents(); + EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK); + SleepForFC(); +} + +/** + * + * @tc.number : ANS_FW_MT_DistributedNotification_DefaultRemindPolicy_00300 + * @tc.name : DefaultRemindPolicy_00300 + * @tc.desc : Publish a notification when local screen off and remote screen off. + */ +HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00300, Function | MediumTest | Level1) +{ + ANS_LOGI("%{public}s", test_info_->name()); + NotificationRequest request = CreateDistributedRequest(test_info_->name()); + + PublishCommonEventScreenStatus(false); + SetDistributedScreenStatus(false); + + TestAnsSubscriber subscriber; + EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + + EXPECT_EQ(NotificationHelper::PublishNotification(request), ERR_OK); + + EventParser parser; + parser.Parse(subscriber.GetEvents()); + auto notificationList = parser.GetOnConsumedReq(); + std::shared_ptr outNotification; + EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true); + EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_IDLE_REMIND); + + subscriber.ClearEvents(); + EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK); + SleepForFC(); +} + +/** + * + * @tc.number : ANS_FW_MT_DistributedNotification_DefaultRemindPolicy_00400 + * @tc.name : DefaultRemindPolicy_00400 + * @tc.desc : Publish a notification when local screen off and remote screen on. + */ +HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00400, Function | MediumTest | Level1) +{ + ANS_LOGI("%{public}s", test_info_->name()); + NotificationRequest request = CreateDistributedRequest(test_info_->name()); + + PublishCommonEventScreenStatus(false); + SetDistributedScreenStatus(true); + + TestAnsSubscriber subscriber; + EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + + EXPECT_EQ(NotificationHelper::PublishNotification(request), ERR_OK); + + EventParser parser; + parser.Parse(subscriber.GetEvents()); + auto notificationList = parser.GetOnConsumedReq(); + std::shared_ptr outNotification; + EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true); + EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_IDLE_DONOT_REMIND); + + subscriber.ClearEvents(); + EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK); + SleepForFC(); +} + +/** + * + * @tc.number : ANS_FW_MT_DistributedNotification_DefaultRemindPolicy_00500 + * @tc.name : DefaultRemindPolicy_00500 + * @tc.desc : Receive distributed notification when screen is on. + */ +HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00500, Function | MediumTest | Level1) +{ + ANS_LOGI("%{public}s", test_info_->name()); + NotificationRequest request = CreateDistributedRequest(test_info_->name()); + request.SetOwnerBundleName(APP_NAME); + request.SetCreatorBundleName(APP_NAME); + request.SetDevicesSupportDisplay({""}); + std::string jsonString; + NotificationJsonConverter::ConvertToJosnString(&request, jsonString); + + PublishCommonEventScreenStatus(true); + + TestAnsSubscriber subscriber; + EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + + std::shared_ptr pointer = + DistributedKv::AnsTestSingleKvStore::GetMockKvStorePointer({KVSTORE_APP_ID}, {KVSTORE_NOTIFICATION_STORE_ID}); + + DistributedKv::Key key(GenerateDistributedKey(request, REMOTE_DEVICE_ID)); + DistributedKv::Value value(jsonString); + pointer->InsertDataToDoCallback(key, value); + SleepForFC(); + + EventParser parser; + parser.Parse(subscriber.GetEvents()); + auto notificationList = parser.GetOnConsumedReq(); + std::shared_ptr outNotification; + EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true); + EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND); + + subscriber.ClearEvents(); + EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK); + SleepForFC(); +} + +/** + * + * @tc.number : ANS_FW_MT_DistributedNotification_DefaultRemindPolicy_00600 + * @tc.name : DefaultRemindPolicy_00600 + * @tc.desc : Receive distributed notification when screen is off. + */ +HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00600, Function | MediumTest | Level1) +{ + ANS_LOGI("%{public}s", test_info_->name()); + NotificationRequest request = CreateDistributedRequest(test_info_->name()); + request.SetOwnerBundleName(APP_NAME); + request.SetCreatorBundleName(APP_NAME); + request.SetDevicesSupportDisplay({""}); + std::string jsonString; + NotificationJsonConverter::ConvertToJosnString(&request, jsonString); + + PublishCommonEventScreenStatus(false); + + TestAnsSubscriber subscriber; + EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + + std::shared_ptr pointer = + DistributedKv::AnsTestSingleKvStore::GetMockKvStorePointer({KVSTORE_APP_ID}, {KVSTORE_NOTIFICATION_STORE_ID}); + + DistributedKv::Key key(GenerateDistributedKey(request, REMOTE_DEVICE_ID)); + DistributedKv::Value value(jsonString); + pointer->InsertDataToDoCallback(key, value); + SleepForFC(); + + EventParser parser; + parser.Parse(subscriber.GetEvents()); + auto notificationList = parser.GetOnConsumedReq(); + std::shared_ptr outNotification; + EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true); + EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_IDLE_DONOT_REMIND); + + subscriber.ClearEvents(); + EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK); + SleepForFC(); +} #endif HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PulbishContinuousTask_07100, Function | MediumTest | Level1) 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 56cf177a8..b0b055942 100644 --- a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp +++ b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp @@ -1298,7 +1298,6 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_08000, Function auto spUserInput2 = NotificationUserInput::Create("uikey2"); ab1->AddNotificationUserInput(spUserInput3); ab1->AddNotificationUserInput(spUserInput2); - auto spOnlyUserInput1 = NotificationUserInput::Create("uionlykey1"); spOnlyUserInput1->SetPermitFreeFormInput(false); spOnlyUserInput1->SetPermitMimeTypes("uionly", true); diff --git a/interfaces/innerkits/ans/native/include/notification.h b/interfaces/innerkits/ans/native/include/notification.h index 5ad3ea065..a12b7f4a0 100644 --- a/interfaces/innerkits/ans/native/include/notification.h +++ b/interfaces/innerkits/ans/native/include/notification.h @@ -185,6 +185,12 @@ public: */ bool IsFloatingIcon() const; + /** + * @brief Obtains the remind type of a notification. + * @return the remind type of a notification. + */ + NotificationConstant::RemindType GetRemindType() const; + /** * Whether to support remove allowed. * @return Current remove allowed status. @@ -246,6 +252,7 @@ private: void SetPostTime(const int64_t &time); void SetSound(const Uri &sound); void SetVibrationStyle(const std::vector &style); + void SetRemindType(const NotificationConstant::RemindType &reminType); std::string GenerateNotificationKey( const std::string &deviceId, int32_t uid, const std::string &label, int32_t id); void SetRemoveAllowed(bool removeAllowed); @@ -271,6 +278,7 @@ private: std::string deviceId_ {""}; int32_t ledLightColor_ {0}; NotificationConstant::VisiblenessType lockscreenVisibleness_ {NotificationConstant::VisiblenessType::NO_OVERRIDE}; + NotificationConstant::RemindType remindType_ {NotificationConstant::RemindType::NONE}; NotificationConstant::SourceType sourceType_ {NotificationConstant::SourceType::TYPE_NORMAL}; sptr request_ {nullptr}; int64_t postTime_ {0}; diff --git a/interfaces/innerkits/ans/native/include/notification_constant.h b/interfaces/innerkits/ans/native/include/notification_constant.h index f3dd82cf1..b4e7bec4c 100644 --- a/interfaces/innerkits/ans/native/include/notification_constant.h +++ b/interfaces/innerkits/ans/native/include/notification_constant.h @@ -91,6 +91,20 @@ public: CLEARLY = 3, // time period }; + enum class RemindType { + NONE = -1, + DEVICE_IDLE_DONOT_REMIND = 0, // The device is not in use, no reminder + DEVICE_IDLE_REMIND = 1, // The device is not in use, remind + DEVICE_ACTIVE_DONOT_REMIND = 2, // The device is in use, no reminder + DEVICE_ACTIVE_REMIND = 3, // The device is in use, reminder + }; + + enum class DistributedReminderPolicy { + DEFAULT, + ALWAYS_REMIND, + DO_NOT_REMIND, + }; + enum class SourceType { /** * general notification. diff --git a/interfaces/innerkits/ans/native/include/notification_helper.h b/interfaces/innerkits/ans/native/include/notification_helper.h index 28a20eb13..665303846 100644 --- a/interfaces/innerkits/ans/native/include/notification_helper.h +++ b/interfaces/innerkits/ans/native/include/notification_helper.h @@ -663,6 +663,15 @@ public: */ static ErrCode IsDistributedEnableByBundle(const NotificationBundleOption &bundleOption, bool &enabled); + /** + * Obtains the device remind type. + * @note Your application must have system signature to call this method. + * + * @param remindType Indicates the device remind type to get. + * @return Returns get device reminder type result. + */ + static ErrCode GetDeviceRemindType(NotificationConstant::RemindType &remindType); + /** * Publishes a continuous task notification. * @param request Indicates the NotificationRequest object for setting the notification content. diff --git a/interfaces/kits/napi/ans/include/common.h b/interfaces/kits/napi/ans/include/common.h index 5cfe78720..f77e712a0 100644 --- a/interfaces/kits/napi/ans/include/common.h +++ b/interfaces/kits/napi/ans/include/common.h @@ -113,6 +113,13 @@ enum class SourceType { TYPE_TIMER = 0x00000002 }; +enum class DeviceRemindType { + IDLE_DONOT_REMIND, + IDLE_REMIND, + ACTIVE_DONOT_REMIND, + ACTIVE_REMIND +}; + struct NotificationSubscribeInfo { std::vector bundleNames; int userId = 0; @@ -407,6 +414,8 @@ public: static bool DoNotDisturbTypeCToJS(const NotificationConstant::DoNotDisturbType &inType, DoNotDisturbType &outType); + static bool DeviceRemindTypeCToJS(const NotificationConstant::RemindType &inType, DeviceRemindType &outType); + static bool SourceTypeCToJS(const NotificationConstant::SourceType &inType, SourceType &outType); static napi_value CreateWantAgentByJS(const napi_env &env, const std::shared_ptr &agent); diff --git a/interfaces/kits/napi/ans/include/constant.h b/interfaces/kits/napi/ans/include/constant.h index 323a4fd0f..d5f8f3e99 100644 --- a/interfaces/kits/napi/ans/include/constant.h +++ b/interfaces/kits/napi/ans/include/constant.h @@ -28,6 +28,7 @@ napi_value DoNotDisturbMode(napi_env env, napi_value exports); napi_value InputEditTypeInit(napi_env env, napi_value exports); napi_value ContentTypeInit(napi_env env, napi_value exports); napi_value DoNotDisturbTypeInit(napi_env env, napi_value exports); +napi_value DeviceRemindTypeInit(napi_env env, napi_value exports); napi_value ConstantInit(napi_env env, napi_value exports); } // namespace NotificationNapi } // namespace OHOS diff --git a/interfaces/kits/napi/ans/include/distributed.h b/interfaces/kits/napi/ans/include/distributed.h index 2beab5b76..4facf772b 100644 --- a/interfaces/kits/napi/ans/include/distributed.h +++ b/interfaces/kits/napi/ans/include/distributed.h @@ -27,6 +27,8 @@ napi_value EnableDistributed(napi_env env, napi_callback_info info); napi_value EnableDistributedByBundle(napi_env env, napi_callback_info info); napi_value EnableDistributedSelf(napi_env env, napi_callback_info info); napi_value IsDistributedEnableByBundle(napi_env env, napi_callback_info info); +napi_value GetDeviceRemindType(napi_env env, napi_callback_info info); + } // namespace NotificationNapi } // namespace OHOS diff --git a/interfaces/kits/napi/ans/src/common.cpp b/interfaces/kits/napi/ans/src/common.cpp index 8849e7e61..65cad546c 100644 --- a/interfaces/kits/napi/ans/src/common.cpp +++ b/interfaces/kits/napi/ans/src/common.cpp @@ -205,6 +205,14 @@ napi_value Common::SetNotificationByDistributedOptions( } napi_set_named_property(env, result, "supportOperateDevices", arrSupportOperateDevices); + // readonly remindType?: number + enum DeviceRemindType outType = DeviceRemindType::IDLE_DONOT_REMIND; + if (!DeviceRemindTypeCToJS(notification->GetRemindType(), outType)) { + return NapiGetBoolean(env, false); + } + napi_create_int32(env, (int32_t)outType, &value); + napi_set_named_property(env, result, "remindType", value); + return NapiGetBoolean(env, true); } @@ -3887,6 +3895,28 @@ bool Common::DoNotDisturbTypeCToJS(const NotificationConstant::DoNotDisturbType return true; } +bool Common::DeviceRemindTypeCToJS(const NotificationConstant::RemindType &inType, DeviceRemindType &outType) +{ + switch (inType) { + case NotificationConstant::RemindType::DEVICE_IDLE_DONOT_REMIND: + outType = DeviceRemindType::IDLE_DONOT_REMIND; + break; + case NotificationConstant::RemindType::DEVICE_IDLE_REMIND: + outType = DeviceRemindType::IDLE_REMIND; + break; + case NotificationConstant::RemindType::DEVICE_ACTIVE_DONOT_REMIND: + outType = DeviceRemindType::ACTIVE_DONOT_REMIND; + break; + case NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND: + outType = DeviceRemindType::ACTIVE_REMIND; + break; + default: + ANS_LOGE("DeviceRemindType %{public}d is an invalid value", inType); + return false; + } + return true; +} + bool Common::SourceTypeCToJS(const NotificationConstant::SourceType &inType, SourceType &outType) { switch (inType) { diff --git a/interfaces/kits/napi/ans/src/constant.cpp b/interfaces/kits/napi/ans/src/constant.cpp index 29fefc600..96443b732 100644 --- a/interfaces/kits/napi/ans/src/constant.cpp +++ b/interfaces/kits/napi/ans/src/constant.cpp @@ -240,6 +240,26 @@ napi_value DoNotDisturbTypeInit(napi_env env, napi_value exports) return exports; } +napi_value DeviceRemindTypeInit(napi_env env, napi_value exports) +{ + ANS_LOGI("%{public}s, called", __func__); + + napi_value obj = nullptr; + napi_create_object(env, &obj); + + SetNamedPropertyByInteger(env, obj, (int32_t)DeviceRemindType::IDLE_DONOT_REMIND, "IDLE_DONOT_REMIND"); + SetNamedPropertyByInteger(env, obj, (int32_t)DeviceRemindType::IDLE_REMIND, "IDLE_REMIND"); + SetNamedPropertyByInteger(env, obj, (int32_t)DeviceRemindType::ACTIVE_DONOT_REMIND, "ACTIVE_DONOT_REMIND"); + SetNamedPropertyByInteger(env, obj, (int32_t)DeviceRemindType::ACTIVE_REMIND, "ACTIVE_REMIND"); + + napi_property_descriptor exportFuncs[] = { + DECLARE_NAPI_PROPERTY("DeviceRemindType", obj), + }; + + napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs); + return exports; +} + napi_value SourceTypeInit(napi_env env, napi_value exports) { ANS_LOGI("%{public}s, called", __func__); @@ -271,6 +291,7 @@ napi_value ConstantInit(napi_env env, napi_value exports) ContentTypeInit(env, exports); SourceTypeInit(env, exports); DoNotDisturbTypeInit(env, exports); + DeviceRemindTypeInit(env, exports); return exports; } } // namespace NotificationNapi diff --git a/interfaces/kits/napi/ans/src/distributed.cpp b/interfaces/kits/napi/ans/src/distributed.cpp index 5739891f1..dee8dcf53 100644 --- a/interfaces/kits/napi/ans/src/distributed.cpp +++ b/interfaces/kits/napi/ans/src/distributed.cpp @@ -69,6 +69,13 @@ struct AsyncCallbackInfoIsEnabledByBundle { bool enable = false; }; +struct AsyncCallbackInfoGetRemindType { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + NotificationConstant::RemindType remindType = NotificationConstant::RemindType::NONE; +}; + napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnabledParams ¶ms) { ANS_LOGI("enter"); @@ -485,5 +492,81 @@ napi_value IsDistributedEnableByBundle(napi_env env, napi_callback_info info) return promise; } } + +void AsyncCompleteCallbackGetDeviceRemindType(napi_env env, napi_status status, void *data) +{ + ANS_LOGI("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + ANS_LOGI("GetDeviceRemindType napi_create_async_work end"); + AsyncCallbackInfoGetRemindType *asynccallbackinfo = (AsyncCallbackInfoGetRemindType *)data; + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + enum DeviceRemindType outType = DeviceRemindType::IDLE_DONOT_REMIND; + if (!Common::DeviceRemindTypeCToJS(asynccallbackinfo->remindType, outType)) { + asynccallbackinfo->info.errorCode = ERROR; + result = Common::NapiGetNull(env); + } + napi_create_int32(env, (int32_t)outType, &result); + } + Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result); + + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + if (asynccallbackinfo) { + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } +} + +napi_value GetDeviceRemindType(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + napi_ref callback = nullptr; + if (Common::ParseParaOnlyCallback(env, info, callback) == nullptr) { + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoGetRemindType *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoGetRemindType {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "getDeviceRemindType", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetDeviceRemindType napi_create_async_work start"); + AsyncCallbackInfoGetRemindType *asynccallbackinfo = (AsyncCallbackInfoGetRemindType *)data; + asynccallbackinfo->info.errorCode = + NotificationHelper::GetDeviceRemindType(asynccallbackinfo->remindType); + }, + AsyncCompleteCallbackGetDeviceRemindType, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} } // namespace NotificationNapi } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/ans/src/init.cpp b/interfaces/kits/napi/ans/src/init.cpp index fa8ed523b..bbb3df5a3 100644 --- a/interfaces/kits/napi/ans/src/init.cpp +++ b/interfaces/kits/napi/ans/src/init.cpp @@ -74,6 +74,7 @@ napi_value NotificationInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("enableDistributedByBundle", EnableDistributedByBundle), DECLARE_NAPI_FUNCTION("enableDistributedSelf", EnableDistributedSelf), DECLARE_NAPI_FUNCTION("isDistributedEnableByBundle", IsDistributedEnableByBundle), + DECLARE_NAPI_FUNCTION("getDeviceRemindType", GetDeviceRemindType), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index e1fb967dc..bab0e6840 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -111,6 +111,7 @@ public: ErrCode EnableDistributedByBundle(const sptr &bundleOption, bool enabled) override; ErrCode EnableDistributedSelf(bool enabled) override; ErrCode IsDistributedEnableByBundle(const sptr &bundleOption, bool &enabled) override; + ErrCode GetDeviceRemindType(NotificationConstant::RemindType &remindType) override; ErrCode ShellDump(const std::string &dumpOption, std::vector &dumpInfo) override; ErrCode PublishContinuousTaskNotification(const sptr &request) override; @@ -173,8 +174,10 @@ private: ErrCode PrepereContinuousTaskNotificationRequest(const sptr &request, const int &uid); bool GetActiveUserId(int& userId); + ErrCode SetNotificationRemindType(sptr notification, bool isLocal); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::vector GetLocalNotificationKeys(const sptr &bundleOption); + NotificationConstant::RemindType GetRemindType(); ErrCode DoDistributedPublish( const sptr bundleOption, const std::shared_ptr record); ErrCode DoDistributedDelete(const std::string deviceId, const sptr notification); @@ -199,6 +202,10 @@ private: std::shared_ptr distributedKvStoreDeathRecipient_ = nullptr; std::shared_ptr systemEventObserver_ = nullptr; DistributedKv::DistributedKvDataManager dataManager_; +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + NotificationConstant::DistributedReminderPolicy distributedReminderPolicy_ = DEFAULT_DISTRIBUTED_REMINDER_POLICY; + bool localScreenOn_ = true; +#endif }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/interface_system_event.h b/services/ans/include/interface_system_event.h index bc3b8f4ad..0c5835503 100644 --- a/services/ans/include/interface_system_event.h +++ b/services/ans/include/interface_system_event.h @@ -25,6 +25,10 @@ namespace OHOS { namespace Notification { struct ISystemEvent { std::function &)> onBundleRemoved; +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + std::function onScreenOn; + std::function onScreenOff; +#endif }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 95ae995b2..f4bc69ca9 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -37,6 +37,7 @@ #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED #include "distributed_notification_manager.h" #include "distributed_preferences.h" +#include "distributed_screen_status_manager.h" #endif namespace OHOS { @@ -197,6 +198,10 @@ AdvancedNotificationService::AdvancedNotificationService() ISystemEvent iSystemEvent = { std::bind(&AdvancedNotificationService::OnBundleRemoved, this, std::placeholders::_1), +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + std::bind(&AdvancedNotificationService::OnScreenOn, this), + std::bind(&AdvancedNotificationService::OnScreenOff, this), +#endif }; systemEventObserver_ = std::make_shared(iSystemEvent); @@ -308,6 +313,7 @@ ErrCode AdvancedNotificationService::PublishPreparedNotification( record->request = request; record->notification = new Notification(request); record->bundleOption = bundleOption; + SetNotificationRemindType(record->notification, true); ErrCode result = ERR_OK; handler_->PostSyncTask(std::bind([&]() { @@ -1797,6 +1803,20 @@ void AdvancedNotificationService::OnBundleRemoved(const sptrSetLocalScreenStatus(true); +} + +void AdvancedNotificationService::OnScreenOff() +{ + localScreenOn_ = false; + DistributedScreenStatusManager::GetInstance()->SetLocalScreenStatus(false); +} +#endif + void AdvancedNotificationService::OnDistributedKvStoreDeathRecipient() { ANS_LOGD("%{public}s", __FUNCTION__); @@ -2415,6 +2435,36 @@ ErrCode AdvancedNotificationService::IsDistributedEnableByBundle( #endif } +ErrCode AdvancedNotificationService::GetDeviceRemindType(NotificationConstant::RemindType &remindType) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + if (!IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!CheckPermission(GetClientBundleName())) { + return ERR_ANS_PERMISSION_DENIED; + } + +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + handler_->PostSyncTask(std::bind([&]() { remindType = GetRemindType(); })); + return ERR_OK; +#else + return ERR_INVALID_OPERATION; +#endif +} + +ErrCode AdvancedNotificationService::SetNotificationRemindType(sptr notification, bool isLocal) +{ +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + notification->SetRemindType(GetRemindType()); +#else + notification->SetRemindType(NotificationConstant::RemindType::NONE); +#endif + return ERR_OK; +} + #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::vector AdvancedNotificationService::GetLocalNotificationKeys( const sptr &bundleOption) @@ -2432,6 +2482,39 @@ std::vector AdvancedNotificationService::GetLocalNotificationKeys( return keys; } +NotificationConstant::RemindType AdvancedNotificationService::GetRemindType() +{ + bool remind = localScreenOn_; + if (distributedReminderPolicy_ == NotificationConstant::DistributedReminderPolicy::DEFAULT) { + bool remoteUsing = false; + ErrCode result = DistributedScreenStatusManager::GetInstance()->CheckRemoteDevicesIsUsing(remoteUsing); + if (result != ERR_OK) { + remind = true; + } + if (!localScreenOn_ && !remoteUsing) { + remind = true; + } + } else if (distributedReminderPolicy_ == NotificationConstant::DistributedReminderPolicy::ALWAYS_REMIND) { + remind = true; + } else if (distributedReminderPolicy_ == NotificationConstant::DistributedReminderPolicy::DO_NOT_REMIND) { + remind = false; + } + + if (localScreenOn_) { + if (remind) { + return NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND; + } else { + return NotificationConstant::RemindType::DEVICE_ACTIVE_DONOT_REMIND; + } + } else { + if (remind) { + return NotificationConstant::RemindType::DEVICE_IDLE_REMIND; + } else { + return NotificationConstant::RemindType::DEVICE_IDLE_DONOT_REMIND; + } + } +} + std::string AdvancedNotificationService::GetNotificationDeviceId(const std::string &key) { for (auto record : notificationList_) { @@ -2524,6 +2607,7 @@ void AdvancedNotificationService::OnDistributedPublish( record->notification = new Notification(deviceId, request); record->bundleOption = bundleOption; record->deviceId = deviceId; + SetNotificationRemindType(record->notification, false); ErrCode result = AssignValidNotificationSlot(record); if (result != ERR_OK) { @@ -2573,6 +2657,7 @@ void AdvancedNotificationService::OnDistributedUpdate( record->notification = new Notification(deviceId, request); record->bundleOption = bundleOption; record->deviceId = deviceId; + SetNotificationRemindType(record->notification, false); ErrCode result = AssignValidNotificationSlot(record); if (result != ERR_OK) { diff --git a/services/ans/src/system_event_observer.cpp b/services/ans/src/system_event_observer.cpp index c87a77748..c910e4299 100644 --- a/services/ans/src/system_event_observer.cpp +++ b/services/ans/src/system_event_observer.cpp @@ -25,6 +25,10 @@ SystemEventObserver::SystemEventObserver(const ISystemEvent &callbacks) : callba { EventFwk::MatchingSkills matchingSkills; matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF); +#endif EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills); subscriber_ = std::make_shared( @@ -50,6 +54,16 @@ void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data) sptr bundleOption = new NotificationBundleOption(bundleName, uid); callbacks_.onBundleRemoved(bundleOption); } +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) { + if (callbacks_.onScreenOn != nullptr) { + callbacks_.onScreenOn(); + } + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) { + if (callbacks_.onScreenOff != nullptr) { + callbacks_.onScreenOff(); + } +#endif } } } // namespace Notification diff --git a/services/distributed/BUILD.gn b/services/distributed/BUILD.gn index 757c4c4f7..2fefaebad 100644 --- a/services/distributed/BUILD.gn +++ b/services/distributed/BUILD.gn @@ -31,6 +31,7 @@ ohos_shared_library("libans_distributed") { "src/distributed_preferences.cpp", "src/distributed_preferences_database.cpp", "src/distributed_preferences_info.cpp", + "src/distributed_screen_status_manager.cpp", ] include_dirs = [ "include" ] diff --git a/services/distributed/include/distributed_screen_status_manager.h b/services/distributed/include/distributed_screen_status_manager.h new file mode 100644 index 000000000..a9308fdba --- /dev/null +++ b/services/distributed/include/distributed_screen_status_manager.h @@ -0,0 +1,64 @@ +/* + * 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. + */ + +#ifndef BASE_NOTIFICATION_ANS_STANDARD_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_SCREEN_STATUS_MANAGER_H +#define BASE_NOTIFICATION_ANS_STANDARD_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_SCREEN_STATUS_MANAGER_H + +#include + +#include "distributed_kv_data_manager.h" +#include "event_handler.h" +#include "event_runner.h" +#include "singleton.h" + +#include "distributed_device_callback.h" +#include "distributed_flow_control.h" + +namespace OHOS { +namespace Notification { +class DistributedScreenStatusManager : private DistributedFlowControl, + public DelayedSingleton { +public: + ErrCode CheckRemoteDevicesIsUsing(bool &isUsing); + ErrCode SetLocalScreenStatus(bool screenOn); + +private: + void OnDeviceConnected(const std::string &deviceId); + void OnDeviceDisconnected(const std::string &deviceId); + + void GetKvDataManager(void); + bool CheckKvDataManager(void); + void GetKvStore(void); + bool CheckKvStore(void); + + std::string GenerateDistributedKey(const std::string &deviceId); + +private: + std::recursive_mutex mutex_; + std::shared_ptr runner_ = nullptr; + std::shared_ptr handler_ = nullptr; + std::unique_ptr kvDataManager_{}; + std::shared_ptr kvStore_{}; + std::shared_ptr deviceCb_{}; + + bool localScreenOn_ = false; + + DECLARE_DELAYED_SINGLETON(DistributedScreenStatusManager); + DISALLOW_COPY_AND_MOVE(DistributedScreenStatusManager); +}; +} // namespace Notification +} // namespace OHOS + +#endif /* BASE_NOTIFICATION_ANS_STANDARD_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_SCREEN_STATUS_MANAGER_H */ diff --git a/services/distributed/src/distributed_screen_status_manager.cpp b/services/distributed/src/distributed_screen_status_manager.cpp new file mode 100644 index 000000000..5503562bf --- /dev/null +++ b/services/distributed/src/distributed_screen_status_manager.cpp @@ -0,0 +1,237 @@ +/* + * 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 "distributed_screen_status_manager.h" + +#include "ans_inner_errors.h" +#include "ans_log_wrapper.h" + +namespace OHOS { +namespace Notification { +namespace { +const std::string APP_ID = "advanced_notification_service"; +const std::string STORE_ID = "distributed_screen_status"; +const std::string DELIMITER = "|"; +const std::string SCREEN_STATUS_LABEL = "screen_status"; +const std::string SCREEN_STATUS_VALUE_ON = "on"; +const std::string SCREEN_STATUS_VALUE_OFF = "off"; +} // namespace + +DistributedScreenStatusManager::DistributedScreenStatusManager() : DistributedFlowControl() +{ + DistributedDeviceCallback::IDeviceChange callback = { + .OnConnected = std::bind(&DistributedScreenStatusManager::OnDeviceConnected, this, std::placeholders::_1), + .OnDisconnected = std::bind(&DistributedScreenStatusManager::OnDeviceDisconnected, this, std::placeholders::_1), + }; + deviceCb_ = std::make_shared(callback); + GetKvDataManager(); + GetKvStore(); +} + +DistributedScreenStatusManager::~DistributedScreenStatusManager() +{ + +} + +void DistributedScreenStatusManager::OnDeviceConnected(const std::string &deviceId) +{ + ANS_LOGI("deviceId:%{public}s", deviceId.c_str()); +} + +void DistributedScreenStatusManager::OnDeviceDisconnected(const std::string &deviceId) +{ + std::lock_guard lock(mutex_); + if (!CheckKvDataManager()) { + return; + } + + std::vector devInfoList; + DistributedKv::Status status = + kvDataManager_->GetDeviceList(devInfoList, DistributedKv::DeviceFilterStrategy::NO_FILTER); + if (status != DistributedKv::Status::SUCCESS) { + ANS_LOGE("kvDataManager GetDeviceList() failed ret = 0x%{public}x", status); + kvDataManager_.reset(); + return; + } + + if (!devInfoList.empty()) { + return; + } + + kvStore_.reset(); + + DistributedKv::AppId appId = {.appId = APP_ID}; + DistributedKv::StoreId storeId = {.storeId = STORE_ID}; + kvDataManager_->DeleteKvStore(appId, storeId); + + if (!CheckKvStore()) { + return; + } + + SetLocalScreenStatus(localScreenOn_); +} + +void DistributedScreenStatusManager::GetKvDataManager(void) +{ + kvDataManager_ = std::make_unique(); + if (kvDataManager_ != nullptr) { + + DistributedKv::Status status = kvDataManager_->StartWatchDeviceChange(deviceCb_); + if (status != DistributedKv::Status::SUCCESS) { + ANS_LOGW("kvDataManager StartWatchDeviceChange failed ret = 0x%{public}x", status); + kvDataManager_.reset(); + } + } + + KvManagerFlowControlClear(); +} + +bool DistributedScreenStatusManager::CheckKvDataManager(void) +{ + if (kvDataManager_ == nullptr) { + GetKvDataManager(); + } + if (kvDataManager_ == nullptr) { + ANS_LOGE("kvDataManager is nullptr."); + return false; + } + return true; +} + +void DistributedScreenStatusManager::GetKvStore(void) +{ + if (!CheckKvDataManager()) { + return; + } + + DistributedKv::Status status; + DistributedKv::Options options; + options.createIfMissing = true; + options.autoSync = true; + options.kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION; + + DistributedKv::AppId appId = {.appId = APP_ID}; + DistributedKv::StoreId storeId = {.storeId = STORE_ID}; + status = kvDataManager_->GetSingleKvStore(options, + appId, + storeId, + kvStore_); + if (status != DistributedKv::Status::SUCCESS) { + ANS_LOGE("kvDataManager GetSingleKvStore failed ret = 0x%{public}x", status); + kvStore_.reset(); + kvDataManager_->StopWatchDeviceChange(deviceCb_); + kvDataManager_.reset(); + return; + } + + KvStoreFlowControlClear(); +} + +bool DistributedScreenStatusManager::CheckKvStore(void) +{ + if (kvStore_ == nullptr) { + GetKvStore(); + } + if (kvStore_ == nullptr) { + ANS_LOGE("kvStore is nullptr."); + return false; + } + return true; +} + +std::string DistributedScreenStatusManager::GenerateDistributedKey(const std::string &deviceId) +{ + return deviceId + DELIMITER + SCREEN_STATUS_LABEL; +} + +ErrCode DistributedScreenStatusManager::CheckRemoteDevicesIsUsing(bool &isUsing) +{ + std::lock_guard lock(mutex_); + if (!CheckKvDataManager() || !CheckKvStore()) { + return ERR_ANS_DISTRIBUTED_OPERATION_FAILED; + } + + if (!KvManagerFlowControl() || !KvStoreFlowControl()) { + ANS_LOGE("flow control."); + return ERR_ANS_DISTRIBUTED_OPERATION_FAILED; + } + + std::vector devInfoList; + DistributedKv::Status status = + kvDataManager_->GetDeviceList(devInfoList, DistributedKv::DeviceFilterStrategy::NO_FILTER); + if (status != DistributedKv::Status::SUCCESS) { + ANS_LOGE("kvDataManager GetDeviceList() failed ret = 0x%{public}x", status); + kvDataManager_.reset(); + return ERR_ANS_DISTRIBUTED_GET_INFO_FAILED; + } + + DistributedKv::Key prefixKey(""); + std::vector entries; + status = kvStore_->GetEntries(prefixKey, entries); + if (status != DistributedKv::Status::SUCCESS) { + ANS_LOGE("kvStore GetEntries() failed ret = 0x%{public}x", status); + kvStore_.reset(); + return ERR_ANS_DISTRIBUTED_GET_INFO_FAILED; + } + + for (auto entry : entries) { + std::string key = entry.key.ToString(); + std::string deviceId = key.substr(0, key.find_first_of(DELIMITER)); + for (auto devInfo : devInfoList) { + if (devInfo.deviceId == deviceId) { + isUsing |= entry.value.ToString() == SCREEN_STATUS_VALUE_ON; + break; + } + } + if (isUsing) { + break; + } + } + + return ERR_OK; +} + +ErrCode DistributedScreenStatusManager::SetLocalScreenStatus(bool screenOn) +{ + std::lock_guard lock(mutex_); + localScreenOn_ = screenOn; + if (!CheckKvStore()) { + return ERR_ANS_DISTRIBUTED_OPERATION_FAILED; + } + + if (!KvManagerFlowControl() || !KvStoreFlowControl()) { + ANS_LOGE("flow control."); + return ERR_ANS_DISTRIBUTED_OPERATION_FAILED; + } + + DistributedKv::DeviceInfo localDevice; + DistributedKv::Status status = kvDataManager_->GetLocalDevice(localDevice); + if (status != DistributedKv::Status::SUCCESS) { + ANS_LOGE("kvDataManager GetLocalDevice() failed ret = 0x%{public}x", status); + return ERR_ANS_DISTRIBUTED_GET_INFO_FAILED; + } + + DistributedKv::Key kvStoreKey = GenerateDistributedKey(localDevice.deviceId); + DistributedKv::Value kvStoreValue = screenOn ? SCREEN_STATUS_VALUE_ON : SCREEN_STATUS_VALUE_OFF; + status = kvStore_->Put(kvStoreKey, kvStoreValue); + if (status != DistributedKv::Status::SUCCESS) { + ANS_LOGE("kvStore Put() failed ret = 0x%{public}x", status); + return ERR_ANS_DISTRIBUTED_OPERATION_FAILED; + } + + return ERR_OK; +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/services/distributed/test/unittest/BUILD.gn b/services/distributed/test/unittest/BUILD.gn index 1a3e4b479..5cdcfe733 100644 --- a/services/distributed/test/unittest/BUILD.gn +++ b/services/distributed/test/unittest/BUILD.gn @@ -48,9 +48,11 @@ ohos_unittest("ans_distributed_unit_test") { "${services_path}/distributed/src/distributed_preferences.cpp", "${services_path}/distributed/src/distributed_preferences_database.cpp", "${services_path}/distributed/src/distributed_preferences_info.cpp", + "${services_path}/distributed/src/distributed_screen_status_manager.cpp", "${services_path}/distributed/test/unittest/distributed_database_test.cpp", "${services_path}/distributed/test/unittest/distributed_notification_manager_test.cpp", "${services_path}/distributed/test/unittest/distributed_preferences_test.cpp", + "${services_path}/distributed/test/unittest/distributed_screen_status_manager_test.cpp", "${services_path}/distributed/test/unittest/mock/ans_test_single_kv_store.cpp", "${services_path}/distributed/test/unittest/mock/mock_blob.cpp", "${services_path}/distributed/test/unittest/mock/mock_change_notification.cpp", diff --git a/services/distributed/test/unittest/distributed_screen_status_manager_test.cpp b/services/distributed/test/unittest/distributed_screen_status_manager_test.cpp new file mode 100644 index 000000000..5a44be1ee --- /dev/null +++ b/services/distributed/test/unittest/distributed_screen_status_manager_test.cpp @@ -0,0 +1,70 @@ +/* + * 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 + +#include "gtest/gtest.h" + +#include "distributed_screen_status_manager.h" + +using namespace testing::ext; +namespace OHOS { +namespace Notification { +class DistributedScreenStatusManagerTest : public testing::Test { +public: + static void SetUpTestCase(){}; + static void TearDownTestCase(){}; + void SetUp() override; + void TearDown() override; +protected: + std::shared_ptr DistributedScreenStatusManager_; +}; + +void DistributedScreenStatusManagerTest::SetUp() +{ + DistributedScreenStatusManager_ = DistributedScreenStatusManager::GetInstance(); +} + +void DistributedScreenStatusManagerTest::TearDown() +{ + DistributedScreenStatusManager_ = nullptr; + DistributedScreenStatusManager::DestroyInstance(); +} + +/** + * @tc.name : DistributedScreenStatusManager_CheckRemoteDevicesIsUsing_00100 + * @tc.number : CheckRemoteDevicesIsUsing_00100 + * @tc.desc : Test CheckRemoteDevicesIsUsing function. + */ +HWTEST_F(DistributedScreenStatusManagerTest, CheckRemoteDevicesIsUsing_00100, Function | SmallTest | Level1) +{ + bool isUsing = true; + + EXPECT_EQ(DistributedScreenStatusManager_->CheckRemoteDevicesIsUsing(isUsing), ERR_OK); +} + +/** + * @tc.name : DistributedScreenStatusManager_SetLocalScreenStatus_00100 + * @tc.number : SetLocalScreenStatus_00100 + * @tc.desc : Get distributed notification enable. + */ +HWTEST_F(DistributedScreenStatusManagerTest, SetLocalScreenStatus_00100, Function | SmallTest | Level1) +{ + bool screenOn = false; + + EXPECT_EQ(DistributedScreenStatusManager_->SetLocalScreenStatus(screenOn), ERR_OK); +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/services/distributed/test/unittest/mock/mock_changeNotification.cpp b/services/distributed/test/unittest/mock/mock_changeNotification.cpp deleted file mode 100644 index 15ab9c823..000000000 --- a/services/distributed/test/unittest/mock/mock_changeNotification.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 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 "change_notification.h" - -namespace OHOS { -namespace DistributedKv { -ChangeNotification::ChangeNotification(const std::list &insertEntries, const std::list &updateEntries, - const std::list &deleteEntries, const std::string &deviceId, const bool isClear) - : insertEntries_(insertEntries), - updateEntries_(updateEntries), - deleteEntries_(deleteEntries), - deviceId_(deviceId), - isClear_(isClear) -{} - -ChangeNotification::~ChangeNotification() -{} - -const std::list &ChangeNotification::GetInsertEntries() const -{ - return this->insertEntries_; -} - -const std::list &ChangeNotification::GetUpdateEntries() const -{ - return this->updateEntries_; -} - -const std::list &ChangeNotification::GetDeleteEntries() const -{ - return this->deleteEntries_; -} - -const std::string &ChangeNotification::GetDeviceId() const -{ - return this->deviceId_; -} - -bool ChangeNotification::IsClear() const -{ - return this->isClear_; -} -} // namespace DistributedKv -} // namespace OHOS \ No newline at end of file diff --git a/services/distributed/test/unittest/mock/mock_event_runner.cpp b/services/distributed/test/unittest/mock/mock_event_runner.cpp index d8890d21f..4e3cdb37c 100644 --- a/services/distributed/test/unittest/mock/mock_event_runner.cpp +++ b/services/distributed/test/unittest/mock/mock_event_runner.cpp @@ -27,4 +27,4 @@ std::shared_ptr EventRunner::Create(bool inNewThread) return std::shared_ptr(new EventRunner(false)); } } // namespace AppExecFwk -} // namespace OHOS \ No newline at end of file +} // namespace OHOS -- Gitee From 11becbd134ccf3e5c8d5ef1356746a6e6d0da847 Mon Sep 17 00:00:00 2001 From: njupthan Date: Tue, 18 Jan 2022 17:11:16 +0800 Subject: [PATCH 2/2] Fix codecheck Signed-off-by: njupthan --- .../test/moduletest/ans_fw_module_test.cpp | 27 +++++++++++-------- .../kits/napi/ans/include/distributed.h | 1 - interfaces/kits/napi/ans/src/distributed.cpp | 2 +- .../distributed_screen_status_manager.h | 6 ++--- .../src/distributed_screen_status_manager.cpp | 10 ++----- ...distributed_screen_status_manager_test.cpp | 2 -- 6 files changed, 22 insertions(+), 26 deletions(-) diff --git a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp index 0e9ef6fd9..b6ac89ee4 100644 --- a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp +++ b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp @@ -406,10 +406,9 @@ public: void SetDistributedScreenStatus(bool isScreenOn) { DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID}; - DistributedKv::StoreId storeId = {.storeId = KVSTORE_NOTIFICATION_STORE_ID}; + DistributedKv::StoreId storeId = {.storeId = KVSTORE_SCREEN_STATUS_STORE_ID}; std::shared_ptr pointer = - DistributedKv::AnsTestSingleKvStore::GetMockKvStorePointer( - {KVSTORE_APP_ID}, {KVSTORE_SCREEN_STATUS_STORE_ID}); + DistributedKv::AnsTestSingleKvStore::GetMockKvStorePointer(appId, storeId); DistributedKv::Key key("" + DELIMITER + "screen_status"); DistributedKv::Value value(isScreenOn ? "on" : "off"); pointer->Put(key, value); @@ -2003,7 +2002,7 @@ HWTEST_F(AnsFWModuleTest, DistributedNotification_Subscribe_00200, Function | Me */ HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_GetDeviceRemindType_00100, Function | MediumTest | Level1) { - auto rType{NotificationConstant::RemindType::NONE}; + auto rType = NotificationConstant::RemindType::NONE; EXPECT_EQ(NotificationHelper::GetDeviceRemindType(rType), ERR_OK); ANS_LOGI("ANS_Interface_MT_GetDeviceRemindType_00100:: rType : %{public}d", static_cast(rType)); @@ -2019,8 +2018,10 @@ HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_GetDeviceRemindType_00100, Function | HWTEST_F(AnsFWModuleTest, ScreenStatusChange_00100, Function | MediumTest | Level1) { ANS_LOGI("%{public}s", test_info_->name()); + DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID}; + DistributedKv::StoreId storeId = {.storeId = KVSTORE_SCREEN_STATUS_STORE_ID}; std::shared_ptr pointer = - DistributedKv::AnsTestSingleKvStore::GetMockKvStorePointer({KVSTORE_APP_ID}, {KVSTORE_SCREEN_STATUS_STORE_ID}); + DistributedKv::AnsTestSingleKvStore::GetMockKvStorePointer(appId, storeId); DistributedKv::Key key(""); std::vector entries; @@ -2170,10 +2171,11 @@ HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00400, Function | MediumTest | Lev HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00500, Function | MediumTest | Level1) { ANS_LOGI("%{public}s", test_info_->name()); + std::vector devices = {""}; NotificationRequest request = CreateDistributedRequest(test_info_->name()); request.SetOwnerBundleName(APP_NAME); request.SetCreatorBundleName(APP_NAME); - request.SetDevicesSupportDisplay({""}); + request.SetDevicesSupportDisplay(devices); std::string jsonString; NotificationJsonConverter::ConvertToJosnString(&request, jsonString); @@ -2181,9 +2183,10 @@ HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00500, Function | MediumTest | Lev TestAnsSubscriber subscriber; EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); - + DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID}; + DistributedKv::StoreId storeId = {.storeId = KVSTORE_SCREEN_STATUS_STORE_ID}; std::shared_ptr pointer = - DistributedKv::AnsTestSingleKvStore::GetMockKvStorePointer({KVSTORE_APP_ID}, {KVSTORE_NOTIFICATION_STORE_ID}); + DistributedKv::AnsTestSingleKvStore::GetMockKvStorePointer(appId, storeId); DistributedKv::Key key(GenerateDistributedKey(request, REMOTE_DEVICE_ID)); DistributedKv::Value value(jsonString); @@ -2211,10 +2214,11 @@ HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00500, Function | MediumTest | Lev HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00600, Function | MediumTest | Level1) { ANS_LOGI("%{public}s", test_info_->name()); + std::vector devices = {""}; NotificationRequest request = CreateDistributedRequest(test_info_->name()); request.SetOwnerBundleName(APP_NAME); request.SetCreatorBundleName(APP_NAME); - request.SetDevicesSupportDisplay({""}); + request.SetDevicesSupportDisplay(devices); std::string jsonString; NotificationJsonConverter::ConvertToJosnString(&request, jsonString); @@ -2222,9 +2226,10 @@ HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00600, Function | MediumTest | Lev TestAnsSubscriber subscriber; EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); - + DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID}; + DistributedKv::StoreId storeId = {.storeId = KVSTORE_SCREEN_STATUS_STORE_ID}; std::shared_ptr pointer = - DistributedKv::AnsTestSingleKvStore::GetMockKvStorePointer({KVSTORE_APP_ID}, {KVSTORE_NOTIFICATION_STORE_ID}); + DistributedKv::AnsTestSingleKvStore::GetMockKvStorePointer(appId, storeId); DistributedKv::Key key(GenerateDistributedKey(request, REMOTE_DEVICE_ID)); DistributedKv::Value value(jsonString); diff --git a/interfaces/kits/napi/ans/include/distributed.h b/interfaces/kits/napi/ans/include/distributed.h index 4facf772b..ad379f9d3 100644 --- a/interfaces/kits/napi/ans/include/distributed.h +++ b/interfaces/kits/napi/ans/include/distributed.h @@ -28,7 +28,6 @@ napi_value EnableDistributedByBundle(napi_env env, napi_callback_info info); napi_value EnableDistributedSelf(napi_env env, napi_callback_info info); napi_value IsDistributedEnableByBundle(napi_env env, napi_callback_info info); napi_value GetDeviceRemindType(napi_env env, napi_callback_info info); - } // namespace NotificationNapi } // namespace OHOS diff --git a/interfaces/kits/napi/ans/src/distributed.cpp b/interfaces/kits/napi/ans/src/distributed.cpp index dee8dcf53..01e1e541d 100644 --- a/interfaces/kits/napi/ans/src/distributed.cpp +++ b/interfaces/kits/napi/ans/src/distributed.cpp @@ -506,7 +506,7 @@ void AsyncCompleteCallbackGetDeviceRemindType(napi_env env, napi_status status, if (asynccallbackinfo->info.errorCode != ERR_OK) { result = Common::NapiGetNull(env); } else { - enum DeviceRemindType outType = DeviceRemindType::IDLE_DONOT_REMIND; + DeviceRemindType outType = DeviceRemindType::IDLE_DONOT_REMIND; if (!Common::DeviceRemindTypeCToJS(asynccallbackinfo->remindType, outType)) { asynccallbackinfo->info.errorCode = ERROR; result = Common::NapiGetNull(env); diff --git a/services/distributed/include/distributed_screen_status_manager.h b/services/distributed/include/distributed_screen_status_manager.h index a9308fdba..9ac03bb99 100644 --- a/services/distributed/include/distributed_screen_status_manager.h +++ b/services/distributed/include/distributed_screen_status_manager.h @@ -49,9 +49,9 @@ private: std::recursive_mutex mutex_; std::shared_ptr runner_ = nullptr; std::shared_ptr handler_ = nullptr; - std::unique_ptr kvDataManager_{}; - std::shared_ptr kvStore_{}; - std::shared_ptr deviceCb_{}; + std::unique_ptr kvDataManager_ = nullptr; + std::shared_ptr kvStore_ = nullptr; + std::shared_ptr deviceCb_ = nullptr; bool localScreenOn_ = false; diff --git a/services/distributed/src/distributed_screen_status_manager.cpp b/services/distributed/src/distributed_screen_status_manager.cpp index 5503562bf..0ef6ee20f 100644 --- a/services/distributed/src/distributed_screen_status_manager.cpp +++ b/services/distributed/src/distributed_screen_status_manager.cpp @@ -41,9 +41,7 @@ DistributedScreenStatusManager::DistributedScreenStatusManager() : DistributedFl } DistributedScreenStatusManager::~DistributedScreenStatusManager() -{ - -} +{} void DistributedScreenStatusManager::OnDeviceConnected(const std::string &deviceId) { @@ -87,7 +85,6 @@ void DistributedScreenStatusManager::GetKvDataManager(void) { kvDataManager_ = std::make_unique(); if (kvDataManager_ != nullptr) { - DistributedKv::Status status = kvDataManager_->StartWatchDeviceChange(deviceCb_); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGW("kvDataManager StartWatchDeviceChange failed ret = 0x%{public}x", status); @@ -124,10 +121,7 @@ void DistributedScreenStatusManager::GetKvStore(void) DistributedKv::AppId appId = {.appId = APP_ID}; DistributedKv::StoreId storeId = {.storeId = STORE_ID}; - status = kvDataManager_->GetSingleKvStore(options, - appId, - storeId, - kvStore_); + status = kvDataManager_->GetSingleKvStore(options, appId, storeId, kvStore_); if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("kvDataManager GetSingleKvStore failed ret = 0x%{public}x", status); kvStore_.reset(); diff --git a/services/distributed/test/unittest/distributed_screen_status_manager_test.cpp b/services/distributed/test/unittest/distributed_screen_status_manager_test.cpp index 5a44be1ee..e1b68b00b 100644 --- a/services/distributed/test/unittest/distributed_screen_status_manager_test.cpp +++ b/services/distributed/test/unittest/distributed_screen_status_manager_test.cpp @@ -24,8 +24,6 @@ namespace OHOS { namespace Notification { class DistributedScreenStatusManagerTest : public testing::Test { public: - static void SetUpTestCase(){}; - static void TearDownTestCase(){}; void SetUp() override; void TearDown() override; protected: -- Gitee