1c29fa5a6Sopenharmony_ci/*
2c29fa5a6Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at
6c29fa5a6Sopenharmony_ci *
7c29fa5a6Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8c29fa5a6Sopenharmony_ci *
9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and
13c29fa5a6Sopenharmony_ci * limitations under the License.
14c29fa5a6Sopenharmony_ci */
15c29fa5a6Sopenharmony_ci
16c29fa5a6Sopenharmony_ci#include "state_machine.h"
17c29fa5a6Sopenharmony_ci
18c29fa5a6Sopenharmony_ci#include "application_state_observer_stub.h"
19c29fa5a6Sopenharmony_ci#include "iservice_registry.h"
20c29fa5a6Sopenharmony_ci#include "system_ability_definition.h"
21c29fa5a6Sopenharmony_ci
22c29fa5a6Sopenharmony_ci#include "common_event_observer.h"
23c29fa5a6Sopenharmony_ci#include "cooperate_events.h"
24c29fa5a6Sopenharmony_ci#include "cooperate_free.h"
25c29fa5a6Sopenharmony_ci#include "cooperate_hisysevent.h"
26c29fa5a6Sopenharmony_ci#include "cooperate_in.h"
27c29fa5a6Sopenharmony_ci#include "cooperate_out.h"
28c29fa5a6Sopenharmony_ci#include "devicestatus_define.h"
29c29fa5a6Sopenharmony_ci#include "devicestatus_errors.h"
30c29fa5a6Sopenharmony_ci#include "event_manager.h"
31c29fa5a6Sopenharmony_ci#include "utility.h"
32c29fa5a6Sopenharmony_ci
33c29fa5a6Sopenharmony_ci#undef LOG_TAG
34c29fa5a6Sopenharmony_ci#define LOG_TAG "StateMachine"
35c29fa5a6Sopenharmony_ci
36c29fa5a6Sopenharmony_cinamespace OHOS {
37c29fa5a6Sopenharmony_cinamespace Msdp {
38c29fa5a6Sopenharmony_cinamespace DeviceStatus {
39c29fa5a6Sopenharmony_cinamespace Cooperate {
40c29fa5a6Sopenharmony_ci
41c29fa5a6Sopenharmony_ciStateMachine::AppStateObserver::AppStateObserver(Channel<CooperateEvent>::Sender sender, int32_t clientPid)
42c29fa5a6Sopenharmony_ci    : sender_(sender), clientPid_(clientPid) {}
43c29fa5a6Sopenharmony_ci
44c29fa5a6Sopenharmony_civoid StateMachine::AppStateObserver::OnProcessDied(const AppExecFwk::ProcessData &processData)
45c29fa5a6Sopenharmony_ci{
46c29fa5a6Sopenharmony_ci    FI_HILOGI("\'%{public}s\' died, pid:%{public}d", processData.bundleName.c_str(), processData.pid);
47c29fa5a6Sopenharmony_ci    if (processData.pid == clientPid_) {
48c29fa5a6Sopenharmony_ci        auto ret = sender_.Send(CooperateEvent(
49c29fa5a6Sopenharmony_ci            CooperateEventType::APP_CLOSED,
50c29fa5a6Sopenharmony_ci            ClientDiedEvent {
51c29fa5a6Sopenharmony_ci                .pid = clientPid_,
52c29fa5a6Sopenharmony_ci            }));
53c29fa5a6Sopenharmony_ci        if (ret != Channel<CooperateEvent>::NO_ERROR) {
54c29fa5a6Sopenharmony_ci            FI_HILOGE("Failed to send event via channel, error:%{public}d", ret);
55c29fa5a6Sopenharmony_ci        }
56c29fa5a6Sopenharmony_ci        FI_HILOGI("Report to handler");
57c29fa5a6Sopenharmony_ci    }
58c29fa5a6Sopenharmony_ci}
59c29fa5a6Sopenharmony_ci
60c29fa5a6Sopenharmony_civoid StateMachine::AppStateObserver::UpdateClientPid(int32_t clientPid)
61c29fa5a6Sopenharmony_ci{
62c29fa5a6Sopenharmony_ci    clientPid_ = clientPid;
63c29fa5a6Sopenharmony_ci}
64c29fa5a6Sopenharmony_ci
65c29fa5a6Sopenharmony_ciStateMachine::StateMachine(IContext *env)
66c29fa5a6Sopenharmony_ci    : env_(env)
67c29fa5a6Sopenharmony_ci{
68c29fa5a6Sopenharmony_ci    states_[COOPERATE_STATE_FREE] = std::make_shared<CooperateFree>(*this, env);
69c29fa5a6Sopenharmony_ci    states_[COOPERATE_STATE_OUT] = std::make_shared<CooperateOut>(*this, env);
70c29fa5a6Sopenharmony_ci    states_[COOPERATE_STATE_IN] = std::make_shared<CooperateIn>(*this, env);
71c29fa5a6Sopenharmony_ci
72c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::ADD_OBSERVER, [this](Context &context, const CooperateEvent &event) {
73c29fa5a6Sopenharmony_ci        this->AddObserver(context, event);
74c29fa5a6Sopenharmony_ci    });
75c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::REMOVE_OBSERVER, [this](Context &context, const CooperateEvent &event) {
76c29fa5a6Sopenharmony_ci        this->RemoveObserver(context, event);
77c29fa5a6Sopenharmony_ci    });
78c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::REGISTER_LISTENER, [this](Context &context, const CooperateEvent &event) {
79c29fa5a6Sopenharmony_ci        this->RegisterListener(context, event);
80c29fa5a6Sopenharmony_ci    });
81c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::UNREGISTER_LISTENER, [this](Context &context, const CooperateEvent &event) {
82c29fa5a6Sopenharmony_ci        this->UnregisterListener(context, event);
83c29fa5a6Sopenharmony_ci    });
84c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::REGISTER_HOTAREA_LISTENER, [this](Context &context, const CooperateEvent &event) {
85c29fa5a6Sopenharmony_ci        this->RegisterHotAreaListener(context, event);
86c29fa5a6Sopenharmony_ci    });
87c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::UNREGISTER_HOTAREA_LISTENER,
88c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
89c29fa5a6Sopenharmony_ci            this->UnregisterHotAreaListener(context, event);
90c29fa5a6Sopenharmony_ci    });
91c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::ENABLE, [this](Context &context, const CooperateEvent &event) {
92c29fa5a6Sopenharmony_ci        this->EnableCooperate(context, event);
93c29fa5a6Sopenharmony_ci    });
94c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::DISABLE, [this](Context &context, const CooperateEvent &event) {
95c29fa5a6Sopenharmony_ci        this->DisableCooperate(context, event);
96c29fa5a6Sopenharmony_ci    });
97c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::START, [this](Context &context, const CooperateEvent &event) {
98c29fa5a6Sopenharmony_ci        this->StartCooperate(context, event);
99c29fa5a6Sopenharmony_ci    });
100c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::GET_COOPERATE_STATE, [this](Context &context, const CooperateEvent &event) {
101c29fa5a6Sopenharmony_ci        this->GetCooperateState(context, event);
102c29fa5a6Sopenharmony_ci    });
103c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::REGISTER_EVENT_LISTENER,
104c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
105c29fa5a6Sopenharmony_ci            this->RegisterEventListener(context, event);
106c29fa5a6Sopenharmony_ci    });
107c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::UNREGISTER_EVENT_LISTENER,
108c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
109c29fa5a6Sopenharmony_ci            this->UnregisterEventListener(context, event);
110c29fa5a6Sopenharmony_ci    });
111c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::DDM_BOARD_ONLINE,
112c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
113c29fa5a6Sopenharmony_ci            this->OnBoardOnline(context, event);
114c29fa5a6Sopenharmony_ci    });
115c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::DDM_BOARD_OFFLINE,
116c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
117c29fa5a6Sopenharmony_ci            this->OnBoardOffline(context, event);
118c29fa5a6Sopenharmony_ci    });
119c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::DDP_COOPERATE_SWITCH_CHANGED,
120c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
121c29fa5a6Sopenharmony_ci            this->OnProfileChanged(context, event);
122c29fa5a6Sopenharmony_ci    });
123c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::INPUT_POINTER_EVENT,
124c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
125c29fa5a6Sopenharmony_ci            this->OnPointerEvent(context, event);
126c29fa5a6Sopenharmony_ci    });
127c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::APP_CLOSED, [this](Context &context, const CooperateEvent &event) {
128c29fa5a6Sopenharmony_ci        this->OnProcessClientDied(context, event);
129c29fa5a6Sopenharmony_ci    });
130c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::DSOFTBUS_SESSION_OPENED,
131c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
132c29fa5a6Sopenharmony_ci            this->OnSoftbusSessionOpened(context, event);
133c29fa5a6Sopenharmony_ci    });
134c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::DSOFTBUS_SESSION_CLOSED,
135c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
136c29fa5a6Sopenharmony_ci            this->OnSoftbusSessionClosed(context, event);
137c29fa5a6Sopenharmony_ci    });
138c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::DSOFTBUS_SUBSCRIBE_MOUSE_LOCATION,
139c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
140c29fa5a6Sopenharmony_ci            this->OnSoftbusSubscribeMouseLocation(context, event);
141c29fa5a6Sopenharmony_ci    });
142c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::DSOFTBUS_UNSUBSCRIBE_MOUSE_LOCATION,
143c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
144c29fa5a6Sopenharmony_ci            this->OnSoftbusUnSubscribeMouseLocation(context, event);
145c29fa5a6Sopenharmony_ci    });
146c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::DSOFTBUS_REPLY_SUBSCRIBE_MOUSE_LOCATION,
147c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
148c29fa5a6Sopenharmony_ci            this->OnSoftbusReplySubscribeMouseLocation(context, event);
149c29fa5a6Sopenharmony_ci    });
150c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::DSOFTBUS_REPLY_UNSUBSCRIBE_MOUSE_LOCATION,
151c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
152c29fa5a6Sopenharmony_ci            this->OnSoftbusReplyUnSubscribeMouseLocation(context, event);
153c29fa5a6Sopenharmony_ci    });
154c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::DSOFTBUS_MOUSE_LOCATION,
155c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
156c29fa5a6Sopenharmony_ci            this->OnSoftbusMouseLocation(context, event);
157c29fa5a6Sopenharmony_ci    });
158c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::DSOFTBUS_START_COOPERATE,
159c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
160c29fa5a6Sopenharmony_ci            this->OnRemoteStart(context, event);
161c29fa5a6Sopenharmony_ci    });
162c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::INPUT_HOTPLUG_EVENT,
163c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
164c29fa5a6Sopenharmony_ci            this->OnHotPlugEvent(context, event);
165c29fa5a6Sopenharmony_ci    });
166c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::DSOFTBUS_INPUT_DEV_HOT_PLUG,
167c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
168c29fa5a6Sopenharmony_ci            this->OnRemoteHotPlug(context, event);
169c29fa5a6Sopenharmony_ci    });
170c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::DSOFTBUS_INPUT_DEV_SYNC,
171c29fa5a6Sopenharmony_ci        [this](Context &context, const CooperateEvent &event) {
172c29fa5a6Sopenharmony_ci            this->OnRemoteInputDevice(context, event);
173c29fa5a6Sopenharmony_ci    });
174c29fa5a6Sopenharmony_ci    AddHandler(CooperateEventType::STOP, [this](Context &context, const CooperateEvent &event) {
175c29fa5a6Sopenharmony_ci        this->StopCooperate(context, event);
176c29fa5a6Sopenharmony_ci    });
177c29fa5a6Sopenharmony_ci}
178c29fa5a6Sopenharmony_ci
179c29fa5a6Sopenharmony_civoid StateMachine::OnEvent(Context &context, const CooperateEvent &event)
180c29fa5a6Sopenharmony_ci{
181c29fa5a6Sopenharmony_ci    if (auto iter = handlers_.find(event.type); iter != handlers_.end()) {
182c29fa5a6Sopenharmony_ci        iter->second(context, event);
183c29fa5a6Sopenharmony_ci    } else {
184c29fa5a6Sopenharmony_ci        Transfer(context, event);
185c29fa5a6Sopenharmony_ci    }
186c29fa5a6Sopenharmony_ci}
187c29fa5a6Sopenharmony_ci
188c29fa5a6Sopenharmony_civoid StateMachine::TransiteTo(Context &context, CooperateState state)
189c29fa5a6Sopenharmony_ci{
190c29fa5a6Sopenharmony_ci    if ((state >= COOPERATE_STATE_FREE) &&
191c29fa5a6Sopenharmony_ci        (state < N_COOPERATE_STATES) &&
192c29fa5a6Sopenharmony_ci        (state != current_)) {
193c29fa5a6Sopenharmony_ci        states_[current_]->OnLeaveState(context);
194c29fa5a6Sopenharmony_ci        current_ = state;
195c29fa5a6Sopenharmony_ci        states_[current_]->OnEnterState(context);
196c29fa5a6Sopenharmony_ci        auto curState = static_cast<OHOS::Msdp::DeviceStatus::CooperateState>(state);
197c29fa5a6Sopenharmony_ci        CooperateDFX::WriteCooperateState(curState);
198c29fa5a6Sopenharmony_ci    }
199c29fa5a6Sopenharmony_ci}
200c29fa5a6Sopenharmony_ci
201c29fa5a6Sopenharmony_civoid StateMachine::AddHandler(CooperateEventType event, std::function<void(Context&, const CooperateEvent&)> handler)
202c29fa5a6Sopenharmony_ci{
203c29fa5a6Sopenharmony_ci    handlers_.emplace(event, handler);
204c29fa5a6Sopenharmony_ci}
205c29fa5a6Sopenharmony_ci
206c29fa5a6Sopenharmony_civoid StateMachine::OnQuit(Context &context)
207c29fa5a6Sopenharmony_ci{
208c29fa5a6Sopenharmony_ci    CALL_DEBUG_ENTER;
209c29fa5a6Sopenharmony_ci    RemoveWatches(context);
210c29fa5a6Sopenharmony_ci    RemoveMonitor(context);
211c29fa5a6Sopenharmony_ci}
212c29fa5a6Sopenharmony_ci
213c29fa5a6Sopenharmony_civoid StateMachine::AddObserver(Context &context, const CooperateEvent &event)
214c29fa5a6Sopenharmony_ci{
215c29fa5a6Sopenharmony_ci    AddObserverEvent notice = std::get<AddObserverEvent>(event.event);
216c29fa5a6Sopenharmony_ci    context.AddObserver(notice.observer);
217c29fa5a6Sopenharmony_ci}
218c29fa5a6Sopenharmony_ci
219c29fa5a6Sopenharmony_civoid StateMachine::RemoveObserver(Context &context, const CooperateEvent &event)
220c29fa5a6Sopenharmony_ci{
221c29fa5a6Sopenharmony_ci    RemoveObserverEvent notice = std::get<RemoveObserverEvent>(event.event);
222c29fa5a6Sopenharmony_ci    context.RemoveObserver(notice.observer);
223c29fa5a6Sopenharmony_ci}
224c29fa5a6Sopenharmony_ci
225c29fa5a6Sopenharmony_civoid StateMachine::RegisterListener(Context &context, const CooperateEvent &event)
226c29fa5a6Sopenharmony_ci{
227c29fa5a6Sopenharmony_ci    RegisterListenerEvent notice = std::get<RegisterListenerEvent>(event.event);
228c29fa5a6Sopenharmony_ci    context.eventMgr_.RegisterListener(notice);
229c29fa5a6Sopenharmony_ci}
230c29fa5a6Sopenharmony_ci
231c29fa5a6Sopenharmony_civoid StateMachine::UnregisterListener(Context &context, const CooperateEvent &event)
232c29fa5a6Sopenharmony_ci{
233c29fa5a6Sopenharmony_ci    UnregisterListenerEvent notice = std::get<UnregisterListenerEvent>(event.event);
234c29fa5a6Sopenharmony_ci    context.eventMgr_.UnregisterListener(notice);
235c29fa5a6Sopenharmony_ci}
236c29fa5a6Sopenharmony_ci
237c29fa5a6Sopenharmony_civoid StateMachine::RegisterHotAreaListener(Context &context, const CooperateEvent &event)
238c29fa5a6Sopenharmony_ci{
239c29fa5a6Sopenharmony_ci    RegisterHotareaListenerEvent notice = std::get<RegisterHotareaListenerEvent>(event.event);
240c29fa5a6Sopenharmony_ci    context.hotArea_.AddListener(notice);
241c29fa5a6Sopenharmony_ci}
242c29fa5a6Sopenharmony_ci
243c29fa5a6Sopenharmony_civoid StateMachine::UnregisterHotAreaListener(Context &context, const CooperateEvent &event)
244c29fa5a6Sopenharmony_ci{
245c29fa5a6Sopenharmony_ci    UnregisterHotareaListenerEvent notice = std::get<UnregisterHotareaListenerEvent>(event.event);
246c29fa5a6Sopenharmony_ci    context.hotArea_.RemoveListener(notice);
247c29fa5a6Sopenharmony_ci}
248c29fa5a6Sopenharmony_ci
249c29fa5a6Sopenharmony_civoid StateMachine::EnableCooperate(Context &context, const CooperateEvent &event)
250c29fa5a6Sopenharmony_ci{
251c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
252c29fa5a6Sopenharmony_ci    EnableCooperateEvent enableEvent = std::get<EnableCooperateEvent>(event.event);
253c29fa5a6Sopenharmony_ci    context.EnableCooperate(enableEvent);
254c29fa5a6Sopenharmony_ci    context.eventMgr_.EnableCooperate(enableEvent);
255c29fa5a6Sopenharmony_ci    context.hotArea_.EnableCooperate(enableEvent);
256c29fa5a6Sopenharmony_ci    observer_ = CommonEventObserver::CreateCommonEventObserver(
257c29fa5a6Sopenharmony_ci        [&context, this] (const std::string &commonEvent) {
258c29fa5a6Sopenharmony_ci            OnCommonEvent(context, commonEvent);
259c29fa5a6Sopenharmony_ci        }
260c29fa5a6Sopenharmony_ci    );
261c29fa5a6Sopenharmony_ci    context.commonEvent_.AddObserver(observer_);
262c29fa5a6Sopenharmony_ci    AddSessionObserver(context, enableEvent);
263c29fa5a6Sopenharmony_ci    AddMonitor(context);
264c29fa5a6Sopenharmony_ci    Transfer(context, event);
265c29fa5a6Sopenharmony_ci}
266c29fa5a6Sopenharmony_ci
267c29fa5a6Sopenharmony_civoid StateMachine::DisableCooperate(Context &context, const CooperateEvent &event)
268c29fa5a6Sopenharmony_ci{
269c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
270c29fa5a6Sopenharmony_ci    DisableCooperateEvent disableEvent = std::get<DisableCooperateEvent>(event.event);
271c29fa5a6Sopenharmony_ci    context.DisableCooperate(disableEvent);
272c29fa5a6Sopenharmony_ci    context.eventMgr_.DisableCooperate(disableEvent);
273c29fa5a6Sopenharmony_ci    context.commonEvent_.RemoveObserver(observer_);
274c29fa5a6Sopenharmony_ci    RemoveSessionObserver(context, disableEvent);
275c29fa5a6Sopenharmony_ci    RemoveMonitor(context);
276c29fa5a6Sopenharmony_ci    Transfer(context, event);
277c29fa5a6Sopenharmony_ci}
278c29fa5a6Sopenharmony_ci
279c29fa5a6Sopenharmony_civoid StateMachine::StartCooperate(Context &context, const CooperateEvent &event)
280c29fa5a6Sopenharmony_ci{
281c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
282c29fa5a6Sopenharmony_ci    StartCooperateEvent startEvent = std::get<StartCooperateEvent>(event.event);
283c29fa5a6Sopenharmony_ci    if (!context.ddm_.CheckSameAccountToLocal(startEvent.remoteNetworkId)) {
284c29fa5a6Sopenharmony_ci        FI_HILOGE("CheckSameAccountToLocal failed");
285c29fa5a6Sopenharmony_ci        startEvent.errCode->set_value(COMMON_PERMISSION_CHECK_ERROR);
286c29fa5a6Sopenharmony_ci        return;
287c29fa5a6Sopenharmony_ci    }
288c29fa5a6Sopenharmony_ci    UpdateApplicationStateObserver(startEvent.pid);
289c29fa5a6Sopenharmony_ci    if (!context.IsAllowCooperate()) {
290c29fa5a6Sopenharmony_ci        FI_HILOGI("Not allow cooperate");
291c29fa5a6Sopenharmony_ci        startEvent.errCode->set_value(COMMON_NOT_ALLOWED_DISTRIBUTED);
292c29fa5a6Sopenharmony_ci        return;
293c29fa5a6Sopenharmony_ci    }
294c29fa5a6Sopenharmony_ci    startEvent.errCode->set_value(RET_OK);
295c29fa5a6Sopenharmony_ci    Transfer(context, event);
296c29fa5a6Sopenharmony_ci}
297c29fa5a6Sopenharmony_ci
298c29fa5a6Sopenharmony_civoid StateMachine::StopCooperate(Context &context, const CooperateEvent &event)
299c29fa5a6Sopenharmony_ci{
300c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
301c29fa5a6Sopenharmony_ci    context.CloseDistributedFileConnection(context.Peer());
302c29fa5a6Sopenharmony_ci    Transfer(context, event);
303c29fa5a6Sopenharmony_ci}
304c29fa5a6Sopenharmony_ci
305c29fa5a6Sopenharmony_civoid StateMachine::GetCooperateState(Context &context, const CooperateEvent &event)
306c29fa5a6Sopenharmony_ci{
307c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
308c29fa5a6Sopenharmony_ci    GetCooperateStateEvent stateEvent = std::get<GetCooperateStateEvent>(event.event);
309c29fa5a6Sopenharmony_ci    UpdateApplicationStateObserver(stateEvent.pid);
310c29fa5a6Sopenharmony_ci    bool switchStatus { false };
311c29fa5a6Sopenharmony_ci    EventManager::CooperateStateNotice notice {
312c29fa5a6Sopenharmony_ci        .pid = stateEvent.pid,
313c29fa5a6Sopenharmony_ci        .msgId = MessageId::COORDINATION_GET_STATE,
314c29fa5a6Sopenharmony_ci        .userData = stateEvent.userData,
315c29fa5a6Sopenharmony_ci        .state = switchStatus,
316c29fa5a6Sopenharmony_ci    };
317c29fa5a6Sopenharmony_ci    context.eventMgr_.GetCooperateState(notice);
318c29fa5a6Sopenharmony_ci}
319c29fa5a6Sopenharmony_ci
320c29fa5a6Sopenharmony_civoid StateMachine::OnProcessClientDied(Context &context, const CooperateEvent &event)
321c29fa5a6Sopenharmony_ci{
322c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
323c29fa5a6Sopenharmony_ci    ClientDiedEvent notice = std::get<ClientDiedEvent>(event.event);
324c29fa5a6Sopenharmony_ci    context.eventMgr_.OnClientDied(notice);
325c29fa5a6Sopenharmony_ci    context.hotArea_.OnClientDied(notice);
326c29fa5a6Sopenharmony_ci    context.mouseLocation_.OnClientDied(notice);
327c29fa5a6Sopenharmony_ci    Transfer(context, event);
328c29fa5a6Sopenharmony_ci}
329c29fa5a6Sopenharmony_ci
330c29fa5a6Sopenharmony_civoid StateMachine::RegisterEventListener(Context &context, const CooperateEvent &event)
331c29fa5a6Sopenharmony_ci{
332c29fa5a6Sopenharmony_ci    RegisterEventListenerEvent notice = std::get<RegisterEventListenerEvent>(event.event);
333c29fa5a6Sopenharmony_ci    context.mouseLocation_.AddListener(notice);
334c29fa5a6Sopenharmony_ci}
335c29fa5a6Sopenharmony_ci
336c29fa5a6Sopenharmony_civoid StateMachine::UnregisterEventListener(Context &context, const CooperateEvent &event)
337c29fa5a6Sopenharmony_ci{
338c29fa5a6Sopenharmony_ci    UnregisterEventListenerEvent notice = std::get<UnregisterEventListenerEvent>(event.event);
339c29fa5a6Sopenharmony_ci    context.mouseLocation_.RemoveListener(notice);
340c29fa5a6Sopenharmony_ci}
341c29fa5a6Sopenharmony_ci
342c29fa5a6Sopenharmony_civoid StateMachine::OnBoardOnline(Context &context, const CooperateEvent &event)
343c29fa5a6Sopenharmony_ci{
344c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
345c29fa5a6Sopenharmony_ci    DDMBoardOnlineEvent onlineEvent = std::get<DDMBoardOnlineEvent>(event.event);
346c29fa5a6Sopenharmony_ci
347c29fa5a6Sopenharmony_ci    auto ret = onlineBoards_.insert(onlineEvent.networkId);
348c29fa5a6Sopenharmony_ci    if (ret.second) {
349c29fa5a6Sopenharmony_ci        FI_HILOGD("Watch \'%{public}s\'", Utility::Anonymize(onlineEvent.networkId).c_str());
350c29fa5a6Sopenharmony_ci        Transfer(context, event);
351c29fa5a6Sopenharmony_ci    }
352c29fa5a6Sopenharmony_ci}
353c29fa5a6Sopenharmony_ci
354c29fa5a6Sopenharmony_civoid StateMachine::OnBoardOffline(Context &context, const CooperateEvent &event)
355c29fa5a6Sopenharmony_ci{
356c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
357c29fa5a6Sopenharmony_ci    DDMBoardOfflineEvent offlineEvent = std::get<DDMBoardOfflineEvent>(event.event);
358c29fa5a6Sopenharmony_ci
359c29fa5a6Sopenharmony_ci    if (auto iter = onlineBoards_.find(offlineEvent.networkId); iter != onlineBoards_.end()) {
360c29fa5a6Sopenharmony_ci        onlineBoards_.erase(iter);
361c29fa5a6Sopenharmony_ci        FI_HILOGD("Remove watch \'%{public}s\'", Utility::Anonymize(offlineEvent.networkId).c_str());
362c29fa5a6Sopenharmony_ci        context.CloseDistributedFileConnection(offlineEvent.networkId);
363c29fa5a6Sopenharmony_ci        Transfer(context, event);
364c29fa5a6Sopenharmony_ci    }
365c29fa5a6Sopenharmony_ci}
366c29fa5a6Sopenharmony_ci
367c29fa5a6Sopenharmony_civoid StateMachine::OnProfileChanged(Context &context, const CooperateEvent &event)
368c29fa5a6Sopenharmony_ci{
369c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
370c29fa5a6Sopenharmony_ci    DDPCooperateSwitchChanged notice = std::get<DDPCooperateSwitchChanged>(event.event);
371c29fa5a6Sopenharmony_ci    context.eventMgr_.OnProfileChanged(notice);
372c29fa5a6Sopenharmony_ci    Transfer(context, event);
373c29fa5a6Sopenharmony_ci}
374c29fa5a6Sopenharmony_ci
375c29fa5a6Sopenharmony_civoid StateMachine::OnPointerEvent(Context &context, const CooperateEvent &event)
376c29fa5a6Sopenharmony_ci{
377c29fa5a6Sopenharmony_ci    CALL_DEBUG_ENTER;
378c29fa5a6Sopenharmony_ci    InputPointerEvent pointerEvent = std::get<InputPointerEvent>(event.event);
379c29fa5a6Sopenharmony_ci    Coordinate cursorPos = context.CursorPosition();
380c29fa5a6Sopenharmony_ci    context.OnPointerEvent(pointerEvent);
381c29fa5a6Sopenharmony_ci    pointerEvent.position = cursorPos;
382c29fa5a6Sopenharmony_ci    Transfer(context, CooperateEvent { CooperateEventType::INPUT_POINTER_EVENT, pointerEvent });
383c29fa5a6Sopenharmony_ci}
384c29fa5a6Sopenharmony_ci
385c29fa5a6Sopenharmony_civoid StateMachine::OnSoftbusSessionClosed(Context &context, const CooperateEvent &event)
386c29fa5a6Sopenharmony_ci{
387c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
388c29fa5a6Sopenharmony_ci    DSoftbusSessionClosed notice = std::get<DSoftbusSessionClosed>(event.event);
389c29fa5a6Sopenharmony_ci    context.eventMgr_.OnSoftbusSessionClosed(notice);
390c29fa5a6Sopenharmony_ci    context.inputDevMgr_.OnSoftbusSessionClosed(notice);
391c29fa5a6Sopenharmony_ci    context.CloseDistributedFileConnection(notice.networkId);
392c29fa5a6Sopenharmony_ci    Transfer(context, event);
393c29fa5a6Sopenharmony_ci}
394c29fa5a6Sopenharmony_ci
395c29fa5a6Sopenharmony_civoid StateMachine::OnSoftbusSessionOpened(Context &context, const CooperateEvent &event)
396c29fa5a6Sopenharmony_ci{
397c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
398c29fa5a6Sopenharmony_ci    DSoftbusSessionOpened notice = std::get<DSoftbusSessionOpened>(event.event);
399c29fa5a6Sopenharmony_ci    context.inputDevMgr_.OnSoftbusSessionOpened(notice);
400c29fa5a6Sopenharmony_ci    Transfer(context, event);
401c29fa5a6Sopenharmony_ci}
402c29fa5a6Sopenharmony_ci
403c29fa5a6Sopenharmony_civoid StateMachine::OnHotPlugEvent(Context &context, const CooperateEvent &event)
404c29fa5a6Sopenharmony_ci{
405c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
406c29fa5a6Sopenharmony_ci    InputHotplugEvent notice = std::get<InputHotplugEvent>(event.event);
407c29fa5a6Sopenharmony_ci    context.inputDevMgr_.OnLocalHotPlug(notice);
408c29fa5a6Sopenharmony_ci    Transfer(context, event);
409c29fa5a6Sopenharmony_ci}
410c29fa5a6Sopenharmony_ci
411c29fa5a6Sopenharmony_civoid StateMachine::OnRemoteInputDevice(Context &context, const CooperateEvent &event)
412c29fa5a6Sopenharmony_ci{
413c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
414c29fa5a6Sopenharmony_ci    DSoftbusSyncInputDevice notice = std::get<DSoftbusSyncInputDevice>(event.event);
415c29fa5a6Sopenharmony_ci    context.inputDevMgr_.OnRemoteInputDevice(notice);
416c29fa5a6Sopenharmony_ci    Transfer(context, event);
417c29fa5a6Sopenharmony_ci}
418c29fa5a6Sopenharmony_ci
419c29fa5a6Sopenharmony_civoid StateMachine::OnRemoteHotPlug(Context &context, const CooperateEvent &event)
420c29fa5a6Sopenharmony_ci{
421c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
422c29fa5a6Sopenharmony_ci    DSoftbusHotPlugEvent notice = std::get<DSoftbusHotPlugEvent>(event.event);
423c29fa5a6Sopenharmony_ci    context.inputDevMgr_.OnRemoteHotPlug(notice);
424c29fa5a6Sopenharmony_ci    Transfer(context, event);
425c29fa5a6Sopenharmony_ci}
426c29fa5a6Sopenharmony_ci
427c29fa5a6Sopenharmony_civoid StateMachine::OnSoftbusSubscribeMouseLocation(Context &context, const CooperateEvent &event)
428c29fa5a6Sopenharmony_ci{
429c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
430c29fa5a6Sopenharmony_ci    DSoftbusSubscribeMouseLocation notice = std::get<DSoftbusSubscribeMouseLocation>(event.event);
431c29fa5a6Sopenharmony_ci    context.mouseLocation_.OnSubscribeMouseLocation(notice);
432c29fa5a6Sopenharmony_ci}
433c29fa5a6Sopenharmony_ci
434c29fa5a6Sopenharmony_civoid StateMachine::OnSoftbusUnSubscribeMouseLocation(Context &context, const CooperateEvent &event)
435c29fa5a6Sopenharmony_ci{
436c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
437c29fa5a6Sopenharmony_ci    DSoftbusUnSubscribeMouseLocation notice = std::get<DSoftbusUnSubscribeMouseLocation>(event.event);
438c29fa5a6Sopenharmony_ci    context.mouseLocation_.OnUnSubscribeMouseLocation(notice);
439c29fa5a6Sopenharmony_ci}
440c29fa5a6Sopenharmony_ci
441c29fa5a6Sopenharmony_civoid StateMachine::OnSoftbusReplySubscribeMouseLocation(Context &context, const CooperateEvent &event)
442c29fa5a6Sopenharmony_ci{
443c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
444c29fa5a6Sopenharmony_ci    DSoftbusReplySubscribeMouseLocation notice = std::get<DSoftbusReplySubscribeMouseLocation>(event.event);
445c29fa5a6Sopenharmony_ci    context.mouseLocation_.OnReplySubscribeMouseLocation(notice);
446c29fa5a6Sopenharmony_ci}
447c29fa5a6Sopenharmony_ci
448c29fa5a6Sopenharmony_civoid StateMachine::OnSoftbusReplyUnSubscribeMouseLocation(Context &context, const CooperateEvent &event)
449c29fa5a6Sopenharmony_ci{
450c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
451c29fa5a6Sopenharmony_ci    DSoftbusReplyUnSubscribeMouseLocation notice = std::get<DSoftbusReplyUnSubscribeMouseLocation>(event.event);
452c29fa5a6Sopenharmony_ci    context.mouseLocation_.OnReplyUnSubscribeMouseLocation(notice);
453c29fa5a6Sopenharmony_ci}
454c29fa5a6Sopenharmony_ci
455c29fa5a6Sopenharmony_civoid StateMachine::OnSoftbusMouseLocation(Context &context, const CooperateEvent &event)
456c29fa5a6Sopenharmony_ci{
457c29fa5a6Sopenharmony_ci    CALL_DEBUG_ENTER;
458c29fa5a6Sopenharmony_ci    DSoftbusSyncMouseLocation notice = std::get<DSoftbusSyncMouseLocation>(event.event);
459c29fa5a6Sopenharmony_ci    context.mouseLocation_.OnRemoteMouseLocation(notice);
460c29fa5a6Sopenharmony_ci}
461c29fa5a6Sopenharmony_ci
462c29fa5a6Sopenharmony_civoid StateMachine::OnRemoteStart(Context &context, const CooperateEvent &event)
463c29fa5a6Sopenharmony_ci{
464c29fa5a6Sopenharmony_ci    DSoftbusStartCooperate startEvent = std::get<DSoftbusStartCooperate>(event.event);
465c29fa5a6Sopenharmony_ci    if (!context.ddm_.CheckSameAccountToLocal(startEvent.originNetworkId)) {
466c29fa5a6Sopenharmony_ci        FI_HILOGE("CheckSameAccountToLocal failed, unchain link");
467c29fa5a6Sopenharmony_ci        CooperateEvent stopEvent(
468c29fa5a6Sopenharmony_ci            CooperateEventType::STOP,
469c29fa5a6Sopenharmony_ci            StopCooperateEvent{
470c29fa5a6Sopenharmony_ci                .isUnchained = true
471c29fa5a6Sopenharmony_ci            }
472c29fa5a6Sopenharmony_ci        );
473c29fa5a6Sopenharmony_ci        Transfer(context, stopEvent);
474c29fa5a6Sopenharmony_ci        return;
475c29fa5a6Sopenharmony_ci    }
476c29fa5a6Sopenharmony_ci    Transfer(context, event);
477c29fa5a6Sopenharmony_ci}
478c29fa5a6Sopenharmony_ci
479c29fa5a6Sopenharmony_civoid StateMachine::Transfer(Context &context, const CooperateEvent &event)
480c29fa5a6Sopenharmony_ci{
481c29fa5a6Sopenharmony_ci    states_[current_]->OnEvent(context, event);
482c29fa5a6Sopenharmony_ci}
483c29fa5a6Sopenharmony_ci
484c29fa5a6Sopenharmony_cisptr<AppExecFwk::IAppMgr> StateMachine::GetAppMgr()
485c29fa5a6Sopenharmony_ci{
486c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
487c29fa5a6Sopenharmony_ci    auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
488c29fa5a6Sopenharmony_ci    CHKPP(saMgr);
489c29fa5a6Sopenharmony_ci    auto appMgrObj = saMgr->GetSystemAbility(APP_MGR_SERVICE_ID);
490c29fa5a6Sopenharmony_ci    CHKPP(appMgrObj);
491c29fa5a6Sopenharmony_ci    return iface_cast<AppExecFwk::IAppMgr>(appMgrObj);
492c29fa5a6Sopenharmony_ci}
493c29fa5a6Sopenharmony_ci
494c29fa5a6Sopenharmony_ciint32_t StateMachine::RegisterApplicationStateObserver(Channel<CooperateEvent>::Sender sender,
495c29fa5a6Sopenharmony_ci    const EnableCooperateEvent &event)
496c29fa5a6Sopenharmony_ci{
497c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
498c29fa5a6Sopenharmony_ci    auto bundleName = GetPackageName(event.tokenId);
499c29fa5a6Sopenharmony_ci    clientBundleNames_.push_back(bundleName);
500c29fa5a6Sopenharmony_ci    FI_HILOGI("Register application %{public}s state observer", bundleName.c_str());
501c29fa5a6Sopenharmony_ci    auto appMgr = GetAppMgr();
502c29fa5a6Sopenharmony_ci    CHKPR(appMgr, RET_ERR);
503c29fa5a6Sopenharmony_ci    appStateObserver_ = sptr<AppStateObserver>::MakeSptr(sender, event.pid);
504c29fa5a6Sopenharmony_ci    auto err = appMgr->RegisterApplicationStateObserver(appStateObserver_, clientBundleNames_);
505c29fa5a6Sopenharmony_ci    if (err != RET_OK) {
506c29fa5a6Sopenharmony_ci        appStateObserver_.clear();
507c29fa5a6Sopenharmony_ci        FI_HILOGE("IAppMgr::RegisterApplicationStateObserver fail, error:%{public}d", err);
508c29fa5a6Sopenharmony_ci        return RET_ERR;
509c29fa5a6Sopenharmony_ci    }
510c29fa5a6Sopenharmony_ci    return RET_OK;
511c29fa5a6Sopenharmony_ci}
512c29fa5a6Sopenharmony_ci
513c29fa5a6Sopenharmony_civoid StateMachine::UnregisterApplicationStateObserver()
514c29fa5a6Sopenharmony_ci{
515c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
516c29fa5a6Sopenharmony_ci    CHKPV(appStateObserver_);
517c29fa5a6Sopenharmony_ci    auto appMgr = GetAppMgr();
518c29fa5a6Sopenharmony_ci    CHKPV(appMgr);
519c29fa5a6Sopenharmony_ci    FI_HILOGI("Unregister application associateassistant state observer");
520c29fa5a6Sopenharmony_ci    auto err = appMgr->UnregisterApplicationStateObserver(appStateObserver_);
521c29fa5a6Sopenharmony_ci    if (err != RET_OK) {
522c29fa5a6Sopenharmony_ci        FI_HILOGE("IAppMgr::UnregisterApplicationStateObserver fail, error:%{public}d", err);
523c29fa5a6Sopenharmony_ci    }
524c29fa5a6Sopenharmony_ci    appStateObserver_.clear();
525c29fa5a6Sopenharmony_ci}
526c29fa5a6Sopenharmony_ci
527c29fa5a6Sopenharmony_civoid StateMachine::UpdateApplicationStateObserver(int32_t clientPid)
528c29fa5a6Sopenharmony_ci{
529c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
530c29fa5a6Sopenharmony_ci    CHKPV(appStateObserver_);
531c29fa5a6Sopenharmony_ci    appStateObserver_->UpdateClientPid(clientPid);
532c29fa5a6Sopenharmony_ci}
533c29fa5a6Sopenharmony_ci
534c29fa5a6Sopenharmony_civoid StateMachine::AddSessionObserver(Context &context, const EnableCooperateEvent &event)
535c29fa5a6Sopenharmony_ci{
536c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
537c29fa5a6Sopenharmony_ci    RegisterApplicationStateObserver(context.Sender(), event);
538c29fa5a6Sopenharmony_ci}
539c29fa5a6Sopenharmony_ci
540c29fa5a6Sopenharmony_cistd::string StateMachine::GetPackageName(Security::AccessToken::AccessTokenID tokenId)
541c29fa5a6Sopenharmony_ci{
542c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
543c29fa5a6Sopenharmony_ci    std::string bundleName {"Default"};
544c29fa5a6Sopenharmony_ci    int32_t tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
545c29fa5a6Sopenharmony_ci    switch (tokenType) {
546c29fa5a6Sopenharmony_ci        case Security::AccessToken::ATokenTypeEnum::TOKEN_HAP: {
547c29fa5a6Sopenharmony_ci            Security::AccessToken::HapTokenInfo hapInfo;
548c29fa5a6Sopenharmony_ci            if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) != RET_OK) {
549c29fa5a6Sopenharmony_ci                FI_HILOGE("Get hap token info failed");
550c29fa5a6Sopenharmony_ci            } else {
551c29fa5a6Sopenharmony_ci                bundleName = hapInfo.bundleName;
552c29fa5a6Sopenharmony_ci            }
553c29fa5a6Sopenharmony_ci            break;
554c29fa5a6Sopenharmony_ci        }
555c29fa5a6Sopenharmony_ci        case Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE:
556c29fa5a6Sopenharmony_ci        case Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL: {
557c29fa5a6Sopenharmony_ci            Security::AccessToken::NativeTokenInfo tokenInfo;
558c29fa5a6Sopenharmony_ci            if (Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(tokenId, tokenInfo) != RET_OK) {
559c29fa5a6Sopenharmony_ci                FI_HILOGE("Get native token info failed");
560c29fa5a6Sopenharmony_ci            } else {
561c29fa5a6Sopenharmony_ci                bundleName = tokenInfo.processName;
562c29fa5a6Sopenharmony_ci            }
563c29fa5a6Sopenharmony_ci            break;
564c29fa5a6Sopenharmony_ci        }
565c29fa5a6Sopenharmony_ci        default: {
566c29fa5a6Sopenharmony_ci            FI_HILOGW("token type not match");
567c29fa5a6Sopenharmony_ci            break;
568c29fa5a6Sopenharmony_ci        }
569c29fa5a6Sopenharmony_ci    }
570c29fa5a6Sopenharmony_ci    return bundleName;
571c29fa5a6Sopenharmony_ci}
572c29fa5a6Sopenharmony_ci
573c29fa5a6Sopenharmony_civoid StateMachine::RemoveSessionObserver(Context &context, const DisableCooperateEvent &event)
574c29fa5a6Sopenharmony_ci{
575c29fa5a6Sopenharmony_ci    UnregisterApplicationStateObserver();
576c29fa5a6Sopenharmony_ci}
577c29fa5a6Sopenharmony_ci
578c29fa5a6Sopenharmony_civoid StateMachine::OnCommonEvent(Context &context, const std::string &commonEvent)
579c29fa5a6Sopenharmony_ci{
580c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
581c29fa5a6Sopenharmony_ci    FI_HILOGI("Current common event:%{public}s", commonEvent.c_str());
582c29fa5a6Sopenharmony_ci    if (commonEvent == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF ||
583c29fa5a6Sopenharmony_ci        commonEvent == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) {
584c29fa5a6Sopenharmony_ci        FI_HILOGI("Receive common event:%{public}s, stop cooperate", commonEvent.c_str());
585c29fa5a6Sopenharmony_ci        CooperateEvent stopEvent(
586c29fa5a6Sopenharmony_ci            CooperateEventType::STOP,
587c29fa5a6Sopenharmony_ci            StopCooperateEvent{
588c29fa5a6Sopenharmony_ci                .isUnchained = false
589c29fa5a6Sopenharmony_ci            }
590c29fa5a6Sopenharmony_ci        );
591c29fa5a6Sopenharmony_ci        Transfer(context, stopEvent);
592c29fa5a6Sopenharmony_ci    }
593c29fa5a6Sopenharmony_ci}
594c29fa5a6Sopenharmony_ci
595c29fa5a6Sopenharmony_civoid StateMachine::AddMonitor(Context &context)
596c29fa5a6Sopenharmony_ci{
597c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
598c29fa5a6Sopenharmony_ci    if (monitorId_ >= 0) {
599c29fa5a6Sopenharmony_ci        return;
600c29fa5a6Sopenharmony_ci    }
601c29fa5a6Sopenharmony_ci    CHKPV(env_);
602c29fa5a6Sopenharmony_ci    monitorId_ = env_->GetInput().AddMonitor(
603c29fa5a6Sopenharmony_ci        [sender = context.Sender(), &hotArea = context.hotArea_, &mouseLocation = context.mouseLocation_] (
604c29fa5a6Sopenharmony_ci            std::shared_ptr<MMI::PointerEvent> pointerEvent) mutable {
605c29fa5a6Sopenharmony_ci            hotArea.ProcessData(pointerEvent);
606c29fa5a6Sopenharmony_ci            mouseLocation.ProcessData(pointerEvent);
607c29fa5a6Sopenharmony_ci
608c29fa5a6Sopenharmony_ci            MMI::PointerEvent::PointerItem pointerItem;
609c29fa5a6Sopenharmony_ci            if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem)) {
610c29fa5a6Sopenharmony_ci                FI_HILOGE("Corrupted pointer event");
611c29fa5a6Sopenharmony_ci                return;
612c29fa5a6Sopenharmony_ci            }
613c29fa5a6Sopenharmony_ci            auto ret = sender.Send(CooperateEvent(
614c29fa5a6Sopenharmony_ci                CooperateEventType::INPUT_POINTER_EVENT,
615c29fa5a6Sopenharmony_ci                InputPointerEvent {
616c29fa5a6Sopenharmony_ci                    .deviceId = pointerEvent->GetDeviceId(),
617c29fa5a6Sopenharmony_ci                    .pointerAction = pointerEvent->GetPointerAction(),
618c29fa5a6Sopenharmony_ci                    .sourceType = pointerEvent->GetSourceType(),
619c29fa5a6Sopenharmony_ci                    .position = Coordinate {
620c29fa5a6Sopenharmony_ci                        .x = pointerItem.GetDisplayX(),
621c29fa5a6Sopenharmony_ci                        .y = pointerItem.GetDisplayY(),
622c29fa5a6Sopenharmony_ci                    }
623c29fa5a6Sopenharmony_ci                }));
624c29fa5a6Sopenharmony_ci            if (ret != Channel<CooperateEvent>::NO_ERROR) {
625c29fa5a6Sopenharmony_ci                FI_HILOGE("Failed to send event via channel, error:%{public}d", ret);
626c29fa5a6Sopenharmony_ci            }
627c29fa5a6Sopenharmony_ci        });
628c29fa5a6Sopenharmony_ci    if (monitorId_ < 0) {
629c29fa5a6Sopenharmony_ci        FI_HILOGE("MMI::Add Monitor fail");
630c29fa5a6Sopenharmony_ci    }
631c29fa5a6Sopenharmony_ci}
632c29fa5a6Sopenharmony_ci
633c29fa5a6Sopenharmony_civoid StateMachine::RemoveMonitor(Context &context)
634c29fa5a6Sopenharmony_ci{
635c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
636c29fa5a6Sopenharmony_ci    if (monitorId_ < 0) {
637c29fa5a6Sopenharmony_ci        return;
638c29fa5a6Sopenharmony_ci    }
639c29fa5a6Sopenharmony_ci    env_->GetInput().RemoveMonitor(monitorId_);
640c29fa5a6Sopenharmony_ci    monitorId_ = -1;
641c29fa5a6Sopenharmony_ci}
642c29fa5a6Sopenharmony_ci
643c29fa5a6Sopenharmony_civoid StateMachine::RemoveWatches(Context &context)
644c29fa5a6Sopenharmony_ci{
645c29fa5a6Sopenharmony_ci    CALL_INFO_TRACE;
646c29fa5a6Sopenharmony_ci    for (auto iter = onlineBoards_.begin();
647c29fa5a6Sopenharmony_ci         iter != onlineBoards_.end(); iter = onlineBoards_.begin()) {
648c29fa5a6Sopenharmony_ci        FI_HILOGD("Remove watch \'%{public}s\'", Utility::Anonymize(*iter).c_str());
649c29fa5a6Sopenharmony_ci        onlineBoards_.erase(iter);
650c29fa5a6Sopenharmony_ci    }
651c29fa5a6Sopenharmony_ci}
652c29fa5a6Sopenharmony_ci} // namespace Cooperate
653c29fa5a6Sopenharmony_ci} // namespace DeviceStatus
654c29fa5a6Sopenharmony_ci} // namespace Msdp
655c29fa5a6Sopenharmony_ci} // namespace OHOS
656