1 /* 2 * Copyright (c) 2023 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 REQUEST_APPLICATION_STATE_OBSERVER_H 17 #define REQUEST_APPLICATION_STATE_OBSERVER_H 18 19 #include <cstdint> 20 #include <functional> 21 #include <map> 22 #include <string> 23 24 #include "application_state_observer_stub.h" 25 #include "c_string_wrapper.h" 26 27 namespace OHOS::Request { 28 class AppProcessState : public AppExecFwk::ApplicationStateObserverStub { 29 public: 30 AppProcessState(); 31 ~AppProcessState(); 32 void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) override; 33 void OnAppStateChanged(const AppExecFwk::AppStateData &appStateData) override; 34 void OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override; 35 void OnExtensionStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override; 36 void OnProcessCreated(const AppExecFwk::ProcessData &processData) override; 37 void OnProcessDied(const AppExecFwk::ProcessData &processData) override; 38 39 public: 40 using RegCallBack = std::function<void(int32_t uid, int32_t state, int32_t pid)>; 41 using ProcessCallBack = std::function<void(int32_t uid, int32_t state, int32_t pid, CStringWrapper bundleName)>; 42 bool RegisterAppStateChanged(RegCallBack &&callback); 43 void RegisterProcessDied(ProcessCallBack &&callback); 44 static sptr<AppProcessState> GetInstance(); 45 46 private: 47 RegCallBack appStateCallback_ = nullptr; 48 ProcessCallBack processCallback_ = nullptr; 49 void RunAppStateCallback(int32_t uid, int32_t state, int32_t pid); 50 void RunProcessDiedCallback(int32_t uid, int32_t state, int32_t pid, const std::string &bundleName); 51 }; 52 53 } // namespace OHOS::Request 54 55 #ifdef __cplusplus 56 extern "C" { 57 #endif 58 59 typedef void (*APPStateCallback)(int32_t, int32_t, int32_t); 60 typedef void (*ProcessStateCallback)(int32_t, int32_t, int32_t, CStringWrapper); 61 void RegisterAPPStateCallback(APPStateCallback fun); 62 void RegisterProcessDiedCallback(ProcessStateCallback fun); 63 64 #ifdef __cplusplus 65 } 66 #endif 67 68 #endif // REQUEST_APPLICATION_STATE_OBSERVER_H 69