diff --git a/frameworks/include/background_task_manager.h b/frameworks/include/background_task_manager.h index c4b7e1228f7336616dbc1978413a4092d76750ac..7f1213fcd9d12ac032bb0a777ceb039bf41609a5 100644 --- a/frameworks/include/background_task_manager.h +++ b/frameworks/include/background_task_manager.h @@ -258,6 +258,13 @@ public: */ ErrCode SetBgTaskConfig(const std::string &configData, int32_t sourceType); + /** + * @brief Request suspend continuous audio playback task. + * @param uid app uid. + * @return Returns ERR_OK if success, else failure. + */ + ErrCode SuspendContinuousAudioTask(int32_t uid); + private: bool GetBackgroundTaskManagerProxy(); diff --git a/frameworks/src/background_task_manager.cpp b/frameworks/src/background_task_manager.cpp index c3ea7c8dd8cc242c77b46d292cea225d33421b3a..6c7d5b2a382ae80d482a5fefb1669bb7f99d29ec 100644 --- a/frameworks/src/background_task_manager.cpp +++ b/frameworks/src/background_task_manager.cpp @@ -464,6 +464,14 @@ ErrCode BackgroundTaskManager::SetBgTaskConfig(const std::string &configData, in return proxy_->SetBgTaskConfig(configData, sourceType); } +ErrCode BackgroundTaskManager::SuspendContinuousAudioTask(int32_t uid) +{ + std::lock_guard lock(mutex_); + GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN + + return proxy_->SuspendContinuousAudioTask(uid); +} + BackgroundTaskManager::BgTaskMgrDeathRecipient::BgTaskMgrDeathRecipient(BackgroundTaskManager &backgroundTaskManager) : backgroundTaskManager_(backgroundTaskManager) {} diff --git a/frameworks/test/unittest/bgtask_framework_abnormal_unit_test.cpp b/frameworks/test/unittest/bgtask_framework_abnormal_unit_test.cpp index 2a46ab3d6c54bfe8857c2ea84c3abb27361b3093..0d4f3dcff87a8a8565d3bd2d15f23cbd941020c7 100644 --- a/frameworks/test/unittest/bgtask_framework_abnormal_unit_test.cpp +++ b/frameworks/test/unittest/bgtask_framework_abnormal_unit_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -649,5 +649,23 @@ HWTEST_F(BgTaskFrameworkAbnormalUnitTest, BackgroundTaskMgrProxyAbnormalTest_019 MessageParcelHelper::BgTaskFwkAbnormalSetWriteReadInt32WithParamFlag(false); EXPECT_EQ(backgroundTaskMgrProxy.ActiveContinuousTask(1, 1, ""), ERR_INVALID_DATA); } + +/** + * @tc.name: BackgroundTaskMgrProxyAbnormalTest_020 + * @tc.desc: test BackgroundTaskMgrProxy abnormal. + * @tc.type: FUNC + * @tc.require: issueICT1ZV + */ +HWTEST_F(BgTaskFrameworkAbnormalUnitTest, BackgroundTaskMgrProxyAbnormalTest_020, TestSize.Level2) +{ + BackgroundTaskMgrProxy backgroundTaskMgrProxy = BackgroundTaskMgrProxy(nullptr); + + MessageParcelHelper::BgTaskFwkAbnormalSetWriteInterfaceTokenFlag(false); + EXPECT_EQ(backgroundTaskMgrProxy.SuspendContinuousAudioTask(1), ERR_INVALID_VALUE); + + MessageParcelHelper::BgTaskFwkAbnormalSetWriteInterfaceTokenFlag(true); + MessageParcelHelper::BgTaskFwkAbnormalSetWriteInt32WithParamFlag(false); + EXPECT_EQ(backgroundTaskMgrProxy.SuspendContinuousAudioTask(1), ERR_INVALID_DATA); +} } } diff --git a/frameworks/test/unittest/bgtask_framework_unit_test.cpp b/frameworks/test/unittest/bgtask_framework_unit_test.cpp index 9a771a93abf131a48026ba263d694eda5e34ac05..d6c713080aa2c3b7f1d9fcf0d7ccd88a3f57ef45 100644 --- a/frameworks/test/unittest/bgtask_framework_unit_test.cpp +++ b/frameworks/test/unittest/bgtask_framework_unit_test.cpp @@ -914,5 +914,23 @@ HWTEST_F(BgTaskFrameworkUnitTest, AVSessionNotifyUpdateNotification_001, TestSiz EXPECT_EQ(DelayedSingleton::GetInstance()-> AVSessionNotifyUpdateNotification(uid, pid, true), ERR_BGTASK_CHECK_TASK_PARAM); } + +/** + * @tc.name: SuspendContinuousAudioTask_001 + * @tc.desc: test SuspendContinuousAudioTask. + * @tc.type: FUNC + * @tc.require: issueICT1ZV + */ +HWTEST_F(BgTaskFrameworkUnitTest, SuspendContinuousAudioTask_001, TestSize.Level1) +{ + DelayedSingleton::GetInstance()->proxy_ = nullptr; + SystemAbilityManagerClient::GetInstance().action_ = "set_null"; + EXPECT_EQ(DelayedSingleton::GetInstance()->SuspendContinuousAudioTask(1), + ERR_BGTASK_SERVICE_NOT_CONNECTED); + + SystemAbilityManagerClient::GetInstance().action_ = ""; + EXPECT_EQ(DelayedSingleton::GetInstance()->SuspendContinuousAudioTask(1), + ERR_BGTASK_PERMISSION_DENIED); +} } } diff --git a/interfaces/innerkits/IBackgroundTaskMgr.idl b/interfaces/innerkits/IBackgroundTaskMgr.idl index e5c98131ee80a8b31172b83d455c86324eeb0451..3d6b2e276aecbb67f0737b8d2e6ceffa0c160a9a 100644 --- a/interfaces/innerkits/IBackgroundTaskMgr.idl +++ b/interfaces/innerkits/IBackgroundTaskMgr.idl @@ -52,4 +52,5 @@ interface OHOS.BackgroundTaskMgr.IBackgroundTaskMgr { void StartTransientTaskTimeForInner([in] int uid); void AVSessionNotifyUpdateNotification([in] int uid, [in] int pid, [in] boolean isPublish); void SetBgTaskConfig([in] String configData, [in] int sourceType); + void SuspendContinuousAudioTask([in] int uid); } diff --git a/interfaces/innerkits/include/background_task_mgr_helper.h b/interfaces/innerkits/include/background_task_mgr_helper.h index a5ff46ed95645a04e02e0f2665ef954ba75aea7c..9f1061fabc739d6b65dc3c20ecbc049d05daac71 100644 --- a/interfaces/innerkits/include/background_task_mgr_helper.h +++ b/interfaces/innerkits/include/background_task_mgr_helper.h @@ -200,6 +200,13 @@ public: * @return Returns ERR_OK if success, else failure. */ static ErrCode SetBgTaskConfig(const std::string &configData, int32_t sourceType); + + /** + * @brief Request suspend continuous audio playback task. + * @param uid app uid. + * @return Returns ERR_OK if success, else failure. + */ + static ErrCode SuspendContinuousAudioTask(int32_t uid); }; } // namespace BackgroundTaskMgr } // namespace OHOS diff --git a/interfaces/innerkits/src/background_task_mgr_helper.cpp b/interfaces/innerkits/src/background_task_mgr_helper.cpp index 320092898d0c47bd101b9b54708be00a0a440b41..218619e6bf7af392218e7a1dc60b3cf56494a818 100644 --- a/interfaces/innerkits/src/background_task_mgr_helper.cpp +++ b/interfaces/innerkits/src/background_task_mgr_helper.cpp @@ -134,5 +134,10 @@ ErrCode BackgroundTaskMgrHelper::AVSessionNotifyUpdateNotification(int32_t uid, return DelayedSingleton::GetInstance()-> AVSessionNotifyUpdateNotification(uid, pid, isPublish); } + +ErrCode BackgroundTaskMgrHelper::SuspendContinuousAudioTask(int32_t uid) +{ + return DelayedSingleton::GetInstance()->SuspendContinuousAudioTask(uid); +} } // namespace BackgroundTaskMgr } // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/bgtask_manager_client_test/bgtask_client_unit_test.cpp b/interfaces/test/unittest/bgtask_manager_client_test/bgtask_client_unit_test.cpp index 05702633a14fcb89d12d5ef1b1c00ce10879a1b0..aa48acb3596e7d077eae739e51002f50e6e544c0 100644 --- a/interfaces/test/unittest/bgtask_manager_client_test/bgtask_client_unit_test.cpp +++ b/interfaces/test/unittest/bgtask_manager_client_test/bgtask_client_unit_test.cpp @@ -892,5 +892,17 @@ HWTEST_F(BgTaskClientUnitTest, AVSessionNotifyUpdateNotification_001, TestSize.L EXPECT_NE(BackgroundTaskMgrHelper::AVSessionNotifyUpdateNotification(uid, pid, true), ERR_OK); EXPECT_NE(BackgroundTaskMgrHelper::AVSessionNotifyUpdateNotification(uid, pid, false), ERR_OK); } + +/** + * @tc.name: SuspendContinuousAudioTask_001 + * @tc.desc: test SuspendContinuousAudioTask interface. + * @tc.type: FUNC + * @tc.require: issueICT1ZV + */ +HWTEST_F(BgTaskClientUnitTest, SuspendContinuousAudioTask_001, TestSize.Level1) +{ + int32_t uid = 1; + EXPECT_EQ(BackgroundTaskMgrHelper::SuspendContinuousAudioTask(uid), ERR_OK); +} } } \ No newline at end of file diff --git a/services/BUILD.gn b/services/BUILD.gn index dc5a71976f80d3ea9c1da1a5fd4e1cd3aba65e9c..ee01af71628107db5d487adb8ad29ae23c7e4ef1 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -34,6 +34,7 @@ BgTaskMgrServicecSrc = [ "common/src/report_hisysevent_data.cpp", "common/src/system_event_observer.cpp", "common/src/time_provider.cpp", + "continuous_task/src/bg_continuous_task_dumper.cpp", "continuous_task/src/bg_continuous_task_mgr.cpp", "continuous_task/src/config_change_observer.cpp", "continuous_task/src/continuous_task_record.cpp", diff --git a/services/continuous_task/include/bg_continuous_task_dumper.h b/services/continuous_task/include/bg_continuous_task_dumper.h new file mode 100644 index 0000000000000000000000000000000000000000..c2917eaf8ca27f5bdb8ac80dc640cd95ba0550e0 --- /dev/null +++ b/services/continuous_task/include/bg_continuous_task_dumper.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025 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 FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CONTINUOUS_TASK_INCLUDE_BG_CONTINUOUS_TASK_DUMPER_H +#define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CONTINUOUS_TASK_INCLUDE_BG_CONTINUOUS_TASK_DUMPER_H + +#include + +#include "singleton.h" + +namespace OHOS { +namespace BackgroundTaskMgr { +class BgContinuousTaskDumper : public DelayedSingleton { +public: + void DumpInnerTask(const std::vector &dumpOption, std::vector &dumpInfo); + void DumpGetTask(const std::vector &dumpOption, + std::vector &dumpInfo); + +private: + DECLARE_DELAYED_SINGLETON(BgContinuousTaskDumper); +}; +} // namespace BackgroundTaskMgr +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/continuous_task/include/bg_continuous_task_mgr.h b/services/continuous_task/include/bg_continuous_task_mgr.h index 43633275433e715420a7bc485d2a588e13c85df9..f6175628768ce668af26d8aa4d1c38053dbf5367 100644 --- a/services/continuous_task/include/bg_continuous_task_mgr.h +++ b/services/continuous_task/include/bg_continuous_task_mgr.h @@ -102,10 +102,13 @@ public: int32_t GetBgTaskUid(); void StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key); void SuspendContinuousTask(int32_t uid, int32_t pid, int32_t reason, const std::string &key); + void SuspendContinuousAudioTask(int32_t uid); void ActiveContinuousTask(int32_t uid, int32_t pid, const std::string &key); void OnConfigurationChanged(const AppExecFwk::Configuration &configuration); void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId); void HandleRemoveTaskByMode(uint32_t mode); + void SetDumperTest(const bool dumperTest); + bool IsDumperTest() const; private: ErrCode StartBackgroundRunningInner(std::shared_ptr &continuousTaskRecordPtr); @@ -127,8 +130,6 @@ private: void CheckPersistenceData(const std::vector &allProcesses); void DumpAllTaskInfo(std::vector &dumpInfo); void DumpCancelTask(const std::vector &dumpOption, bool cleanAll); - void DumpGetTask(const std::vector &dumpOption, std::vector &dumpInfo); - void DumpInnerTask(const std::vector &dumpOption, std::vector &dumpInfo); bool RemoveContinuousTaskRecord(const std::string &mapKey); bool AddAppNameInfos(const AppExecFwk::BundleInfo &bundleInfo, CachedBundleInfo &cachedBundleInfo); bool CheckProcessUidInfo(const std::vector &allProcesses, int32_t uid); @@ -143,6 +144,7 @@ private: bool SetCachedBundleInfo(int32_t uid, int32_t userId, const std::string &bundleName, const std::string &appName); void HandleStopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key); void HandleSuspendContinuousTask(int32_t uid, int32_t pid, int32_t reason, const std::string &key); + void HandleSuspendContinuousAudioTask(int32_t uid); void HandleActiveContinuousTask(int32_t uid, int32_t pid, const std::string &key); void OnRemoteSubscriberDiedInner(const wptr &object); void OnContinuousTaskChanged(const std::shared_ptr continuousTaskInfo, @@ -196,6 +198,7 @@ private: std::vector continuousTaskText_ {}; std::vector continuousTaskSubText_ {}; int32_t continuousTaskIdIndex_ = 0; + bool dumperTest_ {false}; DECLARE_DELAYED_SINGLETON(BgContinuousTaskMgr); }; diff --git a/services/continuous_task/include/continuous_task_record.h b/services/continuous_task/include/continuous_task_record.h index 70eff936dcf32946529ef10dcfd6d1fc2437fdd4..be9e1d46b0d8a0524988c3b8f2dc52f1ae6e904b 100644 --- a/services/continuous_task/include/continuous_task_record.h +++ b/services/continuous_task/include/continuous_task_record.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -61,6 +61,7 @@ public: bool ParseFromJson(const nlohmann::json &value); std::string ToString(std::vector &bgmodes); bool IsSystem() const; + int32_t GetSuspendAudioTaskTimes() const; private: std::vector ToVector(std::string &str); @@ -89,6 +90,7 @@ private: int32_t continuousTaskId_ {-1}; bool suspendState_ {false}; int32_t suspendReason_ {0}; + int32_t suspendAudioTaskTimes_ {0}; friend class BgContinuousTaskMgr; friend class NotificationTools; diff --git a/services/continuous_task/src/bg_continuous_task_dumper.cpp b/services/continuous_task/src/bg_continuous_task_dumper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62fd81d991b1ca7ff17a23fd15bec06963f26975 --- /dev/null +++ b/services/continuous_task/src/bg_continuous_task_dumper.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2025 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 "bg_continuous_task_dumper.h" +#include "bg_continuous_task_mgr.h" + +namespace OHOS { +namespace BackgroundTaskMgr { +namespace { +static constexpr int32_t MAX_DUMP_INNER_PARAM_NUMS = 4; +static constexpr int32_t MAX_DUMP_PARAM_NUMS = 3; +} + +BgContinuousTaskDumper::BgContinuousTaskDumper() {} + +BgContinuousTaskDumper::~BgContinuousTaskDumper() {} + +void BgContinuousTaskDumper::DumpInnerTask(const std::vector &dumpOption, + std::vector &dumpInfo) +{ + if (dumpOption.size() != MAX_DUMP_INNER_PARAM_NUMS) { + dumpInfo.emplace_back("param invaild\n"); + return; + } + std::string modeStr = dumpOption[MAX_DUMP_PARAM_NUMS -1].c_str(); + uint32_t mode = 0; + if (modeStr == "WORKOUT") { + mode = BackgroundMode::WORKOUT; + } + if (mode == 0) { + dumpInfo.emplace_back("param invaild\n"); + return; + } + std::string operationType = dumpOption[MAX_DUMP_PARAM_NUMS].c_str(); + if (operationType != "apply" && operationType != "reset") { + dumpInfo.emplace_back("param invaild\n"); + return; + } + bool isApply = (operationType == "apply"); + sptr taskParam = sptr( + new ContinuousTaskParamForInner(1, mode, isApply)); + ErrCode ret = ERR_OK; + BgContinuousTaskMgr::GetInstance()->SetDumperTest(true); + ret = BgContinuousTaskMgr::GetInstance()->RequestBackgroundRunningForInner(taskParam); + if (ret != ERR_OK) { + dumpInfo.emplace_back("dump inner continuous task fail.\n"); + } else { + dumpInfo.emplace_back("dump inner continuous task success.\n"); + } +} + +void BgContinuousTaskDumper::DumpGetTask(const std::vector &dumpOption, + std::vector &dumpInfo) +{ + if (dumpOption.size() != MAX_DUMP_PARAM_NUMS) { + dumpInfo.emplace_back("param invaild\n"); + return; + } + int32_t uid = std::atoi(dumpOption[MAX_DUMP_PARAM_NUMS - 1].c_str()); + if (uid < 0) { + dumpInfo.emplace_back("param invaild\n"); + return; + } + std::vector> list; + ErrCode ret = BgContinuousTaskMgr::GetInstance()->RequestGetContinuousTasksByUidForInner(uid, list); + if (ret != ERR_OK) { + dumpInfo.emplace_back("param invaild\n"); + return; + } + if (list.empty()) { + dumpInfo.emplace_back("No running continuous task\n"); + return; + } + std::stringstream stream; + uint32_t index = 1; + for (const auto &info : list) { + stream.str(""); + stream.clear(); + stream << "No." << index; + stream << "\t\tabilityName: " << info->GetAbilityName() << "\n"; + stream << "\t\tisFromWebview: " << (info->IsFromWebView() ? "true" : "false") << "\n"; + stream << "\t\tuid: " << info->GetUid() << "\n"; + stream << "\t\tpid: " << info->GetPid() << "\n"; + stream << "\t\tnotificationId: " << info->GetNotificationId() << "\n"; + stream << "\t\tcontinuousTaskId: " << info->GetContinuousTaskId() << "\n"; + stream << "\t\tabilityId: " << info->GetAbilityId() << "\n"; + stream << "\t\twantAgentBundleName: " << info->GetWantAgentBundleName() << "\n"; + stream << "\t\twantAgentAbilityName: " << info->GetWantAgentAbilityName() << "\n"; + stream << "\t\tbackgroundModes: " << info->ToString(info->GetBackgroundModes()) << "\n"; + stream << "\t\tbackgroundSubModes: " << info->ToString(info->GetBackgroundSubModes()) << "\n"; + stream << "\n"; + dumpInfo.emplace_back(stream.str()); + index++; + } +} +} // namespace BackgroundTaskMgr +} // namespace OHOS \ No newline at end of file diff --git a/services/continuous_task/src/bg_continuous_task_mgr.cpp b/services/continuous_task/src/bg_continuous_task_mgr.cpp index cb01a59cef00209748336df7ef6488e45673236d..4b3431f737fd1e133221acaba56e72f182604160 100644 --- a/services/continuous_task/src/bg_continuous_task_mgr.cpp +++ b/services/continuous_task/src/bg_continuous_task_mgr.cpp @@ -54,6 +54,7 @@ #include "background_mode.h" #include "background_sub_mode.h" #include "continuous_task_suspend_reason.h" +#include "bg_continuous_task_dumper.h" namespace OHOS { namespace BackgroundTaskMgr { @@ -89,7 +90,6 @@ static constexpr uint32_t PC_BGMODE_TASK_KEEPING = 256; static constexpr int32_t DELAY_TIME = 2000; static constexpr int32_t RECLAIM_MEMORY_DELAY_TIME = 20 * 60 * 1000; static constexpr int32_t MAX_DUMP_PARAM_NUMS = 3; -static constexpr int32_t MAX_DUMP_INNER_PARAM_NUMS = 4; static constexpr uint32_t INVALID_BGMODE = 0; static constexpr uint32_t BG_MODE_INDEX_HEAD = 1; static constexpr uint32_t BGMODE_NUMS = 10; @@ -615,7 +615,10 @@ ErrCode BgContinuousTaskMgr::RequestBackgroundRunningForInner(const sptruid_) { + if (IsDumperTest()) { + SetDumperTest(false); + BGTASK_LOGW("RequestBackgroundRunningForInner dump test"); + } else if (callingUid != VOIP_SA_UID && callingUid != HEALTHSPORT_SA_UID && callingUid != taskParam->uid_) { BGTASK_LOGE("continuous task param uid %{public}d is invalid, real %{public}d", taskParam->uid_, callingUid); return ERR_BGTASK_CHECK_TASK_PARAM; } @@ -1468,6 +1471,52 @@ ErrCode BgContinuousTaskMgr::AVSessionNotifyUpdateNotificationInner(int32_t uid, return result; } +void BgContinuousTaskMgr::SuspendContinuousAudioTask(int32_t uid) +{ + if (!isSysReady_.load()) { + return; + } + auto self = shared_from_this(); + auto task = [self, uid]() { + if (self) { + self->HandleSuspendContinuousAudioTask(uid); + } + }; + handler_->PostTask(task); +} + +void BgContinuousTaskMgr::HandleSuspendContinuousAudioTask(int32_t uid) +{ + auto iter = continuousTaskInfosMap_.begin(); + while (iter != continuousTaskInfosMap_.end()) { + if (iter->second->GetUid() == uid && + CommonUtils::CheckExistMode(iter->second->bgModeIds_, BackgroundMode::AUDIO_PLAYBACK)) { + NotificationTools::GetInstance()->CancelNotification(iter->second->GetNotificationLabel(), + iter->second->GetNotificationId()); + if (!IsExistCallback(uid, CONTINUOUS_TASK_SUSPEND)) { + iter++; + continue; + } + if (iter->second->GetSuspendAudioTaskTimes() == 0) { + iter->second->suspendState_ = true; + iter->second->suspendAudioTaskTimes_ = 1; + iter->second->suspendReason_ = + static_cast(ContinuousTaskSuspendReason::SYSTEM_SUSPEND_AUDIO_PLAYBACK_NOT_RUNNING); + OnContinuousTaskChanged(iter->second, ContinuousTaskEventTriggerType::TASK_SUSPEND); + RefreshTaskRecord(); + iter++; + } else { + OnContinuousTaskChanged(iter->second, ContinuousTaskEventTriggerType::TASK_CANCEL); + iter = continuousTaskInfosMap_.erase(iter); + RefreshTaskRecord(); + } + } else { + iter++; + } + } + HandleAppContinuousTaskStop(uid); +} + ErrCode BgContinuousTaskMgr::ShellDump(const std::vector &dumpOption, std::vector &dumpInfo) { if (!isSysReady_.load()) { @@ -1492,9 +1541,9 @@ ErrCode BgContinuousTaskMgr::ShellDumpInner(const std::vector &dump } else if (dumpOption[1] == DUMP_PARAM_CANCEL) { DumpCancelTask(dumpOption, false); } else if (dumpOption[1] == DUMP_PARAM_GET) { - DumpGetTask(dumpOption, dumpInfo); + BgContinuousTaskDumper::GetInstance()->DumpGetTask(dumpOption, dumpInfo); } else if (dumpOption[1] == DUMP_INNER_TASK) { - DumpInnerTask(dumpOption, dumpInfo); + BgContinuousTaskDumper::GetInstance()->DumpInnerTask(dumpOption, dumpInfo); } else { BGTASK_LOGW("invalid dump param"); } @@ -1575,88 +1624,6 @@ void BgContinuousTaskMgr::DumpCancelTask(const std::vector &dumpOpt } } -void BgContinuousTaskMgr::DumpGetTask(const std::vector &dumpOption, - std::vector &dumpInfo) -{ - if (dumpOption.size() != MAX_DUMP_PARAM_NUMS) { - dumpInfo.emplace_back("param invaild\n"); - return; - } - int32_t uid = std::atoi(dumpOption[MAX_DUMP_PARAM_NUMS - 1].c_str()); - if (uid < 0) { - dumpInfo.emplace_back("param invaild\n"); - return; - } - std::vector> list; - ErrCode ret = RequestGetContinuousTasksByUidForInner(uid, list); - if (ret != ERR_OK) { - dumpInfo.emplace_back("param invaild\n"); - return; - } - if (list.empty()) { - dumpInfo.emplace_back("No running continuous task\n"); - return; - } - std::stringstream stream; - uint32_t index = 1; - for (const auto &info : list) { - stream.str(""); - stream.clear(); - stream << "No." << index; - stream << "\t\tabilityName: " << info->GetAbilityName() << "\n"; - stream << "\t\tisFromWebview: " << (info->IsFromWebView() ? "true" : "false") << "\n"; - stream << "\t\tuid: " << info->GetUid() << "\n"; - stream << "\t\tpid: " << info->GetPid() << "\n"; - stream << "\t\tnotificationId: " << info->GetNotificationId() << "\n"; - stream << "\t\tcontinuousTaskId: " << info->GetContinuousTaskId() << "\n"; - stream << "\t\tabilityId: " << info->GetAbilityId() << "\n"; - stream << "\t\twantAgentBundleName: " << info->GetWantAgentBundleName() << "\n"; - stream << "\t\twantAgentAbilityName: " << info->GetWantAgentAbilityName() << "\n"; - stream << "\t\tbackgroundModes: " << info->ToString(info->GetBackgroundModes()) << "\n"; - stream << "\t\tbackgroundSubModes: " << info->ToString(info->GetBackgroundSubModes()) << "\n"; - stream << "\n"; - dumpInfo.emplace_back(stream.str()); - index++; - } -} - -void BgContinuousTaskMgr::DumpInnerTask(const std::vector &dumpOption, - std::vector &dumpInfo) -{ - if (dumpOption.size() != MAX_DUMP_INNER_PARAM_NUMS) { - dumpInfo.emplace_back("param invaild\n"); - return; - } - std::string modeStr = dumpOption[2].c_str(); - uint32_t mode = 0; - if (modeStr == "WORKOUT") { - mode = BackgroundMode::WORKOUT; - } - if (mode == 0) { - dumpInfo.emplace_back("param invaild\n"); - return; - } - std::string operationType = dumpOption[3].c_str(); - if (operationType != "apply" && operationType != "reset") { - dumpInfo.emplace_back("param invaild\n"); - return; - } - bool isApply = (operationType == "apply"); - sptr taskParam = sptr( - new ContinuousTaskParamForInner(1, mode, isApply)); - ErrCode ret = ERR_OK; - if (isApply) { - ret = StartBackgroundRunningForInner(taskParam); - } else { - ret = StopBackgroundRunningForInner(taskParam); - } - if (ret != ERR_OK) { - dumpInfo.emplace_back("dump inner continuous task fail.\n"); - } else { - dumpInfo.emplace_back("dump inner continuous task success.\n"); - } -} - void BgContinuousTaskMgr::SetReason(const std::string &mapKey, int32_t reason) { auto iter = continuousTaskInfosMap_.find(mapKey); @@ -2152,5 +2119,15 @@ void BgContinuousTaskMgr::OnRemoveSystemAbility(int32_t systemAbilityId, const s break; } } + +void BgContinuousTaskMgr::SetDumperTest(const bool dumperTest) +{ + dumperTest_ = dumperTest; +} + +bool BgContinuousTaskMgr::IsDumperTest() const +{ + return dumperTest_; +} } // namespace BackgroundTaskMgr } // namespace OHOS diff --git a/services/continuous_task/src/continuous_task_record.cpp b/services/continuous_task/src/continuous_task_record.cpp index 23ae0d3d74e7f82e6190ef2f02ef721bbd995e0b..608720f926e271ace8d059c9541d3adb19149f95 100644 --- a/services/continuous_task/src/continuous_task_record.cpp +++ b/services/continuous_task/src/continuous_task_record.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -147,6 +147,11 @@ bool ContinuousTaskRecord::IsSystem() const return isSystem_; } +int32_t ContinuousTaskRecord::GetSuspendAudioTaskTimes() const +{ + return suspendAudioTaskTimes_; +} + std::string ContinuousTaskRecord::ParseToJsonStr() { nlohmann::json root; diff --git a/services/core/include/background_task_mgr_service.h b/services/core/include/background_task_mgr_service.h index e631ea1c288035fb2eb20c887e4c74d6c4d7516c..cc4d0b9121a4df37561209b97c4a0597b59e640d 100644 --- a/services/core/include/background_task_mgr_service.h +++ b/services/core/include/background_task_mgr_service.h @@ -93,6 +93,7 @@ public: ErrCode ActiveContinuousTask(int32_t uid, int32_t pid, const std::string &key) override; ErrCode AVSessionNotifyUpdateNotification(int32_t uid, int32_t pid, bool isPublish = false) override; ErrCode SetBgTaskConfig(const std::string &configData, int32_t sourceType) override; + ErrCode SuspendContinuousAudioTask(int32_t uid) override; int32_t Dump(int32_t fd, const std::vector &args) override; void ForceCancelSuspendDelay(int32_t requestId); diff --git a/services/core/src/background_task_mgr_service.cpp b/services/core/src/background_task_mgr_service.cpp index 53504310c5de796169a580ea7681794544af982a..987cfafd59880099b3ff184254b4db258ac58edd 100644 --- a/services/core/src/background_task_mgr_service.cpp +++ b/services/core/src/background_task_mgr_service.cpp @@ -531,6 +531,15 @@ ErrCode BackgroundTaskMgrService::SetBgTaskConfig(const std::string &configData, return DelayedSingleton::GetInstance()->SetBgTaskConfig(configData, sourceType); } +ErrCode BackgroundTaskMgrService::SuspendContinuousAudioTask(int32_t uid) +{ + if (!CheckCallingToken() || !CheckCallingProcess()) { + return ERR_BGTASK_PERMISSION_DENIED; + } + BgContinuousTaskMgr::GetInstance()->SuspendContinuousAudioTask(uid); + return ERR_OK; +} + bool BackgroundTaskMgrService::CheckAtomicService() { uint64_t tokenId = IPCSkeleton::GetCallingFullTokenID(); diff --git a/services/test/unittest/bg_continuous_task_mgr_test.cpp b/services/test/unittest/bg_continuous_task_mgr_test.cpp index 319e6ac7c136b6a929f17f3b306d0d379e91ee1b..995cfa39e23681a5eba4d41ee3a26558020b41bf 100644 --- a/services/test/unittest/bg_continuous_task_mgr_test.cpp +++ b/services/test/unittest/bg_continuous_task_mgr_test.cpp @@ -1398,5 +1398,49 @@ HWTEST_F(BgContinuousTaskMgrTest, BgTaskManagerUnitTest_054, TestSize.Level1) bgContinuousTaskMgr_->continuousTaskInfosMap_["key1"] = continuousTaskRecord; EXPECT_EQ(bgContinuousTaskMgr_->SendContinuousTaskNotification(continuousTaskRecord), ERR_OK); } + +/** + * @tc.name: BgTaskManagerUnitTest_057 + * @tc.desc: test SuspendContinuousAudioTask. + * @tc.type: FUNC + * @tc.require: issueICT1ZV + */ +HWTEST_F(BgContinuousTaskMgrTest, BgTaskManagerUnitTest_057, TestSize.Level1) +{ + bgContinuousTaskMgr_->continuousTaskInfosMap_.clear(); + + std::shared_ptr continuousTaskRecord1 = std::make_shared(); + continuousTaskRecord1->uid_ = TEST_NUM_ONE; + continuousTaskRecord1->bgModeId_ = TEST_NUM_TWO; + bgContinuousTaskMgr_->continuousTaskInfosMap_["key1"] = continuousTaskRecord1; + bgContinuousTaskMgr_->SuspendContinuousAudioTask(1); + EXPECT_EQ(bgContinuousTaskMgr_->continuousTaskInfosMap_.size(), 1); +} + +/** + * @tc.name: BgTaskManagerUnitTest_058 + * @tc.desc: test HandleSuspendContinuousAudioTask. + * @tc.type: FUNC + * @tc.require: issueICT1ZV + */ +HWTEST_F(BgContinuousTaskMgrTest, BgTaskManagerUnitTest_058, TestSize.Level1) +{ + bgContinuousTaskMgr_->continuousTaskInfosMap_.clear(); + bgContinuousTaskMgr_->bgTaskSubscribers_.clear(); + + std::shared_ptr continuousTaskRecord = std::make_shared(); + continuousTaskRecord->uid_ = TEST_NUM_ONE; + continuousTaskRecord->bgModeId_ = 2; + continuousTaskRecord->bgModeIds_.push_back(continuousTaskRecord->bgModeId_); + bgContinuousTaskMgr_->continuousTaskInfosMap_["key1"] = continuousTaskRecord; + + TestBackgroundTaskSubscriber subscriber1 = TestBackgroundTaskSubscriber(); + auto info1 = std::make_shared(subscriber1.GetImpl(), 1, 1, 1, 2); + bgContinuousTaskMgr_->bgTaskSubscribers_.emplace_back(info1); + + bgContinuousTaskMgr_->HandleSuspendContinuousAudioTask(1); + bgContinuousTaskMgr_->HandleSuspendContinuousAudioTask(1); + EXPECT_EQ(bgContinuousTaskMgr_->continuousTaskInfosMap_.size(), 0); +} } // namespace BackgroundTaskMgr } // namespace OHOS \ No newline at end of file diff --git a/services/test/unittest/bgtask_manager_abnormal_unit_test/bgtask_manager_abnormal_unit_test.cpp b/services/test/unittest/bgtask_manager_abnormal_unit_test/bgtask_manager_abnormal_unit_test.cpp index b6310ebb4f784c882e2d9b95c077aef703af5923..42986d77779cf62d192deaef6024247932a58261 100644 --- a/services/test/unittest/bgtask_manager_abnormal_unit_test/bgtask_manager_abnormal_unit_test.cpp +++ b/services/test/unittest/bgtask_manager_abnormal_unit_test/bgtask_manager_abnormal_unit_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -77,6 +77,8 @@ HWTEST_F(BgTaskManagerAbnormalUnitTest, BackgroundTaskMgrServiceAbnormalTest_002 EXPECT_EQ(BackgroundTaskMgrService_->SubscribeBackgroundTask(nullptr, 0), ERR_BGTASK_PERMISSION_DENIED); EXPECT_EQ(BackgroundTaskMgrService_->UnsubscribeBackgroundTask(nullptr), ERR_BGTASK_PERMISSION_DENIED); + + EXPECT_EQ(BackgroundTaskMgrService_->SuspendContinuousAudioTask(1), ERR_BGTASK_PERMISSION_DENIED); } /**