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     std::map<std::string, std::set<int32_t>> packageContainUid_;
44     BundleActiveEventList events_;
45     std::set<std::string> packetNamesCache_;
46     BundleActiveEventTracker interactiveTracker_;
47     BundleActiveEventTracker noninteractiveTracker_;
48     BundleActiveEventTracker keyguardShownTracker_;
49     BundleActiveEventTracker keyguardHiddenTracker_;
50     /*
51     * function: BundleActivePeriodStats,default constructor.
52     */
53     BundleActivePeriodStats();
54 
55     /*
56     * function: constructor, provided userId, beginTime.
57     * parameters: userid, beginTime.
58     */
59     BundleActivePeriodStats(int32_t userId, int64_t beginTime);
60 
61     /*
62     * function: GetOrCreateUsageStats, get or create bundle usage statistics object of a bundle.
63     * parameters: bundleName
64     * return: point to bundle usage statistics object.
65     */
66     std::shared_ptr<BundleActivePackageStats> GetOrCreateUsageStats(const std::string& bundleName, const int32_t uid);
67     /*
68     * function: Update, update usage statistics of specific bundle.
69     * parameters: bundleName, longTimeTaskName, timeStamp, eventId, abilityId
70     */
71     void Update(const std::string bundleName, const std::string longTimeTaskName, const int64_t timeStamp,
72         const int32_t eventId, const std::string abilityId, const int32_t uid);
73     /*
74     * function: AddEvent, add a event to member events_.
75     * parameters: event
76     */
77     void AddEvent(BundleActiveEvent event);
78     /*
79     * function: UpdateScreenInteractive, update screen interactive time.
80     * parameters: timeStamp
81     */
82     void UpdateScreenInteractive(const int64_t timeStamp);
83     /*
84     * function: UpdateScreenNonInteractive, update screen non interactive time.
85     * parameters: timeStamp
86     */
87     void UpdateScreenNonInteractive(const int64_t timeStamp);
88     /*
89     * function: UpdateKeyguardShown, key guard shown time.
90     * parameters: timeStamp
91     */
92     void UpdateKeyguardShown(const int64_t timeStamp);
93     /*
94     * function: UpdateKeyguardHidden, key guard hidden time.
95     * parameters: timeStamp
96     */
97     void UpdateKeyguardHidden(const int64_t timeStamp);
98     /*
99     * function: CommitTime, key guard hidden time.
100     * parameters: timeStamp
101     */
102     void CommitTime(const int64_t timeStamp);
103     /*
104     * function: AddEventStatsTo, add all time to eventStatsList.
105     * parameters: eventStatsList
106     */
107     void AddEventStatsTo(std::vector<BundleActiveEventStats>& eventStatsList);
108     /*
109     * function: GetCachedString, store string to cache.
110     * parameters: str
111     * return: string
112     */
113     std::string GetCachedString(std::string str);
114 private:
115     void updateAllPackageStats(const std::string bundleName, const int64_t timeStamp, const int32_t eventId,
116     const std::string abilityId);
117 };
118 }  // namespace DeviceUsageStats
119 }  // namespace OHOS
120 #endif  // BUNDLE_ACTIVE_PERIOD_STATS_H
121 
122