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 OHOS_APP_STATE_OBSERVER_H 17 #define OHOS_APP_STATE_OBSERVER_H 18 19 #include "singleton.h" 20 21 #include "app_mgr_interface.h" 22 #include "application_state_observer_stub.h" 23 #include "iservice_registry.h" 24 #include "mmi_log.h" 25 #include "system_ability_definition.h" 26 27 namespace OHOS { 28 namespace MMI { 29 class ApplicationStateObserver : public AppExecFwk::ApplicationStateObserverStub { 30 public: ApplicationStateObserver()31 ApplicationStateObserver() {}; 32 ~ApplicationStateObserver() = default; 33 void OnProcessStateChanged(const AppExecFwk::ProcessData &processData) override; 34 std::vector<AppExecFwk::AppStateData> GetForegroundAppData(); 35 private: 36 sptr<AppExecFwk::IAppMgr> appManager_ = nullptr; 37 OHOS::sptr<OHOS::AppExecFwk::IAppMgr> GetAppMgr(); 38 int32_t GetForegroundApplicationInfo(std::vector<AppExecFwk::AppStateData> &list); 39 }; 40 41 class AppObserverManager final { 42 DECLARE_DELAYED_SINGLETON(AppObserverManager); 43 public: 44 DISALLOW_COPY_AND_MOVE(AppObserverManager); 45 void InitAppStateObserver(); 46 void SetForegroundAppData(std::vector<AppExecFwk::AppStateData> list); 47 std::vector<AppExecFwk::AppStateData> GetForegroundAppData(); 48 private: 49 bool hasInit_ { false }; 50 std::vector<AppExecFwk::AppStateData> foregroundAppData_ {}; 51 }; 52 53 #define APP_OBSERVER_MGR ::OHOS::DelayedSingleton<AppObserverManager>::GetInstance() 54 } // namespace MMI 55 } // namespace OHOS 56 #endif // OHOS_APP_STATE_OBSERVER_H 57