1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#include "user_controller.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include "ability_manager_service.h"
19eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h"
20eace7efcSopenharmony_ci#include "mock_session_manager_service.h"
21eace7efcSopenharmony_ci#include "os_account_manager_wrapper.h"
22eace7efcSopenharmony_ci#include "scene_board_judgement.h"
23eace7efcSopenharmony_ci
24eace7efcSopenharmony_cinamespace OHOS {
25eace7efcSopenharmony_cinamespace AAFwk {
26eace7efcSopenharmony_ciusing namespace OHOS::AppExecFwk;
27eace7efcSopenharmony_cinamespace {
28eace7efcSopenharmony_ciconst int64_t USER_SWITCH_TIMEOUT = 3 * 1000; // 3s
29eace7efcSopenharmony_ci}
30eace7efcSopenharmony_ci
31eace7efcSopenharmony_ciUserItem::UserItem(int32_t id) : userId_(id)
32eace7efcSopenharmony_ci{}
33eace7efcSopenharmony_ci
34eace7efcSopenharmony_ciUserItem::~UserItem() {}
35eace7efcSopenharmony_ci
36eace7efcSopenharmony_ciint32_t UserItem::GetUserId()
37eace7efcSopenharmony_ci{
38eace7efcSopenharmony_ci    return userId_;
39eace7efcSopenharmony_ci}
40eace7efcSopenharmony_ci
41eace7efcSopenharmony_civoid UserItem::SetState(const UserState &state)
42eace7efcSopenharmony_ci{
43eace7efcSopenharmony_ci    if (curState_ == state) {
44eace7efcSopenharmony_ci        return;
45eace7efcSopenharmony_ci    }
46eace7efcSopenharmony_ci    lastState_ = curState_;
47eace7efcSopenharmony_ci    curState_ = state;
48eace7efcSopenharmony_ci}
49eace7efcSopenharmony_ci
50eace7efcSopenharmony_ciUserState UserItem::GetState()
51eace7efcSopenharmony_ci{
52eace7efcSopenharmony_ci    return curState_;
53eace7efcSopenharmony_ci}
54eace7efcSopenharmony_ci
55eace7efcSopenharmony_ciUserController::UserController()
56eace7efcSopenharmony_ci{
57eace7efcSopenharmony_ci}
58eace7efcSopenharmony_ci
59eace7efcSopenharmony_ciUserController::~UserController()
60eace7efcSopenharmony_ci{
61eace7efcSopenharmony_ci}
62eace7efcSopenharmony_ci
63eace7efcSopenharmony_civoid UserController::Init()
64eace7efcSopenharmony_ci{
65eace7efcSopenharmony_ci    auto handler = DelayedSingleton<AbilityManagerService>::GetInstance()->GetTaskHandler();
66eace7efcSopenharmony_ci    if (!handler) {
67eace7efcSopenharmony_ci        return;
68eace7efcSopenharmony_ci    }
69eace7efcSopenharmony_ci
70eace7efcSopenharmony_ci    if (eventHandler_) {
71eace7efcSopenharmony_ci        return;
72eace7efcSopenharmony_ci    }
73eace7efcSopenharmony_ci    eventHandler_ = std::make_shared<UserEventHandler>(handler, shared_from_this());
74eace7efcSopenharmony_ci}
75eace7efcSopenharmony_ci
76eace7efcSopenharmony_civoid UserController::ClearAbilityUserItems(int32_t userId)
77eace7efcSopenharmony_ci{
78eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> guard(userLock_);
79eace7efcSopenharmony_ci    if (userItems_.count(userId)) {
80eace7efcSopenharmony_ci        userItems_.erase(userId);
81eace7efcSopenharmony_ci    }
82eace7efcSopenharmony_ci}
83eace7efcSopenharmony_ci
84eace7efcSopenharmony_civoid UserController::StartUser(int32_t userId, sptr<IUserCallback> callback, bool isAppRecovery)
85eace7efcSopenharmony_ci{
86eace7efcSopenharmony_ci    if (userId < 0 || userId == USER_ID_NO_HEAD) {
87eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "StartUserId invalid:%{public}d", userId);
88eace7efcSopenharmony_ci        callback->OnStartUserDone(userId, INVALID_USERID_VALUE);
89eace7efcSopenharmony_ci        return;
90eace7efcSopenharmony_ci    }
91eace7efcSopenharmony_ci
92eace7efcSopenharmony_ci    if (IsCurrentUser(userId)) {
93eace7efcSopenharmony_ci        TAG_LOGW(AAFwkTag::ABILITYMGR, "StartUser current:%{public}d", userId);
94eace7efcSopenharmony_ci        callback->OnStartUserDone(userId, ERR_OK);
95eace7efcSopenharmony_ci        return;
96eace7efcSopenharmony_ci    }
97eace7efcSopenharmony_ci
98eace7efcSopenharmony_ci    auto appScheduler = DelayedSingleton<AppScheduler>::GetInstance();
99eace7efcSopenharmony_ci    if (!appScheduler) {
100eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "null appScheduler");
101eace7efcSopenharmony_ci        return;
102eace7efcSopenharmony_ci    }
103eace7efcSopenharmony_ci    appScheduler->SetEnableStartProcessFlagByUserId(userId, true);
104eace7efcSopenharmony_ci
105eace7efcSopenharmony_ci    if (!IsExistOsAccount(userId)) {
106eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "null StartUser account:%{public}d", userId);
107eace7efcSopenharmony_ci        callback->OnStartUserDone(userId, INVALID_USERID_VALUE);
108eace7efcSopenharmony_ci        return;
109eace7efcSopenharmony_ci    }
110eace7efcSopenharmony_ci
111eace7efcSopenharmony_ci    if (GetCurrentUserId() != USER_ID_NO_HEAD && !Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
112eace7efcSopenharmony_ci        // start freezing screen
113eace7efcSopenharmony_ci        SetFreezingNewUserId(userId);
114eace7efcSopenharmony_ci        DelayedSingleton<AbilityManagerService>::GetInstance()->StartFreezingScreen();
115eace7efcSopenharmony_ci    }
116eace7efcSopenharmony_ci
117eace7efcSopenharmony_ci    auto oldUserId = GetCurrentUserId();
118eace7efcSopenharmony_ci    auto userItem = GetOrCreateUserItem(userId);
119eace7efcSopenharmony_ci    auto state = userItem->GetState();
120eace7efcSopenharmony_ci    if (state == STATE_STOPPING || state == STATE_SHUTDOWN) {
121eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "StartUser user stop, userId:%{public}d", userId);
122eace7efcSopenharmony_ci        callback->OnStartUserDone(userId, ERR_DEAD_OBJECT);
123eace7efcSopenharmony_ci        return;
124eace7efcSopenharmony_ci    }
125eace7efcSopenharmony_ci
126eace7efcSopenharmony_ci    SetCurrentUserId(userId);
127eace7efcSopenharmony_ci    // notify wms switching now
128eace7efcSopenharmony_ci
129eace7efcSopenharmony_ci    bool needStart = false;
130eace7efcSopenharmony_ci    if (state == STATE_BOOTING) {
131eace7efcSopenharmony_ci        needStart = true;
132eace7efcSopenharmony_ci        // send user start msg.
133eace7efcSopenharmony_ci        SendSystemUserStart(userId);
134eace7efcSopenharmony_ci    }
135eace7efcSopenharmony_ci
136eace7efcSopenharmony_ci    SendSystemUserCurrent(oldUserId, userId);
137eace7efcSopenharmony_ci    SendReportUserSwitch(oldUserId, userId, userItem);
138eace7efcSopenharmony_ci    SendUserSwitchTimeout(oldUserId, userId, userItem);
139eace7efcSopenharmony_ci
140eace7efcSopenharmony_ci    if (needStart) {
141eace7efcSopenharmony_ci        BroadcastUserStarted(userId);
142eace7efcSopenharmony_ci    }
143eace7efcSopenharmony_ci
144eace7efcSopenharmony_ci    UserBootDone(userItem);
145eace7efcSopenharmony_ci    MoveUserToForeground(oldUserId, userId, callback, isAppRecovery);
146eace7efcSopenharmony_ci}
147eace7efcSopenharmony_ci
148eace7efcSopenharmony_ciint32_t UserController::StopUser(int32_t userId)
149eace7efcSopenharmony_ci{
150eace7efcSopenharmony_ci    if (userId < 0 || userId == USER_ID_NO_HEAD || userId == USER_ID_DEFAULT) {
151eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "userId invalid:%{public}d", userId);
152eace7efcSopenharmony_ci        return -1;
153eace7efcSopenharmony_ci    }
154eace7efcSopenharmony_ci
155eace7efcSopenharmony_ci    if (IsCurrentUser(userId)) {
156eace7efcSopenharmony_ci        TAG_LOGW(AAFwkTag::ABILITYMGR, "user current:%{public}d", userId);
157eace7efcSopenharmony_ci        return 0;
158eace7efcSopenharmony_ci    }
159eace7efcSopenharmony_ci
160eace7efcSopenharmony_ci    if (!IsExistOsAccount(userId)) {
161eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "null account:%{public}d", userId);
162eace7efcSopenharmony_ci        return -1;
163eace7efcSopenharmony_ci    }
164eace7efcSopenharmony_ci
165eace7efcSopenharmony_ci    BroadcastUserStopping(userId);
166eace7efcSopenharmony_ci
167eace7efcSopenharmony_ci    auto appScheduler = DelayedSingleton<AppScheduler>::GetInstance();
168eace7efcSopenharmony_ci    if (!appScheduler) {
169eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "null appScheduler");
170eace7efcSopenharmony_ci        return -1;
171eace7efcSopenharmony_ci    }
172eace7efcSopenharmony_ci    appScheduler->KillProcessesByUserId(userId);
173eace7efcSopenharmony_ci
174eace7efcSopenharmony_ci    auto abilityManagerService = DelayedSingleton<AbilityManagerService>::GetInstance();
175eace7efcSopenharmony_ci    if (!abilityManagerService) {
176eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "abilityManagerService null");
177eace7efcSopenharmony_ci        return -1;
178eace7efcSopenharmony_ci    }
179eace7efcSopenharmony_ci
180eace7efcSopenharmony_ci    if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
181eace7efcSopenharmony_ci        auto missionListWrap = abilityManagerService->GetMissionListWrap();
182eace7efcSopenharmony_ci        if (!missionListWrap) {
183eace7efcSopenharmony_ci            TAG_LOGE(AAFwkTag::ABILITYMGR, "missionListWrap null");
184eace7efcSopenharmony_ci            return -1;
185eace7efcSopenharmony_ci        }
186eace7efcSopenharmony_ci        missionListWrap->RemoveUserDir(userId);
187eace7efcSopenharmony_ci    }
188eace7efcSopenharmony_ci
189eace7efcSopenharmony_ci    abilityManagerService->ClearUserData(userId);
190eace7efcSopenharmony_ci
191eace7efcSopenharmony_ci    BroadcastUserStopped(userId);
192eace7efcSopenharmony_ci    return 0;
193eace7efcSopenharmony_ci}
194eace7efcSopenharmony_ci
195eace7efcSopenharmony_ciint32_t UserController::LogoutUser(int32_t userId)
196eace7efcSopenharmony_ci{
197eace7efcSopenharmony_ci    if (userId < 0 || userId == USER_ID_NO_HEAD) {
198eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "userId invalid:%{public}d", userId);
199eace7efcSopenharmony_ci        return INVALID_USERID_VALUE;
200eace7efcSopenharmony_ci    }
201eace7efcSopenharmony_ci    if (!IsExistOsAccount(userId)) {
202eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "null account:%{public}d", userId);
203eace7efcSopenharmony_ci        return INVALID_USERID_VALUE;
204eace7efcSopenharmony_ci    }
205eace7efcSopenharmony_ci    auto abilityManagerService = DelayedSingleton<AbilityManagerService>::GetInstance();
206eace7efcSopenharmony_ci    if (!abilityManagerService) {
207eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "null abilityManagerService");
208eace7efcSopenharmony_ci        return -1;
209eace7efcSopenharmony_ci    }
210eace7efcSopenharmony_ci    abilityManagerService->RemoveLauncherDeathRecipient(userId);
211eace7efcSopenharmony_ci    if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
212eace7efcSopenharmony_ci        TAG_LOGI(AAFwkTag::ABILITYMGR, "SceneBoard exit normally.");
213eace7efcSopenharmony_ci        Rosen::MockSessionManagerService::GetInstance().NotifyNotKillService();
214eace7efcSopenharmony_ci    }
215eace7efcSopenharmony_ci    auto appScheduler = DelayedSingleton<AppScheduler>::GetInstance();
216eace7efcSopenharmony_ci    if (!appScheduler) {
217eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "null appScheduler");
218eace7efcSopenharmony_ci        return INVALID_USERID_VALUE;
219eace7efcSopenharmony_ci    }
220eace7efcSopenharmony_ci    abilityManagerService->ClearUserData(userId);
221eace7efcSopenharmony_ci    appScheduler->SetEnableStartProcessFlagByUserId(userId, false);
222eace7efcSopenharmony_ci    if (IsCurrentUser(userId)) {
223eace7efcSopenharmony_ci        SetCurrentUserId(0);
224eace7efcSopenharmony_ci    }
225eace7efcSopenharmony_ci    appScheduler->KillProcessesByUserId(userId);
226eace7efcSopenharmony_ci    ClearAbilityUserItems(userId);
227eace7efcSopenharmony_ci    return 0;
228eace7efcSopenharmony_ci}
229eace7efcSopenharmony_ci
230eace7efcSopenharmony_ciint32_t UserController::GetCurrentUserId()
231eace7efcSopenharmony_ci{
232eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> guard(userLock_);
233eace7efcSopenharmony_ci    return currentUserId_;
234eace7efcSopenharmony_ci}
235eace7efcSopenharmony_ci
236eace7efcSopenharmony_cistd::shared_ptr<UserItem> UserController::GetUserItem(int32_t userId)
237eace7efcSopenharmony_ci{
238eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> guard(userLock_);
239eace7efcSopenharmony_ci    auto it = userItems_.find(userId);
240eace7efcSopenharmony_ci    if (it != userItems_.end()) {
241eace7efcSopenharmony_ci        return it->second;
242eace7efcSopenharmony_ci    }
243eace7efcSopenharmony_ci
244eace7efcSopenharmony_ci    return nullptr;
245eace7efcSopenharmony_ci}
246eace7efcSopenharmony_ci
247eace7efcSopenharmony_cibool UserController::IsCurrentUser(int32_t userId)
248eace7efcSopenharmony_ci{
249eace7efcSopenharmony_ci    int32_t oldUserId = GetCurrentUserId();
250eace7efcSopenharmony_ci    if (oldUserId == userId) {
251eace7efcSopenharmony_ci        auto userItem = GetUserItem(userId);
252eace7efcSopenharmony_ci        if (userItem) {
253eace7efcSopenharmony_ci            TAG_LOGW(AAFwkTag::ABILITYMGR, "IsCurrentUserId current:%{public}d", userId);
254eace7efcSopenharmony_ci            return true;
255eace7efcSopenharmony_ci        }
256eace7efcSopenharmony_ci    }
257eace7efcSopenharmony_ci    return false;
258eace7efcSopenharmony_ci}
259eace7efcSopenharmony_ci
260eace7efcSopenharmony_cibool UserController::IsExistOsAccount(int32_t userId)
261eace7efcSopenharmony_ci{
262eace7efcSopenharmony_ci    bool isExist = false;
263eace7efcSopenharmony_ci    auto errCode = DelayedSingleton<OsAccountManagerWrapper>::GetInstance()->IsOsAccountExists(userId, isExist);
264eace7efcSopenharmony_ci    return (errCode == 0) && isExist;
265eace7efcSopenharmony_ci}
266eace7efcSopenharmony_ci
267eace7efcSopenharmony_cistd::shared_ptr<UserItem> UserController::GetOrCreateUserItem(int32_t userId)
268eace7efcSopenharmony_ci{
269eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> guard(userLock_);
270eace7efcSopenharmony_ci    auto it = userItems_.find(userId);
271eace7efcSopenharmony_ci    if (it != userItems_.end()) {
272eace7efcSopenharmony_ci        return it->second;
273eace7efcSopenharmony_ci    }
274eace7efcSopenharmony_ci
275eace7efcSopenharmony_ci    auto userItem = std::make_shared<UserItem>(userId);
276eace7efcSopenharmony_ci    userItems_.emplace(userId, userItem);
277eace7efcSopenharmony_ci    return userItem;
278eace7efcSopenharmony_ci}
279eace7efcSopenharmony_ci
280eace7efcSopenharmony_civoid UserController::SetCurrentUserId(int32_t userId)
281eace7efcSopenharmony_ci{
282eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> guard(userLock_);
283eace7efcSopenharmony_ci    currentUserId_ = userId;
284eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "set current userId: %{public}d", userId);
285eace7efcSopenharmony_ci    DelayedSingleton<AppScheduler>::GetInstance()->SetCurrentUserId(userId);
286eace7efcSopenharmony_ci}
287eace7efcSopenharmony_ci
288eace7efcSopenharmony_civoid UserController::MoveUserToForeground(int32_t oldUserId, int32_t newUserId, sptr<IUserCallback> callback,
289eace7efcSopenharmony_ci    bool isAppRecovery)
290eace7efcSopenharmony_ci{
291eace7efcSopenharmony_ci    auto manager = DelayedSingleton<AbilityManagerService>::GetInstance();
292eace7efcSopenharmony_ci    if (!manager) {
293eace7efcSopenharmony_ci        return;
294eace7efcSopenharmony_ci    }
295eace7efcSopenharmony_ci    manager->SwitchToUser(oldUserId, newUserId, callback, isAppRecovery);
296eace7efcSopenharmony_ci    BroadcastUserBackground(oldUserId);
297eace7efcSopenharmony_ci    BroadcastUserForeground(newUserId);
298eace7efcSopenharmony_ci}
299eace7efcSopenharmony_ci
300eace7efcSopenharmony_civoid UserController::UserBootDone(std::shared_ptr<UserItem> &item)
301eace7efcSopenharmony_ci{
302eace7efcSopenharmony_ci    if (!item) {
303eace7efcSopenharmony_ci        return;
304eace7efcSopenharmony_ci    }
305eace7efcSopenharmony_ci    int32_t userId = item->GetUserId();
306eace7efcSopenharmony_ci
307eace7efcSopenharmony_ci    std::lock_guard<ffrt::mutex> guard(userLock_);
308eace7efcSopenharmony_ci    auto it = userItems_.find(userId);
309eace7efcSopenharmony_ci    if (it != userItems_.end()) {
310eace7efcSopenharmony_ci        return;
311eace7efcSopenharmony_ci    }
312eace7efcSopenharmony_ci
313eace7efcSopenharmony_ci    if (item != it->second) {
314eace7efcSopenharmony_ci        return;
315eace7efcSopenharmony_ci    }
316eace7efcSopenharmony_ci    item->SetState(UserState::STATE_STARTED);
317eace7efcSopenharmony_ci    auto manager = DelayedSingleton<AbilityManagerService>::GetInstance();
318eace7efcSopenharmony_ci    if (!manager) {
319eace7efcSopenharmony_ci        return;
320eace7efcSopenharmony_ci    }
321eace7efcSopenharmony_ci    manager->UserStarted(userId);
322eace7efcSopenharmony_ci}
323eace7efcSopenharmony_ci
324eace7efcSopenharmony_civoid UserController::BroadcastUserStarted(int32_t userId)
325eace7efcSopenharmony_ci{
326eace7efcSopenharmony_ci    // broadcast event user start.
327eace7efcSopenharmony_ci}
328eace7efcSopenharmony_ci
329eace7efcSopenharmony_civoid UserController::BroadcastUserBackground(int32_t userId)
330eace7efcSopenharmony_ci{
331eace7efcSopenharmony_ci    // broadcast event user switch to bg.
332eace7efcSopenharmony_ci}
333eace7efcSopenharmony_ci
334eace7efcSopenharmony_civoid UserController::BroadcastUserForeground(int32_t userId)
335eace7efcSopenharmony_ci{
336eace7efcSopenharmony_ci    // broadcast event user switch to fg.
337eace7efcSopenharmony_ci}
338eace7efcSopenharmony_ci
339eace7efcSopenharmony_civoid UserController::BroadcastUserStopping(int32_t userId)
340eace7efcSopenharmony_ci{
341eace7efcSopenharmony_ci}
342eace7efcSopenharmony_ci
343eace7efcSopenharmony_civoid UserController::BroadcastUserStopped(int32_t userId)
344eace7efcSopenharmony_ci{
345eace7efcSopenharmony_ci}
346eace7efcSopenharmony_ci
347eace7efcSopenharmony_civoid UserController::SendSystemUserStart(int32_t userId)
348eace7efcSopenharmony_ci{
349eace7efcSopenharmony_ci    auto handler = eventHandler_;
350eace7efcSopenharmony_ci    if (!handler) {
351eace7efcSopenharmony_ci        return;
352eace7efcSopenharmony_ci    }
353eace7efcSopenharmony_ci
354eace7efcSopenharmony_ci    auto eventData = std::make_shared<UserEvent>();
355eace7efcSopenharmony_ci    eventData->newUserId = userId;
356eace7efcSopenharmony_ci    handler->SendEvent(EventWrap(UserEventHandler::EVENT_SYSTEM_USER_START, eventData));
357eace7efcSopenharmony_ci}
358eace7efcSopenharmony_ci
359eace7efcSopenharmony_civoid UserController::ProcessEvent(const EventWrap &event)
360eace7efcSopenharmony_ci{
361eace7efcSopenharmony_ci    auto eventId = event.GetEventId();
362eace7efcSopenharmony_ci    auto eventData = static_cast<UserEvent*>(event.GetEventData().get());
363eace7efcSopenharmony_ci    if (!eventData) {
364eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::ABILITYMGR, "no event data, event id: %{public}u.", eventId);
365eace7efcSopenharmony_ci        return;
366eace7efcSopenharmony_ci    }
367eace7efcSopenharmony_ci
368eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::ABILITYMGR, "Event id obtained: %{public}u.", eventId);
369eace7efcSopenharmony_ci    switch (eventId) {
370eace7efcSopenharmony_ci        case UserEventHandler::EVENT_SYSTEM_USER_START: {
371eace7efcSopenharmony_ci            HandleSystemUserStart(eventData->newUserId);
372eace7efcSopenharmony_ci            break;
373eace7efcSopenharmony_ci        }
374eace7efcSopenharmony_ci        case UserEventHandler::EVENT_SYSTEM_USER_CURRENT: {
375eace7efcSopenharmony_ci            HandleSystemUserCurrent(eventData->oldUserId, eventData->newUserId);
376eace7efcSopenharmony_ci            break;
377eace7efcSopenharmony_ci        }
378eace7efcSopenharmony_ci        case UserEventHandler::EVENT_REPORT_USER_SWITCH: {
379eace7efcSopenharmony_ci            HandleReportUserSwitch(eventData->oldUserId, eventData->newUserId, eventData->userItem);
380eace7efcSopenharmony_ci            break;
381eace7efcSopenharmony_ci        }
382eace7efcSopenharmony_ci        case UserEventHandler::EVENT_CONTINUE_USER_SWITCH: {
383eace7efcSopenharmony_ci            HandleContinueUserSwitch(eventData->oldUserId, eventData->newUserId, eventData->userItem);
384eace7efcSopenharmony_ci            break;
385eace7efcSopenharmony_ci        }
386eace7efcSopenharmony_ci        case UserEventHandler::EVENT_USER_SWITCH_TIMEOUT: {
387eace7efcSopenharmony_ci            HandleUserSwitchTimeout(eventData->oldUserId, eventData->newUserId, eventData->userItem);
388eace7efcSopenharmony_ci            break;
389eace7efcSopenharmony_ci        }
390eace7efcSopenharmony_ci        case UserEventHandler::EVENT_REPORT_USER_SWITCH_DONE: {
391eace7efcSopenharmony_ci            HandleUserSwitchDone(eventData->newUserId);
392eace7efcSopenharmony_ci            break;
393eace7efcSopenharmony_ci        }
394eace7efcSopenharmony_ci        default: {
395eace7efcSopenharmony_ci            TAG_LOGW(AAFwkTag::ABILITYMGR, "Unsupported  event.");
396eace7efcSopenharmony_ci            break;
397eace7efcSopenharmony_ci        }
398eace7efcSopenharmony_ci    }
399eace7efcSopenharmony_ci}
400eace7efcSopenharmony_ci
401eace7efcSopenharmony_civoid UserController::SendSystemUserCurrent(int32_t oldUserId, int32_t newUserId)
402eace7efcSopenharmony_ci{
403eace7efcSopenharmony_ci    auto handler = eventHandler_;
404eace7efcSopenharmony_ci    if (!handler) {
405eace7efcSopenharmony_ci        return;
406eace7efcSopenharmony_ci    }
407eace7efcSopenharmony_ci
408eace7efcSopenharmony_ci    auto eventData = std::make_shared<UserEvent>();
409eace7efcSopenharmony_ci    eventData->oldUserId = oldUserId;
410eace7efcSopenharmony_ci    eventData->newUserId = newUserId;
411eace7efcSopenharmony_ci    handler->SendEvent(EventWrap(UserEventHandler::EVENT_SYSTEM_USER_CURRENT, eventData));
412eace7efcSopenharmony_ci}
413eace7efcSopenharmony_ci
414eace7efcSopenharmony_civoid UserController::SendReportUserSwitch(int32_t oldUserId, int32_t newUserId,
415eace7efcSopenharmony_ci    std::shared_ptr<UserItem> &usrItem)
416eace7efcSopenharmony_ci{
417eace7efcSopenharmony_ci    auto handler = eventHandler_;
418eace7efcSopenharmony_ci    if (!handler) {
419eace7efcSopenharmony_ci        return;
420eace7efcSopenharmony_ci    }
421eace7efcSopenharmony_ci
422eace7efcSopenharmony_ci    auto eventData = std::make_shared<UserEvent>();
423eace7efcSopenharmony_ci    eventData->oldUserId = oldUserId;
424eace7efcSopenharmony_ci    eventData->newUserId = newUserId;
425eace7efcSopenharmony_ci    eventData->userItem = usrItem;
426eace7efcSopenharmony_ci    handler->SendEvent(EventWrap(UserEventHandler::EVENT_REPORT_USER_SWITCH, eventData));
427eace7efcSopenharmony_ci}
428eace7efcSopenharmony_ci
429eace7efcSopenharmony_civoid UserController::SendUserSwitchTimeout(int32_t oldUserId, int32_t newUserId,
430eace7efcSopenharmony_ci    std::shared_ptr<UserItem> &usrItem)
431eace7efcSopenharmony_ci{
432eace7efcSopenharmony_ci    auto handler = eventHandler_;
433eace7efcSopenharmony_ci    if (!handler) {
434eace7efcSopenharmony_ci        return;
435eace7efcSopenharmony_ci    }
436eace7efcSopenharmony_ci
437eace7efcSopenharmony_ci    auto eventData = std::make_shared<UserEvent>();
438eace7efcSopenharmony_ci    eventData->oldUserId = oldUserId;
439eace7efcSopenharmony_ci    eventData->newUserId = newUserId;
440eace7efcSopenharmony_ci    eventData->userItem = usrItem;
441eace7efcSopenharmony_ci    handler->SendEvent(EventWrap(UserEventHandler::EVENT_USER_SWITCH_TIMEOUT,
442eace7efcSopenharmony_ci        eventData), USER_SWITCH_TIMEOUT);
443eace7efcSopenharmony_ci}
444eace7efcSopenharmony_ci
445eace7efcSopenharmony_civoid UserController::SendContinueUserSwitch(int32_t oldUserId, int32_t newUserId,
446eace7efcSopenharmony_ci    std::shared_ptr<UserItem> &usrItem)
447eace7efcSopenharmony_ci{
448eace7efcSopenharmony_ci    auto handler = eventHandler_;
449eace7efcSopenharmony_ci    if (!handler) {
450eace7efcSopenharmony_ci        return;
451eace7efcSopenharmony_ci    }
452eace7efcSopenharmony_ci
453eace7efcSopenharmony_ci    auto eventData = std::make_shared<UserEvent>();
454eace7efcSopenharmony_ci    eventData->oldUserId = oldUserId;
455eace7efcSopenharmony_ci    eventData->newUserId = newUserId;
456eace7efcSopenharmony_ci    eventData->userItem = usrItem;
457eace7efcSopenharmony_ci    handler->SendEvent(EventWrap(UserEventHandler::EVENT_CONTINUE_USER_SWITCH, eventData));
458eace7efcSopenharmony_ci}
459eace7efcSopenharmony_ci
460eace7efcSopenharmony_civoid UserController::SendUserSwitchDone(int32_t userId)
461eace7efcSopenharmony_ci{
462eace7efcSopenharmony_ci    auto handler = eventHandler_;
463eace7efcSopenharmony_ci    if (!handler) {
464eace7efcSopenharmony_ci        return;
465eace7efcSopenharmony_ci    }
466eace7efcSopenharmony_ci
467eace7efcSopenharmony_ci    auto eventData = std::make_shared<UserEvent>();
468eace7efcSopenharmony_ci    eventData->newUserId = userId;
469eace7efcSopenharmony_ci    handler->SendEvent(EventWrap(UserEventHandler::EVENT_REPORT_USER_SWITCH_DONE,
470eace7efcSopenharmony_ci        eventData));
471eace7efcSopenharmony_ci}
472eace7efcSopenharmony_ci
473eace7efcSopenharmony_civoid UserController::HandleSystemUserStart(int32_t userId)
474eace7efcSopenharmony_ci{
475eace7efcSopenharmony_ci    // notify system mgr user start.
476eace7efcSopenharmony_ci}
477eace7efcSopenharmony_ci
478eace7efcSopenharmony_civoid UserController::HandleSystemUserCurrent(int32_t oldUserId, int32_t newUserId)
479eace7efcSopenharmony_ci{
480eace7efcSopenharmony_ci    // notify system mgr user switch to new.
481eace7efcSopenharmony_ci}
482eace7efcSopenharmony_ci
483eace7efcSopenharmony_civoid UserController::HandleReportUserSwitch(int32_t oldUserId, int32_t newUserId,
484eace7efcSopenharmony_ci    std::shared_ptr<UserItem> &usrItem)
485eace7efcSopenharmony_ci{
486eace7efcSopenharmony_ci    // notify user switch observers, not support yet.
487eace7efcSopenharmony_ci}
488eace7efcSopenharmony_ci
489eace7efcSopenharmony_civoid UserController::HandleUserSwitchTimeout(int32_t oldUserId, int32_t newUserId,
490eace7efcSopenharmony_ci    std::shared_ptr<UserItem> &usrItem)
491eace7efcSopenharmony_ci{
492eace7efcSopenharmony_ci    // other observers
493eace7efcSopenharmony_ci    SendContinueUserSwitch(oldUserId, newUserId, usrItem);
494eace7efcSopenharmony_ci}
495eace7efcSopenharmony_ci
496eace7efcSopenharmony_civoid UserController::HandleContinueUserSwitch(int32_t oldUserId, int32_t newUserId,
497eace7efcSopenharmony_ci    std::shared_ptr<UserItem> &usrItem)
498eace7efcSopenharmony_ci{
499eace7efcSopenharmony_ci    auto manager = DelayedSingleton<AbilityManagerService>::GetInstance();
500eace7efcSopenharmony_ci    if (manager && !Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
501eace7efcSopenharmony_ci        manager->StopFreezingScreen();
502eace7efcSopenharmony_ci    }
503eace7efcSopenharmony_ci    SendUserSwitchDone(newUserId);
504eace7efcSopenharmony_ci}
505eace7efcSopenharmony_ci
506eace7efcSopenharmony_civoid UserController::HandleUserSwitchDone(int32_t userId)
507eace7efcSopenharmony_ci{
508eace7efcSopenharmony_ci    // notify wms switching done.
509eace7efcSopenharmony_ci    // notify user switch observers.
510eace7efcSopenharmony_ci}
511eace7efcSopenharmony_ci
512eace7efcSopenharmony_ciint32_t UserController::GetFreezingNewUserId() const
513eace7efcSopenharmony_ci{
514eace7efcSopenharmony_ci    return freezingNewUserId_;
515eace7efcSopenharmony_ci}
516eace7efcSopenharmony_ci
517eace7efcSopenharmony_civoid UserController::SetFreezingNewUserId(int32_t userId)
518eace7efcSopenharmony_ci{
519eace7efcSopenharmony_ci    freezingNewUserId_ = userId;
520eace7efcSopenharmony_ci}
521eace7efcSopenharmony_ci}
522eace7efcSopenharmony_ci}
523