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 #include "continuous_task_callback_info.h"
21 
22 #include "core/common/ace_application_info.h"
23 
24 namespace OHOS::Ace {
25 
GetInstance()26 BackgroundTaskHelper& BackgroundTaskHelper::GetInstance()
27 {
28     static BackgroundTaskHelperOhos instance;
29     return instance;
30 }
31 
HasBackgroundTask()32 bool BackgroundTaskHelperOhos::HasBackgroundTask()
33 {
34     int32_t creatorUid = AceApplicationInfo::GetInstance().GetUid();
35     uint32_t backgroundMode = BackgroundTaskMgr::BackgroundMode::AUDIO_PLAYBACK;
36 
37     std::vector<std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo>> continuousTaskList;
38     ErrCode code = BackgroundTaskMgr::BackgroundTaskMgrHelper::GetContinuousTaskApps(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->GetCreatorUid());
46         if (task->GetCreatorUid() != creatorUid) {
47             continue;
48         }
49 
50         std::vector<uint32_t> bgModeIds = task->GetTypeIds();
51         auto it = std::find_if(bgModeIds.begin(), bgModeIds.end(), [ backgroundMode ](auto mode) {
52             return (mode == backgroundMode);
53         });
54         if (it != bgModeIds.end()) {
55             TAG_LOGW(AceLogTag::ACE_VIDEO, "uid=%{public}d is audio playback", creatorUid);
56             return true;
57         }
58     }
59     return false;
60 }
61 
62 } // namespace OHOS::Ace