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_period_stats.h"
17 #include "bundle_active_event.h"
18
19 namespace OHOS {
20 namespace DeviceUsageStats {
BundleActivePeriodStats()21 BundleActivePeriodStats::BundleActivePeriodStats()
22 {
23 userId_ = -1;
24 beginTime_ = 0;
25 endTime_ = 0;
26 lastTimeSaved_ = 0;
27 bundleStats_.clear();
28 events_.events_.clear();
29 packetNamesCache_.clear();
30 }
31
BundleActivePeriodStats(int32_t userId,int64_t beginTime)32 BundleActivePeriodStats::BundleActivePeriodStats(int32_t userId, int64_t beginTime)
33 {
34 userId_ = userId;
35 beginTime_ = beginTime;
36 endTime_ = 0;
37 lastTimeSaved_ = 0;
38 bundleStats_.clear();
39 events_.events_.clear();
40 packetNamesCache_.clear();
41 }
42
GetOrCreateUsageStats(const std::string & bundleName)43 std::shared_ptr<BundleActivePackageStats> BundleActivePeriodStats::GetOrCreateUsageStats(
44 const std::string& bundleName)
45 {
46 std::map<std::string, std::shared_ptr<BundleActivePackageStats>>::iterator it = bundleStats_.find(bundleName);
47 if (it == bundleStats_.end()) {
48 std::shared_ptr<BundleActivePackageStats> insertedStats = std::make_shared<BundleActivePackageStats>();
49 insertedStats->endTimeStamp_ = endTime_;
50 insertedStats->bundleName_ = GetCachedString(bundleName);
51 bundleStats_[insertedStats->bundleName_] = insertedStats;
52 }
53 return bundleStats_[bundleName];
54 }
55
Update(const std::string bundleName,const std::string longTimeTaskName,const int64_t timeStamp,const int32_t eventId,const std::string abilityId)56 void BundleActivePeriodStats::Update(const std::string bundleName, const std::string longTimeTaskName,
57 const int64_t timeStamp, const int32_t eventId, const std::string abilityId)
58 {
59 if (eventId == BundleActiveEvent::SHUTDOWN || eventId == BundleActiveEvent::FLUSH) {
60 for (std::map<std::string, std::shared_ptr<BundleActivePackageStats>>::iterator it = bundleStats_.begin();
61 it != bundleStats_.end(); ++it) {
62 std::shared_ptr<BundleActivePackageStats> tmpUsageStats = it->second;
63 if (tmpUsageStats != nullptr) {
64 tmpUsageStats->Update("", timeStamp, eventId, abilityId);
65 }
66 }
67 } else if (BundleActiveEvent::IsBundleEvent(eventId)) {
68 auto usageStats = GetOrCreateUsageStats(bundleName);
69 usageStats->Update(longTimeTaskName, timeStamp, eventId, abilityId);
70 }
71 if (timeStamp > endTime_) {
72 endTime_ = timeStamp;
73 }
74 }
75
AddEvent(BundleActiveEvent event)76 void BundleActivePeriodStats::AddEvent(BundleActiveEvent event)
77 {
78 event.bundleName_ = GetCachedString(event.bundleName_);
79 if (!event.continuousTaskAbilityName_.empty()) {
80 event.continuousTaskAbilityName_ = GetCachedString(event.continuousTaskAbilityName_);
81 }
82 events_.Insert(event);
83 if (event.timeStamp_ > endTime_) {
84 endTime_ = event.timeStamp_;
85 }
86 }
87
CommitTime(const int64_t timeStamp)88 void BundleActivePeriodStats::CommitTime(const int64_t timeStamp)
89 {
90 interactiveTracker_.CommitTime(timeStamp);
91 noninteractiveTracker_.CommitTime(timeStamp);
92 keyguardShownTracker_.CommitTime(timeStamp);
93 keyguardHiddenTracker_.CommitTime(timeStamp);
94 }
95
UpdateScreenInteractive(const int64_t timeStamp)96 void BundleActivePeriodStats::UpdateScreenInteractive(const int64_t timeStamp)
97 {
98 interactiveTracker_.Update(timeStamp);
99 noninteractiveTracker_.CommitTime(timeStamp);
100 }
101
UpdateScreenNonInteractive(const int64_t timeStamp)102 void BundleActivePeriodStats::UpdateScreenNonInteractive(const int64_t timeStamp)
103 {
104 noninteractiveTracker_.Update(timeStamp);
105 interactiveTracker_.CommitTime(timeStamp);
106 }
107
UpdateKeyguardShown(const int64_t timeStamp)108 void BundleActivePeriodStats::UpdateKeyguardShown(const int64_t timeStamp)
109 {
110 keyguardShownTracker_.Update(timeStamp);
111 keyguardHiddenTracker_.CommitTime(timeStamp);
112 }
113
UpdateKeyguardHidden(const int64_t timeStamp)114 void BundleActivePeriodStats::UpdateKeyguardHidden(const int64_t timeStamp)
115 {
116 keyguardHiddenTracker_.Update(timeStamp);
117 keyguardShownTracker_.CommitTime(timeStamp);
118 }
119
AddEventStatsTo(std::vector<BundleActiveEventStats> & eventStatsList)120 void BundleActivePeriodStats::AddEventStatsTo(std::vector<BundleActiveEventStats>& eventStatsList)
121 {
122 interactiveTracker_.AddToEventStats(
123 eventStatsList, BundleActiveEvent::SCREEN_INTERACTIVE, beginTime_, endTime_);
124 noninteractiveTracker_.AddToEventStats(
125 eventStatsList, BundleActiveEvent::SCREEN_NON_INTERACTIVE, beginTime_, endTime_);
126 keyguardShownTracker_.AddToEventStats(
127 eventStatsList, BundleActiveEvent::KEYGUARD_SHOWN, beginTime_, endTime_);
128 keyguardHiddenTracker_.AddToEventStats(
129 eventStatsList, BundleActiveEvent::KEYGUARD_HIDDEN, beginTime_, endTime_);
130 }
131
GetCachedString(std::string str)132 std::string BundleActivePeriodStats::GetCachedString(std::string str)
133 {
134 std::set<std::string>::iterator it = packetNamesCache_.find(str);
135 if (it == packetNamesCache_.end()) {
136 packetNamesCache_.insert(str);
137 return str;
138 }
139 return *it;
140 }
141 } // namespace DeviceUsageStats
142 } // namespace OHOS
143
144