180922886Sopenharmony_ci/* 280922886Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 380922886Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 480922886Sopenharmony_ci * you may not use this file except in compliance with the License. 580922886Sopenharmony_ci * You may obtain a copy of the License at 680922886Sopenharmony_ci * 780922886Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 880922886Sopenharmony_ci * 980922886Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1080922886Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1180922886Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1280922886Sopenharmony_ci * See the License for the specific language governing permissions and 1380922886Sopenharmony_ci * limitations under the License. 1480922886Sopenharmony_ci */ 1580922886Sopenharmony_ci 1680922886Sopenharmony_ci#ifndef AV_SESSION_APP_MANAGER_ADAPTER_H 1780922886Sopenharmony_ci#define AV_SESSION_APP_MANAGER_ADAPTER_H 1880922886Sopenharmony_ci 1980922886Sopenharmony_ci#include <functional> 2080922886Sopenharmony_ci#include <mutex> 2180922886Sopenharmony_ci#include <set> 2280922886Sopenharmony_ci#include "app_mgr_client.h" 2380922886Sopenharmony_ci#include "app_state_callback_host.h" 2480922886Sopenharmony_ci 2580922886Sopenharmony_cinamespace OHOS::AVSession { 2680922886Sopenharmony_ciclass AppManagerAdapter { 2780922886Sopenharmony_cipublic: 2880922886Sopenharmony_ci static AppManagerAdapter& GetInstance(); 2980922886Sopenharmony_ci 3080922886Sopenharmony_ci ~AppManagerAdapter(); 3180922886Sopenharmony_ci 3280922886Sopenharmony_ci void Init(); 3380922886Sopenharmony_ci 3480922886Sopenharmony_ci bool IsAppBackground(int32_t uid, int32_t pid); 3580922886Sopenharmony_ci 3680922886Sopenharmony_ci void SetAppStateChangeObserver(const std::function<void(int32_t, int32_t, bool)>& observer); 3780922886Sopenharmony_ci 3880922886Sopenharmony_ci void AddObservedApp(int32_t uid); 3980922886Sopenharmony_ci 4080922886Sopenharmony_ci void RemoveObservedApp(int32_t uid); 4180922886Sopenharmony_ci 4280922886Sopenharmony_ci void HandleAppStateChanged(const AppExecFwk::AppProcessData& appProcessData); 4380922886Sopenharmony_ci 4480922886Sopenharmony_ci void SetServiceCallbackForAppStateChange(const std::function<void(int uid, int state)>& callback); 4580922886Sopenharmony_ci 4680922886Sopenharmony_ciprivate: 4780922886Sopenharmony_ci AppManagerAdapter(); 4880922886Sopenharmony_ci 4980922886Sopenharmony_ci AppExecFwk::AppMgrClient appManager_; 5080922886Sopenharmony_ci sptr<AppExecFwk::IAppStateCallback> appStateCallback_; 5180922886Sopenharmony_ci std::function<void(int32_t, int32_t, bool)> appStateChangeObserver_; 5280922886Sopenharmony_ci std::recursive_mutex uidLock_; 5380922886Sopenharmony_ci std::set<int32_t> observedAppUIDs_; 5480922886Sopenharmony_ci std::function<void(int uid, int state)> serviceCallbackForAppStateChange_; 5580922886Sopenharmony_ci 5680922886Sopenharmony_ci static constexpr int RETRY_COUNT_MAX = 5; 5780922886Sopenharmony_ci static constexpr int RETRY_INTERVAL_TIME = 500; 5880922886Sopenharmony_ci}; 5980922886Sopenharmony_ci 6080922886Sopenharmony_ciclass AVSessionAppStateCallback : public AppExecFwk::AppStateCallbackHost { 6180922886Sopenharmony_cipublic: 6280922886Sopenharmony_ci void OnAppStateChanged(const AppExecFwk::AppProcessData&) override; 6380922886Sopenharmony_ci}; 6480922886Sopenharmony_ci} 6580922886Sopenharmony_ci#endif // AV_SESSION_APP_MANAGER_ADAPTER_H