• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "inject_notice_manager.h"
17 
18 #include "ability_manager_client.h"
19 #ifdef OHOS_BUILD_ENABLE_DFX_RADAR
20 #include "dfx_hisysevent.h"
21 #endif // OHOS_BUILD_ENABLE_DFX_RADAR
22 #include "mmi_log.h"
23 
24 #undef MMI_LOG_DOMAIN
25 #define MMI_LOG_DOMAIN MMI_LOG_SERVER
26 #undef MMI_LOG_TAG
27 #define MMI_LOG_TAG "InjectNoticeManage"
28 
29 namespace OHOS {
30 namespace MMI {
31 namespace {
32 constexpr int32_t INVALID_USERID { -1 };
33 constexpr int32_t MESSAGE_PARCEL_KEY_NOTICE_SEND { 0 };
34 constexpr int32_t MESSAGE_PARCEL_KEY_NOTICE_CLOSE { 1 };
35 const std::u16string INJECT_NOTICE_INTERFACE_TOKEN { u"ohos.multimodalinput.IInjectNotice" };
36 }
37 
InjectNoticeManager()38 InjectNoticeManager::InjectNoticeManager() : connectionCallback_(new (std::nothrow) InjectNoticeConnection()) {}
39 
~InjectNoticeManager()40 InjectNoticeManager::~InjectNoticeManager()
41 {
42     connectionCallback_ = nullptr;
43 }
44 
StartNoticeAbility()45 bool InjectNoticeManager::StartNoticeAbility()
46 {
47     CALL_DEBUG_ENTER;
48     if (isStartSrv_) {
49         MMI_HILOGW("The injectNoticeAbility has start");
50         return true;
51     }
52     auto client = AAFwk::AbilityManagerClient::GetInstance();
53     if (client == nullptr) {
54         return false;
55     }
56     AAFwk::Want want;
57     want.SetElementName("com.ohos.powerdialog", "InjectNoticeAbility");
58     auto begin = std::chrono::high_resolution_clock::now();
59     int32_t result = client->StartAbility(want);
60     auto durationMS = std::chrono::duration_cast<std::chrono::milliseconds>(
61         std::chrono::high_resolution_clock::now() - begin).count();
62 #ifdef OHOS_BUILD_ENABLE_DFX_RADAR
63     DfxHisysevent::ReportApiCallTimes(ApiDurationStatistics::Api::ABILITY_MGR_CLIENT_START_ABILITY, durationMS);
64 #endif // OHOS_BUILD_ENABLE_DFX_RADAR
65     if (result != 0) {
66         MMI_HILOGW("Start injectNoticeAbility failed, result:%{public}d", result);
67         return false;
68     }
69     isStartSrv_ = true;
70     MMI_HILOGI("Start injectNoticeAbility success");
71     return true;
72 }
73 
ConnectNoticeSrv()74 bool InjectNoticeManager::ConnectNoticeSrv()
75 {
76     CALL_DEBUG_ENTER;
77     CHKPF(connectionCallback_);
78     if (connectionCallback_->IsConnected()) {
79         MMI_HILOGD("InjectNoticeAbility has connected");
80         return true;
81     }
82     auto abilityMgr = AAFwk::AbilityManagerClient::GetInstance();
83     CHKPF(abilityMgr);
84     AAFwk::Want want;
85     want.SetElementName("com.ohos.powerdialog", "InjectNoticeAbility");
86     auto begin = std::chrono::high_resolution_clock::now();
87     ErrCode result = abilityMgr->ConnectAbility(want, connectionCallback_, INVALID_USERID);
88     auto durationMS = std::chrono::duration_cast<std::chrono::milliseconds>(
89         std::chrono::high_resolution_clock::now() - begin).count();
90 #ifdef OHOS_BUILD_ENABLE_DFX_RADAR
91     DfxHisysevent::ReportApiCallTimes(ApiDurationStatistics::Api::ABILITY_MGR_CONNECT_ABILITY, durationMS);
92 #endif // OHOS_BUILD_ENABLE_DFX_RADAR
93     if (result != ERR_OK) {
94         MMI_HILOGW("Connect InjectNoticeAbility failed, result:%{public}d", result);
95         return false;
96     }
97     MMI_HILOGI("Connect InjectNoticeAbility success");
98     return true;
99 }
100 
IsAbilityStart() const101 bool InjectNoticeManager::IsAbilityStart() const
102 {
103     return isStartSrv_;
104 }
105 
GetConnection() const106 sptr<InjectNoticeManager::InjectNoticeConnection> InjectNoticeManager::GetConnection() const
107 {
108     return connectionCallback_;
109 }
110 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)111 void InjectNoticeManager::InjectNoticeConnection::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
112     const sptr<IRemoteObject>& remoteObject, int resultCode)
113 {
114     CALL_DEBUG_ENTER;
115     std::lock_guard<std::mutex> lock(mutex_);
116     CHKPV(remoteObject);
117     if (remoteObject_ == nullptr) {
118         remoteObject_ = remoteObject;
119     }
120     isConnected_ = true;
121 }
122 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)123 void InjectNoticeManager::InjectNoticeConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
124     int resultCode)
125 {
126     CALL_DEBUG_ENTER;
127     std::lock_guard<std::mutex> lock(mutex_);
128     isConnected_ = false;
129     remoteObject_ = nullptr;
130 }
131 
SendNotice(const InjectNoticeInfo & noticeInfo)132 bool InjectNoticeManager::InjectNoticeConnection::SendNotice(const InjectNoticeInfo& noticeInfo)
133 {
134     CALL_DEBUG_ENTER;
135     MessageParcel data;
136     MessageParcel reply;
137     MessageOption option;
138     data.WriteInterfaceToken(INJECT_NOTICE_INTERFACE_TOKEN);
139     data.WriteInt32(noticeInfo.pid);
140     int32_t cmdCode = MESSAGE_PARCEL_KEY_NOTICE_SEND;
141     std::lock_guard<std::mutex> lock(mutex_);
142     CHKPF(remoteObject_);
143     MMI_HILOGD("Requst send notice begin");
144     int32_t ret = remoteObject_->SendRequest(cmdCode, data, reply, option);
145     if (ret != ERR_OK) {
146         MMI_HILOGW("Requst send notice failed:%{public}d", ret);
147         return false;
148     }
149     MMI_HILOGI("Requst send notice ok");
150     return true;
151 }
152 
CancelNotice(const InjectNoticeInfo & noticeInfo)153 bool InjectNoticeManager::InjectNoticeConnection::CancelNotice(const InjectNoticeInfo& noticeInfo)
154 {
155     CALL_DEBUG_ENTER;
156     MessageParcel data;
157     MessageParcel reply;
158     MessageOption option;
159     data.WriteInterfaceToken(INJECT_NOTICE_INTERFACE_TOKEN);
160     data.WriteInt32(noticeInfo.pid);
161     int32_t cmdCode = MESSAGE_PARCEL_KEY_NOTICE_CLOSE;
162     std::lock_guard<std::mutex> lock(mutex_);
163     CHKPF(remoteObject_);
164     MMI_HILOGD("Requst send close notice begin");
165     int32_t ret = remoteObject_->SendRequest(cmdCode, data, reply, option);
166     if (ret != ERR_OK) {
167         MMI_HILOGW("Requst send close notice failed:%{public}d", ret);
168         return false;
169     }
170     MMI_HILOGI("Requst send close notice ok");
171     return true;
172 }
173 
IsConnected() const174 bool InjectNoticeManager::InjectNoticeConnection::IsConnected() const
175 {
176     return isConnected_;
177 }
178 } // namespace MMI
179 } // namespace OHOS