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_GROUP_CONTROLLER_H
17 #define BUNDLE_ACTIVE_GROUP_CONTROLLER_H
18 
19 #include <memory>
20 #include <mutex>
21 #ifdef DEVICE_USAGES_STATISTICS_POWERMANGER_ENABLE
22 #include "power_mgr_client.h"
23 #endif
24 #include "application_info.h"
25 #include "ffrt.h"
26 
27 #include "ibundle_active_service.h"
28 #include "bundle_active_event.h"
29 #include "bundle_active_user_history.h"
30 #include "bundle_state_inner_errors.h"
31 #include "bundle_mgr_interface.h"
32 
33 namespace OHOS {
34 namespace DeviceUsageStats {
35 using namespace DeviceUsageStatsGroupConst;
36 
37 class BundleActiveGroupHandler;
38 class BundleActiveGroupController : public std::enable_shared_from_this<BundleActiveGroupController> {
39 public:
40 #ifdef DEVICE_USAGES_STATISTICS_POWERMANGER_ENABLE
41     using PowerMgrClient = OHOS::PowerMgr::PowerMgrClient;
42 #endif
43     using IBundleMgr = OHOS::AppExecFwk::IBundleMgr;
44     using ApplicationInfo = OHOS::AppExecFwk::ApplicationInfo;
45     using BundleInfo = OHOS::AppExecFwk::BundleInfo;
46     using BundleFlag = OHOS::AppExecFwk::BundleFlag;
47     using ApplicationFlag = OHOS::AppExecFwk::ApplicationFlag;
48     OHOS::AppExecFwk::ApplicationFlag flag = OHOS::AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO;
49     bool bundleGroupEnable_ = true;
50     const int32_t LEVEL_GROUP[4] = {
51         ACTIVE_GROUP_ALIVE,
52         ACTIVE_GROUP_DAILY,
53         ACTIVE_GROUP_FIXED,
54         ACTIVE_GROUP_RARE
55     };
56     std::vector<int64_t> screenTimeLevel_ = {0, 0, 0, 0};
57     std::vector<int64_t> bootTimeLevel_ = {0, 0, 0, 0};
58     BundleActiveGroupController(const bool debug);
~BundleActiveGroupController()59     ~BundleActiveGroupController() {}
60     std::shared_ptr<BundleActiveUserHistory> bundleUserHistory_;
61     void SetHandlerAndCreateUserHistory(const std::shared_ptr<BundleActiveGroupHandler>& groupHandler,
62         const int64_t bootFromTimeStamp, const std::shared_ptr<BundleActiveCore>& bundleActiveCore);
63     void ReportEvent(const BundleActiveEvent& event, const int64_t bootBasedTimeStamp, const int32_t userId);
64     void CheckAndUpdateGroup(const std::string& bundleName, int32_t userId,
65         const int32_t uid, const int64_t bootBasedTimeStamp);
66     bool CheckEachBundleState(const int32_t userId);
67     void CheckIdleStatsOneTime();
68     void PeriodCheckBundleState(const int32_t userId);
69     void OnUserRemoved(const int32_t userId);
70     void OnBundleUninstalled(const int32_t userId, const std::string& bundleName, const int32_t uid,
71         const int32_t appIndex);
72     void DeleteUsageGroupCache(
73         const std::shared_ptr<std::map<std::string, std::shared_ptr<BundleActivePackageHistory>>>& userHostory,
74         const std::string& bundleName, const int32_t uid, const int32_t appIndex);
75     void OnScreenChanged(const bool& isScreenOn, const int64_t bootFromTimeStamp);
76     int32_t SetAppGroup(const std::string& bundleName, const int32_t userId, int32_t newGroup, uint32_t reason,
77         const int64_t bootBasedTimeStamp, const bool isFlush);
78     void RestoreToDatabase(const int32_t userId);
79     void RestoreDurationToDatabase();
80     bool IsBundleInstalled(const std::string& bundleName, const int32_t userId);
81     bool IsScreenOn();
82     int32_t IsBundleIdle(const std::string& bundleName, const int32_t userId);
83     ErrCode QueryAppGroup(int32_t& appGroup, const std::string& bundleName, const int32_t userId);
84     void ShutDown(const int64_t bootBasedTimeStamp, const int32_t userId);
85     void OnUserSwitched(const int32_t userId, const int32_t currentUsedUser);
86 
87 private:
88     ffrt::mutex mutex_;
89     bool GetBundleMgrProxy();
90     std::weak_ptr<BundleActiveGroupHandler> activeGroupHandler_;
91     int64_t timeoutForDirectlyUse_;
92     int64_t timeoutForNotifySeen_;
93     int64_t timeoutForSystemInteraction_;
94     int64_t timeoutCalculated_ = 0;
95     std::map<int32_t, uint32_t> eventIdMatchReason_;
96     sptr<IBundleMgr> sptrBundleMgr_;
97     bool calculationTimeOut(const std::shared_ptr<BundleActivePackageHistory>& oneBundleHistory,
98         const int64_t bootBasedTimeStamp);
99     int32_t GetNewGroup(const std::string& bundleName, const int32_t userId, const int64_t bootBasedTimeStamp,
100         const int32_t uid);
101     void SendCheckBundleMsg(const BundleActiveEvent& event, const int32_t& userId,
102         const int64_t& timeUntilNextCheck, const int64_t& checkBundleMsgEventId);
103 };
104 }  // namespace DeviceUsageStats
105 }  // namespace OHOS
106 #endif  // BUNDLE_ACTIVE_GROUP_CONTROLLER_H
107 
108