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_CONTINUOUS_TASK_INCLUDE_APP_STATE_OBSERVER_H 17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CONTINUOUS_TASK_INCLUDE_APP_STATE_OBSERVER_H 18 19 #include "singleton.h" 20 #include "app_mgr_interface.h" 21 #include "application_state_observer_stub.h" 22 #include "event_handler.h" 23 #include "iremote_object.h" 24 25 #include "remote_death_recipient.h" 26 27 namespace OHOS { 28 namespace BackgroundTaskMgr { 29 class BgEfficiencyResourcesMgr; 30 class BgContinuousTaskMgr; 31 32 class AppStateObserver : public AppExecFwk::ApplicationStateObserverStub { 33 DECLARE_DELAYED_SINGLETON(AppStateObserver); 34 public: 35 void OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override; 36 void OnProcessDied(const AppExecFwk::ProcessData &processData) override; 37 void OnApplicationStateChanged(const AppExecFwk::AppStateData &appStateData) override; 38 void SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &handler); 39 void SetBgContinuousTaskMgr(const std::shared_ptr<BgContinuousTaskMgr> &bgContinuousTaskMgr); 40 void SetBgEfficiencyResourcesMgr(const std::shared_ptr<BgEfficiencyResourcesMgr> &resourceMgr); 41 bool Subscribe(); 42 bool Unsubscribe(); 43 44 private: 45 bool Connect(); 46 inline bool ValidateAppStateData(const AppExecFwk::AppStateData &appStateData); 47 void OnProcessDiedContinuousTask(const AppExecFwk::ProcessData &processData); 48 void OnProcessDiedEfficiencyRes(const AppExecFwk::ProcessData &processData); 49 bool CheckParamValid(); 50 51 private: 52 std::mutex mutex_ {}; 53 std::weak_ptr<AppExecFwk::EventHandler> handler_ {}; 54 std::weak_ptr<BgContinuousTaskMgr> bgContinuousTaskMgr_ {}; 55 sptr<AppExecFwk::IAppMgr> appMgrProxy_ {nullptr}; 56 sptr<RemoteDeathRecipient> appMgrDeathRecipient_ {nullptr}; 57 std::weak_ptr<BgEfficiencyResourcesMgr> bgEfficiencyResourcesMgr_ {}; 58 }; 59 } // namespace BackgroundTaskMgr 60 } // namespace OHOS 61 #endif // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_CONTINUOUS_TASK_INCLUDE_APP_STATE_OBSERVER_H