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