1 /*
2 * Copyright (c) 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
16 #include "fold_screen_controller/fold_screen_state_machine.h"
17
18 #include "window_manager_hilog.h"
19
20 namespace OHOS {
21 namespace Rosen {
22 namespace {
23 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "FoldScreenStateMachine"};
24 } // namespace
25
26 FoldScreenStateMachine::FoldScreenStateMachine() = default;
27
28 FoldScreenStateMachine::~FoldScreenStateMachine() = default;
29
RegistrationTransitionCallback(const std::shared_ptr<TransitionCallback> & callback)30 void FoldScreenStateMachine::RegistrationTransitionCallback(const std::shared_ptr<TransitionCallback>& callback)
31 {
32 if (!callback) {
33 return;
34 }
35 std::lock_guard<std::recursive_mutex> lock(mutex_);
36 for (auto it = callbacks_.begin(); it != callbacks_.end();) {
37 if (*it == callback) {
38 return;
39 } else {
40 ++it;
41 }
42 }
43 callbacks_.push_back(callback);
44 }
45
UnRegistrationTransitionCallback(const std::shared_ptr<TransitionCallback> & callback)46 void FoldScreenStateMachine::UnRegistrationTransitionCallback(const std::shared_ptr<TransitionCallback>& callback)
47 {
48 if (!callback) {
49 return;
50 }
51 std::lock_guard<std::recursive_mutex> lock(mutex_);
52 for (auto it = callbacks_.begin(); it != callbacks_.end();) {
53 if (*it == callback) {
54 callbacks_.erase(it);
55 return;
56 } else {
57 ++it;
58 }
59 }
60 }
61
TransitionTo(FoldScreenState state)62 void FoldScreenStateMachine::TransitionTo(FoldScreenState state)
63 {
64 std::lock_guard<std::recursive_mutex> lock(mutex_);
65 for(const auto &callback : callbacks_) {
66 if (callback) {
67 callback->OnTransitionEnter(currState_, state);
68 }
69 }
70 auto previous = currState_;
71 currState_ = state;
72 WLOGI("state transition from %{public}u to %{public}u", static_cast<int32_t>(currState_), static_cast<int32_t>(state));
73 for(const auto &callback : callbacks_) {
74 if (callback) {
75 callback->OnTransitionExit(previous, currState_);
76 }
77 }
78 }
79
GetCurrentState() const80 FoldScreenState FoldScreenStateMachine::GetCurrentState() const
81 {
82 return currState_;
83 }
84
GenStateMachineInfo() const85 std::string FoldScreenStateMachine::GenStateMachineInfo() const
86 {
87 std::ostringstream oss;
88 oss << "callbackCount: " << callbacks_.size()
89 << ", currentState: " << static_cast<int32_t>(currState_) << ";";
90 std::string info(oss.str());
91 return info;
92 }
93 } // Rosen
94 } // OHOS