diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 72308111f5334653b8002fd0072bd7e5761d179e..45f7340a3b3bd95ce05685b44df567f3de445346 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 d4ee06e6cd5ded58c35c0dd90b22f5da41368fe2..5432fc717757376547de856de5d75b25d4b34497 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 2a1ae28808c50228aeafd85157c86377867e80ca..831d1e6258498a3752f7b09d52e0f882b3f74d78 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 2c6afcea9e3f93c59b5aa5639f2c40facd777ade..88078cae4364597ed2ddce136dc30fc471685e22 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 bdd0e5bbd636a5c306082e55af91a91225e13e6f..7b0fce8f16e95cf2ac7deb6cee6e1f87394539ce 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 20287108d1d82cffd472e37936f2c6c37ce96070..fd332db0dc436d5a6b09e8c6d20f8dca4534d143 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 ff7a3606ca0b4a9614e607308e3d5816e85adfa5..fc620668a10c95012a4736cd5e04e30abc1e5db4 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 00fb37e7ee692b7dbe23bc0f2b79fea1c01a131c..ea54d047d6c3fef95844e0c3c2b6f0a3a9b810ed 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 df3cedcb6ca5cb6246e999afd50432b70a350cd9..8de7da3d1f8679ab7333bf18683e0c9048338831 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 92edc77a2256cdbfa761ae4f7949c1eade38bf3f..1d01727132631ab19b48b643ef865d47725edf27 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 e447bad8afe68a2c54b960192c0e84c1cd7219ed..8d57b0365fc8cdb1598d1b4560c1bb5dde5b229e 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 dbfaabf1164d1f077c98e2d7111d83d991692a87..715472d49b1d6c121dd4431ad4460e8984057726 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 8623c8d42e43545b472dbbcd81de88ca2941bf66..e73dd2d116d71ec060e75d6eb99643ddbfc7cee2 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 e89508e461a0f19330ac3aa8228be12c21897d5d..b3307a9f9cc42173a39e0e31bfefb57d7724841e 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 9a9dc21118bb311b62bcc2156f847280b4478d3c..6b021c8754bc3c610ef439957f1336e6b076cf7a 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 7ed7f0e9918e9660cd015371868c8b959bd20c99..c54da13788aa9bcb0347e98e1f6d55087a691188 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 e2721b80541806180b68134575d5477726a5be29..e25ea3b7c23f708f158d285e9b8b554fee59f11e 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 093ce403efef2d7d147eb8541f2397f4c14ef9d2..d78f5dc35be5353a6afa0cd2cdc5d61906c7fe43 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 109ae8d0e593a6cb6691bfac752dfc52333412e3..bb1b585d2cdefaa37808faad687723022a4fefb2 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