• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "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 
ClearUpApplicationData(const std::string & bundleName)39 int32_t AbilityManager::ClearUpApplicationData(const std::string &bundleName)
40 {
41     HILOG_DEBUG("%s, %d", __func__, __LINE__);
42     auto object = OHOS::DelayedSingleton<SysMrgClient>::GetInstance()->GetSystemAbility(APP_MGR_SERVICE_ID);
43     sptr<IAppMgr> appMgr_ = iface_cast<IAppMgr>(object);
44     if (appMgr_ == nullptr) {
45         HILOG_ERROR("%s, appMgr_ is nullptr", __func__);
46         return ERR_NULL_OBJECT;
47     }
48 
49     return appMgr_->ClearUpApplicationData(bundleName);
50 }
51 
GetAllRunningProcesses()52 std::vector<RunningProcessInfo> AbilityManager::GetAllRunningProcesses()
53 {
54     HILOG_DEBUG("%s, %d", __func__, __LINE__);
55     auto object = OHOS::DelayedSingleton<SysMrgClient>::GetInstance()->GetSystemAbility(APP_MGR_SERVICE_ID);
56     sptr<IAppMgr> appMgr_ = iface_cast<IAppMgr>(object);
57     std::vector<RunningProcessInfo> info;
58     if (appMgr_ == nullptr) {
59         HILOG_ERROR("%s, appMgr_ is nullptr", __func__);
60         return info;
61     }
62 
63     appMgr_->GetAllRunningProcesses(info);
64     return info;
65 }
66 
KillProcessesByBundleName(const std::string & bundleName)67 int AbilityManager::KillProcessesByBundleName(const std::string &bundleName)
68 {
69     HILOG_DEBUG("%s, %d", __func__, __LINE__);
70     ErrCode error = AAFwk::AbilityManagerClient::GetInstance()->KillProcess(bundleName);
71     if (error != ERR_OK) {
72         HILOG_ERROR("%s failed, error : %d", __func__, error);
73         return error;
74     }
75     return ERR_OK;
76 }
77 }  // namespace AppExecFwk
78 }  // namespace OHOS
79