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 
55 private:
56     class ApplicationStateObserver : public AppExecFwk::ApplicationStateObserverStub {
57     public:
ApplicationStateObserver(DecisionMaker & decisionMaker)58         explicit ApplicationStateObserver(DecisionMaker &decisionMaker) : decisionMaker_(decisionMaker) {}
~ApplicationStateObserver()59         ~ApplicationStateObserver() override {}
60         void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) override;
OnAbilityStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)61         void OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override
62         {}
OnExtensionStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)63         void OnExtensionStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override
64         {}
OnProcessCreated(const AppExecFwk::ProcessData & processData)65         void OnProcessCreated(const AppExecFwk::ProcessData &processData) override
66         {}
OnProcessDied(const AppExecFwk::ProcessData & processData)67         void OnProcessDied(const AppExecFwk::ProcessData &processData) override
68         {}
69 
70     private:
71         DecisionMaker &decisionMaker_;
72     };
73 
74 private:
75     class AppMgrDeathRecipient : public IRemoteObject::DeathRecipient {
76     public:
AppMgrDeathRecipient(DecisionMaker & decisionMaker)77         explicit AppMgrDeathRecipient(DecisionMaker &decisionMaker) : decisionMaker_(decisionMaker) {}
78 
~AppMgrDeathRecipient()79         ~AppMgrDeathRecipient() override {}
80 
81         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
82 
83     private:
84         DecisionMaker &decisionMaker_;
85     };
86 
87 private:
88     int32_t NewDelaySuspendRequestId();
89     int32_t GetDelayTime();
90     void HandleScreenOn();
91     void HandleScreenOff();
92     void HandlePowerConnect();
93     void HandlePowerDisconnect();
94     void HandlePkgRemove(const EventInfo& eventInfo);
95 
96     bool GetAppMgrProxy();
97     void ResetDayQuotaLocked();
98     bool IsAfterOneDay(int64_t lastRequestTime, int64_t currentTime);
99     bool CanStartAccountingLocked(const std::shared_ptr<PkgDelaySuspendInfo>& pkgInfo);
100     int GetAllowRequestTime();
101     ErrCode CheckQuotaTime(const std::shared_ptr<PkgDelaySuspendInfo>& pkgInfo, const std::string &name,
102         int32_t uid, const std::shared_ptr<KeyInfo>& key, bool& needSetTime);
103 
104     const int32_t initRequestId_ = 1;
105     int32_t requestId_ {initRequestId_};
106     std::mutex lock_;
107     int64_t lastRequestTime_ {0};
108     SuspendController suspendController_;
109     std::shared_ptr<TimerManager> timerManager_ {nullptr};
110     std::shared_ptr<DeviceInfoManager> deviceInfoManager_ {nullptr};
111     std::map<std::shared_ptr<KeyInfo>, std::shared_ptr<PkgDelaySuspendInfo>, KeyInfoComp> pkgDelaySuspendInfoMap_;
112     std::map<std::shared_ptr<KeyInfo>, int32_t, KeyInfoComp> pkgBgDurationMap_;
113     sptr<AppExecFwk::IAppMgr> appMgrProxy_ {nullptr};
114     sptr<AppMgrDeathRecipient> recipient_;
115     sptr<ApplicationStateObserver> observer_ {nullptr};
116 };
117 }  // namespace BackgroundTaskMgr
118 }  // namespace OHOS
119 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_TRANSIENT_TASK_INCLUDE_DECISION_MAKER_H