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