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 MISCSERVICES_REQUEST_APPLICATION_STATE_OBSERVER_H 17 #define MISCSERVICES_REQUEST_APPLICATION_STATE_OBSERVER_H 18 19 #include <functional> 20 #include <map> 21 #include <string> 22 23 #include "application_state_observer_stub.h" 24 25 namespace OHOS::Request::Download { 26 class ApplicationStateObserver { 27 public: 28 ~ApplicationStateObserver(); 29 using RegCallBack = std::function<void(const std::string bundleName, int32_t uid, int32_t state)>; 30 static ApplicationStateObserver& GetInstance(); 31 bool RegisterAppStateChanged(RegCallBack &&callback); 32 private: 33 class AppProcessState : public AppExecFwk::ApplicationStateObserverStub { 34 public: AppProcessState(ApplicationStateObserver & appStateObserver)35 explicit AppProcessState(ApplicationStateObserver &appStateObserver) : appStateObserver_(appStateObserver) {} 36 ~AppProcessState() override = default; 37 void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) override; 38 void OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override; 39 void OnExtensionStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override; 40 void OnProcessCreated(const AppExecFwk::ProcessData &processData) override; 41 void OnProcessDied(const AppExecFwk::ProcessData &processData) override; 42 private: 43 void RunCallback(const std::string bundleName, int32_t uid, int32_t state); 44 ApplicationStateObserver& appStateObserver_; 45 }; 46 ApplicationStateObserver(); 47 RegCallBack callback_ = nullptr; 48 }; 49 } 50 51 #endif // MISCSERVICES_REQUEST_APPLICATION_STATE_OBSERVER_H 52