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