1 /*
2  * Copyright (c) 2021-2024 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_tag_wrapper.h"
18 #include "hitrace_meter.h"
19 #include "singleton.h"
20 #include "sys_mgr_client.h"
21 #include "system_ability_definition.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
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     TAG_LOGD(AAFwkTag::APPKIT, "%s, %d", __func__, __LINE__);
34     ErrCode error = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, requestCode);
35     if (error != ERR_OK) {
36         TAG_LOGE(AAFwkTag::APPKIT, "%s failed, error : %d", __func__, error);
37     }
38 }
39 
ClearUpApplicationData(const std::string & bundleName)40 int32_t AbilityManager::ClearUpApplicationData(const std::string &bundleName)
41 {
42     TAG_LOGI(AAFwkTag::APPKIT, "%s, %d", __func__, __LINE__);
43     auto object = OHOS::DelayedSingleton<SysMrgClient>::GetInstance()->GetSystemAbility(APP_MGR_SERVICE_ID);
44     sptr<IAppMgr> appMgr_ = iface_cast<IAppMgr>(object);
45     if (appMgr_ == nullptr) {
46         TAG_LOGE(AAFwkTag::APPKIT, "%s, appMgr_ is nullptr", __func__);
47         return ERR_NULL_OBJECT;
48     }
49 
50     return appMgr_->ClearUpApplicationData(bundleName);
51 }
52 
GetAllRunningProcesses()53 std::vector<RunningProcessInfo> AbilityManager::GetAllRunningProcesses()
54 {
55     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
56     TAG_LOGD(AAFwkTag::APPKIT, "%s, %d", __func__, __LINE__);
57     auto object = OHOS::DelayedSingleton<SysMrgClient>::GetInstance()->GetSystemAbility(APP_MGR_SERVICE_ID);
58     sptr<IAppMgr> appMgr_ = iface_cast<IAppMgr>(object);
59     std::vector<RunningProcessInfo> info;
60     if (appMgr_ == nullptr) {
61         TAG_LOGE(AAFwkTag::APPKIT, "%s, appMgr_ is nullptr", __func__);
62         return info;
63     }
64 
65     appMgr_->GetAllRunningProcesses(info);
66     return info;
67 }
68 
KillProcessesByBundleName(const std::string & bundleName,const bool clearPageStack)69 int AbilityManager::KillProcessesByBundleName(const std::string &bundleName, const bool clearPageStack)
70 {
71     TAG_LOGD(AAFwkTag::APPKIT, "%s, %d", __func__, __LINE__);
72     ErrCode error = AAFwk::AbilityManagerClient::GetInstance()->KillProcess(bundleName, clearPageStack);
73     if (error != ERR_OK) {
74         TAG_LOGE(AAFwkTag::APPKIT, "%s failed, error : %d", __func__, error);
75         return error;
76     }
77     return ERR_OK;
78 }
79 }  // namespace AppExecFwk
80 }  // namespace OHOS
81