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 #ifndef OHOS_ROSEN_FOLD_SCREEN_STATE_MACHINE_H
17 #define OHOS_ROSEN_FOLD_SCREEN_STATE_MACHINE_H
18 
19 #include <deque>
20 #include <memory>
21 #include <mutex>
22 #include <sstream>
23 
24 #include "refbase.h"
25 #include "ws_common.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 
30 enum class FoldScreenState : uint32_t {
31     UNKNOWN,
32     FOLDED,
33     HALF_FOLDED,
34     FULL
35 };
36 
37 class TransitionCallback {
38 public:
39     virtual ~TransitionCallback() = default;
40 
OnTransitionEnter(FoldScreenState current,FoldScreenState next)41     virtual void OnTransitionEnter(FoldScreenState current, FoldScreenState next) { }
42 
OnTransitionExit(FoldScreenState previous,FoldScreenState current)43     virtual void OnTransitionExit(FoldScreenState previous, FoldScreenState current) { }
44 };
45 
46 class FoldScreenStateMachine : public RefBase {
47 public:
48     FoldScreenStateMachine();
49 
50     ~FoldScreenStateMachine();
51 
52     void TransitionTo(FoldScreenState state);
53 
54     void RegistrationTransitionCallback(const std::shared_ptr<TransitionCallback>& callback);
55 
56     void UnRegistrationTransitionCallback(const std::shared_ptr<TransitionCallback>& callback);
57 
58     FoldScreenState GetCurrentState() const;
59 
60     std::string GenStateMachineInfo() const;
61 
62 private:
63     std::deque<std::shared_ptr<TransitionCallback>> callbacks_;
64     FoldScreenState currState_ = FoldScreenState::UNKNOWN;
65     std::recursive_mutex mutex_;
66 };
67 } // namespace Rosen
68 } // namespace OHOS
69 #endif
70