1 /*
2 * Copyright (c) 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_subscriber_proxy.h"
17
18 #include "memmgr_log.h"
19 #include <message_parcel.h>
20
21 namespace OHOS {
22 namespace Memory {
23 namespace {
24 const std::string TAG = "AppStateSubscriberProxy";
25 }
AppStateSubscriberProxy(const sptr<IRemoteObject> & impl)26 AppStateSubscriberProxy::AppStateSubscriberProxy(const sptr<IRemoteObject> &impl)
27 : IRemoteProxy<IAppStateSubscriber> (impl)
28 {}
~AppStateSubscriberProxy()29 AppStateSubscriberProxy::~AppStateSubscriberProxy() {}
30
OnConnected()31 void AppStateSubscriberProxy::OnConnected()
32 {
33 sptr<IRemoteObject> remote = Remote();
34 if (remote == nullptr) {
35 HILOGE("remote is dead.");
36 return;
37 }
38 MessageParcel data;
39 if (!data.WriteInterfaceToken(AppStateSubscriberProxy::GetDescriptor())) {
40 HILOGE("write interface token failed");
41 return;
42 }
43
44 MessageParcel reply;
45 MessageOption option = { MessageOption::TF_ASYNC };
46 int32_t ret = remote->SendRequest(
47 static_cast<uint32_t>(AppStateSubscriberInterfaceCode::ON_CONNECTED), data, reply, option);
48 if (ret != ERR_NONE) {
49 HILOGE("send request failed, error code: %d", ret);
50 }
51 }
52
OnDisconnected()53 void AppStateSubscriberProxy::OnDisconnected()
54 {
55 sptr<IRemoteObject> remote = Remote();
56 if (remote == nullptr) {
57 HILOGE("remote is dead.");
58 return;
59 }
60 MessageParcel data;
61 if (!data.WriteInterfaceToken(AppStateSubscriberProxy::GetDescriptor())) {
62 HILOGE("write interface token failed");
63 return;
64 }
65
66 MessageParcel reply;
67 MessageOption option = { MessageOption::TF_SYNC };
68 int32_t ret = remote->SendRequest(
69 static_cast<uint32_t>(AppStateSubscriberInterfaceCode::ON_DISCONNECTED), data, reply, option);
70 if (ret != ERR_NONE) {
71 HILOGE("send request failed, error code: %d", ret);
72 }
73 }
74
OnAppStateChanged(int32_t pid,int32_t uid,int32_t state)75 void AppStateSubscriberProxy::OnAppStateChanged(int32_t pid, int32_t uid, int32_t state)
76 {
77 sptr<IRemoteObject> remote = Remote();
78 if (remote == nullptr) {
79 HILOGE("remote is dead.");
80 return;
81 }
82 MessageParcel data;
83 if (!data.WriteInterfaceToken(AppStateSubscriberProxy::GetDescriptor())) {
84 HILOGE("write interface token failed");
85 return;
86 }
87
88 if (!data.WriteInt32(pid) || !data.WriteInt32(uid) || !data.WriteInt32(state)) {
89 HILOGE("write app state failed");
90 return;
91 }
92
93 MessageParcel reply;
94 MessageOption option = { MessageOption::TF_ASYNC };
95 int32_t ret = remote->SendRequest(
96 static_cast<uint32_t>(AppStateSubscriberInterfaceCode::ON_APP_STATE_CHANGED), data, reply, option);
97 if (ret != ERR_NONE) {
98 HILOGE("send request failed, error code: %d", ret);
99 }
100 }
101
OnTrim(SystemMemoryLevel level)102 void AppStateSubscriberProxy::OnTrim(SystemMemoryLevel level)
103 {
104 sptr<IRemoteObject> remote = Remote();
105 if (remote == nullptr) {
106 HILOGE("remote is dead.");
107 return;
108 }
109 MessageParcel data;
110 if (!data.WriteInterfaceToken(AppStateSubscriberProxy::GetDescriptor())) {
111 HILOGE("write interface token failed");
112 return;
113 }
114
115 if (!data.WriteInt32(static_cast<int32_t>(level))) {
116 HILOGE("write memorylevel of system failed");
117 return;
118 }
119
120 MessageParcel reply;
121 MessageOption option = { MessageOption::TF_ASYNC };
122 int32_t ret = remote->SendRequest(
123 static_cast<uint32_t>(AppStateSubscriberInterfaceCode::ON_TRIM), data, reply, option);
124 if (ret != ERR_NONE) {
125 HILOGE("send request failed, error code: %d", ret);
126 }
127 }
128
ForceReclaim(int32_t pid,int32_t uid)129 void AppStateSubscriberProxy::ForceReclaim(int32_t pid, int32_t uid)
130 {
131 sptr<IRemoteObject> remote = Remote();
132 if (remote == nullptr) {
133 HILOGE("remote is dead.");
134 return;
135 }
136 MessageParcel data;
137 if (!data.WriteInterfaceToken(AppStateSubscriberProxy::GetDescriptor())) {
138 HILOGE("write interface token failed");
139 return;
140 }
141
142 if (!data.WriteInt32(pid) || !data.WriteInt32(uid)) {
143 HILOGE("write pid and uid failed");
144 return;
145 }
146
147 MessageParcel reply;
148 MessageOption option = { MessageOption::TF_ASYNC };
149 int32_t ret = remote->SendRequest(
150 static_cast<uint32_t>(AppStateSubscriberInterfaceCode::FORCE_RECLAIM), data, reply, option);
151 if (ret != ERR_NONE) {
152 HILOGE("send request failed, error code: %d", ret);
153 }
154 }
155 } // namespace Memory
156 } // namespace OHOS