1 /*
2 * Copyright (c) 2021-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 "application_state_observer_proxy.h"
17
18 #include "hilog_wrapper.h"
19 #include "ipc_types.h"
20
21
22 namespace OHOS {
23 namespace AppExecFwk {
ApplicationStateObserverProxy(const sptr<IRemoteObject> & impl)24 ApplicationStateObserverProxy::ApplicationStateObserverProxy(
25 const sptr<IRemoteObject> &impl) : IRemoteProxy<IApplicationStateObserver>(impl)
26 {}
27
WriteInterfaceToken(MessageParcel & data)28 bool ApplicationStateObserverProxy::WriteInterfaceToken(MessageParcel &data)
29 {
30 if (!data.WriteInterfaceToken(ApplicationStateObserverProxy::GetDescriptor())) {
31 HILOG_ERROR("write interface token failed");
32 return false;
33 }
34 return true;
35 }
36
OnForegroundApplicationChanged(const AppStateData & appStateData)37 void ApplicationStateObserverProxy::OnForegroundApplicationChanged(const AppStateData &appStateData)
38 {
39 MessageParcel data;
40 MessageParcel reply;
41 MessageOption option(MessageOption::TF_ASYNC);
42 if (!WriteInterfaceToken(data)) {
43 return;
44 }
45 data.WriteParcelable(&appStateData);
46 sptr<IRemoteObject> remote = Remote();
47 if (remote == nullptr) {
48 HILOG_ERROR("Remote() is NULL");
49 return;
50 }
51 int32_t ret = remote->SendRequest(
52 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_FOREGROUND_APPLICATION_CHANGED),
53 data, reply, option);
54 if (ret != NO_ERROR) {
55 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
56 }
57 }
58
OnAbilityStateChanged(const AbilityStateData & abilityStateData)59 void ApplicationStateObserverProxy::OnAbilityStateChanged(const AbilityStateData &abilityStateData)
60 {
61 MessageParcel data;
62 MessageParcel reply;
63 MessageOption option(MessageOption::TF_ASYNC);
64 if (!WriteInterfaceToken(data)) {
65 return;
66 }
67 data.WriteParcelable(&abilityStateData);
68 sptr<IRemoteObject> remote = Remote();
69 if (remote == nullptr) {
70 HILOG_ERROR("Remote() is NULL");
71 return;
72 }
73 int32_t ret = remote->SendRequest(
74 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_ABILITY_STATE_CHANGED),
75 data, reply, option);
76 if (ret != NO_ERROR) {
77 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
78 }
79 }
80
OnExtensionStateChanged(const AbilityStateData & abilityStateData)81 void ApplicationStateObserverProxy::OnExtensionStateChanged(const AbilityStateData &abilityStateData)
82 {
83 MessageParcel data;
84 MessageParcel reply;
85 MessageOption option(MessageOption::TF_ASYNC);
86 if (!WriteInterfaceToken(data)) {
87 return;
88 }
89 data.WriteParcelable(&abilityStateData);
90 sptr<IRemoteObject> remote = Remote();
91 if (remote == nullptr) {
92 HILOG_ERROR("Remote() is NULL");
93 return;
94 }
95 int32_t ret = remote->SendRequest(
96 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_EXTENSION_STATE_CHANGED),
97 data, reply, option);
98 if (ret != NO_ERROR) {
99 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
100 }
101 }
102
OnProcessCreated(const ProcessData & processData)103 void ApplicationStateObserverProxy::OnProcessCreated(const ProcessData &processData)
104 {
105 MessageParcel data;
106 MessageParcel reply;
107 MessageOption option(MessageOption::TF_ASYNC);
108 if (!WriteInterfaceToken(data)) {
109 return;
110 }
111 data.WriteParcelable(&processData);
112 sptr<IRemoteObject> remote = Remote();
113 if (remote == nullptr) {
114 HILOG_ERROR("Remote() is NULL");
115 return;
116 }
117 int32_t ret = remote->SendRequest(
118 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_CREATED),
119 data, reply, option);
120 if (ret != NO_ERROR) {
121 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
122 }
123 }
124
OnProcessReused(const ProcessData & processData)125 void ApplicationStateObserverProxy::OnProcessReused(const ProcessData &processData)
126 {
127 MessageParcel data;
128 MessageParcel reply;
129 MessageOption option(MessageOption::TF_ASYNC);
130 if (!WriteInterfaceToken(data)) {
131 return;
132 }
133 data.WriteParcelable(&processData);
134 sptr<IRemoteObject> remote = Remote();
135 if (remote == nullptr) {
136 HILOG_ERROR("Remote() is NULL");
137 return;
138 }
139 int32_t ret = remote->SendRequest(
140 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_REUSED),
141 data, reply, option);
142 if (ret != NO_ERROR) {
143 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
144 }
145 }
146
OnProcessStateChanged(const ProcessData & processData)147 void ApplicationStateObserverProxy::OnProcessStateChanged(const ProcessData &processData)
148 {
149 MessageParcel data;
150 MessageParcel reply;
151 MessageOption option(MessageOption::TF_ASYNC);
152 if (!WriteInterfaceToken(data)) {
153 return;
154 }
155 data.WriteParcelable(&processData);
156 sptr<IRemoteObject> remote = Remote();
157 if (remote == nullptr) {
158 HILOG_ERROR("Remote() is NULL");
159 return;
160 }
161 int32_t ret = remote->SendRequest(
162 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_STATE_CHANGED),
163 data, reply, option);
164 if (ret != NO_ERROR) {
165 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
166 }
167 }
168
OnProcessDied(const ProcessData & processData)169 void ApplicationStateObserverProxy::OnProcessDied(const ProcessData &processData)
170 {
171 MessageParcel data;
172 MessageParcel reply;
173 MessageOption option(MessageOption::TF_ASYNC);
174 if (!WriteInterfaceToken(data)) {
175 return;
176 }
177 data.WriteParcelable(&processData);
178 sptr<IRemoteObject> remote = Remote();
179 if (remote == nullptr) {
180 HILOG_ERROR("Remote() is NULL");
181 return;
182 }
183 int32_t ret = remote->SendRequest(
184 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_DIED),
185 data, reply, option);
186 if (ret != NO_ERROR) {
187 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
188 }
189 }
190
OnApplicationStateChanged(const AppStateData & appStateData)191 void ApplicationStateObserverProxy::OnApplicationStateChanged(const AppStateData &appStateData)
192 {
193 MessageParcel data;
194 MessageParcel reply;
195 MessageOption option(MessageOption::TF_ASYNC);
196 if (!WriteInterfaceToken(data)) {
197 HILOG_ERROR("OnApplicationStateChanged, WriteInterfaceToken failed");
198 return;
199 }
200 data.WriteParcelable(&appStateData);
201 sptr<IRemoteObject> remote = Remote();
202 if (remote == nullptr) {
203 HILOG_ERROR("OnApplicationStateChanged, Remote() is NULL");
204 return;
205 }
206 int32_t ret = remote->SendRequest(
207 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APPLICATION_STATE_CHANGED),
208 data, reply, option);
209 if (ret != NO_ERROR) {
210 HILOG_WARN("OnApplicationStateChanged, SendRequest is failed, error code: %{public}d", ret);
211 }
212 }
213
OnAppStateChanged(const AppStateData & appStateData)214 void ApplicationStateObserverProxy::OnAppStateChanged(const AppStateData &appStateData)
215 {
216 MessageParcel data;
217 MessageParcel reply;
218 MessageOption option(MessageOption::TF_ASYNC);
219 if (!WriteInterfaceToken(data)) {
220 HILOG_ERROR("OnAppStateChanged, WriteInterfaceToken failed");
221 return;
222 }
223 data.WriteParcelable(&appStateData);
224 sptr<IRemoteObject> remote = Remote();
225 if (remote == nullptr) {
226 HILOG_ERROR("OnAppStateChanged, Remote() is NULL");
227 return;
228 }
229 int32_t ret = remote->SendRequest(
230 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APP_STATE_CHANGED),
231 data, reply, option);
232 if (ret != NO_ERROR) {
233 HILOG_WARN("OnAppStateChanged, SendRequest is failed, error code: %{public}d", ret);
234 }
235 }
236
OnAppStarted(const AppStateData & appStateData)237 void ApplicationStateObserverProxy::OnAppStarted(const AppStateData &appStateData)
238 {
239 MessageParcel data;
240 MessageParcel reply;
241 MessageOption option(MessageOption::TF_ASYNC);
242 if (!WriteInterfaceToken(data)) {
243 HILOG_ERROR("OnAppStarted, WriteInterfaceToken failed");
244 return;
245 }
246 data.WriteParcelable(&appStateData);
247 sptr<IRemoteObject> remote = Remote();
248 if (remote == nullptr) {
249 HILOG_ERROR("OnAppStarted, Remote() is NULL");
250 return;
251 }
252 int32_t ret = remote->SendRequest(
253 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APP_STARTED),
254 data, reply, option);
255 if (ret != NO_ERROR) {
256 HILOG_WARN("OnAppStarted, SendRequest is failed, error code: %{public}d", ret);
257 }
258 }
259
OnAppStopped(const AppStateData & appStateData)260 void ApplicationStateObserverProxy::OnAppStopped(const AppStateData &appStateData)
261 {
262 MessageParcel data;
263 MessageParcel reply;
264 MessageOption option(MessageOption::TF_ASYNC);
265 if (!WriteInterfaceToken(data)) {
266 HILOG_ERROR("OnAppStopped, WriteInterfaceToken failed");
267 return;
268 }
269 data.WriteParcelable(&appStateData);
270 sptr<IRemoteObject> remote = Remote();
271 if (remote == nullptr) {
272 HILOG_ERROR("OnAppStopped, Remote() is NULL");
273 return;
274 }
275 int32_t ret = remote->SendRequest(
276 static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APP_STOPPED),
277 data, reply, option);
278 if (ret != NO_ERROR) {
279 HILOG_WARN("OnAppStopped, SendRequest is failed, error code: %{public}d", ret);
280 }
281 }
282
283 } // namespace AppExecFwk
284 } // namespace OHOS
285