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 { 39 public: 40 static BundleActiveGroupController& GetInstance(); 41 void Init(const bool debug); 42 void DeInit(); 43 #ifdef DEVICE_USAGES_STATISTICS_POWERMANGER_ENABLE 44 using PowerMgrClient = OHOS::PowerMgr::PowerMgrClient; 45 #endif 46 using IBundleMgr = OHOS::AppExecFwk::IBundleMgr; 47 using ApplicationInfo = OHOS::AppExecFwk::ApplicationInfo; 48 using BundleInfo = OHOS::AppExecFwk::BundleInfo; 49 using BundleFlag = OHOS::AppExecFwk::BundleFlag; 50 using ApplicationFlag = OHOS::AppExecFwk::ApplicationFlag; 51 OHOS::AppExecFwk::ApplicationFlag flag = OHOS::AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO; 52 const int32_t LEVEL_GROUP[4] = { 53 ACTIVE_GROUP_ALIVE, 54 ACTIVE_GROUP_DAILY, 55 ACTIVE_GROUP_FIXED, 56 ACTIVE_GROUP_RARE 57 }; 58 std::vector<int64_t> screenTimeLevel_ = {0, 0, 0, 0}; 59 std::vector<int64_t> bootTimeLevel_ = {0, 0, 0, 0}; 60 std::shared_ptr<BundleActiveUserHistory> bundleUserHistory_; 61 void CreateUserHistory( 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 void SetBundleGroupEnable(bool bundleGroupEnable); 87 bool GetBundleGroupEnable(); 88 bool IsUsedOverOneWeek(const std::string& bundleName, const int32_t userId); 89 std::shared_ptr<BundleActiveGroupHandler> GetBundleGroupHandler(); 90 private: 91 BundleActiveGroupController() = default; 92 ~BundleActiveGroupController() = default; 93 ffrt::mutex mutex_; 94 ffrt::mutex initMutex_; 95 std::shared_ptr<BundleActiveGroupHandler> activeGroupHandler_; 96 int64_t timeoutForDirectlyUse_; 97 int64_t timeoutForNotifySeen_; 98 int64_t timeoutForSystemInteraction_; 99 int64_t timeoutCalculated_ = 0; 100 std::map<int32_t, uint32_t> eventIdMatchReason_; 101 bool bundleGroupEnable_ = true; 102 bool isInit_ = false; 103 bool calculationTimeOut(const std::shared_ptr<BundleActivePackageHistory>& oneBundleHistory, 104 const int64_t bootBasedTimeStamp); 105 int32_t GetNewGroup(const std::string& bundleName, const int32_t userId, const int64_t bootBasedTimeStamp, 106 const int32_t uid); 107 void SendCheckBundleMsg(const BundleActiveEvent& event, const int32_t& userId, 108 const int64_t& timeUntilNextCheck, const int64_t& checkBundleMsgEventId); 109 }; 110 } // namespace DeviceUsageStats 111 } // namespace OHOS 112 #endif // BUNDLE_ACTIVE_GROUP_CONTROLLER_H 113 114