1 /*
2  * Copyright (C) 2021-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 OHOS_WIFI_APP_STATE_AWARE_H
17 #define OHOS_WIFI_APP_STATE_AWARE_H
18 
19 #ifndef OHOS_ARCH_LITE
20 #include "appmgr/app_mgr_interface.h"
21 #include "appmgr/app_state_data.h"
22 #include "iremote_object.h"
23 #include "wifi_event_handler.h"
24 #include "wifi_errcode.h"
25 #include "appmgr/application_state_observer_stub.h"
26 
27 namespace OHOS {
28 namespace Wifi {
29 
30 struct WifiAppStateAwareCallbacks {
31     std::function<void(const AppExecFwk::AppStateData &appStateData,
32         const int mInstId)> OnForegroundAppChanged;
33 };
34 class AppStateObserver;
35 
36 class WifiAppStateAware {
37 public:
38     explicit WifiAppStateAware(int instId = 0);
39     ~WifiAppStateAware();
40     static WifiAppStateAware &GetInstance();
41     sptr<AppExecFwk::IAppMgr> GetAppMgr();
42     ErrCode InitAppStateAware(const WifiAppStateAwareCallbacks &wifiAppStateAwareCallbacks);
43     void RegisterAppStateObserver();
44     void UnSubscribeAppState();
45     void OnForegroundAppChanged(const AppExecFwk::AppStateData &appStateData, const int mInstId = 0);
46     ErrCode GetProcessRunningInfos(std::vector<AppExecFwk::RunningProcessInfo> &info);
47     bool IsForegroundApp(int32_t uid);
48     bool IsForegroundApp(const std::string &bundleName);
49     std::string GetRunningProcessNameByPid(const int uid, const int pid);
50 private:
51     void RegisterAppStateChangedCallback(const int64_t delayTime = 0);
52     std::mutex mutex_ {};
53     std::unique_ptr<WifiEventHandler> appChangeEventHandler = nullptr;
54     sptr<AppStateObserver> mAppStateObserver {nullptr};
55     WifiAppStateAwareCallbacks mWifiAppStateAwareCallbacks;
56 };
57 
58 class AppStateObserver : public AppExecFwk::ApplicationStateObserverStub {
59 public:
60     /**
61      * Will be called when the application start.
62      *
63      * @param appStateData Application state data.
64      */
65     void OnAppStarted(const AppExecFwk::AppStateData &appStateData) override;
66 
67     /**
68      * Will be called when the application stop.
69      *
70      * @param appStateData Application state data.
71      */
72     void OnAppStopped(const AppExecFwk::AppStateData &appStateData) override;
73 
74     /**
75      * Application foreground state changed callback.
76      *
77      * @param appStateData Application Process data.
78      */
79     void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) override;
80 };
81 } // namespace Wifi
82 } // namespace OHOS
83 #endif
84 #endif