1bc2ed2b3Sopenharmony_ci/* 2bc2ed2b3Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd. 3bc2ed2b3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4bc2ed2b3Sopenharmony_ci * you may not use this file except in compliance with the License. 5bc2ed2b3Sopenharmony_ci * You may obtain a copy of the License at 6bc2ed2b3Sopenharmony_ci * 7bc2ed2b3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8bc2ed2b3Sopenharmony_ci * 9bc2ed2b3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10bc2ed2b3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11bc2ed2b3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12bc2ed2b3Sopenharmony_ci * See the License for the specific language governing permissions and 13bc2ed2b3Sopenharmony_ci * limitations under the License. 14bc2ed2b3Sopenharmony_ci */ 15bc2ed2b3Sopenharmony_ci 16bc2ed2b3Sopenharmony_ci#include "app_state_observer.h" 17bc2ed2b3Sopenharmony_ci 18bc2ed2b3Sopenharmony_ci#include "app_mgr_constants.h" 19bc2ed2b3Sopenharmony_ci#include "iservice_registry.h" 20bc2ed2b3Sopenharmony_ci#include "ability_manager_client.h" 21bc2ed2b3Sopenharmony_ci#include "system_ability_definition.h" 22bc2ed2b3Sopenharmony_ci#include "loghelper.h" 23bc2ed2b3Sopenharmony_ci 24bc2ed2b3Sopenharmony_cinamespace OHOS { 25bc2ed2b3Sopenharmony_cinamespace NFC { 26bc2ed2b3Sopenharmony_cistd::mutex mutex_ {}; 27bc2ed2b3Sopenharmony_cisptr<AppExecFwk::IAppMgr> appMgrProxy_ {nullptr}; 28bc2ed2b3Sopenharmony_cisptr<AppMgrDeathRecipient> appMgrDeathRecipient_(new (std::nothrow) AppMgrDeathRecipient()); 29bc2ed2b3Sopenharmony_ci 30bc2ed2b3Sopenharmony_civoid AppMgrDeathRecipient::OnRemoteDied([[maybe_unused]] const wptr<IRemoteObject> &remote) 31bc2ed2b3Sopenharmony_ci{ 32bc2ed2b3Sopenharmony_ci ErrorLog("On AppMgr died"); 33bc2ed2b3Sopenharmony_ci std::lock_guard<std::mutex> guard(mutex_); 34bc2ed2b3Sopenharmony_ci appMgrProxy_ = nullptr; 35bc2ed2b3Sopenharmony_ci}; 36bc2ed2b3Sopenharmony_ci 37bc2ed2b3Sopenharmony_ciAppStateObserver::AppStateObserver(INfcAppStateObserver *nfcAppStateChangeCallback) 38bc2ed2b3Sopenharmony_ci{ 39bc2ed2b3Sopenharmony_ci SubscribeAppState(); 40bc2ed2b3Sopenharmony_ci RegisterAppStateChangeCallback(nfcAppStateChangeCallback); 41bc2ed2b3Sopenharmony_ci} 42bc2ed2b3Sopenharmony_ci 43bc2ed2b3Sopenharmony_ciAppStateObserver::~AppStateObserver() 44bc2ed2b3Sopenharmony_ci{ 45bc2ed2b3Sopenharmony_ci UnSubscribeAppState(); 46bc2ed2b3Sopenharmony_ci} 47bc2ed2b3Sopenharmony_ci 48bc2ed2b3Sopenharmony_cibool AppStateObserver::SubscribeAppState() 49bc2ed2b3Sopenharmony_ci{ 50bc2ed2b3Sopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 51bc2ed2b3Sopenharmony_ci if (appStateAwareObserver_) { 52bc2ed2b3Sopenharmony_ci ErrorLog("SubscribeAppState: appStateAwareObserver_ has register"); 53bc2ed2b3Sopenharmony_ci return false; 54bc2ed2b3Sopenharmony_ci } 55bc2ed2b3Sopenharmony_ci if (!Connect()) { 56bc2ed2b3Sopenharmony_ci return false; 57bc2ed2b3Sopenharmony_ci } 58bc2ed2b3Sopenharmony_ci appStateAwareObserver_ = new (std::nothrow)AppStateAwareObserver(); 59bc2ed2b3Sopenharmony_ci auto err = appMgrProxy_->RegisterApplicationStateObserver(appStateAwareObserver_); 60bc2ed2b3Sopenharmony_ci if (err != 0) { 61bc2ed2b3Sopenharmony_ci ErrorLog("SubscribeAppState error, code = %{public}d", err); 62bc2ed2b3Sopenharmony_ci appStateAwareObserver_ = nullptr; 63bc2ed2b3Sopenharmony_ci return false; 64bc2ed2b3Sopenharmony_ci } 65bc2ed2b3Sopenharmony_ci DebugLog("SubscribeAppState success"); 66bc2ed2b3Sopenharmony_ci return true; 67bc2ed2b3Sopenharmony_ci} 68bc2ed2b3Sopenharmony_ci 69bc2ed2b3Sopenharmony_cibool AppStateObserver::UnSubscribeAppState() 70bc2ed2b3Sopenharmony_ci{ 71bc2ed2b3Sopenharmony_ci InfoLog("UnSubscribeAppState start"); 72bc2ed2b3Sopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 73bc2ed2b3Sopenharmony_ci if (!appStateAwareObserver_) { 74bc2ed2b3Sopenharmony_ci ErrorLog("UnSubscribeAppState: appStateAwareObserver_ is nullptr"); 75bc2ed2b3Sopenharmony_ci return false; 76bc2ed2b3Sopenharmony_ci } 77bc2ed2b3Sopenharmony_ci if (appMgrProxy_) { 78bc2ed2b3Sopenharmony_ci appMgrProxy_->UnregisterApplicationStateObserver(appStateAwareObserver_); 79bc2ed2b3Sopenharmony_ci appMgrProxy_ = nullptr; 80bc2ed2b3Sopenharmony_ci appStateAwareObserver_ = nullptr; 81bc2ed2b3Sopenharmony_ci } 82bc2ed2b3Sopenharmony_ci InfoLog("UnSubscribeAppState end"); 83bc2ed2b3Sopenharmony_ci return true; 84bc2ed2b3Sopenharmony_ci} 85bc2ed2b3Sopenharmony_ci 86bc2ed2b3Sopenharmony_cibool AppStateObserver::Connect() 87bc2ed2b3Sopenharmony_ci{ 88bc2ed2b3Sopenharmony_ci if (appMgrProxy_ != nullptr) { 89bc2ed2b3Sopenharmony_ci InfoLog("already connect"); 90bc2ed2b3Sopenharmony_ci return true; 91bc2ed2b3Sopenharmony_ci } 92bc2ed2b3Sopenharmony_ci 93bc2ed2b3Sopenharmony_ci sptr<ISystemAbilityManager> systemAbilityManager = 94bc2ed2b3Sopenharmony_ci SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 95bc2ed2b3Sopenharmony_ci if (systemAbilityManager == nullptr) { 96bc2ed2b3Sopenharmony_ci ErrorLog("get SystemAbilityManager failed"); 97bc2ed2b3Sopenharmony_ci return false; 98bc2ed2b3Sopenharmony_ci } 99bc2ed2b3Sopenharmony_ci 100bc2ed2b3Sopenharmony_ci sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID); 101bc2ed2b3Sopenharmony_ci if (remoteObject == nullptr) { 102bc2ed2b3Sopenharmony_ci ErrorLog("get App Manager Service failed"); 103bc2ed2b3Sopenharmony_ci return false; 104bc2ed2b3Sopenharmony_ci } 105bc2ed2b3Sopenharmony_ci 106bc2ed2b3Sopenharmony_ci appMgrProxy_ = iface_cast<AppExecFwk::IAppMgr>(remoteObject); 107bc2ed2b3Sopenharmony_ci if (!appMgrProxy_ || !appMgrProxy_->AsObject()) { 108bc2ed2b3Sopenharmony_ci ErrorLog("get app mgr proxy failed!"); 109bc2ed2b3Sopenharmony_ci return false; 110bc2ed2b3Sopenharmony_ci } 111bc2ed2b3Sopenharmony_ci appMgrProxy_->AsObject()->AddDeathRecipient(appMgrDeathRecipient_); 112bc2ed2b3Sopenharmony_ci return true; 113bc2ed2b3Sopenharmony_ci} 114bc2ed2b3Sopenharmony_ci 115bc2ed2b3Sopenharmony_cibool AppStateObserver::RegisterAppStateChangeCallback(INfcAppStateObserver *nfcAppStateChangeCallback) 116bc2ed2b3Sopenharmony_ci{ 117bc2ed2b3Sopenharmony_ci if (!appStateAwareObserver_) { 118bc2ed2b3Sopenharmony_ci ErrorLog("UnSubscribeAppState: appStateAwareObserver_ is nullptr"); 119bc2ed2b3Sopenharmony_ci return false; 120bc2ed2b3Sopenharmony_ci } 121bc2ed2b3Sopenharmony_ci if (nfcAppStateChangeCallback == nullptr) { 122bc2ed2b3Sopenharmony_ci ErrorLog("nfcAppStateChangeCallback_ is nullptr"); 123bc2ed2b3Sopenharmony_ci return false; 124bc2ed2b3Sopenharmony_ci } 125bc2ed2b3Sopenharmony_ci return appStateAwareObserver_->RegisterAppStateChangeCallback(nfcAppStateChangeCallback); 126bc2ed2b3Sopenharmony_ci} 127bc2ed2b3Sopenharmony_ci 128bc2ed2b3Sopenharmony_civoid AppStateObserver::AppStateAwareObserver::OnAbilityStateChanged( 129bc2ed2b3Sopenharmony_ci const AppExecFwk::AbilityStateData &abilityStateData) 130bc2ed2b3Sopenharmony_ci{ 131bc2ed2b3Sopenharmony_ci if (nfcAppStateChangeCallback_ == nullptr) { 132bc2ed2b3Sopenharmony_ci ErrorLog("nfcAppStateChangeCallback_ is nullptr"); 133bc2ed2b3Sopenharmony_ci return; 134bc2ed2b3Sopenharmony_ci } 135bc2ed2b3Sopenharmony_ci nfcAppStateChangeCallback_->HandleAppStateChanged(abilityStateData.bundleName, abilityStateData.abilityName, 136bc2ed2b3Sopenharmony_ci abilityStateData.abilityState); 137bc2ed2b3Sopenharmony_ci} 138bc2ed2b3Sopenharmony_ci 139bc2ed2b3Sopenharmony_civoid AppStateObserver::AppStateAwareObserver::OnForegroundApplicationChanged( 140bc2ed2b3Sopenharmony_ci const AppExecFwk::AppStateData &appStateData) 141bc2ed2b3Sopenharmony_ci{ 142bc2ed2b3Sopenharmony_ci} 143bc2ed2b3Sopenharmony_ci 144bc2ed2b3Sopenharmony_cibool AppStateObserver::AppStateAwareObserver::RegisterAppStateChangeCallback( 145bc2ed2b3Sopenharmony_ci INfcAppStateObserver *nfcAppStateChangeCallback) 146bc2ed2b3Sopenharmony_ci{ 147bc2ed2b3Sopenharmony_ci if (nfcAppStateChangeCallback == nullptr) { 148bc2ed2b3Sopenharmony_ci ErrorLog("nfcAppStateChangeCallback_ is nullptr"); 149bc2ed2b3Sopenharmony_ci return false; 150bc2ed2b3Sopenharmony_ci } 151bc2ed2b3Sopenharmony_ci nfcAppStateChangeCallback_ = nfcAppStateChangeCallback; 152bc2ed2b3Sopenharmony_ci return true; 153bc2ed2b3Sopenharmony_ci} 154bc2ed2b3Sopenharmony_ci 155bc2ed2b3Sopenharmony_civoid AppStateObserver::AppStateAwareObserver::OnProcessDied(const AppExecFwk::ProcessData &processData) 156bc2ed2b3Sopenharmony_ci{ 157bc2ed2b3Sopenharmony_ci} 158bc2ed2b3Sopenharmony_ci 159bc2ed2b3Sopenharmony_cibool AppStateObserver::IsForegroundApp(const std::string &bundleName) 160bc2ed2b3Sopenharmony_ci{ 161bc2ed2b3Sopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 162bc2ed2b3Sopenharmony_ci if (!Connect()) { 163bc2ed2b3Sopenharmony_ci ErrorLog("IsForegroundApp connect failed"); 164bc2ed2b3Sopenharmony_ci return false; 165bc2ed2b3Sopenharmony_ci } 166bc2ed2b3Sopenharmony_ci std::vector<AppExecFwk::AppStateData> appList; 167bc2ed2b3Sopenharmony_ci appMgrProxy_->GetForegroundApplications(appList); 168bc2ed2b3Sopenharmony_ci for (AppExecFwk::AppStateData appData : appList) { 169bc2ed2b3Sopenharmony_ci if (bundleName.compare(appData.bundleName) == 0) { 170bc2ed2b3Sopenharmony_ci InfoLog("IsForegroundApp: %{public}s is foreground", bundleName.c_str()); 171bc2ed2b3Sopenharmony_ci return true; 172bc2ed2b3Sopenharmony_ci } 173bc2ed2b3Sopenharmony_ci } 174bc2ed2b3Sopenharmony_ci ErrorLog("IsForegroundApp: %{public}s is not foreground", bundleName.c_str()); 175bc2ed2b3Sopenharmony_ci return false; 176bc2ed2b3Sopenharmony_ci} 177bc2ed2b3Sopenharmony_ci} // namespace NFC 178bc2ed2b3Sopenharmony_ci} // namespace OHOS