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_package_stats.h"
17 #include "bundle_active_event.h"
18
19 namespace OHOS {
20 namespace DeviceUsageStats {
BundleActivePackageStats()21 BundleActivePackageStats::BundleActivePackageStats()
22 {
23 bundleName_.clear();
24 beginTimeStamp_ = 0; // start time of counting
25 endTimeStamp_ = 0; // stop time of counting
26 lastTimeUsed_ = -1; // the timestamp of last launch
27 totalInFrontTime_ = 0; // the total time of bundle in front.
28 lastContiniousTaskUsed_ = -1; // the timestamp of bundle calling a continuous task.
29 totalContiniousTaskUsedTime_ = 0; // the total time of bundle use continuous tasks.
30 startCount_ = 0;
31 bundleStartedCount_ = 0;
32 lastEvent_ = 0;
33 userId_ = 0;
34 uid_ = 0;
35 appIndex_ = 0;
36 }
37
BundleActivePackageStats(const BundleActivePackageStats & orig)38 BundleActivePackageStats::BundleActivePackageStats (const BundleActivePackageStats& orig)
39 {
40 bundleName_ = orig.bundleName_;
41 beginTimeStamp_ = orig.beginTimeStamp_;
42 endTimeStamp_ = orig.endTimeStamp_;
43 lastTimeUsed_ = orig.lastTimeUsed_;
44 lastContiniousTaskUsed_ = orig.lastContiniousTaskUsed_;
45 totalContiniousTaskUsedTime_ = orig.totalContiniousTaskUsedTime_;
46 totalInFrontTime_ = orig.totalInFrontTime_;
47 startCount_ = orig.startCount_;
48 bundleStartedCount_ = orig.bundleStartedCount_;
49 abilities_ = orig.abilities_;
50 longTimeTasks_ = orig.longTimeTasks_;
51 lastEvent_ = orig.lastEvent_;
52 userId_ = orig.userId_;
53 uid_ = orig.uid_;
54 appIndex_ = orig.appIndex_;
55 }
56
HasFrontAbility()57 bool BundleActivePackageStats::HasFrontAbility()
58 {
59 for (auto ability : abilities_) {
60 if (ability.second == BundleActiveEvent::ABILITY_FOREGROUND) {
61 return true;
62 }
63 }
64 return false;
65 }
66
AnyLongTimeTaskStarted()67 bool BundleActivePackageStats::AnyLongTimeTaskStarted()
68 {
69 return !longTimeTasks_.empty();
70 }
71
IncrementTimeUsed(const int64_t timeStamp)72 void BundleActivePackageStats::IncrementTimeUsed(const int64_t timeStamp)
73 {
74 if (timeStamp > lastTimeUsed_) {
75 totalInFrontTime_ += timeStamp - lastTimeUsed_;
76 lastTimeUsed_ = timeStamp;
77 }
78 }
79
IncrementServiceTimeUsed(const int64_t timeStamp)80 void BundleActivePackageStats::IncrementServiceTimeUsed(const int64_t timeStamp)
81 {
82 if (timeStamp > lastContiniousTaskUsed_) {
83 totalContiniousTaskUsedTime_ += timeStamp - lastContiniousTaskUsed_;
84 lastContiniousTaskUsed_ = timeStamp;
85 }
86 }
87
IncrementBundleLaunchedCount()88 void BundleActivePackageStats::IncrementBundleLaunchedCount()
89 {
90 bundleStartedCount_ += 1;
91 }
92
UpdateAbility(const int64_t timeStamp,const int32_t eventId,const std::string & abilityId)93 void BundleActivePackageStats::UpdateAbility(const int64_t timeStamp, const int32_t eventId,
94 const std::string& abilityId)
95 {
96 if (eventId != BundleActiveEvent::ABILITY_FOREGROUND && eventId != BundleActiveEvent::ABILITY_BACKGROUND &&
97 eventId != BundleActiveEvent::ABILITY_STOP) {
98 return;
99 }
100 if (abilities_.empty() && eventId == BundleActiveEvent::ABILITY_FOREGROUND) {
101 beginTimeStamp_ = timeStamp;
102 startCount_ += 1;
103 }
104 std::map<std::string, int>::iterator it = abilities_.find(abilityId);
105 if (it != abilities_.end()) {
106 int32_t lastEventId = it->second;
107 // When we receive a new event, first update the time stats according to the last event in map.
108 switch (lastEventId) {
109 case BundleActiveEvent::ABILITY_FOREGROUND:
110 IncrementTimeUsed(timeStamp);
111 break;
112 case BundleActiveEvent::ABILITY_BACKGROUND:
113 if (eventId == BundleActiveEvent::ABILITY_FOREGROUND) {
114 startCount_ += 1;
115 }
116 break;
117 default:
118 break;
119 }
120 }
121
122 switch (eventId) {
123 case BundleActiveEvent::ABILITY_FOREGROUND:
124 if (!HasFrontAbility()) {
125 lastTimeUsed_ = timeStamp;
126 }
127 abilities_[abilityId] = eventId;
128 break;
129 case BundleActiveEvent::ABILITY_BACKGROUND:
130 abilities_[abilityId] = eventId;
131 break;
132 case BundleActiveEvent::ABILITY_STOP:
133 abilities_.erase(abilityId);
134 break;
135 default:
136 break;
137 }
138 }
139
UpdateLongTimeTask(const std::string & longTimeTaskName,const int64_t timeStamp,const int32_t eventId)140 void BundleActivePackageStats::UpdateLongTimeTask(const std::string& longTimeTaskName, const int64_t timeStamp,
141 const int32_t eventId)
142 {
143 if (eventId != BundleActiveEvent::LONG_TIME_TASK_STARTTED && eventId != BundleActiveEvent::LONG_TIME_TASK_ENDED) {
144 return;
145 }
146
147 // When we receive a new event, first update the time stats according to the last service event in map.
148 std::map<std::string, int>::iterator it = longTimeTasks_.find(longTimeTaskName);
149 if (it != longTimeTasks_.end()) {
150 int32_t lastEventId = it->second;
151 switch (lastEventId) {
152 case BundleActiveEvent::LONG_TIME_TASK_STARTTED:
153 IncrementServiceTimeUsed(timeStamp);
154 break;
155 default:
156 break;
157 }
158 }
159
160 switch (eventId) {
161 case BundleActiveEvent::LONG_TIME_TASK_STARTTED:
162 if (!AnyLongTimeTaskStarted()) {
163 lastContiniousTaskUsed_ = timeStamp;
164 }
165 longTimeTasks_[longTimeTaskName] = eventId;
166 break;
167 case BundleActiveEvent::LONG_TIME_TASK_ENDED:
168 longTimeTasks_.erase(longTimeTaskName);
169 break;
170 default:
171 break;
172 }
173 }
174
Update(const std::string & longTimeTaskName,const int64_t timeStamp,const int32_t eventId,const std::string & abilityId,const int32_t uid)175 void BundleActivePackageStats::Update(const std::string& longTimeTaskName, const int64_t timeStamp,
176 const int32_t eventId, const std::string& abilityId, const int32_t uid)
177 {
178 switch (eventId) {
179 case BundleActiveEvent::ABILITY_FOREGROUND:
180 case BundleActiveEvent::ABILITY_BACKGROUND:
181 case BundleActiveEvent::ABILITY_STOP:
182 UpdateAbility(timeStamp, eventId, abilityId);
183 break;
184 case BundleActiveEvent::END_OF_THE_DAY:
185 if (HasFrontAbility()) {
186 IncrementTimeUsed(timeStamp);
187 }
188 if (AnyLongTimeTaskStarted()) {
189 IncrementServiceTimeUsed(timeStamp);
190 }
191 break;
192 case BundleActiveEvent::LONG_TIME_TASK_STARTTED:
193 case BundleActiveEvent::LONG_TIME_TASK_ENDED:
194 UpdateLongTimeTask(longTimeTaskName, timeStamp, eventId);
195 break;
196 case BundleActiveEvent::SHUTDOWN:
197 case BundleActiveEvent::FLUSH:
198 if (HasFrontAbility()) {
199 IncrementTimeUsed(timeStamp);
200 }
201 if (AnyLongTimeTaskStarted()) {
202 IncrementServiceTimeUsed(timeStamp);
203 }
204 break;
205 default:
206 break;
207 }
208 endTimeStamp_ = timeStamp;
209 }
210
Marshalling(Parcel & parcel) const211 bool BundleActivePackageStats::Marshalling(Parcel &parcel) const
212 {
213 if (parcel.WriteString(bundleName_) &&
214 parcel.WriteInt64(beginTimeStamp_) &&
215 parcel.WriteInt64(lastTimeUsed_) &&
216 parcel.WriteInt64(totalInFrontTime_) &&
217 parcel.WriteInt64(lastContiniousTaskUsed_) &&
218 parcel.WriteInt64(totalContiniousTaskUsedTime_) &&
219 parcel.WriteInt32(startCount_) &&
220 parcel.WriteInt32(userId_) &&
221 parcel.WriteInt32(appIndex_)) {
222 return true;
223 }
224 return false;
225 }
226
Unmarshalling(Parcel & parcel)227 BundleActivePackageStats *BundleActivePackageStats::Unmarshalling(Parcel &parcel)
228 {
229 BundleActivePackageStats *result = new (std::nothrow) BundleActivePackageStats();
230 if (result == nullptr) {
231 return nullptr;
232 }
233 result->bundleName_ = parcel.ReadString();
234 result->beginTimeStamp_ = parcel.ReadInt64();
235 result->lastTimeUsed_ = parcel.ReadInt64();
236 result->totalInFrontTime_ = parcel.ReadInt64();
237 result->lastContiniousTaskUsed_ = parcel.ReadInt64();
238 result->totalContiniousTaskUsedTime_ = parcel.ReadInt64();
239 result->startCount_ = parcel.ReadInt32();
240 result->userId_ = parcel.ReadInt32();
241 result->appIndex_ = parcel.ReadInt32();
242 return result;
243 }
244
ToString()245 std::string BundleActivePackageStats::ToString()
246 {
247 return "bundle name is " + this->bundleName_ +
248 ", uid is " + std::to_string(this->uid_) +
249 ", last used time stamp is " + std::to_string(this->lastTimeUsed_) +
250 ", total time in front is " + std::to_string(this->totalInFrontTime_) +
251 ", last continuous task used time is " + std::to_string(this->lastContiniousTaskUsed_) +
252 ", total continuous task time is " +
253 std::to_string(this->totalContiniousTaskUsedTime_) +
254 ", start count is " + std::to_string(this->startCount_) +"\n";
255 }
256 } // namespace DeviceUsageStats
257 } // namespace OHOS
258
259