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 "app_state_callback_proxy.h"
17
18 #include "ipc_types.h"
19
20 #include "hilog_wrapper.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
AppStateCallbackProxy(const sptr<IRemoteObject> & impl)24 AppStateCallbackProxy::AppStateCallbackProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IAppStateCallback>(impl)
25 {}
26
WriteInterfaceToken(MessageParcel & data)27 bool AppStateCallbackProxy::WriteInterfaceToken(MessageParcel &data)
28 {
29 if (!data.WriteInterfaceToken(AppStateCallbackProxy::GetDescriptor())) {
30 HILOG_ERROR("write interface token failed");
31 return false;
32 }
33 return true;
34 }
35
OnAbilityRequestDone(const sptr<IRemoteObject> & token,const AbilityState state)36 void AppStateCallbackProxy::OnAbilityRequestDone(const sptr<IRemoteObject> &token, const AbilityState state)
37 {
38 HILOG_DEBUG("begin");
39 MessageParcel data;
40 MessageParcel reply;
41 MessageOption option(MessageOption::TF_ASYNC);
42 if (!WriteInterfaceToken(data)) {
43 return;
44 }
45
46 if (token) {
47 if (!data.WriteBool(true) || !data.WriteRemoteObject(token.GetRefPtr())) {
48 HILOG_ERROR("Failed to write flag and token");
49 return;
50 }
51 } else {
52 if (!data.WriteBool(false)) {
53 HILOG_ERROR("Failed to write flag");
54 return;
55 }
56 }
57
58 int32_t abilityState = static_cast<int32_t>(state);
59 data.WriteInt32(abilityState);
60 sptr<IRemoteObject> remote = Remote();
61 if (remote == nullptr) {
62 HILOG_ERROR("Remote() is NULL");
63 return;
64 }
65 int32_t ret = remote->SendRequest(
66 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_ABILITY_REQUEST_DONE), data, reply, option);
67 if (ret != NO_ERROR) {
68 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
69 }
70 HILOG_DEBUG("end");
71 }
72
OnAppStateChanged(const AppProcessData & appProcessData)73 void AppStateCallbackProxy::OnAppStateChanged(const AppProcessData &appProcessData)
74 {
75 HILOG_DEBUG("begin");
76 MessageParcel data;
77 MessageParcel reply;
78 MessageOption option(MessageOption::TF_ASYNC);
79 if (!WriteInterfaceToken(data)) {
80 return;
81 }
82 data.WriteParcelable(&appProcessData);
83 sptr<IRemoteObject> remote = Remote();
84 if (remote == nullptr) {
85 HILOG_ERROR("Remote() is NULL");
86 return;
87 }
88 int32_t ret = remote->SendRequest(
89 static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_APP_STATE_CHANGED), data, reply, option);
90 if (ret != NO_ERROR) {
91 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
92 }
93 HILOG_DEBUG("end");
94 }
95 } // namespace AppExecFwk
96 } // namespace OHOS
97