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::ContinuousTaskInfo>> continuousTaskList;
37 ErrCode code = BackgroundTaskMgr::BackgroundTaskMgrHelper::RequestGetContinuousTasksByUidForInner(
38 creatorUid, continuousTaskList);
39 if (code != OHOS::ERR_OK) {
40 TAG_LOGW(AceLogTag::ACE_VIDEO, "uid=%{public}d no continuous task list, code=%{public}d", creatorUid, code);
41 return false;
42 }
43
44 for (const auto &task : continuousTaskList) {
45 TAG_LOGW(AceLogTag::ACE_VIDEO, "uid=%{public}d taskCreatorUid=%{public}d", creatorUid, task->GetUid());
46
47 std::vector<uint32_t> bgMode = task->GetBackgroundModes();
48 auto it = std::find_if(bgMode.begin(), bgMode.end(), [ backgroundMode ](auto mode) {
49 return (mode == backgroundMode);
50 });
51 if (it != bgMode.end()) {
52 TAG_LOGW(AceLogTag::ACE_VIDEO, "uid=%{public}d is audio playback", creatorUid);
53 return true;
54 }
55 }
56 return false;
57 }
58
59 } // namespace OHOS::Ace