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 "session/host/include/sub_session.h"
17 #include "screen_session_manager/include/screen_session_manager_client.h"
18
19 #include "common/include/session_permission.h"
20 #include "key_event.h"
21 #include "window_helper.h"
22 #include "parameters.h"
23 #include "pointer_event.h"
24 #include "window_manager_hilog.h"
25
26
27 namespace OHOS::Rosen {
28 namespace {
29 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "SubSession" };
30 } // namespace
31
SubSession(const SessionInfo & info,const sptr<SpecificSessionCallback> & specificCallback)32 SubSession::SubSession(const SessionInfo& info, const sptr<SpecificSessionCallback>& specificCallback)
33 : SceneSession(info, specificCallback)
34 {
35 moveDragController_ = new (std::nothrow) MoveDragController(GetPersistentId());
36 if (moveDragController_ != nullptr && specificCallback != nullptr &&
37 specificCallback->onWindowInputPidChangeCallback_ != nullptr) {
38 moveDragController_->SetNotifyWindowPidChangeCallback(specificCallback->onWindowInputPidChangeCallback_);
39 }
40 SetMoveDragCallback();
41 TLOGD(WmsLogTag::WMS_LIFE, "Create SubSession");
42 }
43
~SubSession()44 SubSession::~SubSession()
45 {
46 TLOGD(WmsLogTag::WMS_LIFE, "~SubSession, id: %{public}d", GetPersistentId());
47 }
48
Show(sptr<WindowSessionProperty> property)49 WSError SubSession::Show(sptr<WindowSessionProperty> property)
50 {
51 if (!CheckPermissionWithPropertyAnimation(property)) {
52 return WSError::WS_ERROR_NOT_SYSTEM_APP;
53 }
54 auto task = [weakThis = wptr(this), property]() {
55 auto session = weakThis.promote();
56 if (!session) {
57 TLOGE(WmsLogTag::WMS_SUB, "session is null");
58 return WSError::WS_ERROR_DESTROYED_OBJECT;
59 }
60 TLOGI(WmsLogTag::WMS_LIFE, "Show session, id: %{public}d", session->GetPersistentId());
61
62 // use property from client
63 auto sessionProperty = session->GetSessionProperty();
64 if (property && property->GetAnimationFlag() == static_cast<uint32_t>(WindowAnimation::CUSTOM) &&
65 sessionProperty) {
66 sessionProperty->SetAnimationFlag(static_cast<uint32_t>(WindowAnimation::CUSTOM));
67 session->NotifyIsCustomAnimationPlaying(true);
68 }
69 auto ret = session->SceneSession::Foreground(property);
70 return ret;
71 };
72 PostTask(task, "Show");
73 return WSError::WS_OK;
74 }
75
Hide()76 WSError SubSession::Hide()
77 {
78 if (!CheckPermissionWithPropertyAnimation(GetSessionProperty())) {
79 return WSError::WS_ERROR_NOT_SYSTEM_APP;
80 }
81 auto task = [weakThis = wptr(this)]() {
82 auto session = weakThis.promote();
83 if (!session) {
84 TLOGE(WmsLogTag::WMS_SUB, "session is null");
85 return WSError::WS_ERROR_DESTROYED_OBJECT;
86 }
87 TLOGI(WmsLogTag::WMS_LIFE, "Hide session, id: %{public}d", session->GetPersistentId());
88 auto ret = session->SetActive(false);
89 if (ret != WSError::WS_OK) {
90 return ret;
91 }
92 // background will remove surfaceNode, custom not execute
93 // not animation playing when already background; inactive may be animation playing
94 auto sessionProperty = session->GetSessionProperty();
95 if (sessionProperty &&
96 sessionProperty->GetAnimationFlag() == static_cast<uint32_t>(WindowAnimation::CUSTOM)) {
97 session->NotifyIsCustomAnimationPlaying(true);
98 return WSError::WS_OK;
99 }
100 ret = session->SceneSession::Background();
101 return ret;
102 };
103 PostTask(task, "Hide");
104 return WSError::WS_OK;
105 }
106
ProcessPointDownSession(int32_t posX,int32_t posY)107 WSError SubSession::ProcessPointDownSession(int32_t posX, int32_t posY)
108 {
109 const auto& id = GetPersistentId();
110 WLOGFI("id: %{public}d, type: %{public}d", id, GetWindowType());
111 auto parentSession = GetParentSession();
112 if (parentSession && parentSession->CheckDialogOnForeground()) {
113 WLOGFI("Has dialog foreground, id: %{public}d, type: %{public}d", id, GetWindowType());
114 return WSError::WS_OK;
115 }
116 auto sessionProperty = GetSessionProperty();
117 if (sessionProperty && sessionProperty->GetRaiseEnabled()) {
118 RaiseToAppTopForPointDown();
119 }
120 PresentFocusIfPointDown();
121 return SceneSession::ProcessPointDownSession(posX, posY);
122 }
123
GetMissionId() const124 int32_t SubSession::GetMissionId() const
125 {
126 auto parentSession = GetParentSession();
127 return parentSession != nullptr ? parentSession->GetPersistentId() : SceneSession::GetMissionId();
128 }
129
TransferKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent)130 WSError SubSession::TransferKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
131 {
132 if (!IsSessionValid()) {
133 return WSError::WS_ERROR_INVALID_SESSION;
134 }
135 if (keyEvent == nullptr) {
136 WLOGFE("KeyEvent is nullptr");
137 return WSError::WS_ERROR_NULLPTR;
138 }
139 auto parentSession = GetParentSession();
140 if (parentSession && parentSession->CheckDialogOnForeground()) {
141 TLOGD(WmsLogTag::WMS_DIALOG, "Its main window has dialog on foreground, not transfer pointer event");
142 return WSError::WS_ERROR_INVALID_PERMISSION;
143 }
144
145 WSError ret = Session::TransferKeyEvent(keyEvent);
146 return ret;
147 }
148
UpdatePointerArea(const WSRect & rect)149 void SubSession::UpdatePointerArea(const WSRect& rect)
150 {
151 auto property = GetSessionProperty();
152 if (!(property->IsDecorEnable() && GetWindowMode() == WindowMode::WINDOW_MODE_FLOATING)) {
153 return;
154 }
155 Session::UpdatePointerArea(rect);
156 }
157
CheckPointerEventDispatch(const std::shared_ptr<MMI::PointerEvent> & pointerEvent) const158 bool SubSession::CheckPointerEventDispatch(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) const
159 {
160 auto sessionState = GetSessionState();
161 int32_t action = pointerEvent->GetPointerAction();
162 auto isPC = systemConfig_.uiType_ == UI_TYPE_PC;
163 if (isPC && sessionState != SessionState::STATE_FOREGROUND &&
164 sessionState != SessionState::STATE_ACTIVE &&
165 action != MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW) {
166 WLOGFW("Current Session Info: [persistentId: %{public}d, "
167 "state: %{public}d, action:%{public}d]", GetPersistentId(), GetSessionState(), action);
168 return false;
169 }
170 return true;
171 }
172
RectCheck(uint32_t curWidth,uint32_t curHeight)173 void SubSession::RectCheck(uint32_t curWidth, uint32_t curHeight)
174 {
175 uint32_t minWidth = GetSystemConfig().miniWidthOfSubWindow_;
176 uint32_t minHeight = GetSystemConfig().miniHeightOfSubWindow_;
177 uint32_t maxFloatingWindowSize = GetSystemConfig().maxFloatingWindowSize_;
178 RectSizeCheckProcess(curWidth, curHeight, minWidth, minHeight, maxFloatingWindowSize);
179 }
180
IsTopmost() const181 bool SubSession::IsTopmost() const
182 {
183 bool isTopmost = false;
184 auto sessionProperty = GetSessionProperty();
185 if (sessionProperty) {
186 isTopmost = sessionProperty->IsTopmost();
187 }
188 TLOGI(WmsLogTag::WMS_SUB, "isTopmost: %{public}d", isTopmost);
189 return isTopmost;
190 }
191
IsModal() const192 bool SubSession::IsModal() const
193 {
194 bool isModal = false;
195 auto property = GetSessionProperty();
196 if (property != nullptr) {
197 isModal = WindowHelper::IsModalSubWindow(property->GetWindowType(), property->GetWindowFlags());
198 }
199 return isModal;
200 }
201
IsVisibleForeground() const202 bool SubSession::IsVisibleForeground() const
203 {
204 if (parentSession_ && WindowHelper::IsMainWindow(parentSession_->GetWindowType())) {
205 return parentSession_->IsVisibleForeground() && Session::IsVisibleForeground();
206 }
207 return Session::IsVisibleForeground();
208 }
209 } // namespace OHOS::Rosen
210