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 "input_device_cooperate_state_out.h"
17
18 #include "cooperation_message.h"
19 #include "device_cooperate_softbus_adapter.h"
20 #include "distributed_input_adapter.h"
21 #include "input_device_cooperate_sm.h"
22 #include "input_device_cooperate_util.h"
23 #include "input_device_manager.h"
24
25 namespace OHOS {
26 namespace MMI {
27 namespace {
28 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "InputDeviceCooperateStateOut"};
29 } // namespace
30
InputDeviceCooperateStateOut(const std::string & startDhid)31 InputDeviceCooperateStateOut::InputDeviceCooperateStateOut(const std::string& startDhid)
32 : startDhid_(startDhid)
33 {}
34
StopInputDeviceCooperate(const std::string & networkId)35 int32_t InputDeviceCooperateStateOut::StopInputDeviceCooperate(const std::string &networkId)
36 {
37 CALL_DEBUG_ENTER;
38 std::string srcNetworkId = networkId;
39 if (srcNetworkId.empty()) {
40 std::pair<std::string, std::string> prepared = InputDevCooSM->GetPreparedDevices();
41 srcNetworkId = prepared.first;
42 }
43 int32_t ret = DevCooperateSoftbusAdapter->StopRemoteCooperate(networkId);
44 if (ret != RET_OK) {
45 MMI_HILOGE("Stop input device cooperate fail");
46 return static_cast<int32_t>(CooperationMessage::COOPERATE_FAIL);
47 }
48 std::string taskName = "process_stop_task";
49 std::function<void()> handleProcessStopFunc =
50 std::bind(&InputDeviceCooperateStateOut::ProcessStop, this, srcNetworkId);
51 CHKPR(eventHandler_, RET_ERR);
52 eventHandler_->ProxyPostTask(handleProcessStopFunc, taskName, 0);
53 return RET_OK;
54 }
55
ProcessStop(const std::string & srcNetworkId)56 void InputDeviceCooperateStateOut::ProcessStop(const std::string& srcNetworkId)
57 {
58 CALL_DEBUG_ENTER;
59 std::string sink = GetLocalDeviceId();
60 std::vector<std::string> dhids = InputDevMgr->GetCooperateDhids(startDhid_);
61 if (dhids.empty()) {
62 InputDevCooSM->OnStopFinish(false, srcNetworkId);
63 }
64 int32_t ret = DistributedAdapter->StopRemoteInput(srcNetworkId, sink, dhids, [this, srcNetworkId](bool isSuccess) {
65 this->OnStopRemoteInput(isSuccess, srcNetworkId);
66 });
67 if (ret != RET_OK) {
68 InputDevCooSM->OnStopFinish(false, srcNetworkId);
69 }
70 }
71
OnStopRemoteInput(bool isSuccess,const std::string & srcNetworkId)72 void InputDeviceCooperateStateOut::OnStopRemoteInput(bool isSuccess, const std::string &srcNetworkId)
73 {
74 CALL_DEBUG_ENTER;
75 std::string taskName = "stop_finish_task";
76 std::function<void()> handleStopFinishFunc =
77 std::bind(&InputDeviceCooperateSM::OnStopFinish, InputDevCooSM, isSuccess, srcNetworkId);
78 CHKPV(eventHandler_);
79 eventHandler_->ProxyPostTask(handleStopFinishFunc, taskName, 0);
80 }
81
OnKeyboardOnline(const std::string & dhid)82 void InputDeviceCooperateStateOut::OnKeyboardOnline(const std::string &dhid)
83 {
84 std::pair<std::string, std::string> networkIds = InputDevCooSM->GetPreparedDevices();
85 std::vector<std::string> dhids;
86 dhids.push_back(dhid);
87 DistributedAdapter->StartRemoteInput(networkIds.first, networkIds.second, dhids, [](bool isSuccess) {});
88 }
89 } // namespace MMI
90 } // namespace OHOS
91