From 80804fa2e0481ea7cc13894ceeed6da7b15e11da Mon Sep 17 00:00:00 2001 From: pancras Date: Fri, 15 Nov 2024 15:29:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9A=82=E5=81=9C=E5=92=8C?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E9=95=BF=E6=97=B6=E4=BB=BB=E5=8A=A1=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E6=A0=8F=E8=AE=A1=E6=97=B6=E5=99=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: pancras --- frameworks/ans/src/notification_helper.cpp | 5 + .../unittest/notification_helper_test.cpp | 14 ++ .../core/include/ans_manager_interface.h | 8 ++ frameworks/core/include/ans_manager_proxy.h | 8 ++ frameworks/core/include/ans_manager_stub.h | 8 ++ frameworks/core/include/ans_notification.h | 8 ++ ..._notification_service_ipc_interface_code.h | 1 + frameworks/core/src/ans_notification.cpp | 10 ++ .../core/src/manager/ans_manager_proxy.cpp | 133 ++++++++++++++++++ .../core/src/manager/ans_manager_stub.cpp | 16 +++ .../src/manager/ans_manager_stub_invalid.cpp | 6 + .../ans_manager_proxy_unit_test.cpp | 22 +++ .../ans_manager_stub_test.cpp | 13 ++ .../ans_notification_branch_test.cpp | 5 + interfaces/inner_api/notification_helper.h | 8 ++ .../include/advanced_notification_service.h | 3 +- ...dvanced_notification_live_view_service.cpp | 28 ++++ .../ans/src/advanced_notification_service.cpp | 21 +++ ...ed_notification_live_view_service_test.cpp | 32 +++++ 19 files changed, 348 insertions(+), 1 deletion(-) diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 72308111f..45f7340a3 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -582,5 +582,10 @@ ErrCode NotificationHelper::GetDoNotDisturbProfile(int32_t id, sptr::GetInstance()->GetDoNotDisturbProfile(id, profile); } + +ErrCode NotificationHelper::UpdateNotificationTimerByUid(const int32_t uid, const bool isPaused) +{ + return DelayedSingleton::GetInstance()->UpdateNotificationTimerByUid(uid, isPaused); +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/test/unittest/notification_helper_test.cpp b/frameworks/ans/test/unittest/notification_helper_test.cpp index d4ee06e6c..5432fc717 100644 --- a/frameworks/ans/test/unittest/notification_helper_test.cpp +++ b/frameworks/ans/test/unittest/notification_helper_test.cpp @@ -1292,5 +1292,19 @@ HWTEST_F(NotificationHelperTest, IsNeedSilentInDoNotDisturbMode_00001, Function ErrCode ret = notificationHelper.IsNeedSilentInDoNotDisturbMode(phoneNumber, callerType); EXPECT_EQ(ret, (int)ERR_ANS_PERMISSION_DENIED); } + +/** + * @tc.name: UpdateNotificationTimerByUid_00001 + * @tc.desc: Test UpdateNotificationTimerByUid. + * @tc.type: FUNC + */ +HWTEST_F(NotificationHelperTest, UpdateNotificationTimerByUid_00001, Function | SmallTest | Level1) +{ + int32_t uid = 20099999; + bool isPaused = true; + NotificationHelper notificationHelper; + ErrCode ret = notificationHelper.UpdateNotificationTimerByUid(uid, isPaused); + EXPECT_EQ(ret, (int)ERR_ANS_NOT_SYSTEM_SERVICE); +} } } diff --git a/frameworks/core/include/ans_manager_interface.h b/frameworks/core/include/ans_manager_interface.h index 2a1ae2880..831d1e625 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -957,6 +957,14 @@ public: */ virtual ErrCode RegisterSwingCallback(const sptr& swingCallback) = 0; #endif + + /** + * @brief Update Notification Timer by uid. + * + * @param uid uid. + * @return Returns Update result. + */ + virtual ErrCode UpdateNotificationTimerByUid(const int32_t uid, const bool isPaused) = 0; }; } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/include/ans_manager_proxy.h b/frameworks/core/include/ans_manager_proxy.h index 2c6afcea9..88078cae4 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -947,6 +947,14 @@ public: ErrCode RegisterSwingCallback(const sptr &swingCallback) override; #endif + /** + * @brief Update Notification Timer by uid. + * + * @param uid uid. + * @return Returns Update result. + */ + ErrCode UpdateNotificationTimerByUid(const int32_t uid, const bool isPaused) override; + private: ErrCode InnerTransact(NotificationInterfaceCode code, MessageOption &flags, MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/core/include/ans_manager_stub.h b/frameworks/core/include/ans_manager_stub.h index bdd0e5bbd..7b0fce8f1 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -948,6 +948,14 @@ public: */ ErrCode RegisterSwingCallback(const sptr& swingCallback) override; #endif + + /** + * @brief Update Notification Timer by uid. + * + * @param uid uid. + * @return Returns Update result. + */ + virtual ErrCode UpdateNotificationTimerByUid(const int32_t uid, const bool isPaused) override; private: ErrCode HandlePublish(MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index 20287108d..fd332db0d 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -1189,6 +1189,14 @@ public: * @brief Ans service died, OnRemoteDied called. */ void OnServiceDied(); + + /** + * @brief Update Notification Timer by uid. + * + * @param uid uid. + * @return Returns Update result. + */ + ErrCode UpdateNotificationTimerByUid(const int32_t uid, const bool isPaused); private: /** * @brief Gets Ans Manager proxy. diff --git a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h index ff7a3606c..fc620668a 100644 --- a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h +++ b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h @@ -151,6 +151,7 @@ namespace Notification { DEL_EXCLUDE_DATES_REMINDER, GET_EXCLUDE_DATES_REMINDER, GET_DONOTDISTURB_PROFILE, + UPDATE_NOTIFICATION_TIMER, }; } } diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index 00fb37e7e..ea54d047d 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -2024,5 +2024,15 @@ ErrCode AnsNotification::RegisterSwingCallback(const std::functionRegisterSwingCallback(swingCallBackStub_->AsObject()); } #endif + +ErrCode AnsNotification::UpdateNotificationTimerByUid(const int32_t uid, const bool isPaused) +{ + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("UpdateNotificationTimerByUid fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + return proxy->UpdateNotificationTimerByUid(uid, isPaused); +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/src/manager/ans_manager_proxy.cpp b/frameworks/core/src/manager/ans_manager_proxy.cpp index df3cedcb6..8de7da3d1 100644 --- a/frameworks/core/src/manager/ans_manager_proxy.cpp +++ b/frameworks/core/src/manager/ans_manager_proxy.cpp @@ -947,5 +947,138 @@ ErrCode AnsManagerProxy::CancelAsBundleWithAgent(const sptr &swingCallback) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteRemoteObject(swingCallback)) { + ANS_LOGE("write swingCallback failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = { MessageOption::TF_SYNC }; + ErrCode result = InnerTransact(NotificationInterfaceCode::REGISTER_SWING_CALLBACK, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} +#endif + +ErrCode AnsManagerProxy::UpdateNotificationTimerByUid(const int32_t uid, const bool isPaused) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(uid)) { + ANS_LOGE("write uid failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteBool(isPaused)) { + ANS_LOGE("write isPaused failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = { MessageOption::TF_SYNC }; + ErrCode result = InnerTransact(NotificationInterfaceCode::UPDATE_NOTIFICATION_TIMER, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/src/manager/ans_manager_stub.cpp b/frameworks/core/src/manager/ans_manager_stub.cpp index 92edc77a2..1d0172713 100644 --- a/frameworks/core/src/manager/ans_manager_stub.cpp +++ b/frameworks/core/src/manager/ans_manager_stub.cpp @@ -449,6 +449,10 @@ int32_t AnsManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Mess result = HandleGetDoNotDisturbProfile(data, reply); break; } + case static_cast(NotificationInterfaceCode::UPDATE_NOTIFICATION_TIMER): { + result = HandleUpdateNotificationTimerByUid(data, reply); + break; + } default: { ANS_LOGE("[OnRemoteRequest] fail: unknown code!"); return IPCObjectStub::OnRemoteRequest(code, data, reply, flags); @@ -2650,5 +2654,17 @@ ErrCode AnsManagerStub::HandleRegisterSwingCallback(MessageParcel &data, Message return result; } #endif + +ErrCode AnsManagerStub::HandleUpdateNotificationTimerByUid(MessageParcel &data, MessageParcel &reply) +{ + int32_t uid = data.ReadInt32(); + bool isPaused = data.ReadBool(); + ErrCode result = UpdateNotificationTimerByUid(uid, isPaused); + if (!reply.WriteInt32(result)) { + ANS_LOGE("[HandleUpdateNotificationTimerByUid] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + return ERR_OK; +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/src/manager/ans_manager_stub_invalid.cpp b/frameworks/core/src/manager/ans_manager_stub_invalid.cpp index e447bad8a..8d57b0365 100644 --- a/frameworks/core/src/manager/ans_manager_stub_invalid.cpp +++ b/frameworks/core/src/manager/ans_manager_stub_invalid.cpp @@ -659,5 +659,11 @@ ErrCode AnsManagerStub::RegisterSwingCallback(const sptr& swingCa return ERR_INVALID_OPERATION; } #endif + +ErrCode AnsManagerStub::UpdateNotificationTimerByUid(const int32_t uid, const bool isPaused) +{ + ANS_LOGE("UpdateNotificationTimerByUid called!"); + return ERR_INVALID_OPERATION; +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/test/unittest/ans_manager_proxy_test/ans_manager_proxy_unit_test.cpp b/frameworks/core/test/unittest/ans_manager_proxy_test/ans_manager_proxy_unit_test.cpp index dbfaabf11..715472d49 100644 --- a/frameworks/core/test/unittest/ans_manager_proxy_test/ans_manager_proxy_unit_test.cpp +++ b/frameworks/core/test/unittest/ans_manager_proxy_test/ans_manager_proxy_unit_test.cpp @@ -7977,5 +7977,27 @@ HWTEST_F(AnsManagerProxyUnitTest, IsNeedSilentInDoNotDisturbModeTest_0500, Funct int32_t result = proxy->IsNeedSilentInDoNotDisturbMode(phoneNumber, callerType); EXPECT_EQ(ERR_OK, result); } + +/* + * @tc.name: UpdateNotificationTimerByUid_0100 + * @tc.desc: test UpdateNotificationTimerByUid function + * @tc.type: FUNC + */ +HWTEST_F(AnsManagerProxyUnitTest, UpdateNotificationTimerByUid_0100, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsManagerProxyUnitTest, UpdateNotificationTimerByUid_0100, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1) + .WillRepeatedly(DoAll(Invoke(std::bind(SendRequestReplace, _1, _2, _3, _4, + ERR_OK, true, false, false)), Return(NO_ERROR))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + int32_t uid = 20099999; + bool isPaused = true; + int32_t result = proxy->UpdateNotificationTimerByUid(uid, isPaused); + EXPECT_EQ(ERR_OK, result); +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/test/unittest/ans_manager_stub_test/ans_manager_stub_test.cpp b/frameworks/core/test/unittest/ans_manager_stub_test/ans_manager_stub_test.cpp index 8623c8d42..e73dd2d11 100644 --- a/frameworks/core/test/unittest/ans_manager_stub_test/ans_manager_stub_test.cpp +++ b/frameworks/core/test/unittest/ans_manager_stub_test/ans_manager_stub_test.cpp @@ -5413,5 +5413,18 @@ HWTEST_F(AnsManagerStubTest, IsNeedSilentInDoNotDisturbMode01, Function | SmallT ErrCode result = ansManagerStub_->IsNeedSilentInDoNotDisturbMode(phoneNumber, callerType); EXPECT_EQ(result, (int)ERR_INVALID_OPERATION); } + +/** + * @tc.name: UpdateNotificationTimerByUid_01 + * @tc.desc: Test UpdateNotificationTimerByUid return. + * @tc.type: FUNC + */ +HWTEST_F(AnsManagerStubTest, UpdateNotificationTimerByUid_01, Function | SmallTest | Level1) +{ + int32_t uid = 20099999; + bool isPaused = true; + ErrCode result = ansManagerStub_->UpdateNotificationTimerByUid(uid, isPaused); + EXPECT_EQ(result, (int)ERR_INVALID_OPERATION); +} } } diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index e89508e46..b3307a9f9 100644 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -583,6 +583,11 @@ public: return ERR_ANS_INVALID_PARAM; } #endif + + ErrCode UpdateNotificationTimerByUid(const int32_t uid, const bool isPaused) override + { + return ERR_ANS_INVALID_PARAM; + } }; class AnsNotificationBranchTest : public testing::Test { diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 9a9dc2111..6b021c875 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -1101,6 +1101,14 @@ public: * @return Returns ERR_OK on success, others on failure. */ static ErrCode GetDoNotDisturbProfile(int32_t id, sptr &profile); + + /** + * @brief Update Notification Timer by uid + * + * @param uid uid. + * @return Returns Update result. + */ + static ErrCode UpdateNotificationTimerByUid(const int32_t uid, const bool isPaused); }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 7ed7f0e99..c54da1378 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1413,7 +1413,8 @@ private: bool enabled, bool isForceControl); void ResetDistributedEnabled(); ErrCode OnRecoverLiveView(const std::vector &keys); - + void HandleUpdateLiveViewNotificationTimer(const int32_t uid, const bool isPaused); + private: static sptr instance_; static std::mutex instanceMutex_; diff --git a/services/ans/src/advanced_notification_live_view_service.cpp b/services/ans/src/advanced_notification_live_view_service.cpp index e2721b805..e25ea3b7c 100644 --- a/services/ans/src/advanced_notification_live_view_service.cpp +++ b/services/ans/src/advanced_notification_live_view_service.cpp @@ -40,6 +40,7 @@ namespace OHOS { namespace Notification { const std::string LOCK_SCREEN_PICTURE_TAG = "lock_screen_picture"; const std::string PROGRESS_VALUE = "progressValue"; +constexpr int32_t BGTASK_UID = 3051; void AdvancedNotificationService::RecoverLiveViewFromDb(int32_t userId) { if (notificationSvrQueue_ == nullptr) { @@ -746,5 +747,32 @@ void AdvancedNotificationService::StartFinishTimerForUpdate( NotificationConstant::TRIGGER_THIRTY_MINUTES_REASON_DELETE); } } + +void AdvancedNotificationService::HandleUpdateLiveViewNotificationTimer(const int32_t uid, const bool isPaused) +{ + for (const auto &record : notificationList_) { + const auto &request = record->request; + if (!request || request->GetOwnerUid() != uid) { + continue; + } + if (!request->GetContent() || !request->GetContent()->GetNotificationContent()) { + continue; + } + + bool isContinuousLiveView = request->IsSystemLiveView() && request->GetCreatorUid() == BGTASK_UID; + const auto &liveViewContent = std::static_pointer_cast( + request->GetContent()->GetNotificationContent()); + if (isContinuousLiveView && liveViewContent->GetType() == TYPE_CODE_DOWNLOAD) { + if (isPaused) { + ANS_LOGI("liveview notification timer is being cancelled, uid: %{public}d", uid); + CancelTimer(record->notification->GetFinishTimer()); + } else { + ANS_LOGI("liveview notification timer is being reset, uid: %{public}d", uid); + StartFinishTimer(record, GetCurrentTime() + NotificationConstant::FIFTEEN_MINUTES, + NotificationConstant::TRIGGER_THIRTY_MINUTES_REASON_DELETE); + } + } + } +} } } diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 093ce403e..d78f5dc35 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -89,6 +89,7 @@ constexpr int32_t MAX_LIVEVIEW_HINT_COUNT = 1; constexpr int32_t MAX_SOUND_ITEM_LENGTH = 2048; constexpr int32_t BUNDLE_OPTION_UID_DEFAULT_VALUE = 0; constexpr int32_t RSS_UID = 3051; +constexpr int32_t RESSCHED_UID = 1096; const std::string DO_NOT_DISTURB_MODE = "1"; constexpr const char *KEY_UNIFIED_GROUP_ENABLE = "unified_group_enable"; @@ -2342,6 +2343,26 @@ ErrCode AdvancedNotificationService::RegisterSwingCallback(const sptrsubmit_h(std::bind([&]() { + HandleUpdateLiveViewNotificationTimer(uid, isPaused); + })); + notificationSvrQueue_->wait(handler); + return ERR_OK; +} + void PushCallbackRecipient::OnRemoteDied(const wptr &remote) { ANS_LOGI("Push Callback died, remove the proxy object"); diff --git a/services/ans/test/unittest/advanced_notification_live_view_service_test.cpp b/services/ans/test/unittest/advanced_notification_live_view_service_test.cpp index 109ae8d0e..bb1b585d2 100644 --- a/services/ans/test/unittest/advanced_notification_live_view_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_live_view_service_test.cpp @@ -306,5 +306,37 @@ HWTEST_F(AnsLiveViewServiceTest, IsSaCreateSystemLiveViewAsBundle_002, Function flag = advancedNotificationService_->IsSaCreateSystemLiveViewAsBundle(record, creatorUid); ASSERT_EQ(flag, false); } + +/** + * @tc.name: HandleUpdateLiveViewNotificationTimer_001 + * @tc.desc: Test HandleUpdateLiveViewNotificationTimer + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsLiveViewServiceTest, HandleUpdateLiveViewNotificationTimer_001, Function | SmallTest | Level1) +{ + sptr request = new (std::nothrow) NotificationRequest(); + auto slotType = NotificationConstant::SlotType::LIVE_VIEW; + request->SetSlotType(slotType); + request->SetNotificationId(1); + int32_t TYPE_CODE_DOWNLOAD = 8; + auto localLiveViewContent = std::make_shared(); + localLiveViewContent->SetType(TYPE_CODE_DOWNLOAD); + auto content = std::make_shared(localLiveViewContent); + request->SetContent(content); + int creatorUid = 3051; + request->SetCreatorUid(creatorUid); + int ownerUid = 20099999; + request->SetOwnerUid(ownerUid); + sptr bundle = new NotificationBundleOption("test", ownerUid); + auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle); + advancedNotificationService_->AddToNotificationList(record); + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE); + auto timer = record->notification->GetFinishTimer(); + advancedNotificationService_->HandleUpdateLiveViewNotificationTimer(ownerUid, true); + ASSERT_EQ(timer, record->notification->GetFinishTimer()); + advancedNotificationService_->HandleUpdateLiveViewNotificationTimer(ownerUid, false); + ASSERT_NE(timer, record->notification->GetFinishTimer()); +} } // namespace Notification } // namespace OHOS -- Gitee