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