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 "bundle_active_proxy.h"
17 #include "ibundle_active_service_ipc_interface_code.h"
18
19 namespace OHOS {
20 namespace DeviceUsageStats {
ReportEvent(BundleActiveEvent & event,const int32_t userId)21 ErrCode BundleActiveProxy::ReportEvent(BundleActiveEvent& event, const int32_t userId)
22 {
23 MessageParcel data;
24 MessageParcel reply;
25 MessageOption option;
26 if (!data.WriteInterfaceToken(GetDescriptor())) {
27 return ERR_PARCEL_WRITE_FALIED;
28 }
29 data.WriteInt32(userId);
30 event.Marshalling(data);
31 Remote() -> SendRequest(
32 static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::REPORT_EVENT), data, reply, option);
33
34 int32_t result = reply.ReadInt32();
35 return result;
36 }
37
IsBundleIdle(bool & isBundleIdle,const std::string & bundleName,int32_t userId)38 ErrCode BundleActiveProxy::IsBundleIdle(bool& isBundleIdle, const std::string& bundleName, int32_t userId)
39 {
40 MessageParcel data;
41 MessageParcel reply;
42 MessageOption option;
43 if (!data.WriteInterfaceToken(GetDescriptor()) ||
44 !data.WriteString(bundleName) ||
45 !data.WriteInt32(userId)) {
46 return ERR_PARCEL_WRITE_FALIED;
47 }
48 Remote() -> SendRequest(
49 static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::IS_BUNDLE_IDLE), data, reply, option);
50 isBundleIdle = reply.ReadInt32();
51 return reply.ReadInt32();
52 }
53
QueryBundleStatsInfoByInterval(std::vector<BundleActivePackageStats> & PackageStats,const int32_t intervalType,const int64_t beginTime,const int64_t endTime,int32_t userId)54 ErrCode BundleActiveProxy::QueryBundleStatsInfoByInterval(std::vector<BundleActivePackageStats>& PackageStats,
55 const int32_t intervalType, const int64_t beginTime, const int64_t endTime, int32_t userId)
56 {
57 MessageParcel data;
58 MessageParcel reply;
59 MessageOption option;
60 if (!data.WriteInterfaceToken(GetDescriptor())) {
61 return ERR_PARCEL_WRITE_FALIED;
62 }
63 data.WriteInt32(intervalType);
64 data.WriteInt64(beginTime);
65 data.WriteInt64(endTime);
66 data.WriteInt32(userId);
67 Remote() -> SendRequest(static_cast<uint32_t>(
68 IBundleActiveServiceInterfaceCode::QUERY_BUNDLE_STATS_INFO_BY_INTERVAL), data, reply, option);
69 ErrCode errCode = reply.ReadInt32();
70 if (errCode == ERR_QUERY_RESULT_TOO_LARGE) {
71 return errCode;
72 }
73 int32_t size = reply.ReadInt32();
74 std::shared_ptr<BundleActivePackageStats> tmp;
75 for (int32_t i = 0; i < size; i++) {
76 tmp = tmp->UnMarshalling(reply);
77 if (tmp == nullptr) {
78 continue;
79 }
80 PackageStats.push_back(*tmp);
81 }
82 for (uint32_t i = 0; i < PackageStats.size(); i++) {
83 BUNDLE_ACTIVE_LOGD("QueryBundleStatsInfoByInterval PackageStats idx is %{public}d, bundleName_ is %{public}s, "
84 "lastTimeUsed_ is %{public}lld, lastContiniousTaskUsed_ is %{public}lld, "
85 "totalInFrontTime_ is %{public}lld, totalContiniousTaskUsedTime_ is %{public}lld",
86 i + 1, PackageStats[i].bundleName_.c_str(),
87 (long long)PackageStats[i].lastTimeUsed_, (long long)PackageStats[i].lastContiniousTaskUsed_,
88 (long long)PackageStats[i].totalInFrontTime_, (long long)PackageStats[i].totalContiniousTaskUsedTime_);
89 }
90 return errCode;
91 }
92
QueryBundleEvents(std::vector<BundleActiveEvent> & bundleActiveEvents,const int64_t beginTime,const int64_t endTime,int32_t userId)93 ErrCode BundleActiveProxy::QueryBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvents,
94 const int64_t beginTime, const int64_t endTime, int32_t userId)
95 {
96 MessageParcel data;
97 MessageParcel reply;
98 MessageOption option;
99 if (!data.WriteInterfaceToken(GetDescriptor())) {
100 return ERR_PARCEL_WRITE_FALIED;
101 }
102 data.WriteInt64(beginTime);
103 data.WriteInt64(endTime);
104 data.WriteInt32(userId);
105 Remote() -> SendRequest(static_cast<uint32_t>(
106 IBundleActiveServiceInterfaceCode::QUERY_BUNDLE_EVENTS), data, reply, option);
107 ErrCode errCode = reply.ReadInt32();
108 if (errCode == ERR_QUERY_RESULT_TOO_LARGE) {
109 return errCode;
110 }
111 int32_t size = reply.ReadInt32();
112 std::shared_ptr<BundleActiveEvent> tmp;
113 for (int32_t i = 0; i < size; i++) {
114 tmp = tmp->UnMarshalling(reply);
115 if (tmp == nullptr) {
116 continue;
117 }
118 bundleActiveEvents.push_back(*tmp);
119 }
120 for (uint32_t i = 0; i < bundleActiveEvents.size(); i++) {
121 bundleActiveEvents[i].PrintEvent(true);
122 }
123 return errCode;
124 }
125
SetAppGroup(const std::string & bundleName,int32_t newGroup,int32_t userId)126 ErrCode BundleActiveProxy::SetAppGroup(const std::string& bundleName, int32_t newGroup, int32_t userId)
127 {
128 MessageParcel data;
129 MessageParcel reply;
130 MessageOption option;
131 if (!data.WriteInterfaceToken(GetDescriptor())) {
132 return ERR_PARCEL_WRITE_FALIED;
133 }
134 data.WriteString(bundleName);
135 data.WriteInt32(newGroup);
136 data.WriteInt32(userId);
137
138 Remote() -> SendRequest(static_cast<uint32_t>(
139 IBundleActiveServiceInterfaceCode::SET_APP_GROUP), data, reply, option);
140 return reply.ReadInt32();
141 }
142
QueryBundleStatsInfos(std::vector<BundleActivePackageStats> & bundleActivePackageStats,const int32_t intervalType,const int64_t beginTime,const int64_t endTime)143 ErrCode BundleActiveProxy::QueryBundleStatsInfos(std::vector<BundleActivePackageStats>& bundleActivePackageStats,
144 const int32_t intervalType, const int64_t beginTime, const int64_t endTime)
145 {
146 MessageParcel data;
147 MessageParcel reply;
148 MessageOption option;
149 if (!data.WriteInterfaceToken(GetDescriptor())) {
150 return ERR_PARCEL_WRITE_FALIED;
151 }
152 data.WriteInt32(intervalType);
153 data.WriteInt64(beginTime);
154 data.WriteInt64(endTime);
155 Remote() -> SendRequest(static_cast<uint32_t>(
156 IBundleActiveServiceInterfaceCode::QUERY_BUNDLE_STATS_INFOS), data, reply, option);
157 ErrCode errCode = reply.ReadInt32();
158 if (errCode == ERR_QUERY_RESULT_TOO_LARGE) {
159 return errCode;
160 }
161 int32_t size = reply.ReadInt32();
162 std::shared_ptr<BundleActivePackageStats> tmp;
163 for (int32_t i = 0; i < size; i++) {
164 tmp = tmp->UnMarshalling(reply);
165 if (tmp == nullptr) {
166 continue;
167 }
168 bundleActivePackageStats.push_back(*tmp);
169 }
170 for (uint32_t i = 0; i < bundleActivePackageStats.size(); i++) {
171 BUNDLE_ACTIVE_LOGD("bundleActivePackageStats idx is %{public}d, bundleName_ is %{public}s, "
172 "lastTimeUsed_ is %{public}lld, lastContiniousTaskUsed_ is %{public}lld, "
173 "totalInFrontTime_ is %{public}lld, totalContiniousTaskUsedTime_ is %{public}lld",
174 i + 1, bundleActivePackageStats[i].bundleName_.c_str(),
175 (long long)bundleActivePackageStats[i].lastTimeUsed_,
176 (long long)bundleActivePackageStats[i].lastContiniousTaskUsed_,
177 (long long)bundleActivePackageStats[i].totalInFrontTime_,
178 (long long)bundleActivePackageStats[i].totalContiniousTaskUsedTime_);
179 }
180 return errCode;
181 }
182
QueryCurrentBundleEvents(std::vector<BundleActiveEvent> & bundleActiveEvents,const int64_t beginTime,const int64_t endTime)183 ErrCode BundleActiveProxy::QueryCurrentBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvents,
184 const int64_t beginTime, const int64_t endTime)
185 {
186 MessageParcel data;
187 MessageParcel reply;
188 MessageOption option;
189 if (!data.WriteInterfaceToken(GetDescriptor())) {
190 return ERR_PARCEL_WRITE_FALIED;
191 }
192 data.WriteInt64(beginTime);
193 data.WriteInt64(endTime);
194 Remote() -> SendRequest(static_cast<uint32_t>(
195 IBundleActiveServiceInterfaceCode::QUERY_CURRENT_BUNDLE_EVENTS), data, reply, option);
196 ErrCode errCode = reply.ReadInt32();
197 if (errCode == ERR_QUERY_RESULT_TOO_LARGE) {
198 return errCode;
199 }
200 int32_t size = reply.ReadInt32();
201 std::shared_ptr<BundleActiveEvent> tmp;
202 for (int32_t i = 0; i < size; i++) {
203 tmp = tmp->UnMarshalling(reply);
204 if (tmp == nullptr) {
205 continue;
206 }
207 bundleActiveEvents.push_back(*tmp);
208 }
209 for (uint32_t i = 0; i < bundleActiveEvents.size(); i++) {
210 BUNDLE_ACTIVE_LOGD("QueryCurrentBundleEvents event id is %{public}d, bundle name is %{public}s,"
211 "time stamp is %{public}lld", bundleActiveEvents[i].eventId_, bundleActiveEvents[i].bundleName_.c_str(),
212 (long long)bundleActiveEvents[i].timeStamp_);
213 }
214 return errCode;
215 }
216
QueryAppGroup(int32_t & appGroup,std::string & bundleName,const int32_t userId)217 ErrCode BundleActiveProxy::QueryAppGroup(int32_t& appGroup, std::string& bundleName, const int32_t userId)
218 {
219 MessageParcel data;
220 MessageParcel reply;
221 MessageOption option;
222
223 if (!data.WriteInterfaceToken(GetDescriptor())) {
224 return ERR_PARCEL_WRITE_FALIED;
225 }
226
227 data.WriteString(bundleName);
228 data.WriteInt32(userId);
229 Remote() -> SendRequest(static_cast<uint32_t>(
230 IBundleActiveServiceInterfaceCode::QUERY_APP_GROUP), data, reply, option);
231 appGroup = reply.ReadInt32();
232 return reply.ReadInt32();
233 }
234
QueryModuleUsageRecords(int32_t maxNum,std::vector<BundleActiveModuleRecord> & results,int32_t userId)235 ErrCode BundleActiveProxy::QueryModuleUsageRecords(int32_t maxNum, std::vector<BundleActiveModuleRecord>& results,
236 int32_t userId)
237 {
238 MessageParcel data;
239 MessageParcel reply;
240 MessageOption option;
241 if (!data.WriteInterfaceToken(GetDescriptor())) {
242 return ERR_PARCEL_WRITE_FALIED;
243 }
244 data.WriteInt32(maxNum);
245 data.WriteInt32(userId);
246 Remote() -> SendRequest(static_cast<uint32_t>(
247 IBundleActiveServiceInterfaceCode::QUERY_MODULE_USAGE_RECORDS), data, reply, option);
248 ErrCode errCode = reply.ReadInt32();
249 if (errCode == ERR_QUERY_RESULT_TOO_LARGE) {
250 return errCode;
251 }
252 int32_t size = reply.ReadInt32();
253 std::shared_ptr<BundleActiveModuleRecord> tmp;
254 for (int32_t i = 0; i < size; i++) {
255 tmp = tmp->UnMarshalling(reply);
256 if (tmp == nullptr) {
257 continue;
258 }
259 results.emplace_back(*tmp);
260 }
261 for (const auto& oneModule : results) {
262 BUNDLE_ACTIVE_LOGD("bundle name is %{public}s, module name is %{public}s, "
263 "lastusedtime is %{public}lld, launchcount is %{public}d", oneModule.bundleName_.c_str(),
264 oneModule.moduleName_.c_str(), (long long)oneModule.lastModuleUsedTime_, oneModule.launchedCount_);
265 for (const auto& oneForm : oneModule.formRecords_) {
266 BUNDLE_ACTIVE_LOGD("form name is %{public}s, form dimension is %{public}d, form id is %{public}lld, "
267 "lasttouchtime is %{public}lld, touchcount is %{public}d", oneForm.formName_.c_str(),
268 oneForm.formDimension_, (long long)oneForm.formId_,
269 (long long)oneForm.formLastUsedTime_, oneForm.count_);
270 }
271 }
272 return errCode;
273 }
274
RegisterAppGroupCallBack(const sptr<IAppGroupCallback> & observer)275 ErrCode BundleActiveProxy::RegisterAppGroupCallBack(const sptr<IAppGroupCallback> &observer)
276 {
277 if (!observer) {
278 BUNDLE_ACTIVE_LOGE("RegisterAppGroupCallBack observer is nullptr");
279 return ERR_MEMORY_OPERATION_FAILED;
280 }
281 MessageParcel data;
282 MessageParcel reply;
283 MessageOption option;
284 if (!data.WriteInterfaceToken(GetDescriptor())) {
285 BUNDLE_ACTIVE_LOGE("RegisterAppGroupCallBack WriteInterfaceToken fail");
286 return ERR_PARCEL_WRITE_FALIED;
287 }
288 if (!data.WriteRemoteObject(observer->AsObject())) {
289 BUNDLE_ACTIVE_LOGE("RegisterAppGroupCallBack observer write failed.");
290 return ERR_PARCEL_WRITE_FALIED;
291 }
292 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(
293 IBundleActiveServiceInterfaceCode::REGISTER_APP_GROUP_CALLBACK), data, reply, option);
294 if (ret!= ERR_OK) {
295 BUNDLE_ACTIVE_LOGE("RegisterAppGroupCallBack SendRequest failed, error code: %{public}d", ret);
296 }
297 return reply.ReadInt32();
298 }
299
UnRegisterAppGroupCallBack(const sptr<IAppGroupCallback> & observer)300 ErrCode BundleActiveProxy::UnRegisterAppGroupCallBack(const sptr<IAppGroupCallback> &observer)
301 {
302 if (!observer) {
303 BUNDLE_ACTIVE_LOGE("UnRegisterAppGroupCallBack observer is nullptr");
304 return ERR_MEMORY_OPERATION_FAILED;
305 }
306 MessageParcel data;
307 MessageParcel reply;
308 MessageOption option;
309 if (!data.WriteInterfaceToken(GetDescriptor())) {
310 return ERR_PARCEL_WRITE_FALIED;
311 }
312 if (!data.WriteRemoteObject(observer->AsObject())) {
313 BUNDLE_ACTIVE_LOGE("UnRegisterAppGroupCallBack observer write failed.");
314 return ERR_PARCEL_WRITE_FALIED;
315 }
316 Remote()->SendRequest(static_cast<uint32_t>(
317 IBundleActiveServiceInterfaceCode::UNREGISTER_APP_GROUP_CALLBACK), data, reply, option);
318 return reply.ReadInt32();
319 }
320
QueryDeviceEventStats(int64_t beginTime,int64_t endTime,std::vector<BundleActiveEventStats> & eventStats,int32_t userId)321 ErrCode BundleActiveProxy::QueryDeviceEventStats(int64_t beginTime, int64_t endTime,
322 std::vector<BundleActiveEventStats>& eventStats, int32_t userId)
323 {
324 ErrCode errCode = IPCCommunication(beginTime, endTime, eventStats, userId, static_cast<uint32_t>(
325 IBundleActiveServiceInterfaceCode::QUERY_DEVICE_EVENT_STATES));
326 for (const auto& singleEvent : eventStats) {
327 BUNDLE_ACTIVE_LOGD("QueryDeviceEventStats name is %{public}s, eventId is %{public}d, count is %{public}d",
328 singleEvent.name_.c_str(), singleEvent.eventId_, singleEvent.count_);
329 }
330 return errCode;
331 }
332
QueryNotificationEventStats(int64_t beginTime,int64_t endTime,std::vector<BundleActiveEventStats> & eventStats,int32_t userId)333 ErrCode BundleActiveProxy::QueryNotificationEventStats(int64_t beginTime, int64_t endTime,
334 std::vector<BundleActiveEventStats>& eventStats, int32_t userId)
335 {
336 ErrCode errCode = IPCCommunication(beginTime, endTime, eventStats, userId, static_cast<uint32_t>(
337 IBundleActiveServiceInterfaceCode::QUERY_NOTIFICATION_NUMBER));
338 for (const auto& singleEvent : eventStats) {
339 BUNDLE_ACTIVE_LOGD("QueryNotificationEventStats name is %{public}s, eventId is %{public}d, count is %{public}d",
340 singleEvent.name_.c_str(), singleEvent.eventId_, singleEvent.count_);
341 }
342 return errCode;
343 }
344
IPCCommunication(int64_t beginTime,int64_t endTime,std::vector<BundleActiveEventStats> & eventStats,int32_t userId,int32_t communicationFlag)345 ErrCode BundleActiveProxy::IPCCommunication(int64_t beginTime, int64_t endTime,
346 std::vector<BundleActiveEventStats>& eventStats, int32_t userId, int32_t communicationFlag)
347 {
348 MessageParcel data;
349 MessageParcel reply;
350 MessageOption option;
351 if (!data.WriteInterfaceToken(GetDescriptor())) {
352 return ERR_PARCEL_WRITE_FALIED;
353 }
354 data.WriteInt64(beginTime);
355 data.WriteInt64(endTime);
356 data.WriteInt32(userId);
357 Remote() -> SendRequest(communicationFlag, data, reply, option);
358 ErrCode errCode = reply.ReadInt32();
359 if (errCode == ERR_QUERY_RESULT_TOO_LARGE) {
360 return errCode;
361 }
362 int32_t size = reply.ReadInt32();
363 std::shared_ptr<BundleActiveEventStats> tmp;
364 for (int32_t i = 0; i < size; i++) {
365 tmp = tmp->UnMarshalling(reply);
366 if (!tmp) {
367 continue;
368 }
369 eventStats.emplace_back(*tmp);
370 }
371 return errCode;
372 }
373 } // namespace DeviceUsageStats
374 } // namespace OHOS
375
376