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
HandleSensorChange(FoldStatus nextState,float angle,sptr<FoldScreenPolicy> foldScreenPolicy)41 void SensorFoldStateManager::HandleSensorChange(FoldStatus nextState, float angle,
42 sptr<FoldScreenPolicy> foldScreenPolicy)
43 {
44 std::lock_guard<std::recursive_mutex> lock(mutex_);
45 if (nextState == FoldStatus::UNKNOWN) {
46 WLOGFW("fold state is UNKNOWN");
47 return;
48 }
49 if (mState_ == nextState) {
50 WLOGFD("fold state doesn't change, foldState = %{public}d.", mState_);
51 return;
52 }
53 WLOGFI("current state: %{public}d, next state: %{public}d.", mState_, nextState);
54 ReportNotifyFoldStatusChange((int32_t)mState_, (int32_t)nextState, angle);
55 PowerMgr::PowerMgrClient::GetInstance().RefreshActivity();
56
57 NotifyReportFoldStatusToScb(mState_, nextState, angle);
58
59 mState_ = nextState;
60 if (foldScreenPolicy != nullptr) {
61 foldScreenPolicy->SetFoldStatus(mState_);
62 }
63 ScreenSessionManager::GetInstance().NotifyFoldStatusChanged(mState_);
64 if (foldScreenPolicy != nullptr && foldScreenPolicy->lockDisplayStatus_ != true) {
65 foldScreenPolicy->SendSensorResult(mState_);
66 }
67 }
68
GetCurrentState()69 FoldStatus SensorFoldStateManager::GetCurrentState()
70 {
71 return mState_;
72 }
73
ReportNotifyFoldStatusChange(int32_t currentStatus,int32_t nextStatus,float postureAngle)74 void SensorFoldStateManager::ReportNotifyFoldStatusChange(int32_t currentStatus, int32_t nextStatus,
75 float postureAngle)
76 {
77 WLOGI("ReportNotifyFoldStatusChange currentStatus: %{public}d, nextStatus: %{public}d, postureAngle: %{public}f",
78 currentStatus, nextStatus, postureAngle);
79 int32_t ret = HiSysEventWrite(
80 OHOS::HiviewDFX::HiSysEvent::Domain::WINDOW_MANAGER,
81 "NOTIFY_FOLD_STATE_CHANGE",
82 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
83 "CURRENT_FOLD_STATUS", currentStatus,
84 "NEXT_FOLD_STATUS", nextStatus,
85 "SENSOR_POSTURE", postureAngle);
86 if (ret != 0) {
87 WLOGE("ReportNotifyFoldStatusChange Write HiSysEvent error, ret: %{public}d", ret);
88 }
89 }
90
ClearState(sptr<FoldScreenPolicy> foldScreenPolicy)91 void SensorFoldStateManager::ClearState(sptr<FoldScreenPolicy> foldScreenPolicy)
92 {
93 mState_ = FoldStatus::UNKNOWN;
94 foldScreenPolicy->ClearState();
95 }
96
RegisterApplicationStateObserver()97 void SensorFoldStateManager::RegisterApplicationStateObserver() {}
98
99
NotifyReportFoldStatusToScb(FoldStatus currentStatus,FoldStatus nextStatus,float postureAngle)100 void SensorFoldStateManager::NotifyReportFoldStatusToScb(FoldStatus currentStatus, FoldStatus nextStatus,
101 float postureAngle)
102 {
103 std::chrono::time_point<std::chrono::system_clock> timeNow = std::chrono::system_clock::now();
104 int32_t duration = static_cast<int32_t>(
105 std::chrono::duration_cast<std::chrono::seconds>(timeNow - mLastStateClock_).count());
106 mLastStateClock_ = timeNow;
107
108 std::vector<std::string> screenFoldInfo {std::to_string(static_cast<int32_t>(currentStatus)),
109 std::to_string(static_cast<int32_t>(nextStatus)), std::to_string(duration), std::to_string(postureAngle)};
110 ScreenSessionManager::GetInstance().ReportFoldStatusToScb(screenFoldInfo);
111 }
112 } // namespace OHOS::Rosen