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