1 /*
2  * Copyright (C) 2024-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 #ifndef FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_ACTIONS_ACTION_H
17 #define FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_ACTIONS_ACTION_H
18 
19 #include <cstdint>
20 #include <functional>
21 
22 namespace OHOS {
23 namespace MiscServices {
24 
25 enum RunningState : uint32_t {
26     RUNNING_STATE_IDLE = 0,
27     RUNNING_STATE_RUNNING,
28     RUNNING_STATE_PAUSED,
29     RUNNING_STATE_COMPLETED,
30     RUNNING_STATE_ERROR,
31 };
32 
33 class Action {
34 public:
35     Action() = default;
Action(std::function<void()> func)36     Action(std::function<void()> func) : func_(func) { }
37     virtual ~Action() = default;
38 
Execute()39     virtual RunningState Execute()
40     {
41         if (state_ != RUNNING_STATE_IDLE) {
42             return RUNNING_STATE_ERROR;
43         }
44 
45         state_ = RUNNING_STATE_RUNNING;
46         if (func_) {
47             func_();
48         }
49         state_ = RUNNING_STATE_COMPLETED;
50         return state_;
51     }
52 
Resume(uint64_t resumeId)53     virtual RunningState Resume(uint64_t resumeId)
54     {
55         return state_;
56     }
57 
GetState() const58     RunningState GetState() const
59     {
60         return state_;
61     }
62 
63 protected:
64     RunningState state_ { RUNNING_STATE_IDLE };
65     std::function<void()> func_;
66 };
67 } // namespace MiscServices
68 } // namespace OHOS
69 #endif // FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_ACTIONS_ACTION_H