1 /*
2  * Copyright (c) 2023-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 "app_foreground_state_observer_proxy.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "ipc_types.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
AppForegroundStateObserverProxy(const sptr<IRemoteObject> & impl)23 AppForegroundStateObserverProxy::AppForegroundStateObserverProxy(const sptr<IRemoteObject> &impl)
24     : IRemoteProxy<IAppForegroundStateObserver>(impl)
25 {}
26 
WriteInterfaceToken(MessageParcel & data)27 bool AppForegroundStateObserverProxy::WriteInterfaceToken(MessageParcel &data)
28 {
29     if (!data.WriteInterfaceToken(AppForegroundStateObserverProxy::GetDescriptor())) {
30         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
31         return false;
32     }
33     return true;
34 }
35 
OnAppStateChanged(const AppStateData & appStateData)36 void AppForegroundStateObserverProxy::OnAppStateChanged(const AppStateData &appStateData)
37 {
38     MessageParcel data;
39     if (!WriteInterfaceToken(data)) {
40         TAG_LOGE(AAFwkTag::APPMGR, "Write Token failed.");
41         return;
42     }
43     if (!data.WriteParcelable(&appStateData)) {
44         TAG_LOGE(AAFwkTag::APPMGR, "Fail to write appStateData.");
45         return;
46     }
47     sptr<IRemoteObject> remote = Remote();
48     if (remote == nullptr) {
49         TAG_LOGE(AAFwkTag::APPMGR, "Remote is NULL.");
50         return;
51     }
52     MessageParcel reply;
53     MessageOption option(MessageOption::TF_ASYNC);
54     int32_t ret = remote->SendRequest(
55         static_cast<uint32_t>(IAppForegroundStateObserver::Message::ON_APP_STATE_CHANGED), data, reply, option);
56     if (ret != NO_ERROR) {
57         TAG_LOGE(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d.", ret);
58     }
59 }
60 } // namespace AppExecFwk
61 } // namespace OHOS
62