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