• 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     CHKPR(connectionCallback_, false);
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     MMI_HILOGI("InjectNotice connected,remoteObject_:%{private}p", &remoteObject_);
122 }
123 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)124 void InjectNoticeManager::InjectNoticeConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
125     int resultCode)
126 {
127     CALL_DEBUG_ENTER;
128     std::lock_guard<std::mutex> lock(mutex_);
129     isConnected_ = false;
130     MMI_HILOGI("InjectNotice disconnected,remoteObject_:%{private}p", &remoteObject_);
131     remoteObject_ = nullptr;
132 }
133 
SendNotice(const InjectNoticeInfo & noticeInfo)134 bool InjectNoticeManager::InjectNoticeConnection::SendNotice(const InjectNoticeInfo& noticeInfo)
135 {
136     CALL_DEBUG_ENTER;
137     MessageParcel data;
138     MessageParcel reply;
139     MessageOption option;
140     data.WriteInterfaceToken(INJECT_NOTICE_INTERFACE_TOKEN);
141     data.WriteInt32(noticeInfo.pid);
142     int32_t cmdCode = MESSAGE_PARCEL_KEY_NOTICE_SEND;
143     std::lock_guard<std::mutex> lock(mutex_);
144     CHKPF(remoteObject_);
145     MMI_HILOGD("Requst send notice begin");
146     int32_t ret = remoteObject_->SendRequest(cmdCode, data, reply, option);
147     if (ret != ERR_OK) {
148         MMI_HILOGW("Requst send notice failed:%{public}d", ret);
149         return false;
150     }
151     MMI_HILOGI("Requst send notice ok");
152     return true;
153 }
154 
CancelNotice(const InjectNoticeInfo & noticeInfo)155 bool InjectNoticeManager::InjectNoticeConnection::CancelNotice(const InjectNoticeInfo& noticeInfo)
156 {
157     CALL_DEBUG_ENTER;
158     MessageParcel data;
159     MessageParcel reply;
160     MessageOption option;
161     data.WriteInterfaceToken(INJECT_NOTICE_INTERFACE_TOKEN);
162     data.WriteInt32(noticeInfo.pid);
163     int32_t cmdCode = MESSAGE_PARCEL_KEY_NOTICE_CLOSE;
164     std::lock_guard<std::mutex> lock(mutex_);
165     CHKPF(remoteObject_);
166     MMI_HILOGD("Requst send close notice begin");
167     int32_t ret = remoteObject_->SendRequest(cmdCode, data, reply, option);
168     if (ret != ERR_OK) {
169         MMI_HILOGW("Requst send close notice failed:%{public}d", ret);
170         return false;
171     }
172     MMI_HILOGI("Requst send close notice ok");
173     return true;
174 }
175 
IsConnected() const176 bool InjectNoticeManager::InjectNoticeConnection::IsConnected() const
177 {
178     return isConnected_;
179 }
180 } // namespace MMI
181 } // namespace OHOS