1 /*
2 * Copyright (C) 2025 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 "app_state_aware.h"
17
18 #include "app_mgr_constants.h"
19 #include "iservice_registry.h"
20 #include <ability_manager_client.h>
21 #include "system_ability_definition.h"
22 #include "net_mgr_log_wrapper.h"
23
24 namespace OHOS {
25 namespace NetManagerStandard {
26 std::mutex AppStateAwareManager::instanceMutex_;
AppStateAwareManager()27 AppStateAwareManager::AppStateAwareManager()
28 {
29 SubscribeAppState();
30 }
31
~AppStateAwareManager()32 AppStateAwareManager::~AppStateAwareManager()
33 {
34 UnSubscribeAppState();
35 }
36
GetInstance()37 AppStateAwareManager &AppStateAwareManager::GetInstance()
38 {
39 std::lock_guard<std::mutex> lock(instanceMutex_);
40 static AppStateAwareManager gAppStateAwareManager;
41 if (!gAppStateAwareManager.appStateObserver_) {
42 if (gAppStateAwareManager.retryCount_ < MAX_RETRY_COUNT) {
43 gAppStateAwareManager.SubscribeAppState();
44 }
45 }
46 return gAppStateAwareManager;
47 }
48
SubscribeAppState()49 bool AppStateAwareManager::SubscribeAppState()
50 {
51 std::lock_guard<std::mutex> lock(mutex_);
52 retryCount_++;
53 if (appStateObserver_) {
54 NETMGR_LOG_I("SubscribeAppState: appStateObserver_ has register");
55 return false;
56 }
57 sptr<AppExecFwk::IAppMgr> appMgrProxy = GetAppMgr();
58 if (!appMgrProxy) {
59 return false;
60 }
61 appStateObserver_ = new (std::nothrow)AppStateObserver();
62 auto err = appMgrProxy->RegisterApplicationStateObserver(appStateObserver_);
63 if (err != 0) {
64 NETMGR_LOG_I("SubscribeAppState error, code = %{public}d", err);
65 appStateObserver_ = nullptr;
66 return false;
67 }
68 return true;
69 }
70
UnSubscribeAppState()71 void AppStateAwareManager::UnSubscribeAppState()
72 {
73 NETMGR_LOG_I("UnSubscribeAppState start");
74 std::lock_guard<std::mutex> lock(mutex_);
75 if (!appStateObserver_) {
76 NETMGR_LOG_I("UnSubscribeAppState: appStateObserver_ is nullptr");
77 return;
78 }
79 sptr<AppExecFwk::IAppMgr> appMgrProxy = GetAppMgr();
80 if (appMgrProxy) {
81 appMgrProxy->UnregisterApplicationStateObserver(appStateObserver_);
82 appStateObserver_ = nullptr;
83 retryCount_ = 0;
84 }
85 NETMGR_LOG_I("UnSubscribeAppState end");
86 }
87
RegisterAppStateAwareCallback(const AppStateAwareCallback & appStateAwareCallback)88 void AppStateAwareManager::RegisterAppStateAwareCallback(const AppStateAwareCallback &appStateAwareCallback)
89 {
90 appStateAwareCallback_ = appStateAwareCallback;
91 }
92
GetAppMgr()93 sptr<AppExecFwk::IAppMgr> AppStateAwareManager::GetAppMgr()
94 {
95 sptr<ISystemAbilityManager> systemAbilityManager =
96 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
97 if (systemAbilityManager == nullptr) {
98 NETMGR_LOG_I("get SystemAbilityManager failed");
99 return nullptr;
100 }
101
102 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID);
103 if (remoteObject == nullptr) {
104 NETMGR_LOG_I("get App Manager Service failed");
105 return nullptr;
106 }
107 return iface_cast<AppExecFwk::IAppMgr>(remoteObject);
108 }
109
IsForegroundApp(const uint32_t uid)110 bool AppStateAwareManager::IsForegroundApp(const uint32_t uid)
111 {
112 if (!appStateObserver_) {
113 return false;
114 }
115 uint32_t foregroundAppUid = foregroundAppUid_.load();
116 return foregroundAppUid == uid;
117 }
118
OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)119 void AppStateAwareManager::OnForegroundApplicationChanged(
120 const AppExecFwk::AppStateData &appStateData)
121 {
122 if (foregroundAppUid_ != appStateData.uid) {
123 foregroundAppUid_ = appStateData.uid;
124 if (appStateAwareCallback_.OnForegroundAppChanged != nullptr) {
125 appStateAwareCallback_.OnForegroundAppChanged(foregroundAppUid_);
126 }
127 }
128 }
129
OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)130 void AppStateObserver::OnForegroundApplicationChanged(
131 const AppExecFwk::AppStateData &appStateData)
132 {
133 NETMGR_LOG_I("%{public}s bundleName: %{public}s, uid: %{public}d, state: %{public}d, isFocused: %{public}d",
134 __func__, appStateData.bundleName.c_str(), appStateData.uid, appStateData.state, appStateData.isFocused);
135 AppStateAwareManager::GetInstance().OnForegroundApplicationChanged(appStateData);
136 }
137
138
139 } // namespace NetManagerStandard
140 } // namespace OHOS
141