1 /*
2  * Copyright (c) 2022-2023 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 #ifdef RESSCHED_REQUEST_REQUEST
16 #include "download_upload_observer.h"
17 #include "nlohmann/json.hpp"
18 #include "res_sched_log.h"
19 #include "res_sched_mgr.h"
20 #include "res_type.h"
21 namespace OHOS {
22 namespace ResourceSchedule {
OnRunningTaskCountUpdate(int count)23 void DownLoadUploadObserver::OnRunningTaskCountUpdate(int count)
24 {
25     RESSCHED_LOGI("download upload on running task count %{public}d", count);
26     if (IsReporEnterScene(count)) {
27         HandleEnterScene();
28         return;
29     }
30     if (IsReporExitScene(count)) {
31         HandleExitScene();
32     }
33 }
34 
IsReporEnterScene(int count)35 bool DownLoadUploadObserver::IsReporEnterScene(int count)
36 {
37     if (count > 0 && !isReportedScene_) {
38         return true;
39     }
40     return false;
41 }
42 
IsReporExitScene(int count)43 bool DownLoadUploadObserver::IsReporExitScene(int count)
44 {
45     if (count == 0 && isReportedScene_) {
46         return true;
47     }
48     return false;
49 }
50 
HandleEnterScene()51 void DownLoadUploadObserver::HandleEnterScene()
52 {
53     const nlohmann::json payload = nlohmann::json::object();
54     ResSchedMgr::GetInstance().ReportData(ResType::RES_TYPE_UPLOAD_DOWNLOAD,
55         ResType::KeyUploadOrDownloadStatus::ENTER_UPLOAD_DOWNLOAD_SCENE, payload);
56     isReportedScene_ = true;
57 }
58 
HandleExitScene()59 void DownLoadUploadObserver::HandleExitScene()
60 {
61     const nlohmann::json payload = nlohmann::json::object();
62     ResSchedMgr::GetInstance().ReportData(ResType::RES_TYPE_UPLOAD_DOWNLOAD,
63         ResType::KeyUploadOrDownloadStatus::EXIT_UPLOAD_DOWNLOAD_SCENE, payload);
64     isReportedScene_ = false;
65 }
66 } // namespace ResourceSchedule
67 } // namespace OHOS
68 #endif
69