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 CHKPV(remoteObject);
105 if (remoteObject_ == nullptr) {
106 remoteObject_ = remoteObject;
107 }
108 isConnected_ = true;
109 }
110
OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int resultCode)111 void InjectNoticeManager::InjectNoticeConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
112 int resultCode)
113 {
114 CALL_DEBUG_ENTER;
115 isConnected_ = false;
116 remoteObject_ = nullptr;
117 }
118
SendNotice(const InjectNoticeInfo& noticeInfo)119 bool InjectNoticeManager::InjectNoticeConnection::SendNotice(const InjectNoticeInfo& noticeInfo)
120 {
121 CALL_DEBUG_ENTER;
122 MessageParcel data;
123 MessageParcel reply;
124 MessageOption option;
125 data.WriteInt32(noticeInfo.pid);
126 int32_t cmdCode = MESSAGE_PARCEL_KEY_NOTICE_SEND;
127 MMI_HILOGD("Requst send notice begin");
128 CHKPF(remoteObject_);
129 int32_t ret = remoteObject_->SendRequest(cmdCode, data, reply, option);
130 if (ret != ERR_OK) {
131 MMI_HILOGW("Requst send notice failed:%{public}d", ret);
132 return false;
133 }
134 MMI_HILOGI("Requst send notice ok");
135 return true;
136 }
137
CancelNotice(const InjectNoticeInfo& noticeInfo)138 bool InjectNoticeManager::InjectNoticeConnection::CancelNotice(const InjectNoticeInfo& noticeInfo)
139 {
140 CALL_DEBUG_ENTER;
141 MessageParcel data;
142 MessageParcel reply;
143 MessageOption option;
144 data.WriteInt32(noticeInfo.pid);
145 int32_t cmdCode = MESSAGE_PARCEL_KEY_NOTICE_CLOSE;
146 MMI_HILOGD("Requst send close notice begin");
147 CHKPF(remoteObject_);
148 int32_t ret = remoteObject_->SendRequest(cmdCode, data, reply, option);
149 if (ret != ERR_OK) {
150 MMI_HILOGW("Requst send close notice failed: %{public}d", ret);
151 return false;
152 }
153 MMI_HILOGI("Requst send close notice ok");
154 return true;
155 }
156
IsConnected() const157 bool InjectNoticeManager::InjectNoticeConnection::IsConnected() const
158 {
159 return isConnected_;
160 }
161 } // namespace MMI
162 } // namespace OHOS