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