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