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 }
97 std::map<std::string, int>::iterator it = abilities_.find(abilityId);
98 if (it != abilities_.end()) {
99 int32_t lastEventId = it->second;
100 // When we receive a new event, first update the time stats according to the last event in map.
101 switch (lastEventId) {
102 case BundleActiveEvent::ABILITY_FOREGROUND:
103 IncrementTimeUsed(timeStamp);
104 break;
105 default:
106 break;
107 }
108 }
109
110 switch (eventId) {
111 case BundleActiveEvent::ABILITY_FOREGROUND:
112 if (!HasFrontAbility()) {
113 lastTimeUsed_ = timeStamp;
114 }
115 abilities_[abilityId] = eventId;
116 break;
117 case BundleActiveEvent::ABILITY_BACKGROUND:
118 abilities_[abilityId] = eventId;
119 break;
120 case BundleActiveEvent::ABILITY_STOP:
121 abilities_.erase(abilityId);
122 break;
123 default:
124 break;
125 }
126 }
127
UpdateLongTimeTask(const std::string & longTimeTaskName,const int64_t timeStamp,const int32_t eventId)128 void BundleActivePackageStats::UpdateLongTimeTask(const std::string& longTimeTaskName, const int64_t timeStamp,
129 const int32_t eventId)
130 {
131 if (eventId != BundleActiveEvent::LONG_TIME_TASK_STARTTED && eventId != BundleActiveEvent::LONG_TIME_TASK_ENDED) {
132 return;
133 }
134
135 // When we receive a new event, first update the time stats according to the last service event in map.
136 std::map<std::string, int>::iterator it = longTimeTasks_.find(longTimeTaskName);
137 if (it != longTimeTasks_.end()) {
138 int32_t lastEventId = it->second;
139 switch (lastEventId) {
140 case BundleActiveEvent::LONG_TIME_TASK_STARTTED:
141 IncrementServiceTimeUsed(timeStamp);
142 break;
143 default:
144 break;
145 }
146 }
147
148 switch (eventId) {
149 case BundleActiveEvent::LONG_TIME_TASK_STARTTED:
150 if (!AnyLongTimeTaskStarted()) {
151 lastContiniousTaskUsed_ = timeStamp;
152 }
153 longTimeTasks_[longTimeTaskName] = eventId;
154 break;
155 case BundleActiveEvent::LONG_TIME_TASK_ENDED:
156 longTimeTasks_.erase(longTimeTaskName);
157 break;
158 default:
159 break;
160 }
161 }
162
Update(const std::string & longTimeTaskName,const int64_t timeStamp,const int32_t eventId,const std::string & abilityId)163 void BundleActivePackageStats::Update(const std::string& longTimeTaskName, const int64_t timeStamp,
164 const int32_t eventId, const std::string& abilityId)
165 {
166 switch (eventId) {
167 case BundleActiveEvent::ABILITY_FOREGROUND:
168 case BundleActiveEvent::ABILITY_BACKGROUND:
169 case BundleActiveEvent::ABILITY_STOP:
170 UpdateAbility(timeStamp, eventId, abilityId);
171 break;
172 case BundleActiveEvent::END_OF_THE_DAY:
173 if (HasFrontAbility()) {
174 IncrementTimeUsed(timeStamp);
175 }
176 if (AnyLongTimeTaskStarted()) {
177 IncrementServiceTimeUsed(timeStamp);
178 }
179 break;
180 case BundleActiveEvent::LONG_TIME_TASK_STARTTED:
181 case BundleActiveEvent::LONG_TIME_TASK_ENDED:
182 UpdateLongTimeTask(longTimeTaskName, timeStamp, eventId);
183 break;
184 case BundleActiveEvent::SHUTDOWN:
185 case BundleActiveEvent::FLUSH:
186 if (HasFrontAbility()) {
187 IncrementTimeUsed(timeStamp);
188 }
189 if (AnyLongTimeTaskStarted()) {
190 IncrementServiceTimeUsed(timeStamp);
191 }
192 break;
193 default:
194 break;
195 }
196 endTimeStamp_ = timeStamp;
197 if (eventId == BundleActiveEvent::ABILITY_FOREGROUND) {
198 startCount_ += 1;
199 }
200 }
201
Marshalling(Parcel & parcel) const202 bool BundleActivePackageStats::Marshalling(Parcel &parcel) const
203 {
204 if (parcel.WriteString(bundleName_) &&
205 parcel.WriteInt64(beginTimeStamp_) &&
206 parcel.WriteInt64(lastTimeUsed_) &&
207 parcel.WriteInt64(totalInFrontTime_) &&
208 parcel.WriteInt64(lastContiniousTaskUsed_) &&
209 parcel.WriteInt64(totalContiniousTaskUsedTime_)) {
210 return true;
211 }
212 return false;
213 }
214
UnMarshalling(Parcel & parcel)215 std::shared_ptr<BundleActivePackageStats> BundleActivePackageStats::UnMarshalling(Parcel &parcel)
216 {
217 std::shared_ptr<BundleActivePackageStats> result = std::make_shared<BundleActivePackageStats>();
218 result->bundleName_ = parcel.ReadString();
219 result->beginTimeStamp_ = parcel.ReadInt64();
220 result->lastTimeUsed_ = parcel.ReadInt64();
221 result->totalInFrontTime_ = parcel.ReadInt64();
222 result->lastContiniousTaskUsed_ = parcel.ReadInt64();
223 result->totalContiniousTaskUsedTime_ = parcel.ReadInt64();
224 return result;
225 }
226
ToString()227 std::string BundleActivePackageStats::ToString()
228 {
229 return "bundle name is " + this->bundleName_ +
230 ", last used time stamp is " + std::to_string(this->lastTimeUsed_) +
231 ", total time in front is " + std::to_string(this->totalInFrontTime_) +
232 ", last continuous task used time is " + std::to_string(this->lastContiniousTaskUsed_) +
233 ", total continuous task time is " +
234 std::to_string(this->totalContiniousTaskUsedTime_) + "\n";
235 }
236 } // namespace DeviceUsageStats
237 } // namespace OHOS
238
239