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"
22 namespace OHOS {
23 namespace Rosen {
24 namespace {
25 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "StateMachine"};
26 } // namespace
27 
WindowNodeStateMachine()28 WindowNodeStateMachine::WindowNodeStateMachine()
29 {
30 }
31 
~WindowNodeStateMachine()32 WindowNodeStateMachine::~WindowNodeStateMachine()
33 {
34 }
35 
SetDestroyTaskParam(bool onlySelf)36 void WindowNodeStateMachine::SetDestroyTaskParam(bool onlySelf)
37 {
38     destroyOnlySelf_ = destroyOnlySelf_ && onlySelf;
39 }
40 
GetDestroyTaskParam()41 bool WindowNodeStateMachine::GetDestroyTaskParam()
42 {
43     return destroyOnlySelf_;
44 }
45 
GetDestroyTask(StateTask & task)46 bool 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 
SetDestroyTask(StateTask task)60 void WindowNodeStateMachine::SetDestroyTask(StateTask task)
61 {
62     std::lock_guard<std::recursive_mutex> lock(mutex_);
63     destroyTask_ = task;
64 }
65 
TransitionTo(WindowNodeState state)66 void 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 
UpdateAnimationTaskCount(bool isAdd)76 void 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 
ResetAnimationTaskCount(int32_t taskCount)94 void WindowNodeStateMachine::ResetAnimationTaskCount(int32_t taskCount)
95 {
96     std::lock_guard<std::recursive_mutex> lock(mutex_);
97     taskCount_ = taskCount;
98 }
99 
GetAnimationCount()100 int32_t WindowNodeStateMachine::GetAnimationCount()
101 {
102     std::lock_guard<std::recursive_mutex> lock(mutex_);
103     return taskCount_;
104 }
105 
IsRemoteAnimationPlaying()106 bool 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 
IsShowAnimationPlaying()117 bool 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 
IsHideAnimationPlaying()124 bool 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 
IsWindowNodeShownOrShowing()131 bool 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 
IsWindowNodeHiddenOrHiding()142 bool 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 
GetCurrentState()153 WindowNodeState WindowNodeStateMachine::GetCurrentState()
154 {
155     return currState_;
156 }
157 
GenStateMachineInfo()158 std::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