• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "app_state_observer.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 "loghelper.h"
23 
24 namespace OHOS {
25 namespace NFC {
26 std::mutex mutex_ {};
27 sptr<AppExecFwk::IAppMgr> appMgrProxy_ {nullptr};
28 sptr<AppMgrDeathRecipient> appMgrDeathRecipient_(new AppMgrDeathRecipient());
29 
OnRemoteDied(const wptr<IRemoteObject> & remote)30 void AppMgrDeathRecipient::OnRemoteDied([[maybe_unused]] const wptr<IRemoteObject> &remote)
31 {
32     ErrorLog("On AppMgr died");
33     std::lock_guard<std::mutex> guard(mutex_);
34     appMgrProxy_ = nullptr;
35 };
36 
AppStateObserver(INfcAppStateObserver * nfcAppStateChangeCallback)37 AppStateObserver::AppStateObserver(INfcAppStateObserver *nfcAppStateChangeCallback)
38 {
39     SubscribeAppState();
40     RegisterAppStateChangeCallback(nfcAppStateChangeCallback);
41 }
42 
~AppStateObserver()43 AppStateObserver::~AppStateObserver()
44 {
45     UnSubscribeAppState();
46 }
47 
SubscribeAppState()48 bool AppStateObserver::SubscribeAppState()
49 {
50     std::lock_guard<std::mutex> lock(mutex_);
51     if (appStateAwareObserver_) {
52         ErrorLog("SubscribeAppState: appStateAwareObserver_ has register");
53         return false;
54     }
55     if (!Connect()) {
56         return false;
57     }
58     appStateAwareObserver_ = new (std::nothrow)AppStateAwareObserver();
59     if (appStateAwareObserver_ == nullptr) {
60         ErrorLog("SubscribeAppState: appStateAwareObserver_ is nullptr");
61         return false;
62     }
63     auto err = appMgrProxy_->RegisterApplicationStateObserver(appStateAwareObserver_);
64     if (err != 0) {
65         ErrorLog("SubscribeAppState error, code = %{public}d", err);
66         appStateAwareObserver_ = nullptr;
67         return false;
68     }
69     DebugLog("SubscribeAppState success");
70     return true;
71 }
72 
UnSubscribeAppState()73 bool AppStateObserver::UnSubscribeAppState()
74 {
75     InfoLog("UnSubscribeAppState start");
76     std::lock_guard<std::mutex> lock(mutex_);
77     if (!appStateAwareObserver_) {
78         ErrorLog("UnSubscribeAppState: appStateAwareObserver_ is nullptr");
79         return false;
80     }
81     if (appMgrProxy_) {
82         appMgrProxy_->UnregisterApplicationStateObserver(appStateAwareObserver_);
83         appMgrProxy_ = nullptr;
84         appStateAwareObserver_ = nullptr;
85     }
86     InfoLog("UnSubscribeAppState end");
87     return true;
88 }
89 
Connect()90 bool AppStateObserver::Connect()
91 {
92     if (appMgrProxy_ != nullptr) {
93         InfoLog("already connect");
94         return true;
95     }
96 
97     sptr<ISystemAbilityManager> systemAbilityManager =
98         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
99     if (systemAbilityManager == nullptr) {
100         ErrorLog("get SystemAbilityManager failed");
101         return false;
102     }
103 
104     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID);
105     if (remoteObject == nullptr) {
106         ErrorLog("get App Manager Service failed");
107         return false;
108     }
109 
110     appMgrProxy_ = iface_cast<AppExecFwk::IAppMgr>(remoteObject);
111     if (!appMgrProxy_ || !appMgrProxy_->AsObject()) {
112         ErrorLog("get app mgr proxy failed!");
113         return false;
114     }
115     appMgrProxy_->AsObject()->AddDeathRecipient(appMgrDeathRecipient_);
116     return true;
117 }
118 
RegisterAppStateChangeCallback(INfcAppStateObserver * nfcAppStateChangeCallback)119 bool AppStateObserver::RegisterAppStateChangeCallback(INfcAppStateObserver *nfcAppStateChangeCallback)
120 {
121     if (!appStateAwareObserver_) {
122         ErrorLog("UnSubscribeAppState: appStateAwareObserver_ is nullptr");
123         return false;
124     }
125     if (nfcAppStateChangeCallback == nullptr) {
126         ErrorLog("nfcAppStateChangeCallback_ is nullptr");
127         return false;
128     }
129     return appStateAwareObserver_->RegisterAppStateChangeCallback(nfcAppStateChangeCallback);
130 }
131 
OnAbilityStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)132 void AppStateObserver::AppStateAwareObserver::OnAbilityStateChanged(
133     const AppExecFwk::AbilityStateData &abilityStateData)
134 {
135     if (nfcAppStateChangeCallback_ == nullptr) {
136         ErrorLog("nfcAppStateChangeCallback_ is nullptr");
137         return;
138     }
139     nfcAppStateChangeCallback_->HandleAppStateChanged(abilityStateData.bundleName, abilityStateData.abilityName,
140         abilityStateData.abilityState);
141 }
142 
OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)143 void AppStateObserver::AppStateAwareObserver::OnForegroundApplicationChanged(
144     const AppExecFwk::AppStateData &appStateData)
145 {
146 }
147 
RegisterAppStateChangeCallback(INfcAppStateObserver * nfcAppStateChangeCallback)148 bool AppStateObserver::AppStateAwareObserver::RegisterAppStateChangeCallback(
149     INfcAppStateObserver *nfcAppStateChangeCallback)
150 {
151     if (nfcAppStateChangeCallback == nullptr) {
152         ErrorLog("nfcAppStateChangeCallback_ is nullptr");
153         return false;
154     }
155     nfcAppStateChangeCallback_ = nfcAppStateChangeCallback;
156     return true;
157 }
158 
OnProcessDied(const AppExecFwk::ProcessData & processData)159 void AppStateObserver::AppStateAwareObserver::OnProcessDied(const AppExecFwk::ProcessData &processData)
160 {
161 }
162 
IsForegroundApp(const std::string & bundleName)163 bool AppStateObserver::IsForegroundApp(const std::string &bundleName)
164 {
165     std::lock_guard<std::mutex> lock(mutex_);
166     if (!Connect()) {
167         ErrorLog("IsForegroundApp connect failed");
168         return false;
169     }
170     std::vector<AppExecFwk::AppStateData> appList;
171     appMgrProxy_->GetForegroundApplications(appList);
172     for (AppExecFwk::AppStateData appData : appList) {
173         if (bundleName.compare(appData.bundleName) == 0) {
174             InfoLog("IsForegroundApp: %{public}s is foreground", bundleName.c_str());
175             return true;
176         }
177     }
178     ErrorLog("IsForegroundApp: %{public}s is not foreground", bundleName.c_str());
179     return false;
180 }
181 } // namespace NFC
182 } // namespace OHOS