1 /*
2 * Copyright (c) 2021 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 "ability_manager.h"
17 #include "app_log_wrapper.h"
18 #include "singleton.h"
19 #include "system_ability_definition.h"
20 #include "sys_mgr_client.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
24
GetInstance()25 AbilityManager &AbilityManager::GetInstance()
26 {
27 static AbilityManager abilityManager;
28 return abilityManager;
29 }
30
StartAbility(const Want & want,int requestCode=-1)31 void AbilityManager::StartAbility(const Want &want, int requestCode = -1)
32 {
33 APP_LOGD("%s, %d", __func__, __LINE__);
34 ErrCode error = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, requestCode);
35 if (error != ERR_OK) {
36 APP_LOGE("%s failed, error : %d", __func__, error);
37 }
38 }
39
MoveMissionToTop(int missionId)40 void AbilityManager::MoveMissionToTop(int missionId)
41 {
42 APP_LOGD("%s, %d", __func__, __LINE__);
43 ErrCode error = AAFwk::AbilityManagerClient::GetInstance()->MoveMissionToTop(missionId);
44 if (error != ERR_OK) {
45 APP_LOGE("%s failed, error : %d", __func__, error);
46 }
47 }
48
GetAllStackInfo() const49 StackInfo AbilityManager::GetAllStackInfo() const
50 {
51 APP_LOGD("%s, %d", __func__, __LINE__);
52 StackInfo info;
53 ErrCode error = AAFwk::AbilityManagerClient::GetInstance()->GetAllStackInfo(info);
54 if (error != ERR_OK) {
55 APP_LOGE("%s failed, error : %d", __func__, error);
56 }
57
58 return info;
59 }
60
QueryRecentAbilityMissionInfo(int numMax,int flags) const61 std::vector<AbilityMissionInfo> AbilityManager::QueryRecentAbilityMissionInfo(int numMax, int flags) const
62 {
63 APP_LOGD("%s, %d", __func__, __LINE__);
64 std::vector<AbilityMissionInfo> info;
65 ErrCode error = AAFwk::AbilityManagerClient::GetInstance()->GetRecentMissions(numMax, flags, info);
66 if (error != ERR_OK) {
67 APP_LOGE("%s failed, error : %d", __func__, error);
68 }
69
70 return info;
71 }
72
QueryRunningAbilityMissionInfo(int numMax) const73 std::vector<AbilityMissionInfo> AbilityManager::QueryRunningAbilityMissionInfo(int numMax) const
74 {
75 APP_LOGD("%s, %d", __func__, __LINE__);
76 std::vector<AbilityMissionInfo> info;
77 ErrCode error =
78 AAFwk::AbilityManagerClient::GetInstance()->GetRecentMissions(numMax, RECENT_IGNORE_UNAVAILABLE, info);
79 if (error != ERR_OK) {
80 APP_LOGE("%s failed, error : %d", __func__, error);
81 }
82
83 return info;
84 }
85
RemoveMissions(const std::vector<int> & missionId)86 void AbilityManager::RemoveMissions(const std::vector<int> &missionId)
87 {
88 APP_LOGD("%s, %d", __func__, __LINE__);
89 ErrCode error = AAFwk::AbilityManagerClient::GetInstance()->RemoveMissions(missionId);
90 if (error != ERR_OK) {
91 APP_LOGE("%s failed, error : %d", __func__, error);
92 }
93 }
94
ClearUpApplicationData(const std::string & bundleName)95 int32_t AbilityManager::ClearUpApplicationData(const std::string &bundleName)
96 {
97 APP_LOGD("%s, %d", __func__, __LINE__);
98 auto object = OHOS::DelayedSingleton<SysMrgClient>::GetInstance()->GetSystemAbility(APP_MGR_SERVICE_ID);
99 sptr<IAppMgr> appMgr_ = iface_cast<IAppMgr>(object);
100 if (appMgr_ == nullptr) {
101 APP_LOGE("%s, appMgr_ is nullptr", __func__);
102 return ERR_NULL_OBJECT;
103 }
104
105 return appMgr_->ClearUpApplicationData(bundleName);
106 }
107
GetAllRunningProcesses()108 std::vector<RunningProcessInfo> AbilityManager::GetAllRunningProcesses()
109 {
110 APP_LOGD("%s, %d", __func__, __LINE__);
111 auto object = OHOS::DelayedSingleton<SysMrgClient>::GetInstance()->GetSystemAbility(APP_MGR_SERVICE_ID);
112 sptr<IAppMgr> appMgr_ = iface_cast<IAppMgr>(object);
113 std::vector<RunningProcessInfo> info;
114 if (appMgr_ == nullptr) {
115 APP_LOGE("%s, appMgr_ is nullptr", __func__);
116 return info;
117 }
118
119 appMgr_->GetAllRunningProcesses(info);
120 return info;
121 }
122
KillProcessesByBundleName(const std::string & bundleName)123 int AbilityManager::KillProcessesByBundleName(const std::string &bundleName)
124 {
125 APP_LOGD("%s, %d", __func__, __LINE__);
126 ErrCode error = AAFwk::AbilityManagerClient::GetInstance()->KillProcess(bundleName);
127 if (error != ERR_OK) {
128 APP_LOGE("%s failed, error : %d", __func__, error);
129 return error;
130 }
131 return ERR_OK;
132 }
133
134 } // namespace AppExecFwk
135 } // namespace OHOS