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 "fold_display_mode_observer.h"
17 
18 #include "nlohmann/json.hpp"
19 #include "res_common_util.h"
20 #include "res_sched_log.h"
21 #include "res_sched_mgr.h"
22 #include "res_type.h"
23 
24 namespace OHOS {
25 namespace ResourceSchedule {
26 using OHOS::Rosen::FoldDisplayMode;
27 
28 namespace {
29     const std::string DISPLAY_MODE_FULL = "displayFull";
30     const std::string DISPLAY_MODE_SUB = "displaySub";
31     const std::string DISPLAY_MODE_MAIN = "displayMain";
32     const std::string DEVICE_MODE_STR = "deviceMode";
33 }
34 
OnDisplayModeChanged(FoldDisplayMode diplayMode)35 void FoldDisplayModeObserver::OnDisplayModeChanged(FoldDisplayMode diplayMode)
36 {
37     // FULL SUB MAIN report enter the device mode, other display mode quit the current device mode
38     RESSCHED_LOGD("Fold display mode %{public}d last %{public}s", diplayMode, currentDisplayMode.c_str());
39     if (diplayMode == FoldDisplayMode::FULL) {
40         ReportDisplayModeStatus(ResType::DeviceModeStatus::MODE_ENTER, DISPLAY_MODE_FULL);
41         currentDisplayMode = DISPLAY_MODE_FULL;
42     } else if (diplayMode == FoldDisplayMode::SUB) {
43         ReportDisplayModeStatus(ResType::DeviceModeStatus::MODE_ENTER, DISPLAY_MODE_SUB);
44         currentDisplayMode = DISPLAY_MODE_SUB;
45     } else if (diplayMode == FoldDisplayMode::MAIN) {
46         ReportDisplayModeStatus(ResType::DeviceModeStatus::MODE_ENTER, DISPLAY_MODE_MAIN);
47         currentDisplayMode = DISPLAY_MODE_MAIN;
48     } else {
49         ReportDisplayModeStatus(ResType::DeviceModeStatus::MODE_QUIT, currentDisplayMode);
50         currentDisplayMode = DISPLAY_MODE_UNKOWN;
51     }
52 }
53 
ReportDisplayModeStatus(int64_t status,const std::string & mode)54 void FoldDisplayModeObserver::ReportDisplayModeStatus(int64_t status, const std::string& mode)
55 {
56     nlohmann::json payload;
57     payload[DEVICE_MODE_STR] = mode;
58     ResSchedMgr::GetInstance().ReportData(ResType::RES_TYPE_DEVICE_MODE_STATUS, status, payload);
59 }
60 } // namespace ResourceSchedule
61 } // namespace OHOS