1 /*
2 * Copyright (c) 2021-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 #include "ability_window.h"
17 #include "ability.h"
18 #include "ability_handler.h"
19 #include "hilog_tag_wrapper.h"
20 #include "scene_board_judgement.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
AbilityWindow()24 AbilityWindow::AbilityWindow()
25 {}
26
~AbilityWindow()27 AbilityWindow::~AbilityWindow()
28 {}
29
30 /**
31 * @brief Init the AbilityWindow object.
32 *
33 * @param handler The EventHandler of the Ability the AbilityWindow belong.
34 */
Init(std::shared_ptr<AbilityHandler> & handler,std::shared_ptr<Ability> ability)35 void AbilityWindow::Init(std::shared_ptr<AbilityHandler>& handler, std::shared_ptr<Ability> ability)
36 {
37 TAG_LOGD(AAFwkTag::ABILITY, "called");
38 handler_ = handler;
39 ability_ = std::weak_ptr<IAbilityEvent>(ability);
40 windowScene_ = std::make_shared<Rosen::WindowScene>();
41 }
42
InitWindow(std::shared_ptr<AbilityRuntime::AbilityContext> & abilityContext,sptr<Rosen::IWindowLifeCycle> & listener,int32_t displayId,sptr<Rosen::WindowOption> option,bool isPrivacy)43 bool AbilityWindow::InitWindow(std::shared_ptr<AbilityRuntime::AbilityContext> &abilityContext,
44 sptr<Rosen::IWindowLifeCycle> &listener, int32_t displayId, sptr<Rosen::WindowOption> option, bool isPrivacy)
45 {
46 TAG_LOGD(AAFwkTag::ABILITY, "called");
47 if (windowScene_ == nullptr) {
48 windowScene_ = std::make_shared<Rosen::WindowScene>();
49 }
50 Rosen::WMError ret = Rosen::WMError::WM_OK;
51 auto sessionToken = GetSessionToken();
52 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled() && sessionToken != nullptr) {
53 ret = windowScene_->Init(displayId, abilityContext, listener, option, sessionToken);
54 } else {
55 ret = windowScene_->Init(displayId, abilityContext, listener, option);
56 }
57 if (ret != OHOS::Rosen::WMError::WM_OK) {
58 TAG_LOGE(AAFwkTag::ABILITY, "init window scene failed");
59 return false;
60 }
61
62 auto window = windowScene_->GetMainWindow();
63 if (!window) {
64 TAG_LOGE(AAFwkTag::ABILITY, "null window");
65 return false;
66 }
67
68 if (isPrivacy) {
69 window->SetSystemPrivacyMode(true);
70 }
71
72 isWindowAttached = true;
73 TAG_LOGD(AAFwkTag::ABILITY, "end");
74 return true;
75 }
76
77 /**
78 * @brief Called when this ability is background.
79 *
80 */
OnPostAbilityBackground(uint32_t sceneFlag)81 void AbilityWindow::OnPostAbilityBackground(uint32_t sceneFlag)
82 {
83 TAG_LOGD(AAFwkTag::ABILITY, "called");
84 if (!isWindowAttached) {
85 TAG_LOGE(AAFwkTag::ABILITY, "window not attached");
86 return;
87 }
88
89 if (windowScene_) {
90 TAG_LOGD(AAFwkTag::ABILITY, "sceneFlag:%{public}d", sceneFlag);
91 windowScene_->GoBackground(sceneFlag);
92 TAG_LOGD(AAFwkTag::ABILITY, "go back end");
93 }
94
95 TAG_LOGD(AAFwkTag::ABILITY, "end");
96 }
97
98 /**
99 * @brief Called when this ability is foreground.
100 *
101 */
OnPostAbilityForeground(uint32_t sceneFlag)102 void AbilityWindow::OnPostAbilityForeground(uint32_t sceneFlag)
103 {
104 TAG_LOGD(AAFwkTag::ABILITY, "called");
105 if (!isWindowAttached) {
106 TAG_LOGE(AAFwkTag::ABILITY, "window not attached");
107 return;
108 }
109
110 if (windowScene_) {
111 TAG_LOGD(AAFwkTag::ABILITY, "sceneFlag:%{public}d", sceneFlag);
112 windowScene_->GoForeground(sceneFlag);
113 TAG_LOGD(AAFwkTag::ABILITY, "go fore end");
114 }
115
116 TAG_LOGD(AAFwkTag::ABILITY, "end");
117 }
118
119 /**
120 * @brief Called when this ability is stopped.
121 *
122 */
OnPostAbilityStop()123 void AbilityWindow::OnPostAbilityStop()
124 {
125 TAG_LOGD(AAFwkTag::ABILITY, "called");
126 if (!isWindowAttached) {
127 TAG_LOGE(AAFwkTag::ABILITY, "window not attached");
128 return;
129 }
130
131 if (windowScene_) {
132 windowScene_->GoDestroy();
133 }
134 isWindowAttached = false;
135 TAG_LOGD(AAFwkTag::ABILITY, "end");
136 }
137
138 /**
139 * @brief Get the window belong to the ability.
140 *
141 * @return Returns a Window object pointer.
142 */
GetWindow()143 const sptr<Rosen::Window> AbilityWindow::GetWindow()
144 {
145 if (!isWindowAttached) {
146 TAG_LOGE(AAFwkTag::ABILITY, "window not attached");
147 return nullptr;
148 }
149 return windowScene_ ? windowScene_->GetMainWindow() : nullptr;
150 }
151
152 #ifdef SUPPORT_GRAPHICS
SetMissionLabel(const std::string & label)153 ErrCode AbilityWindow::SetMissionLabel(const std::string &label)
154 {
155 TAG_LOGD(AAFwkTag::ABILITY, "called");
156 auto window = GetWindow();
157 if (!window) {
158 TAG_LOGE(AAFwkTag::ABILITY, "get window failed");
159 return -1;
160 }
161
162 auto ret = window->SetAPPWindowLabel(label);
163 if (ret != OHOS::Rosen::WMError::WM_OK) {
164 TAG_LOGE(AAFwkTag::ABILITY, "errCode:%{public}d", ret);
165 return -1;
166 }
167
168 return ERR_OK;
169 }
170
SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> & icon)171 ErrCode AbilityWindow::SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> &icon)
172 {
173 TAG_LOGD(AAFwkTag::ABILITY, "called");
174 auto window = GetWindow();
175 if (!window) {
176 TAG_LOGE(AAFwkTag::ABILITY, "get window failed");
177 return -1;
178 }
179
180 auto ret = window->SetAPPWindowIcon(icon);
181 if (ret != OHOS::Rosen::WMError::WM_OK) {
182 TAG_LOGE(AAFwkTag::ABILITY, "errCode:%{public}d", ret);
183 return -1;
184 }
185
186 return ERR_OK;
187 }
188 #endif
189
SetSessionToken(sptr<IRemoteObject> sessionToken)190 void AbilityWindow::SetSessionToken(sptr<IRemoteObject> sessionToken)
191 {
192 std::lock_guard lock(sessionTokenMutex_);
193 sessionToken_ = sessionToken;
194 }
195
GetSessionToken()196 sptr<IRemoteObject> AbilityWindow::GetSessionToken()
197 {
198 std::lock_guard lock(sessionTokenMutex_);
199 return sessionToken_;
200 }
201 } // namespace AppExecFwk
202 } // namespace OHOS
203