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_screen_controller/sensor_fold_state_manager/sensor_fold_state_manager.h"
17 #include <hisysevent.h>
18 #include <chrono>
19 
20 #include "fold_screen_controller/fold_screen_policy.h"
21 #include "fold_screen_state_internel.h"
22 #include "session_manager/include/screen_session_manager.h"
23 #include "window_manager_hilog.h"
24 
25 #ifdef POWER_MANAGER_ENABLE
26 #include <power_mgr_client.h>
27 #endif
28 
29 namespace OHOS::Rosen {
30 namespace {
31 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "SensorFoldStateManager"};
32 } // namespace
33 
34 SensorFoldStateManager::SensorFoldStateManager() = default;
35 SensorFoldStateManager::~SensorFoldStateManager() = default;
36 
HandleAngleChange(float angle,int hall,sptr<FoldScreenPolicy> foldScreenPolicy)37 void SensorFoldStateManager::HandleAngleChange(float angle, int hall, sptr<FoldScreenPolicy> foldScreenPolicy) {}
38 
HandleHallChange(float angle,int hall,sptr<FoldScreenPolicy> foldScreenPolicy)39 void SensorFoldStateManager::HandleHallChange(float angle, int hall, sptr<FoldScreenPolicy> foldScreenPolicy) {}
40 
HandleTentChange(bool isTent,sptr<FoldScreenPolicy> foldScreenPolicy)41 void SensorFoldStateManager::HandleTentChange(bool isTent, sptr<FoldScreenPolicy> foldScreenPolicy) {}
42 
HandleSensorChange(FoldStatus nextState,float angle,sptr<FoldScreenPolicy> foldScreenPolicy)43 void SensorFoldStateManager::HandleSensorChange(FoldStatus nextState, float angle,
44     sptr<FoldScreenPolicy> foldScreenPolicy)
45 {
46     std::lock_guard<std::recursive_mutex> lock(mutex_);
47     if (foldScreenPolicy == nullptr) {
48         TLOGE(WmsLogTag::DMS, "foldScreenPolicy is nullptr");
49         return;
50     }
51     if (nextState == FoldStatus::UNKNOWN) {
52         WLOGFW("fold state is UNKNOWN");
53         return;
54     }
55     if (mState_ == nextState) {
56         WLOGFD("fold state doesn't change, foldState = %{public}d.", mState_);
57         return;
58     }
59     WLOGFI("current state: %{public}d, next state: %{public}d.", mState_, nextState);
60     ReportNotifyFoldStatusChange((int32_t)mState_, (int32_t)nextState, angle);
61     PowerMgr::PowerMgrClient::GetInstance().RefreshActivity();
62 
63     NotifyReportFoldStatusToScb(mState_, nextState, angle);
64 
65     mState_ = nextState;
66     if (foldScreenPolicy != nullptr) {
67         foldScreenPolicy->SetFoldStatus(mState_);
68     }
69     ScreenSessionManager::GetInstance().NotifyFoldStatusChanged(mState_);
70     if (foldScreenPolicy != nullptr && foldScreenPolicy->lockDisplayStatus_ != true) {
71         foldScreenPolicy->SendSensorResult(mState_);
72     }
73 }
74 
GetCurrentState()75 FoldStatus SensorFoldStateManager::GetCurrentState()
76 {
77     return mState_;
78 }
79 
ReportNotifyFoldStatusChange(int32_t currentStatus,int32_t nextStatus,float postureAngle)80 void SensorFoldStateManager::ReportNotifyFoldStatusChange(int32_t currentStatus, int32_t nextStatus,
81     float postureAngle)
82 {
83     WLOGI("ReportNotifyFoldStatusChange currentStatus: %{public}d, nextStatus: %{public}d, postureAngle: %{public}f",
84         currentStatus, nextStatus, postureAngle);
85     int32_t ret = HiSysEventWrite(
86         OHOS::HiviewDFX::HiSysEvent::Domain::WINDOW_MANAGER,
87         "NOTIFY_FOLD_STATE_CHANGE",
88         OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
89         "CURRENT_FOLD_STATUS", currentStatus,
90         "NEXT_FOLD_STATUS", nextStatus,
91         "SENSOR_POSTURE", postureAngle);
92     if (ret != 0) {
93         WLOGE("ReportNotifyFoldStatusChange Write HiSysEvent error, ret: %{public}d", ret);
94     }
95 }
96 
ClearState(sptr<FoldScreenPolicy> foldScreenPolicy)97 void SensorFoldStateManager::ClearState(sptr<FoldScreenPolicy> foldScreenPolicy)
98 {
99     mState_ = FoldStatus::UNKNOWN;
100     foldScreenPolicy->ClearState();
101 }
102 
RegisterApplicationStateObserver()103 void SensorFoldStateManager::RegisterApplicationStateObserver() {}
104 
105 
NotifyReportFoldStatusToScb(FoldStatus currentStatus,FoldStatus nextStatus,float postureAngle)106 void SensorFoldStateManager::NotifyReportFoldStatusToScb(FoldStatus currentStatus, FoldStatus nextStatus,
107     float postureAngle)
108 {
109     std::chrono::time_point<std::chrono::system_clock> timeNow = std::chrono::system_clock::now();
110     int32_t duration = static_cast<int32_t>(
111         std::chrono::duration_cast<std::chrono::seconds>(timeNow - mLastStateClock_).count());
112     mLastStateClock_ = timeNow;
113 
114     std::vector<std::string> screenFoldInfo {std::to_string(static_cast<int32_t>(currentStatus)),
115         std::to_string(static_cast<int32_t>(nextStatus)), std::to_string(duration), std::to_string(postureAngle)};
116     ScreenSessionManager::GetInstance().ReportFoldStatusToScb(screenFoldInfo);
117 }
118 
IsTentMode()119 bool SensorFoldStateManager::IsTentMode()
120 {
121     return isTentMode_;
122 }
123 
SetTentMode(bool status)124 void SensorFoldStateManager::SetTentMode(bool status)
125 {
126     TLOGI(WmsLogTag::DMS, "tent mode changing: %{public}d -> %{public}d", isTentMode_, status);
127     isTentMode_ = status;
128     if (status) {
129         ScreenRotationProperty::HandleHoverStatusEventInput(DeviceHoverStatus::TENT_STATUS);
130     } else {
131         ScreenRotationProperty::HandleHoverStatusEventInput(DeviceHoverStatus::TENT_STATUS_CANCEL);
132     }
133 }
134 } // namespace OHOS::Rosen