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