• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "multimodal_input_connect_remoter.h"
17 
18 #include <chrono>
19 #include <thread>
20 
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 
24 #include "mmi_log.h"
25 #include "multimodal_input_connect_death_recipient.h"
26 #include "multimodal_input_connect_define.h"
27 #include "util.h"
28 
29 namespace OHOS {
30 namespace MMI {
31 namespace {
32 std::shared_ptr<MultimodalInputConnectRemoter> g_instance = nullptr;
33 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "MultimodalInputConnectRemoter"};
34 } // namespace
35 
GetInstance()36 std::shared_ptr<MultimodalInputConnectRemoter> MultimodalInputConnectRemoter::GetInstance()
37 {
38     static std::once_flag flag;
39     std::call_once(flag, [&]() {
40         g_instance.reset(new (std::nothrow) MultimodalInputConnectRemoter());
41     });
42     return g_instance;
43 }
44 
StartRemoteCooperate(const std::string & localDeviceId,const std::string & remoteDeviceId)45 int32_t MultimodalInputConnectRemoter::StartRemoteCooperate(const std::string &localDeviceId,
46     const std::string &remoteDeviceId)
47 {
48     CALL_DEBUG_ENTER;
49     auto proxy = GetProxyById(remoteDeviceId);
50     CHKPR(proxy, RET_ERR);
51     return proxy->StartRemoteCooperate(localDeviceId);
52 }
53 
StartRemoteCooperateResult(const std::string & remoteDeviceId,bool isSuccess,const std::string & startDhid,int32_t xPercent,int32_t yPercent)54 int32_t MultimodalInputConnectRemoter::StartRemoteCooperateResult(const std::string &remoteDeviceId, bool isSuccess,
55     const std::string &startDhid, int32_t xPercent, int32_t yPercent)
56 {
57     CALL_DEBUG_ENTER;
58     auto proxy = GetProxyById(remoteDeviceId);
59     CHKPR(proxy, RET_ERR);
60     return proxy->StartRemoteCooperateResult(isSuccess, startDhid, xPercent, yPercent);
61 }
62 
StopRemoteCooperate(const std::string & remoteDeviceId)63 int32_t MultimodalInputConnectRemoter::StopRemoteCooperate(const std::string &remoteDeviceId)
64 {
65     CALL_DEBUG_ENTER;
66     auto proxy = GetProxyById(remoteDeviceId);
67     CHKPR(proxy, RET_ERR);
68     return proxy->StopRemoteCooperate();
69 }
70 
StopRemoteCooperateResult(const std::string & remoteDeviceId,bool isSuccess)71 int32_t MultimodalInputConnectRemoter::StopRemoteCooperateResult(const std::string &remoteDeviceId, bool isSuccess)
72 {
73     CALL_DEBUG_ENTER;
74     auto proxy = GetProxyById(remoteDeviceId);
75     CHKPR(proxy, RET_ERR);
76     return proxy->StopRemoteCooperateResult(isSuccess);
77 }
78 
StartCooperateOtherResult(const std::string & remoteDeviceId,const std::string & srcNetworkId)79 int32_t MultimodalInputConnectRemoter::StartCooperateOtherResult(const std::string &remoteDeviceId,
80     const std::string &srcNetworkId)
81 {
82     CALL_DEBUG_ENTER;
83     auto proxy = GetProxyById(remoteDeviceId);
84     CHKPR(proxy, RET_ERR);
85     return proxy->StartCooperateOtherResult(srcNetworkId);
86 }
87 
GetProxyById(const std::string & remoteDeviceId)88 sptr<IMultimodalInputConnect> MultimodalInputConnectRemoter::GetProxyById(const std::string &remoteDeviceId)
89 {
90     CALL_DEBUG_ENTER;
91     std::lock_guard<std::mutex> guard(lock_);
92     auto iterService = mmiRemoteServices_.find(remoteDeviceId);
93     if (iterService != mmiRemoteServices_.end()) {
94         return iterService->second;
95     }
96     auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
97     CHKPP(sm);
98     auto sa = sm->GetSystemAbility(IMultimodalInputConnect::MULTIMODAL_INPUT_CONNECT_SERVICE_ID, remoteDeviceId);
99     CHKPP(sa);
100     std::weak_ptr<MultimodalInputConnectRemoter> weakPtr = shared_from_this();
101     auto deathCallback = [remoteDeviceId, weakPtr](const wptr<IRemoteObject> &object) {
102         auto sharedPtr = weakPtr.lock();
103         if (sharedPtr != nullptr) {
104             sharedPtr->OnRemoteDeath(remoteDeviceId);
105         }
106     };
107     sptr<IRemoteObject::DeathRecipient> deathRecipient =
108         new (std::nothrow) MultimodalInputConnectDeathRecipient(deathCallback);
109     CHKPP(deathRecipient);
110     sa->AddDeathRecipient(deathRecipient);
111     auto retDeath = mmiDeathRecipients_.emplace(remoteDeviceId, deathRecipient);
112     if (!retDeath.second) {
113         MMI_HILOGE("Get proxy faild, death recipient not set");
114         return nullptr;
115     }
116     sptr<IMultimodalInputConnect> remoteService = iface_cast<IMultimodalInputConnect>(sa);
117     if (remoteService == nullptr) {
118         MMI_HILOGE("Get proxy faild, get remote service is null!");
119         OnRemoteDeath(remoteDeviceId);
120         return nullptr;
121     }
122     auto retService = mmiRemoteServices_.emplace(remoteDeviceId, remoteService);
123     if (!retService.second) {
124         MMI_HILOGE("Get proxy faild, remote service not set!");
125         OnRemoteDeath(remoteDeviceId);
126         return nullptr;
127     }
128     return remoteService;
129 }
130 
OnRemoteDeath(const std::string & remoteDeviceId)131 void MultimodalInputConnectRemoter::OnRemoteDeath(const std::string &remoteDeviceId)
132 {
133     CALL_DEBUG_ENTER;
134     auto iterService = mmiRemoteServices_.find(remoteDeviceId);
135     if (iterService != mmiRemoteServices_.end()) {
136         mmiRemoteServices_.erase(iterService);
137     }
138     auto iterRecipient = mmiDeathRecipients_.find(remoteDeviceId);
139     if (iterRecipient != mmiDeathRecipients_.end()) {
140         mmiDeathRecipients_.erase(iterRecipient);
141     }
142 }
143 } // namespace MMI
144 } // namespace OHOS