1/*
2 * Copyright (c) 2022 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 "window_node_state_machine.h"
17
18#include "parameters.h"
19#include "remote_animation.h"
20#include "window_helper.h"
21#include "window_manager_hilog.h"
22namespace OHOS {
23namespace Rosen {
24namespace {
25constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "StateMachine"};
26} // namespace
27
28WindowNodeStateMachine::WindowNodeStateMachine()
29{
30}
31
32WindowNodeStateMachine::~WindowNodeStateMachine()
33{
34}
35
36void WindowNodeStateMachine::SetDestroyTaskParam(bool onlySelf)
37{
38    destroyOnlySelf_ = destroyOnlySelf_ && onlySelf;
39}
40
41bool WindowNodeStateMachine::GetDestroyTaskParam()
42{
43    return destroyOnlySelf_;
44}
45
46bool WindowNodeStateMachine::GetDestroyTask(StateTask& task)
47{
48    if (!RemoteAnimation::IsAnimationFirst()) {
49        return false;
50    }
51    std::lock_guard<std::recursive_mutex> lock(mutex_);
52    if (destroyTask_ != nullptr) {
53        task = destroyTask_;
54        WLOGI("GetDestroyTask success:%{public}u", windowId_);
55        return true;
56    }
57    return false;
58}
59
60void WindowNodeStateMachine::SetDestroyTask(StateTask task)
61{
62    std::lock_guard<std::recursive_mutex> lock(mutex_);
63    destroyTask_ = task;
64}
65
66void WindowNodeStateMachine::TransitionTo(WindowNodeState state)
67{
68    std::lock_guard<std::recursive_mutex> lock(mutex_);
69    if (WindowHelper::IsSystemWindow(type_)) {
70        WLOGFD("system window no need to use stateMachine");
71        return;
72    }
73    currState_ = state;
74}
75
76void WindowNodeStateMachine::UpdateAnimationTaskCount(bool isAdd)
77{
78    if (!RemoteAnimation::IsAnimationFirst()) {
79        WLOGI("not animation first!");
80        return;
81    }
82    std::lock_guard<std::recursive_mutex> lock(mutex_);
83    if (isAdd) {
84        taskCount_++;
85        count1++;
86        WLOGFD("after add UpdateAnimationTaskCount1: %{public}u id:%{public}u", count1, windowId_);
87    } else {
88        taskCount_--;
89        count2++;
90        WLOGFD("after sub UpdateAnimationTaskCount1: %{public}u id:%{public}u", count2, windowId_);
91    }
92}
93
94void WindowNodeStateMachine::ResetAnimationTaskCount(int32_t taskCount)
95{
96    std::lock_guard<std::recursive_mutex> lock(mutex_);
97    taskCount_ = taskCount;
98}
99
100int32_t WindowNodeStateMachine::GetAnimationCount()
101{
102    std::lock_guard<std::recursive_mutex> lock(mutex_);
103    return taskCount_;
104}
105
106bool WindowNodeStateMachine::IsRemoteAnimationPlaying()
107{
108    std::lock_guard<std::recursive_mutex> lock(mutex_);
109    WLOGFD("IsRemoteAnimationPlaying id:%{public}u state:%{public}u", windowId_, static_cast<uint32_t>(currState_));
110    if (currState_ == WindowNodeState::SHOW_ANIMATION_PLAYING ||
111        currState_ == WindowNodeState::HIDE_ANIMATION_PLAYING) {
112            return true;
113    }
114    return false;
115}
116
117bool WindowNodeStateMachine::IsShowAnimationPlaying()
118{
119    std::lock_guard<std::recursive_mutex> lock(mutex_);
120    WLOGFD("IsShowAnimationPlaying id:%{public}u state:%{public}u", windowId_, static_cast<uint32_t>(currState_));
121    return currState_ == WindowNodeState::SHOW_ANIMATION_PLAYING;
122}
123
124bool WindowNodeStateMachine::IsHideAnimationPlaying()
125{
126    std::lock_guard<std::recursive_mutex> lock(mutex_);
127    WLOGFD("IsHideAnimationPlaying id:%{public}u state:%{public}u", windowId_, static_cast<uint32_t>(currState_));
128    return currState_ == WindowNodeState::HIDE_ANIMATION_PLAYING;
129}
130
131bool WindowNodeStateMachine::IsWindowNodeShownOrShowing()
132{
133    std::lock_guard<std::recursive_mutex> lock(mutex_);
134    WLOGFD("IsWindowNodeShownOrShowing id:%{public}u state:%{public}u", windowId_, static_cast<uint32_t>(currState_));
135    if (currState_ == WindowNodeState::SHOW_ANIMATION_PLAYING ||
136        currState_ == WindowNodeState::SHOW_ANIMATION_DONE || currState_ == WindowNodeState::SHOWN) {
137            return true; // not play show animation again when
138    }
139    return false;
140}
141
142bool WindowNodeStateMachine::IsWindowNodeHiddenOrHiding()
143{
144    std::lock_guard<std::recursive_mutex> lock(mutex_);
145    WLOGFD("IsWindowNodeHiddenOrHiding id:%{public}u state:%{public}u", windowId_, static_cast<uint32_t>(currState_));
146    if (currState_ == WindowNodeState::HIDE_ANIMATION_PLAYING ||
147        currState_ == WindowNodeState::HIDE_ANIMATION_DONE || currState_ == WindowNodeState::HIDDEN) {
148            return true; // not play show animation again when
149    }
150    return false;
151}
152
153WindowNodeState WindowNodeStateMachine::GetCurrentState()
154{
155    return currState_;
156}
157
158std::string WindowNodeStateMachine::GenStateMachineInfo()
159{
160    std::ostringstream oss;
161    oss << "windowId: " << windowId_
162        << ", animationTask: " << count1 << ", callbackTaskCount: " << count2++
163        << ", totalCount: " << taskCount_
164        << ", currentState: " << static_cast<int32_t>(currState_) << ";";
165    std::string info(oss.str());
166    return info;
167}
168} // Rosen
169} // OHOS