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