1 /*
2  * Copyright (c) 2022-2024 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_HISTORY_H
17 #define BUNDLE_ACTIVE_USER_HISTORY_H
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 
23 #include "ffrt.h"
24 #include "bundle_active_package_history.h"
25 #include "bundle_active_group_common.h"
26 #include "bundle_active_usage_database.h"
27 
28 namespace OHOS {
29 namespace DeviceUsageStats {
30 using namespace DeviceUsageStatsGroupConst;
31 class BundleActiveCore;
32 
33 class BundleActiveUserHistory {
34 public:
35     std::map<int, std::shared_ptr<std::map<std::string, std::shared_ptr<BundleActivePackageHistory>>>> userHistory_;
36     int64_t bootBasedTimeStamp_;
37     int64_t bootBasedDuration_;
38     int64_t screenOnTimeStamp_;
39     int64_t ScreenOnDuration_;
40     BundleActiveUsageDatabase database_;
41     BundleActiveUserHistory(const int64_t bootBasedTimeStamp,
42         const std::shared_ptr<BundleActiveCore>& bundleActiveCore);
43     std::shared_ptr<BundleActivePackageHistory> GetUsageHistoryForBundle(const std::string& bundleName,
44         const int32_t userId, const int64_t bootBasedTimeStamp, const bool create, const int32_t uid);
45     std::shared_ptr<std::map<std::string, std::shared_ptr<BundleActivePackageHistory>>> GetUserHistory(
46             const int32_t userId, const bool create);
47     std::shared_ptr<BundleActivePackageHistory> GetUsageHistoryInUserHistory(std::shared_ptr<std::map<std::string,
48         std::shared_ptr<BundleActivePackageHistory>>> oneUserHistory, std::string bundleName,
49         int64_t bootBasedTimeStamp, bool create, const int32_t uid);
50     int64_t GetBootBasedTimeStamp(int64_t bootBasedTimeStamp);
51     int64_t GetScreenOnTimeStamp(int64_t bootBasedTimeStamp);
52     void ReportUsage(std::shared_ptr<BundleActivePackageHistory> oneBundleUsageHistory, const std::string& bundleName,
53         const int32_t newGroup, const uint32_t groupReason, const int64_t bootBasedTimeStamp,
54         const int64_t timeUntilNextCheck, const int32_t userId, const int32_t uid);
55     int32_t SetAppGroup(const std::string& bundleName, const int32_t userId, const int32_t uid,
56         int64_t bootBasedTimeStamp, int32_t newGroup, uint32_t groupReason, const bool isFlush);
57     int32_t GetLevelIndex(const std::string& bundleName, const int32_t userId, const int64_t bootBasedTimeStamp,
58         const std::vector<int64_t> screenTimeLeve, const std::vector<int64_t> bootFromTimeLevel, const int32_t uid);
59     void WriteDeviceDuration();
60     void WriteBundleUsage(const int32_t userId);
61     void PrintData(int32_t userId);
62     void UpdateBootBasedAndScreenTime(const bool& isScreenOn, const int64_t bootBasedTimeStamp,
63         const bool& isShutdown = false);
64     void OnBundleUninstalled(const int32_t userId, const std::string bundleName, const int32_t uid,
65         const int32_t appIndex);
66 
67 private:
68     ffrt::mutex setGroupMutex_;
69     bool isScreenOn_;
70     std::weak_ptr<BundleActiveCore> bundleActiveCore_;
71 };
72 }  // namespace DeviceUsageStats
73 }  // namespace OHOS
74 #endif  // BUNDLE_ACTIVE_USER_HISTORY_H
75 
76