1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "background_task_helper_ohos.h"
17
18 #include "background_mode.h"
19 #include "background_task_mgr_helper.h"
20
21 #include "core/common/ace_application_info.h"
22
23 namespace OHOS::Ace {
24
GetInstance()25 BackgroundTaskHelper& BackgroundTaskHelper::GetInstance()
26 {
27 static BackgroundTaskHelperOhos instance;
28 return instance;
29 }
30
HasBackgroundTask()31 bool BackgroundTaskHelperOhos::HasBackgroundTask()
32 {
33 int32_t creatorUid = AceApplicationInfo::GetInstance().GetUid();
34 uint32_t backgroundMode = BackgroundTaskMgr::BackgroundMode::AUDIO_PLAYBACK;
35
36 std::vector<std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo>> continuousTaskList;
37 ErrCode code = BackgroundTaskMgr::BackgroundTaskMgrHelper::GetContinuousTaskApps(continuousTaskList);
38 if (code != OHOS::ERR_OK) {
39 TAG_LOGW(AceLogTag::ACE_VIDEO, "uid=%{public}d no continuous task list, code=%{public}d", creatorUid, code);
40 return false;
41 }
42
43 for (const auto &task : continuousTaskList) {
44 TAG_LOGW(AceLogTag::ACE_VIDEO, "uid=%{public}d taskCreatorUid=%{public}d", creatorUid, task->GetCreatorUid());
45 if (task->GetCreatorUid() != creatorUid) {
46 continue;
47 }
48
49 std::vector<uint32_t> bgModeIds = task->GetTypeIds();
50 auto it = std::find_if(bgModeIds.begin(), bgModeIds.end(), [ backgroundMode ](auto mode) {
51 return (mode == backgroundMode);
52 });
53 if (it != bgModeIds.end()) {
54 TAG_LOGW(AceLogTag::ACE_VIDEO, "uid=%{public}d is audio playback", creatorUid);
55 return true;
56 }
57 }
58 return false;
59 }
60
61 } // namespace OHOS::Ace