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