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 FoldScreenStateMachine::FoldScreenStateMachine() = default;
23 
24 FoldScreenStateMachine::~FoldScreenStateMachine() = default;
25 
RegistrationTransitionCallback(const std::shared_ptr<TransitionCallback> & callback)26 void FoldScreenStateMachine::RegistrationTransitionCallback(const std::shared_ptr<TransitionCallback>& callback)
27 {
28     if (!callback) {
29         return;
30     }
31     std::lock_guard<std::recursive_mutex> lock(mutex_);
32     for (auto it = callbacks_.begin(); it != callbacks_.end();) {
33         if (*it == callback) {
34             return;
35         } else {
36             ++it;
37         }
38     }
39     callbacks_.push_back(callback);
40 }
41 
UnRegistrationTransitionCallback(const std::shared_ptr<TransitionCallback> & callback)42 void FoldScreenStateMachine::UnRegistrationTransitionCallback(const std::shared_ptr<TransitionCallback>& callback)
43 {
44     if (!callback) {
45         return;
46     }
47     std::lock_guard<std::recursive_mutex> lock(mutex_);
48     for (auto it = callbacks_.begin(); it != callbacks_.end();) {
49         if (*it == callback) {
50             callbacks_.erase(it);
51             return;
52         } else {
53             ++it;
54         }
55     }
56 }
57 
TransitionTo(FoldScreenState state)58 void FoldScreenStateMachine::TransitionTo(FoldScreenState state)
59 {
60     std::lock_guard<std::recursive_mutex> lock(mutex_);
61     for (const auto &callback : callbacks_) {
62         if (callback) {
63             callback->OnTransitionEnter(currState_, state);
64         }
65     }
66     auto previous = currState_;
67     currState_ = state;
68     TLOGI(WmsLogTag::DMS, "state transition from %{public}u to %{public}u",
69           static_cast<int32_t>(currState_), static_cast<int32_t>(state));
70     for (const auto &callback : callbacks_) {
71         if (callback) {
72             callback->OnTransitionExit(previous, currState_);
73         }
74     }
75 }
76 
GetCurrentState() const77 FoldScreenState FoldScreenStateMachine::GetCurrentState() const
78 {
79     return currState_;
80 }
81 
GenStateMachineInfo() const82 std::string FoldScreenStateMachine::GenStateMachineInfo() const
83 {
84     std::ostringstream oss;
85     oss << "callbackCount: " << callbacks_.size()
86         << ", currentState: " << static_cast<int32_t>(currState_) << ";";
87     std::string info(oss.str());
88     return info;
89 }
90 } // Rosen
91 } // OHOS