/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_DECISION_MAKER_H #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_DECISION_MAKER_H #include #include #include #include #include #include #include #include "app_mgr_constants.h" #include "app_mgr_proxy.h" #include "application_state_observer_stub.h" #include "delay_suspend_info_ex.h" #include "device_info_manager.h" #include "input_manager.h" #include "iremote_object.h" #include "key_info.h" #include "pkg_delay_suspend_info.h" #include "suspend_controller.h" #include "timer_manager.h" namespace OHOS { namespace BackgroundTaskMgr { class DecisionMaker : public IEventListener { public: DecisionMaker(const std::shared_ptr& timerManager, const std::shared_ptr& device); ~DecisionMaker() override; ErrCode Decide(const std::shared_ptr& key, const std::shared_ptr& delayInfo); ErrCode PauseTransientTaskTimeForInner(int32_t uid, const std::string &name); ErrCode StartTransientTaskTimeForInner(int32_t uid, const std::string &name); void RemoveRequest(const std::shared_ptr& key, const int32_t requestId); int32_t GetRemainingDelayTime(const std::shared_ptr& key, const int32_t requestId); void OnInputEvent(const EventInfo& eventInfo) override; int32_t GetQuota(const std::shared_ptr& key); bool IsFrontApp(const string& pkgName, int32_t uid); void ResetAppMgrProxy(); private: class ApplicationStateObserver : public AppExecFwk::ApplicationStateObserverStub { public: explicit ApplicationStateObserver(DecisionMaker &decisionMaker) : decisionMaker_(decisionMaker) {} ~ApplicationStateObserver() override {} void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) override; void OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override {} void OnExtensionStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override {} void OnProcessCreated(const AppExecFwk::ProcessData &processData) override {} void OnProcessDied(const AppExecFwk::ProcessData &processData) override {} private: DecisionMaker &decisionMaker_; }; private: class AppMgrDeathRecipient : public IRemoteObject::DeathRecipient { public: explicit AppMgrDeathRecipient(DecisionMaker &decisionMaker) : decisionMaker_(decisionMaker) {} ~AppMgrDeathRecipient() override {} void OnRemoteDied(const wptr &remote) override; private: DecisionMaker &decisionMaker_; }; private: int32_t NewDelaySuspendRequestId(); int32_t GetDelayTime(); void HandleScreenOn(); void HandleScreenOff(); void HandlePowerConnect(); void HandlePowerDisconnect(); void HandlePkgRemove(const EventInfo& eventInfo); bool GetAppMgrProxy(); void ResetDayQuotaLocked(); bool IsAfterOneDay(int64_t lastRequestTime, int64_t currentTime); bool CanStartAccountingLocked(const std::shared_ptr& pkgInfo); int GetAllowRequestTime(); ErrCode CheckQuotaTime(const std::shared_ptr& pkgInfo, const std::string &name, int32_t uid, const std::shared_ptr& key, bool& needSetTime); const int32_t initRequestId_ = 1; int32_t requestId_ {initRequestId_}; std::mutex lock_; int64_t lastRequestTime_ {0}; SuspendController suspendController_; std::shared_ptr timerManager_ {nullptr}; std::shared_ptr deviceInfoManager_ {nullptr}; std::map, std::shared_ptr, KeyInfoComp> pkgDelaySuspendInfoMap_; std::map, int32_t, KeyInfoComp> pkgBgDurationMap_; sptr appMgrProxy_ {nullptr}; sptr recipient_; sptr observer_ {nullptr}; }; } // namespace BackgroundTaskMgr } // namespace OHOS #endif // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_DECISION_MAKER_H