1 /*
2 * Copyright (c) 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 "application_state_observer.h"
17 #include "app_mgr_interface.h"
18 #include "app_process_data.h"
19 #include "app_mgr_client.h"
20 #include "iservice_registry.h"
21 #include "system_ability.h"
22 #include "system_ability_definition.h"
23 #include "sys_mgr_client.h"
24 #include "log.h"
25
26 namespace OHOS::Request::Download {
ApplicationStateObserver()27 ApplicationStateObserver::ApplicationStateObserver()
28 {}
29
~ApplicationStateObserver()30 ApplicationStateObserver::~ApplicationStateObserver()
31 {}
32
GetInstance()33 ApplicationStateObserver &ApplicationStateObserver::GetInstance()
34 {
35 static ApplicationStateObserver observer;
36 return observer;
37 }
38
RegisterAppStateChanged(RegCallBack && callback)39 bool ApplicationStateObserver::RegisterAppStateChanged(RegCallBack&& callback)
40 {
41 DOWNLOAD_HILOGD("RegisterAppState");
42 sptr<AppProcessState> appProcessState_ = new (std::nothrow) AppProcessState(*this);
43 if (appProcessState_ == nullptr) {
44 DOWNLOAD_HILOGE("create AppProcessState fail, not enough memory");
45 return false;
46 }
47 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
48 if (systemAbilityManager == nullptr) {
49 DOWNLOAD_HILOGE("get SystemAbilityManager failed.");
50 return false;
51 }
52 sptr<AppExecFwk::IAppMgr> appObject =
53 iface_cast<AppExecFwk::IAppMgr>(systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID));
54 if (appObject) {
55 int ret = appObject->RegisterApplicationStateObserver(appProcessState_);
56 if (ret == ERR_OK) {
57 DOWNLOAD_HILOGD("register success");
58 callback_ = callback;
59 return true;
60 }
61 DOWNLOAD_HILOGE("register fail, ret = %{public}d", ret);
62 return false;
63 }
64 DOWNLOAD_HILOGE("get SystemAbilityManager fail");
65 return false;
66 }
67
OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)68 void ApplicationStateObserver::AppProcessState::OnForegroundApplicationChanged(
69 const AppExecFwk::AppStateData &appStateData)
70 {
71 }
72
OnAbilityStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)73 void ApplicationStateObserver::AppProcessState::OnAbilityStateChanged(
74 const AppExecFwk::AbilityStateData &abilityStateData)
75 {
76 DOWNLOAD_HILOGD("OnAbilityStateChanged uid=%{public}d, bundleName=%{public}s,state=%{public}d",
77 abilityStateData.uid, abilityStateData.bundleName.c_str(), abilityStateData.abilityState);
78 RunCallback(abilityStateData.bundleName, abilityStateData.uid, abilityStateData.abilityState);
79 }
80
OnExtensionStateChanged(const AppExecFwk::AbilityStateData & extensionStateData)81 void ApplicationStateObserver::AppProcessState::OnExtensionStateChanged(
82 const AppExecFwk::AbilityStateData &extensionStateData)
83 {
84 }
85
OnProcessCreated(const AppExecFwk::ProcessData & processData)86 void ApplicationStateObserver::AppProcessState::OnProcessCreated(const AppExecFwk::ProcessData &processData)
87 {
88 }
89
OnProcessDied(const AppExecFwk::ProcessData & processData)90 void ApplicationStateObserver::AppProcessState::OnProcessDied(const AppExecFwk::ProcessData &processData)
91 {
92 DOWNLOAD_HILOGD("OnProcessDied uid=%{public}d, bundleName=%{public}s", processData.uid,
93 processData.bundleName.c_str());
94 RunCallback(processData.bundleName, processData.uid, static_cast<int32_t>(processData.state));
95 }
96
RunCallback(const std::string bundleName,int32_t uid,int32_t state)97 void ApplicationStateObserver::AppProcessState::RunCallback(const std::string bundleName, int32_t uid, int32_t state)
98 {
99 if (appStateObserver_.callback_ != nullptr) {
100 appStateObserver_.callback_(bundleName, uid, state);
101 }
102 }
103 }
104