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 #ifndef BUNDLE_ACTIVE_PERIOD_STATS_H
17 #define BUNDLE_ACTIVE_PERIOD_STATS_H
18 
19 #include <memory>
20 
21 #include "ibundle_active_service.h"
22 #include "bundle_active_event.h"
23 #include "bundle_active_package_stats.h"
24 #include "bundle_active_event_list.h"
25 #include "bundle_active_event_tracker.h"
26 
27 
28 namespace OHOS {
29 namespace DeviceUsageStats {
30 class BundleActivePeriodStats {
31 public:
32     static const int32_t PERIOD_DAILY = 0;
33     static const int32_t PERIOD_WEEKLY = 1;
34     static const int32_t PERIOD_MONTHLY = 2;
35     static const int32_t PERIOD_YEARLY = 3;
36     static const int32_t PERIOD_BEST = 4;
37     static const int32_t PERIOD_COUNT = 4;
38     int64_t beginTime_;
39     int64_t endTime_;
40     int64_t lastTimeSaved_;
41     int32_t userId_;
42     std::map<std::string, std::shared_ptr<BundleActivePackageStats>> bundleStats_;
43     BundleActiveEventList events_;
44     std::set<std::string> packetNamesCache_;
45     BundleActiveEventTracker interactiveTracker_;
46     BundleActiveEventTracker noninteractiveTracker_;
47     BundleActiveEventTracker keyguardShownTracker_;
48     BundleActiveEventTracker keyguardHiddenTracker_;
49     /*
50     * function: BundleActivePeriodStats,default constructor.
51     */
52     BundleActivePeriodStats();
53 
54     /*
55     * function: constructor, provided userId, beginTime.
56     * parameters: userid, beginTime.
57     */
58     BundleActivePeriodStats(int32_t userId, int64_t beginTime);
59 
60     /*
61     * function: GetOrCreateUsageStats, get or create bundle usage statistics object of a bundle.
62     * parameters: bundleName
63     * return: point to bundle usage statistics object.
64     */
65     std::shared_ptr<BundleActivePackageStats> GetOrCreateUsageStats(const std::string& bundleName, const int32_t uid);
66     /*
67     * function: Update, update usage statistics of specific bundle.
68     * parameters: bundleName, longTimeTaskName, timeStamp, eventId, abilityId
69     */
70     void Update(const std::string bundleName, const std::string longTimeTaskName, const int64_t timeStamp,
71         const int32_t eventId, const std::string abilityId, const int32_t uid);
72     /*
73     * function: AddEvent, add a event to member events_.
74     * parameters: event
75     */
76     void AddEvent(BundleActiveEvent event);
77     /*
78     * function: UpdateScreenInteractive, update screen interactive time.
79     * parameters: timeStamp
80     */
81     void UpdateScreenInteractive(const int64_t timeStamp);
82     /*
83     * function: UpdateScreenNonInteractive, update screen non interactive time.
84     * parameters: timeStamp
85     */
86     void UpdateScreenNonInteractive(const int64_t timeStamp);
87     /*
88     * function: UpdateKeyguardShown, key guard shown time.
89     * parameters: timeStamp
90     */
91     void UpdateKeyguardShown(const int64_t timeStamp);
92     /*
93     * function: UpdateKeyguardHidden, key guard hidden time.
94     * parameters: timeStamp
95     */
96     void UpdateKeyguardHidden(const int64_t timeStamp);
97     /*
98     * function: CommitTime, key guard hidden time.
99     * parameters: timeStamp
100     */
101     void CommitTime(const int64_t timeStamp);
102     /*
103     * function: AddEventStatsTo, add all time to eventStatsList.
104     * parameters: eventStatsList
105     */
106     void AddEventStatsTo(std::vector<BundleActiveEventStats>& eventStatsList);
107     /*
108     * function: GetCachedString, store string to cache.
109     * parameters: str
110     * return: string
111     */
112     std::string GetCachedString(std::string str);
113 private:
114     void updateAllPackageStats(const std::string bundleName, const int64_t timeStamp, const int32_t eventId,
115     const std::string abilityId);
116 };
117 }  // namespace DeviceUsageStats
118 }  // namespace OHOS
119 #endif  // BUNDLE_ACTIVE_PERIOD_STATS_H
120 
121