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 #include "wifi_app_state_aware.h"
17 #include "iservice_registry.h"
18 #include "ability_manager_client.h"
19 #include "system_ability_definition.h"
20 #include "app_mgr_client.h"
21 #include "wifi_protect_manager.h"
22 #include "wifi_service_manager.h"
23 #include "sta_app_acceleration.h"
24 #include "app_network_speed_limit_service.h"
25 #include "wifi_logger.h"
26
27 namespace OHOS {
28 namespace Wifi {
29
30 DEFINE_WIFILOG_LABEL("WifiAppStateAware");
31 constexpr const char *WIFI_APP_STATE_AWARE_THREAD = "WIFI_APP_STATE_AWARE_THREAD";
32 constexpr int32_t UID_CALLINGUID_TRANSFORM_DIVISOR = 200000;
33 constexpr const int APP_INFO_USERID = 100;
34 constexpr int64_t WIFI_APP_STATE_SUBSCRIBE_TIME_DELAY = 3 * 1000;
WifiAppStateAware(int instId)35 WifiAppStateAware::WifiAppStateAware(int instId)
36 {
37 appChangeEventHandler = std::make_unique<WifiEventHandler>(WIFI_APP_STATE_AWARE_THREAD);
38 RegisterAppStateChangedCallback();
39 WIFI_LOGI("Register app state observer successful.");
40 }
41
~WifiAppStateAware()42 WifiAppStateAware::~WifiAppStateAware()
43 {
44 if (appChangeEventHandler) {
45 appChangeEventHandler.reset();
46 }
47 UnSubscribeAppState();
48 }
49
GetInstance()50 WifiAppStateAware &WifiAppStateAware::GetInstance()
51 {
52 static WifiAppStateAware gWifiAppStateAware;
53 return gWifiAppStateAware;
54 }
55
InitAppStateAware(const WifiAppStateAwareCallbacks & wifiAppStateAwareCallbacks)56 ErrCode WifiAppStateAware::InitAppStateAware(const WifiAppStateAwareCallbacks &wifiAppStateAwareCallbacks)
57 {
58 mWifiAppStateAwareCallbacks = wifiAppStateAwareCallbacks;
59 return WIFI_OPT_SUCCESS;
60 }
61
GetAppMgr()62 sptr<AppExecFwk::IAppMgr> WifiAppStateAware::GetAppMgr()
63 {
64 //获取AppMgr
65 sptr<ISystemAbilityManager> systemAbilityManager =
66 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
67 if (systemAbilityManager == nullptr) {
68 WIFI_LOGE("get SystemAbilityManager failed");
69 return nullptr;
70 }
71
72 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID);
73 if (remoteObject == nullptr) {
74 WIFI_LOGE("get App Manager Service failed");
75 return nullptr;
76 }
77 return iface_cast<AppExecFwk::IAppMgr>(remoteObject);
78 }
79
RegisterAppStateChangedCallback(const int64_t delayTime)80 void WifiAppStateAware::RegisterAppStateChangedCallback(const int64_t delayTime)
81 {
82 WIFI_LOGI("%{public}s enter", __func__);
83 if (appChangeEventHandler) {
84 std::function<void()> RegisterAppStateObserverFunc =
85 std::bind(&WifiAppStateAware::RegisterAppStateObserver, this);
86 appChangeEventHandler->PostAsyncTask(RegisterAppStateObserverFunc, delayTime);
87 } else {
88 WIFI_LOGE("%{public}s appChangeEventHandler is null", __func__);
89 }
90 }
91
RegisterAppStateObserver()92 void WifiAppStateAware::RegisterAppStateObserver()
93 {
94 WIFI_LOGI("%{public}s called", __func__);
95 std::lock_guard<std::mutex> lock(mutex_);
96 if (mAppStateObserver) {
97 WIFI_LOGI("mAppStateObserver already registered");
98 }
99 auto appMgrProxy = GetAppMgr();
100 if (appMgrProxy == nullptr) {
101 WIFI_LOGI("%{public}s GetAppMgr fail", __func__);
102 RegisterAppStateChangedCallback(WIFI_APP_STATE_SUBSCRIBE_TIME_DELAY);
103 return;
104 }
105 mAppStateObserver = sptr<AppStateObserver>(new (std::nothrow) AppStateObserver());
106 int ret = appMgrProxy->RegisterApplicationStateObserver(mAppStateObserver);
107 if (ret != ERR_OK) {
108 WIFI_LOGE("register application state observer fail, ret = %{public}d", ret);
109 RegisterAppStateChangedCallback(WIFI_APP_STATE_SUBSCRIBE_TIME_DELAY);
110 return;
111 }
112 WIFI_LOGI("register application state observer success.");
113 }
114
UnSubscribeAppState()115 void WifiAppStateAware::UnSubscribeAppState()
116 {
117 WIFI_LOGI("%{public}s called", __func__);
118 auto appMgrProxy = GetAppMgr();
119 if (appMgrProxy == nullptr) {
120 WIFI_LOGI("%{public}s GetAppMgr fail", __func__);
121 return;
122 }
123 std::lock_guard<std::mutex> lock(mutex_);
124 if (!mAppStateObserver) {
125 WIFI_LOGE("UnSubscribeAppState: mAppStateObserver is nullptr");
126 return;
127 }
128 appMgrProxy->UnregisterApplicationStateObserver(mAppStateObserver);
129 mAppStateObserver = nullptr;
130 WIFI_LOGI("UnSubscribeAppState end");
131 return;
132 }
133
OnForegroundAppChanged(const AppExecFwk::AppStateData & appStateData,const int mInstId)134 void WifiAppStateAware::OnForegroundAppChanged(const AppExecFwk::AppStateData &appStateData, const int mInstId)
135 {
136 WifiProtectManager::GetInstance().OnAppForegroudChanged(appStateData.bundleName, appStateData.state);
137 #ifndef OHOS_ARCH_LITE
138 AppNetworkSpeedLimitService::GetInstance().HandleForegroundAppChangedAction(appStateData);
139 if (mWifiAppStateAwareCallbacks.OnForegroundAppChanged != nullptr) {
140 mWifiAppStateAwareCallbacks.OnForegroundAppChanged(appStateData, mInstId);
141 }
142 #endif
143 }
144
GetProcessRunningInfos(std::vector<AppExecFwk::RunningProcessInfo> & info)145 ErrCode WifiAppStateAware::GetProcessRunningInfos(std::vector<AppExecFwk::RunningProcessInfo> &info)
146 {
147 auto appMgrProxy = GetAppMgr();
148 if (appMgrProxy == nullptr) {
149 WIFI_LOGE("%{public}s GetAppMgr failed", __FUNCTION__);
150 return WIFI_OPT_FAILED;
151 }
152 if (appMgrProxy->GetProcessRunningInfosByUserId(info, APP_INFO_USERID)
153 != AppExecFwk::AppMgrResultCode::RESULT_OK) {
154 WIFI_LOGE("%{public}s GetProcessRunningInfoByUserId failed", __FUNCTION__);
155 return WIFI_OPT_FAILED;
156 }
157 return WIFI_OPT_SUCCESS;
158 }
159
IsForegroundApp(int32_t uid)160 bool WifiAppStateAware::IsForegroundApp(int32_t uid)
161 {
162 auto appMgrProxy = GetAppMgr();
163 if (appMgrProxy == nullptr) {
164 WIFI_LOGE("%{public}s GetAppMgr failed", __FUNCTION__);
165 return false;
166 }
167 std::vector<AppExecFwk::AppStateData> curForegroundApps;
168 appMgrProxy->GetForegroundApplications(curForegroundApps);
169
170 for (auto foregroudApp : curForegroundApps) {
171 if (foregroudApp.uid == uid) {
172 return true;
173 }
174 }
175 return false;
176 }
177
IsForegroundApp(const std::string & bundleName)178 bool WifiAppStateAware::IsForegroundApp(const std::string &bundleName)
179 {
180 auto appMgrProxy = GetAppMgr();
181 if (appMgrProxy == nullptr) {
182 WIFI_LOGE("%{public}s GetAppMgr failed", __FUNCTION__);
183 return false;
184 }
185 std::vector<AppExecFwk::AppStateData> curForegroundApps;
186 appMgrProxy->GetForegroundApplications(curForegroundApps);
187 for (auto foregroudApp : curForegroundApps) {
188 if (foregroudApp.bundleName == bundleName) {
189 return true;
190 }
191 }
192 return false;
193 }
194
GetRunningProcessNameByPid(const int uid,const int pid)195 std::string WifiAppStateAware::GetRunningProcessNameByPid(const int uid, const int pid)
196 {
197 auto appMgrProxy = GetAppMgr();
198 if (appMgrProxy == nullptr) {
199 WIFI_LOGE("%{public}s GetAppMgr failed", __FUNCTION__);
200 return "";
201 }
202 int32_t userId = static_cast<int32_t>(uid / UID_CALLINGUID_TRANSFORM_DIVISOR);
203 std::vector<AppExecFwk::RunningProcessInfo> infos;
204 int ret = appMgrProxy->GetProcessRunningInfosByUserId(infos, userId);
205 if (ret != ERR_OK) {
206 WIFI_LOGE("GetProcessRunningInfosByUserId fail, ret = [%{public}d]", ret);
207 return "";
208 }
209 std::string processName = "";
210 auto iter = infos.begin();
211 while (iter != infos.end()) {
212 if (iter->pid_ == pid) {
213 processName = iter->processName_;
214 break;
215 }
216 iter++;
217 }
218 return processName;
219 }
220
OnAppStarted(const AppExecFwk::AppStateData & appStateData)221 void AppStateObserver::OnAppStarted(const AppExecFwk::AppStateData &appStateData)
222 {
223 WIFI_LOGD("%{public}s bundleName: %{public}s, uid: %{public}d, state: %{public}d, isFocused: %{public}d",
224 __func__, appStateData.bundleName.c_str(), appStateData.uid,
225 appStateData.state, appStateData.isFocused);
226 }
227
OnAppStopped(const AppExecFwk::AppStateData & appStateData)228 void AppStateObserver::OnAppStopped(const AppExecFwk::AppStateData &appStateData)
229 {
230 WIFI_LOGD("%{public}s bundleName: %{public}s, uid: %{public}d, state: %{public}d, isFocused: %{public}d",
231 __func__, appStateData.bundleName.c_str(), appStateData.uid,
232 appStateData.state, appStateData.isFocused);
233
234 if (appStateData.bundleName.empty()) {
235 WIFI_LOGE("App bundle name is empty");
236 return;
237 }
238 WifiProtectManager::GetInstance().OnAppDied(appStateData.bundleName);
239 return;
240 }
241
OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)242 void AppStateObserver::OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData)
243 {
244 WIFI_LOGI("%{public}s bundleName: %{public}s, uid: %{public}d, state: %{public}d, isFocused: %{public}d",
245 __func__, appStateData.bundleName.c_str(), appStateData.uid, appStateData.state, appStateData.isFocused);
246 WifiAppStateAware::GetInstance().OnForegroundAppChanged(appStateData);
247 }
248 } // namespace Wifi
249 } // namespace OHOS
250