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 FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_DECISION_MAKER_H 17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_DECISION_MAKER_H 18 19 #include <array> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <set> 24 #include <string> 25 #include <vector> 26 27 #include "app_mgr_constants.h" 28 #include "app_mgr_proxy.h" 29 #include "application_state_observer_stub.h" 30 #include "delay_suspend_info_ex.h" 31 #include "device_info_manager.h" 32 #include "input_manager.h" 33 #include "iremote_object.h" 34 #include "key_info.h" 35 #include "pkg_delay_suspend_info.h" 36 #include "suspend_controller.h" 37 #include "timer_manager.h" 38 39 namespace OHOS { 40 namespace BackgroundTaskMgr { 41 class DecisionMaker : public IEventListener { 42 public: 43 DecisionMaker(const std::shared_ptr<TimerManager>& timerManager, const std::shared_ptr<DeviceInfoManager>& device); 44 ~DecisionMaker() override; 45 ErrCode Decide(const std::shared_ptr<KeyInfo>& key, const std::shared_ptr<DelaySuspendInfoEx>& delayInfo); 46 ErrCode PauseTransientTaskTimeForInner(int32_t uid, const std::string &name); 47 ErrCode StartTransientTaskTimeForInner(int32_t uid, const std::string &name); 48 void RemoveRequest(const std::shared_ptr<KeyInfo>& key, const int32_t requestId); 49 int32_t GetRemainingDelayTime(const std::shared_ptr<KeyInfo>& key, const int32_t requestId); 50 void OnInputEvent(const EventInfo& eventInfo) override; 51 int32_t GetQuota(const std::shared_ptr<KeyInfo>& key); 52 bool IsFrontApp(const string& pkgName, int32_t uid); 53 void ResetAppMgrProxy(); 54 vector<int32_t> GetRequestIdListByKey(const std::shared_ptr<KeyInfo>& key); 55 56 private: 57 class ApplicationStateObserver : public AppExecFwk::ApplicationStateObserverStub { 58 public: ApplicationStateObserver(DecisionMaker & decisionMaker)59 explicit ApplicationStateObserver(DecisionMaker &decisionMaker) : decisionMaker_(decisionMaker) {} ~ApplicationStateObserver()60 ~ApplicationStateObserver() override {} 61 void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) override; OnAbilityStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)62 void OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override 63 {} OnExtensionStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)64 void OnExtensionStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override 65 {} OnProcessCreated(const AppExecFwk::ProcessData & processData)66 void OnProcessCreated(const AppExecFwk::ProcessData &processData) override 67 {} OnProcessDied(const AppExecFwk::ProcessData & processData)68 void OnProcessDied(const AppExecFwk::ProcessData &processData) override 69 {} 70 71 private: 72 DecisionMaker &decisionMaker_; 73 }; 74 75 private: 76 class AppMgrDeathRecipient : public IRemoteObject::DeathRecipient { 77 public: AppMgrDeathRecipient(DecisionMaker & decisionMaker)78 explicit AppMgrDeathRecipient(DecisionMaker &decisionMaker) : decisionMaker_(decisionMaker) {} 79 ~AppMgrDeathRecipient()80 ~AppMgrDeathRecipient() override {} 81 82 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 83 84 private: 85 DecisionMaker &decisionMaker_; 86 }; 87 88 private: 89 int32_t NewDelaySuspendRequestId(); 90 int32_t GetDelayTime(); 91 void HandleScreenOn(); 92 void HandleScreenOff(); 93 void HandlePowerConnect(); 94 void HandlePowerDisconnect(); 95 void HandlePkgRemove(const EventInfo& eventInfo); 96 97 bool GetAppMgrProxy(); 98 void ResetDayQuotaLocked(); 99 bool IsAfterOneDay(int64_t lastRequestTime, int64_t currentTime); 100 bool CanStartAccountingLocked(const std::shared_ptr<PkgDelaySuspendInfo>& pkgInfo); 101 int GetAllowRequestTime(); 102 ErrCode CheckQuotaTime(const std::shared_ptr<PkgDelaySuspendInfo>& pkgInfo, const std::string &name, 103 int32_t uid, const std::shared_ptr<KeyInfo>& key, bool& needSetTime); 104 105 const int32_t initRequestId_ = 1; 106 int32_t requestId_ {initRequestId_}; 107 std::mutex lock_; 108 int64_t lastRequestTime_ {0}; 109 SuspendController suspendController_; 110 std::shared_ptr<TimerManager> timerManager_ {nullptr}; 111 std::shared_ptr<DeviceInfoManager> deviceInfoManager_ {nullptr}; 112 std::map<std::shared_ptr<KeyInfo>, std::shared_ptr<PkgDelaySuspendInfo>, KeyInfoComp> pkgDelaySuspendInfoMap_; 113 std::map<std::shared_ptr<KeyInfo>, int32_t, KeyInfoComp> pkgBgDurationMap_; 114 sptr<AppExecFwk::IAppMgr> appMgrProxy_ {nullptr}; 115 sptr<AppMgrDeathRecipient> recipient_; 116 sptr<ApplicationStateObserver> observer_ {nullptr}; 117 }; 118 } // namespace BackgroundTaskMgr 119 } // namespace OHOS 120 #endif // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_DECISION_MAKER_H