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 "ability_manager_helper.h"
17
18 #include "app_log_wrapper.h"
19 #include "system_ability_helper.h"
20 #include "system_ability_definition.h"
21
22 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
23 #include "app_mgr_interface.h"
24 #include "running_process_info.h"
25 #endif
26
27 namespace OHOS {
28 namespace AppExecFwk {
UninstallApplicationProcesses(const std::string & bundleName,const int uid)29 bool AbilityManagerHelper::UninstallApplicationProcesses(const std::string &bundleName, const int uid)
30 {
31 #ifdef ABILITY_RUNTIME_ENABLE
32 APP_LOGI("uninstall kill running processes, app name is %{public}s", bundleName.c_str());
33 if (SystemAbilityHelper::UninstallApp(bundleName, uid) != 0) {
34 APP_LOGE("kill application process failed");
35
36 return false;
37 }
38 return true;
39 #else
40 APP_LOGI("ABILITY_RUNTIME_ENABLE is false");
41 return true;
42 #endif
43 }
44
IsRunning(const std::string bundleName,const int bundleUid)45 int AbilityManagerHelper::IsRunning(const std::string bundleName, const int bundleUid)
46 {
47 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
48 APP_LOGI("check app is running, app name is %{public}s", bundleName.c_str());
49 sptr<IAppMgr> appMgrProxy = iface_cast<IAppMgr>(SystemAbilityHelper::GetSystemAbility(APP_MGR_SERVICE_ID));
50 if (appMgrProxy == nullptr) {
51 APP_LOGE("fail to find the app mgr service to check app is running");
52 return FAILED;
53 }
54 if (bundleUid < 0) {
55 APP_LOGE("bundleUid is error.");
56 return FAILED;
57 }
58 std::vector<RunningProcessInfo> runningList;
59 int result = appMgrProxy->GetAllRunningProcesses(runningList);
60 if (result != ERR_OK) {
61 APP_LOGE("GetAllRunningProcesses failed.");
62 return FAILED;
63 }
64 for (RunningProcessInfo info : runningList) {
65 if (info.uid_ == bundleUid) {
66 auto res = std::any_of(info.bundleNames.begin(), info.bundleNames.end(),
67 [bundleName](const auto &bundleNameInRunningProcessInfo) {
68 return bundleNameInRunningProcessInfo == bundleName;
69 });
70 if (res) {
71 return RUNNING;
72 }
73 }
74 }
75 APP_LOGI("nothing app running.");
76 return NOT_RUNNING;
77 #else
78 APP_LOGI("BUNDLE_FRAMEWORK_FREE_INSTALL is false");
79 return FAILED;
80 #endif
81 }
82 } // namespace AppExecFwk
83 } // namespace OHOS
84