• 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 #ifndef BUNDLE_ACTIVE_USER_SERVICE_H
17 #define BUNDLE_ACTIVE_USER_SERVICE_H
18 
19 #include <memory>
20 
21 #include "ibundle_active_service.h"
22 #include "bundle_active_event.h"
23 #include "bundle_active_period_stats.h"
24 #include "bundle_active_event_stats.h"
25 #include "bundle_active_calendar.h"
26 #include "bundle_active_stats_combiner.h"
27 #include "bundle_active_usage_database.h"
28 #include "bundle_active_constant.h"
29 #include "bundle_active_module_record.h"
30 
31 namespace OHOS {
32 namespace DeviceUsageStats {
33 class BundleActiveCore;
34 
35 class BundleActiveUserService {
36 public:
37     BundleActiveUserService() = delete;
BundleActiveUserService(const int32_t userId,BundleActiveCore & listener,const bool debug)38     BundleActiveUserService(const int32_t userId, BundleActiveCore& listener, const bool debug):listener_(listener)
39     {
40         for (int32_t i = 0; i < BundleActivePeriodStats::PERIOD_COUNT; i++) {
41             currentStats_.push_back(nullptr);
42         }
43         userId_ = userId;
44         dailyExpiryDate_.SetMilliseconds(0);
45         statsChanged_ = false;
46         if (debug) {
47             dailyExpiryDate_.ChangeToDebug();
48             database_.ChangeToDebug();
49             debugUserService_ = true;
50             periodLength_ = {ONE_DAY_TIME_DEBUG, ONE_WEEK_TIME_DEBUG, ONE_MONTH_TIME_DEBUG, ONE_YEAR_TIME_DEBUG};
51         } else {
52             debugUserService_ = false;
53             periodLength_ = {ONE_DAY_TIME, ONE_WEEK_TIME, ONE_MONTH_TIME, ONE_YEAR_TIME};
54         }
55     }
56     void Init(const int64_t timeStamp);
~BundleActiveUserService()57     ~BundleActiveUserService() {}
58     void ReportForShutdown(const BundleActiveEvent& event);
59     void ReportEvent(const BundleActiveEvent& event);
60     void ReportFormEvent(const BundleActiveEvent& event);
61     void ReportModuleEvent(const BundleActiveEvent& event);
62     void RestoreStats(bool forced);
63     void RenewStatsInMemory(const int64_t timeStamp);
64     void RenewTableTime(int64_t oldTime, int64_t newTime);
65     void OnUserRemoved();
66     void DeleteUninstalledBundleStats(const std::string& bundleName);
67     int32_t userId_;
68     BundleActiveCalendar dailyExpiryDate_;
69     ErrCode QueryBundleStatsInfos(std::vector<BundleActivePackageStats>& PackageStats, int32_t intervalType,
70         const int64_t beginTime, const int64_t endTime, const int32_t userId, const std::string& bundleName);
71     ErrCode QueryBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvent, const int64_t beginTime,
72         const int64_t endTime, const int32_t userId, const std::string& bundleName);
73     int32_t QueryModuleUsageRecords(int32_t maxNum, std::vector<BundleActiveModuleRecord>& results);
74     int32_t QueryDeviceEventStats(int64_t beginTime, int64_t endTime,
75         std::vector<BundleActiveEventStats>& eventStats, int32_t userId);
76     int32_t QueryNotificationEventStats(int64_t beginTime, int64_t endTime,
77         std::vector<BundleActiveEventStats>& eventStats, int32_t userId);
78     void LoadActiveStats(const int64_t timeStamp, const bool& force, const bool& timeChanged);
79     void LoadModuleAndFormStats();
80 
81 private:
82     static const int64_t ONE_SECOND_MILLISECONDS = 1000;
83     BundleActiveUsageDatabase database_;
84     std::vector<std::shared_ptr<BundleActivePeriodStats>> currentStats_;
85     std::map<std::string, std::shared_ptr<BundleActiveModuleRecord>> moduleRecords_;
86     bool statsChanged_;
87     bool debugUserService_;
88     std::string lastForegroundBundle_;
89     BundleActiveCore& listener_;
90     std::vector<int64_t> periodLength_ = {0, 0, 0, 0};
91     void NotifyStatsChanged();
92     void NotifyNewUpdate();
93     std::shared_ptr<BundleActiveModuleRecord> GetOrCreateModuleRecord(const BundleActiveEvent& event);
94     void PrintInMemPackageStats(const int32_t idx, const bool debug);
95     void PrintInMemEventStats(const bool debug);
96     void PrintInMemFormStats(const bool debug, const bool printform);
97     void GetCachedSystemEvents(std::shared_ptr<BundleActivePeriodStats> currentStats, int64_t beginTime,
98         int64_t endTime, std::map<std::string, BundleActiveEventStats>& systemEventStats);
99     void GetCachedNotificationEvents(std::shared_ptr<BundleActivePeriodStats> currentStats, int64_t beginTime,
100         int64_t endTime, std::map<std::string, BundleActiveEventStats>& notificationEventStats);
101 };
102 }  // namespace DeviceUsageStats
103 }  // namespace OHOS
104 #endif  // BUNDLE_ACTIVE_USER_SERVICE_H
105 
106