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 #include "appstate_observer.h" 16 #include "dfs_error.h" 17 #include "singleton.h" 18 #include "utils_log.h" 19 20 namespace OHOS { 21 namespace FileManagement { 22 namespace CloudDisk { 23 using namespace std; 24 GetInstance()25AppStateObserverManager &AppStateObserverManager::GetInstance() 26 { 27 static AppStateObserverManager instance; 28 return instance; 29 } 30 SubscribeAppState(const std::vector<std::string> & bundleNameList)31void AppStateObserverManager::SubscribeAppState(const std::vector<std::string> &bundleNameList) 32 { 33 auto appMgrClient = DelayedSingleton<OHOS::AppExecFwk::AppMgrClient>::GetInstance(); 34 if (appMgrClient == nullptr) { 35 LOGE("appMgrClient_ is nullptr"); 36 return; 37 } 38 if (appStateObserver_ != nullptr) { 39 LOGE("appStateObserver has been registed"); 40 return; 41 } 42 appStateObserver_ = sptr<AppStateObserver>(new (std::nothrow) AppStateObserver()); 43 if (appStateObserver_ == nullptr) { 44 LOGE("get appStateObserver failed"); 45 return; 46 } 47 48 int32_t result = appMgrClient->RegisterApplicationStateObserver(appStateObserver_, bundleNameList); 49 if (result != E_OK) { 50 LOGE("RegistApplicationStateObserver failed result = %{public}d", result); 51 appStateObserver_ = nullptr; 52 } 53 } 54 UnSubscribeAppState()55void AppStateObserverManager::UnSubscribeAppState() 56 { 57 if (appStateObserver_ == nullptr) { 58 LOGE("appStateObserver is nullptr"); 59 return; 60 } 61 62 auto appMgrClient = DelayedSingleton<OHOS::AppExecFwk::AppMgrClient>::GetInstance(); 63 if (appMgrClient == nullptr) { 64 LOGE("appMgrClient_ is nullptr"); 65 return; 66 } 67 int32_t result = appMgrClient->UnregisterApplicationStateObserver(appStateObserver_); 68 if (result != E_OK) { 69 LOGE("UnregisterApplicationStateObserver failed result = %{public}d", result); 70 } 71 appStateObserver_ = nullptr; 72 } 73 OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)74void AppStateObserver::OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) 75 { 76 IoMessageManager::GetInstance().OnReceiveEvent(appStateData); 77 } 78 } // namespace CloudDisk 79 } // namespace FileManagement 80 } // namespace OHOS