• 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 #include "ffrt.h"
31 
32 namespace OHOS {
33 namespace DeviceUsageStats {
34 class BundleActiveCore;
35 
36 class BundleActiveUserService {
37 public:
38     BundleActiveUserService() = delete;
BundleActiveUserService(const int32_t userId,BundleActiveCore & listener,const bool debug)39     BundleActiveUserService(const int32_t userId, BundleActiveCore& listener, const bool debug):listener_(listener)
40     {
41         for (int32_t i = 0; i < BundleActivePeriodStats::PERIOD_COUNT; i++) {
42             currentStats_.push_back(nullptr);
43         }
44         userId_ = userId;
45         dailyExpiryDate_.SetMilliseconds(0);
46         statsChanged_ = false;
47         if (debug) {
48             dailyExpiryDate_.ChangeToDebug();
49             database_.ChangeToDebug();
50             debugUserService_ = true;
51             periodLength_ = {ONE_DAY_TIME_DEBUG, ONE_WEEK_TIME_DEBUG, ONE_MONTH_TIME_DEBUG, ONE_YEAR_TIME_DEBUG};
52         } else {
53             debugUserService_ = false;
54             periodLength_ = {ONE_DAY_TIME, ONE_WEEK_TIME, ONE_MONTH_TIME, ONE_YEAR_TIME};
55         }
56     }
57     void Init(const int64_t timeStamp);
~BundleActiveUserService()58     ~BundleActiveUserService() {}
59     void ReportForShutdown(const BundleActiveEvent& event);
60     void ReportEvent(const BundleActiveEvent& event);
61     void ReportFormEvent(const BundleActiveEvent& event);
62     void ReportModuleEvent(const BundleActiveEvent& event);
63     void RestoreStats(bool forced);
64     void RenewStatsInMemory(const int64_t timeStamp);
65     void RenewTableTime(int64_t oldTime, int64_t newTime);
66     void OnUserRemoved();
67     void DeleteUninstalledBundleStats(const std::string& bundleName, const int32_t uid, const int32_t appIndex);
68     int32_t userId_;
69     BundleActiveCalendar dailyExpiryDate_;
70     ErrCode QueryBundleStatsInfos(std::vector<BundleActivePackageStats>& PackageStats, int32_t intervalType,
71         const int64_t beginTime, const int64_t endTime, const int32_t userId, const std::string& bundleName);
72     ErrCode QueryBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvent, const int64_t beginTime,
73         const int64_t endTime, const int32_t userId, const std::string& bundleName);
74     int32_t QueryModuleUsageRecords(int32_t maxNum, std::vector<BundleActiveModuleRecord>& results);
75     int32_t QueryDeviceEventStats(int64_t beginTime, int64_t endTime,
76         std::vector<BundleActiveEventStats>& eventStats, int32_t userId);
77     int32_t QueryNotificationEventStats(int64_t beginTime, int64_t endTime,
78         std::vector<BundleActiveEventStats>& eventStats, int32_t userId);
79     void LoadActiveStats(const int64_t timeStamp, const bool& force, const bool& timeChanged);
80     void LoadModuleAndFormStats();
81     void DeleteExcessiveEventTableData(int32_t deleteDays);
82 
83 private:
84     static const int64_t ONE_SECOND_MILLISECONDS = 1000;
85     BundleActiveUsageDatabase database_;
86     std::vector<std::shared_ptr<BundleActivePeriodStats>> currentStats_;
87     std::map<std::string, std::shared_ptr<BundleActiveModuleRecord>> moduleRecords_;
88     bool statsChanged_;
89     bool debugUserService_;
90     std::string lastForegroundBundle_;
91     BundleActiveCore& listener_;
92     std::vector<int64_t> periodLength_ = {0, 0, 0, 0};
93     ffrt::recursive_mutex statsMutex_;
94     void NotifyStatsChanged();
95     void NotifyNewUpdate();
96     std::shared_ptr<BundleActiveModuleRecord> GetOrCreateModuleRecord(const BundleActiveEvent& event);
97     void UpdateContinueAbilitiesMemory(const int64_t& beginTime,
98     const std::map<std::string, std::map<std::string, int>>& continueAbilities, const std::string& continueBundleName,
99     const std::vector<std::shared_ptr<BundleActivePeriodStats>>::iterator& itInterval);
100     void UpdateContinueServicesMemory(const int64_t& beginTime,
101     const std::map<std::string, std::map<std::string, int>>& continueServices, const std::string& continueBundleName,
102     const std::vector<std::shared_ptr<BundleActivePeriodStats>>::iterator& itInterval);
103     void PrintInMemPackageStats(const int32_t idx, const bool debug);
104     void PrintInMemEventStats(const bool debug);
105     void PrintInMemFormStats(const bool debug, const bool printform);
106     void GetCachedSystemEvents(std::shared_ptr<BundleActivePeriodStats> currentStats, int64_t beginTime,
107         int64_t endTime, std::map<std::string, BundleActiveEventStats>& systemEventStats);
108     void GetCachedNotificationEvents(std::shared_ptr<BundleActivePeriodStats> currentStats, int64_t beginTime,
109         int64_t endTime, std::map<std::string, BundleActiveEventStats>& notificationEventStats);
110     void FlushDataInMem(std::set<std::string> &continueBundles,
111         std::map<std::string, std::map<std::string, int>> &continueAbilities,
112         std::map<std::string, std::map<std::string, int>> &continueServices);
113     void DeleteMemUsageStats(const std::shared_ptr<BundleActivePeriodStats>& currentStats,
114         const std::string& bundleName, const int32_t deletedUid, const int32_t appIndex);
115     void DeleteMemEvent(const std::shared_ptr<BundleActivePeriodStats>& currentStats, const std::string& bundleName,
116         const int32_t deletedUid, const int32_t appIndex);
117     void DeleteMemRecords(const std::shared_ptr<BundleActivePeriodStats>& currentStats, const std::string& bundleName,
118         const int32_t deletedUid, const int32_t appIndex);
119     void DeleteMemPackageUidSet(const std::shared_ptr<BundleActivePeriodStats>& currentStats,
120         const std::string& bundleName, const int32_t deletedUid, const int32_t appIndex);
121     void UpdateExpiryDate(const bool timeChanged, BundleActiveCalendar& tmpCalendar, const int64_t timeStamp);
122     void UpdatePeriodStats(const BundleActiveEvent& event, const bool& incrementBundleLaunch);
123 };
124 }  // namespace DeviceUsageStats
125 }  // namespace OHOS
126 #endif  // BUNDLE_ACTIVE_USER_SERVICE_H
127 
128