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