• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     MessageParcel data;
127     MessageParcel reply;
128     MessageOption option(MessageOption::TF_ASYNC);
129     if (!WriteInterfaceToken(data)) {
130         return;
131     }
132     data.WriteParcelable(&processData);
133     sptr<IRemoteObject> remote = Remote();
134     if (remote == nullptr) {
135         HILOG_ERROR("Remote() is NULL");
136         return;
137     }
138     int32_t ret = remote->SendRequest(
139         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_REUSED),
140         data, reply, option);
141     if (ret != NO_ERROR) {
142         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
143     }
144 }
145 
OnProcessStateChanged(const ProcessData & processData)146 void ApplicationStateObserverProxy::OnProcessStateChanged(const ProcessData &processData)
147 {
148     MessageParcel data;
149     MessageParcel reply;
150     MessageOption option(MessageOption::TF_ASYNC);
151     if (!WriteInterfaceToken(data)) {
152         return;
153     }
154     data.WriteParcelable(&processData);
155     sptr<IRemoteObject> remote = Remote();
156     if (remote == nullptr) {
157         HILOG_ERROR("Remote() is NULL");
158         return;
159     }
160     int32_t ret = remote->SendRequest(
161         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_STATE_CHANGED),
162         data, reply, option);
163     if (ret != NO_ERROR) {
164         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
165     }
166 }
167 
OnProcessDied(const ProcessData & processData)168 void ApplicationStateObserverProxy::OnProcessDied(const ProcessData &processData)
169 {
170     MessageParcel data;
171     MessageParcel reply;
172     MessageOption option(MessageOption::TF_ASYNC);
173     if (!WriteInterfaceToken(data)) {
174         return;
175     }
176     data.WriteParcelable(&processData);
177     sptr<IRemoteObject> remote = Remote();
178     if (remote == nullptr) {
179         HILOG_ERROR("Remote() is NULL");
180         return;
181     }
182     int32_t ret = remote->SendRequest(
183         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_PROCESS_DIED),
184         data, reply, option);
185     if (ret != NO_ERROR) {
186         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
187     }
188 }
189 
OnApplicationStateChanged(const AppStateData & appStateData)190 void ApplicationStateObserverProxy::OnApplicationStateChanged(const AppStateData &appStateData)
191 {
192     MessageParcel data;
193     MessageParcel reply;
194     MessageOption option(MessageOption::TF_ASYNC);
195     if (!WriteInterfaceToken(data)) {
196         HILOG_ERROR("OnApplicationStateChanged, WriteInterfaceToken failed");
197         return;
198     }
199     data.WriteParcelable(&appStateData);
200     sptr<IRemoteObject> remote = Remote();
201     if (remote == nullptr) {
202         HILOG_ERROR("OnApplicationStateChanged, Remote() is NULL");
203         return;
204     }
205     int32_t ret = remote->SendRequest(
206         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APPLICATION_STATE_CHANGED),
207         data, reply, option);
208     if (ret != NO_ERROR) {
209         HILOG_WARN("OnApplicationStateChanged, SendRequest is failed, error code: %{public}d", ret);
210     }
211 }
212 
OnAppStateChanged(const AppStateData & appStateData)213 void ApplicationStateObserverProxy::OnAppStateChanged(const AppStateData &appStateData)
214 {
215     MessageParcel data;
216     MessageParcel reply;
217     MessageOption option(MessageOption::TF_ASYNC);
218     if (!WriteInterfaceToken(data)) {
219         HILOG_ERROR("OnAppStateChanged, WriteInterfaceToken failed");
220         return;
221     }
222     data.WriteParcelable(&appStateData);
223     sptr<IRemoteObject> remote = Remote();
224     if (remote == nullptr) {
225         HILOG_ERROR("OnAppStateChanged, Remote() is NULL");
226         return;
227     }
228     int32_t ret = remote->SendRequest(
229         static_cast<uint32_t>(IApplicationStateObserver::Message::TRANSACT_ON_APP_STATE_CHANGED),
230         data, reply, option);
231     if (ret != NO_ERROR) {
232         HILOG_WARN("OnAppStateChanged, SendRequest is failed, error code: %{public}d", ret);
233     }
234 }
235 }  // namespace AppExecFwk
236 }  // namespace OHOS
237