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_stub.h"
17
18 #include "ipc_object_stub.h"
19 #include "iremote_broker.h"
20
21 #include "bundle_active_event.h"
22 #include "bundle_active_event_stats.h"
23 #include "bundle_state_inner_errors.h"
24 #include "bundle_active_log.h"
25 #include "bundle_active_module_record.h"
26 #include "bundle_active_package_stats.h"
27 #include "iapp_group_callback.h"
28
29
30 namespace OHOS {
31 namespace DeviceUsageStats {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel &reply,
33 MessageOption &option)
34 {
35 if (data.ReadInterfaceToken() != GetDescriptor()) {
36 return -1;
37 }
38 switch (code) {
39 case REPORT_EVENT: {
40 int32_t userId = data.ReadInt32();
41 std::shared_ptr<BundleActiveEvent> tmpEvent = BundleActiveEvent::UnMarshalling(data);
42 if (!tmpEvent) {
43 return -1;
44 }
45 int32_t result = ReportEvent(*tmpEvent, userId);
46 return reply.WriteInt32(result);
47 }
48 case IS_BUNDLE_IDLE: {
49 bool isBundleIdle = false;
50 std::string bundleName = data.ReadString();
51 int32_t userId = data.ReadInt32();
52 ErrCode errCode = IsBundleIdle(isBundleIdle, bundleName, userId);
53 reply.WriteInt32(isBundleIdle);
54 return reply.WriteInt32(errCode);
55 }
56 case QUERY_BUNDLE_STATS_INFO_BY_INTERVAL: {
57 std::vector<BundleActivePackageStats> result;
58 int32_t intervalType = data.ReadInt32();
59 BUNDLE_ACTIVE_LOGI("OnRemoteRequest intervaltype is %{public}d", intervalType);
60 int64_t beginTime = data.ReadInt64();
61 int64_t endTime = data.ReadInt64();
62 int32_t userId = data.ReadInt32();
63 ErrCode errCode = QueryBundleStatsInfoByInterval(result, intervalType, beginTime, endTime, userId);
64 reply.WriteInt32(errCode);
65 int32_t size = static_cast<int32_t>(result.size());
66 BUNDLE_ACTIVE_LOGI("OnRemoteRequest result size is %{public}d", size);
67 reply.WriteInt32(size);
68 for (int32_t i = 0; i < size; i++) {
69 bool tmp = result[i].Marshalling(reply);
70 if (tmp == false) {
71 return 1;
72 }
73 }
74 return size == 0;
75 }
76 case QUERY_BUNDLE_EVENTS: {
77 std::vector<BundleActiveEvent> result;
78 int64_t beginTime = data.ReadInt64();
79 int64_t endTime = data.ReadInt64();
80 int32_t userId = data.ReadInt32();
81 ErrCode errCode = QueryBundleEvents(result, beginTime, endTime, userId);
82 int32_t size = static_cast<int32_t>(result.size());
83 reply.WriteInt32(errCode);
84 reply.WriteInt32(size);
85 for (int32_t i = 0; i < size; i++) {
86 bool tmp = result[i].Marshalling(reply);
87 if (tmp == false) {
88 return 1;
89 }
90 }
91 return size == 0;
92 }
93 case SET_APP_GROUP: {
94 std::string bundleName = data.ReadString();
95 int32_t newGroup = data.ReadInt32();
96 int32_t userId = data.ReadInt32();
97 ErrCode errCode = SetAppGroup(bundleName, newGroup, userId);
98 return reply.WriteInt32(errCode);
99 }
100 case QUERY_BUNDLE_STATS_INFOS: {
101 std::vector<BundleActivePackageStats> result;
102 int32_t intervalType = data.ReadInt32();
103 BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_BUNDLE_STATS_INFOS intervaltype is %{public}d", intervalType);
104 int64_t beginTime = data.ReadInt64();
105 int64_t endTime = data.ReadInt64();
106 ErrCode errCode = QueryBundleStatsInfos(result, intervalType, beginTime, endTime);
107 int32_t size = static_cast<int32_t>(result.size());
108 BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_BUNDLE_STATS_INFOS result size is %{public}d", size);
109 reply.WriteInt32(errCode);
110 reply.WriteInt32(size);
111 for (int32_t i = 0; i < size; i++) {
112 bool tmp = result[i].Marshalling(reply);
113 if (tmp == false) {
114 return 1;
115 }
116 }
117 return size == 0;
118 }
119 case QUERY_CURRENT_BUNDLE_EVENTS: {
120 std::vector<BundleActiveEvent> result;
121 int64_t beginTime = data.ReadInt64();
122 int64_t endTime = data.ReadInt64();
123 ErrCode errCode = QueryCurrentBundleEvents(result, beginTime, endTime);
124 int32_t size = static_cast<int32_t>(result.size());
125 reply.WriteInt32(errCode);
126 reply.WriteInt32(size);
127 for (int32_t i = 0; i < size; i++) {
128 bool tmp = result[i].Marshalling(reply);
129 if (tmp == false) {
130 return 1;
131 }
132 }
133 return size == 0;
134 }
135 case QUERY_APP_GROUP: {
136 int32_t appGroup = -1;
137 std::string bundleName = data.ReadString();
138 int32_t userId = data.ReadInt32();
139 ErrCode errCode = QueryAppGroup(appGroup, bundleName, userId);
140 reply.WriteInt32(appGroup);
141 return reply.WriteInt32(errCode);
142 }
143 case QUERY_MODULE_USAGE_RECORDS: {
144 std::vector<BundleActiveModuleRecord> results;
145 int32_t maxNum = data.ReadInt32();
146 int32_t userId = data.ReadInt32();
147 ErrCode errCode = QueryModuleUsageRecords(maxNum, results, userId);
148 int32_t size = static_cast<int32_t>(results.size());
149 reply.WriteInt32(errCode);
150 reply.WriteInt32(size);
151 for (int32_t i = 0; i < size; i++) {
152 bool tmp = results[i].Marshalling(reply);
153 if (tmp == false) {
154 return 1;
155 }
156 }
157 return size == 0;
158 }
159 case REGISTER_APP_GROUP_CALLBACK: {
160 auto observer = iface_cast<IAppGroupCallback>(data.ReadRemoteObject());
161 if (!observer) {
162 BUNDLE_ACTIVE_LOGE("RegisterAppGroupCallBack observer is null, return");
163 return false;
164 }
165 BUNDLE_ACTIVE_LOGI("RegisterAppGroupCallBack observer is ok");
166 ErrCode errCode = RegisterAppGroupCallBack(observer);
167 return reply.WriteInt32(errCode);
168 }
169 case UNREGISTER_APP_GROUP_CALLBACK: {
170 auto observer = iface_cast<IAppGroupCallback>(data.ReadRemoteObject());
171 if (!observer) {
172 BUNDLE_ACTIVE_LOGE("UnRegisterAppGroupCallBack observer is null, return");
173 return false;
174 }
175 ErrCode errCode = UnRegisterAppGroupCallBack(observer);
176 return reply.WriteInt32(errCode);
177 }
178 case QUERY_DEVICE_EVENT_STATES: {
179 std::vector<BundleActiveEventStats> result;
180 int64_t beginTime = data.ReadInt64();
181 int64_t endTime = data.ReadInt64();
182 int32_t userId = data.ReadInt32();
183 ErrCode errCode = QueryDeviceEventStats(beginTime, endTime, result, userId);
184 int32_t size = static_cast<int32_t>(result.size());
185 reply.WriteInt32(errCode);
186 reply.WriteInt32(size);
187 for (int32_t i = 0; i < size; i++) {
188 bool tmp = result[i].Marshalling(reply);
189 if (!tmp) {
190 return 1;
191 }
192 }
193 return size == 0;
194 }
195 case QUERY_NOTIFICATION_NUMBER: {
196 std::vector<BundleActiveEventStats> result;
197 int64_t beginTime = data.ReadInt64();
198 int64_t endTime = data.ReadInt64();
199 int32_t userId = data.ReadInt32();
200 ErrCode errCode = QueryNotificationEventStats(beginTime, endTime, result, userId);
201 int32_t size = static_cast<int32_t>(result.size());
202 reply.WriteInt32(errCode);
203 reply.WriteInt32(size);
204 for (int32_t i = 0; i < size; i++) {
205 bool tmp = result[i].Marshalling(reply);
206 if (!tmp) {
207 return 1;
208 }
209 }
210 return size == 0;
211 }
212 default:
213 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
214 }
215 }
216 } // namespace DeviceUsageStats
217 } // namespace OHOS
218
219