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