xref: /test/ostest/wukong/common/src/app_manager.cpp (revision a69a01cd)
1a69a01cdSopenharmony_ci/*
2a69a01cdSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3a69a01cdSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a69a01cdSopenharmony_ci * you may not use this file except in compliance with the License.
5a69a01cdSopenharmony_ci * You may obtain a copy of the License at
6a69a01cdSopenharmony_ci *
7a69a01cdSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a69a01cdSopenharmony_ci *
9a69a01cdSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a69a01cdSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a69a01cdSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a69a01cdSopenharmony_ci * See the License for the specific language governing permissions and
13a69a01cdSopenharmony_ci * limitations under the License.
14a69a01cdSopenharmony_ci */
15a69a01cdSopenharmony_ci
16a69a01cdSopenharmony_ci#include "app_manager.h"
17a69a01cdSopenharmony_ci
18a69a01cdSopenharmony_ci#include "ability_manager_client.h"
19a69a01cdSopenharmony_ci#include "element_name.h"
20a69a01cdSopenharmony_ci#include "wukong_define.h"
21a69a01cdSopenharmony_ci#include "wukong_util.h"
22a69a01cdSopenharmony_ci
23a69a01cdSopenharmony_cinamespace OHOS {
24a69a01cdSopenharmony_cinamespace WuKong {
25a69a01cdSopenharmony_cinamespace {
26a69a01cdSopenharmony_ciconst std::string HELP_MSG_NO_ABILITY_NAME_OPTION = "error: -a <ability-name> is expected";
27a69a01cdSopenharmony_ciconst std::string HELP_MSG_NO_BUNDLE_NAME_OPTION = "error: -b <bundle-name> is expected";
28a69a01cdSopenharmony_ci
29a69a01cdSopenharmony_ciconst std::string STRING_START_ABILITY_OK = "start ability successfully.";
30a69a01cdSopenharmony_ciconst std::string STRING_START_ABILITY_NG = "error: failed to start ability.";
31a69a01cdSopenharmony_ci}  // namespace
32a69a01cdSopenharmony_ci
33a69a01cdSopenharmony_cibool AppManager::BlockAbilityController::AllowAbilityStart(const AAFwk::Want &want, const std::string &bundleName)
34a69a01cdSopenharmony_ci{
35a69a01cdSopenharmony_ci    TRACK_LOG_STD();
36a69a01cdSopenharmony_ci    std::vector<std::string> blocklist;
37a69a01cdSopenharmony_ci    std::vector<std::string> tempAllowList;
38a69a01cdSopenharmony_ci    bool orderFlag;
39a69a01cdSopenharmony_ci    auto util = WuKongUtil::GetInstance();
40a69a01cdSopenharmony_ci
41a69a01cdSopenharmony_ci    tempAllowList = util->GetTempAllowList();
42a69a01cdSopenharmony_ci    // if bundleName in the tempAllow list to allow ability start.
43a69a01cdSopenharmony_ci    auto it = find(tempAllowList.begin(), tempAllowList.end(), bundleName);
44a69a01cdSopenharmony_ci    orderFlag = util->GetOrderFlag();
45a69a01cdSopenharmony_ci    if (orderFlag && tempAllowList.size() != 0) {
46a69a01cdSopenharmony_ci        if (it != tempAllowList.end()) {
47a69a01cdSopenharmony_ci            DEBUG_LOG("bundle start allow");
48a69a01cdSopenharmony_ci            return true;
49a69a01cdSopenharmony_ci        } else {
50a69a01cdSopenharmony_ci            return false;
51a69a01cdSopenharmony_ci        }
52a69a01cdSopenharmony_ci    }
53a69a01cdSopenharmony_ci
54a69a01cdSopenharmony_ci    util->GetBlockList(blocklist);
55a69a01cdSopenharmony_ci    DEBUG_LOG_STR("BundleName: %s", bundleName.c_str());
56a69a01cdSopenharmony_ci
57a69a01cdSopenharmony_ci    // if bundleName in the block list to unallow ability start.
58a69a01cdSopenharmony_ci    it = find(blocklist.begin(), blocklist.end(), bundleName);
59a69a01cdSopenharmony_ci    if (it == blocklist.end()) {
60a69a01cdSopenharmony_ci        DEBUG_LOG("bundle start prohibition");
61a69a01cdSopenharmony_ci        return true;
62a69a01cdSopenharmony_ci    }
63a69a01cdSopenharmony_ci
64a69a01cdSopenharmony_ci    TRACK_LOG_END();
65a69a01cdSopenharmony_ci    return false;
66a69a01cdSopenharmony_ci}
67a69a01cdSopenharmony_ci
68a69a01cdSopenharmony_ci// turn to background
69a69a01cdSopenharmony_cibool AppManager::BlockAbilityController::AllowAbilityBackground(const std::string &bundleName)
70a69a01cdSopenharmony_ci{
71a69a01cdSopenharmony_ci    if (WuKongUtil::GetInstance()->GetOrderFlag()) {
72a69a01cdSopenharmony_ci        return false;
73a69a01cdSopenharmony_ci    } else {
74a69a01cdSopenharmony_ci        return true;
75a69a01cdSopenharmony_ci    }
76a69a01cdSopenharmony_ci}
77a69a01cdSopenharmony_ci
78a69a01cdSopenharmony_ciErrCode AppManager::StartAbilityByBundleInfo(std::string abilityName, std::string bundleName)
79a69a01cdSopenharmony_ci{
80a69a01cdSopenharmony_ci    TRACK_LOG_STD();
81a69a01cdSopenharmony_ci    AAFwk::Want want;
82a69a01cdSopenharmony_ci    int result;
83a69a01cdSopenharmony_ci    std::string output;
84a69a01cdSopenharmony_ci    if (abilityName.size() == 0 || bundleName.size() == 0) {
85a69a01cdSopenharmony_ci        if (abilityName.size() == 0) {
86a69a01cdSopenharmony_ci            output.append(HELP_MSG_NO_ABILITY_NAME_OPTION + "\n");
87a69a01cdSopenharmony_ci        }
88a69a01cdSopenharmony_ci        if (bundleName.size() == 0) {
89a69a01cdSopenharmony_ci            output.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
90a69a01cdSopenharmony_ci        }
91a69a01cdSopenharmony_ci        result = OHOS::ERR_INVALID_VALUE;
92a69a01cdSopenharmony_ci    } else {
93a69a01cdSopenharmony_ci        AppExecFwk::ElementName element("", bundleName, abilityName);
94a69a01cdSopenharmony_ci        want.SetElement(element);
95a69a01cdSopenharmony_ci        result = OHOS::AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
96a69a01cdSopenharmony_ci        if (result == OHOS::ERR_OK) {
97a69a01cdSopenharmony_ci            output = STRING_START_ABILITY_OK;
98a69a01cdSopenharmony_ci        } else {
99a69a01cdSopenharmony_ci            output = STRING_START_ABILITY_NG;
100a69a01cdSopenharmony_ci        }
101a69a01cdSopenharmony_ci    }
102a69a01cdSopenharmony_ci    DEBUG_LOG_STR("%s", output.c_str());
103a69a01cdSopenharmony_ci    TRACK_LOG_STR("result %s", std::to_string(result).c_str());
104a69a01cdSopenharmony_ci    return result;
105a69a01cdSopenharmony_ci}
106a69a01cdSopenharmony_ci
107a69a01cdSopenharmony_ciErrCode AppManager::StartAbilityByUriAndType(const std::string uri, const std::string typeVal)
108a69a01cdSopenharmony_ci{
109a69a01cdSopenharmony_ci    TRACK_LOG_STD();
110a69a01cdSopenharmony_ci    AAFwk::Want want;
111a69a01cdSopenharmony_ci    int result;
112a69a01cdSopenharmony_ci    want.SetUri(uri);
113a69a01cdSopenharmony_ci    want.SetType(typeVal);
114a69a01cdSopenharmony_ci    result = OHOS::AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
115a69a01cdSopenharmony_ci    if (result == OHOS::ERR_OK) {
116a69a01cdSopenharmony_ci        INFO_LOG("The specified URI page is opened successfully.");
117a69a01cdSopenharmony_ci    } else {
118a69a01cdSopenharmony_ci        ERROR_LOG("Failed to open the page with the specified URI.");
119a69a01cdSopenharmony_ci    }
120a69a01cdSopenharmony_ci    TRACK_LOG_STR("result %s", std::to_string(result).c_str());
121a69a01cdSopenharmony_ci    return result;
122a69a01cdSopenharmony_ci}
123a69a01cdSopenharmony_ci
124a69a01cdSopenharmony_ciErrCode AppManager::StartAbilityByAbilityAndUri(const std::string uri, const std::vector<std::string> &abilityName)
125a69a01cdSopenharmony_ci{
126a69a01cdSopenharmony_ci    TRACK_LOG_STD();
127a69a01cdSopenharmony_ci    AAFwk::Want want;
128a69a01cdSopenharmony_ci    int result;
129a69a01cdSopenharmony_ci    std::vector<std::string> allowList;
130a69a01cdSopenharmony_ci    WuKongUtil::GetInstance()->GetAllowList(allowList);
131a69a01cdSopenharmony_ci    AppExecFwk::ElementName element("", allowList[0], abilityName[0]);
132a69a01cdSopenharmony_ci    want.SetElement(element);
133a69a01cdSopenharmony_ci    want.SetUri(uri);
134a69a01cdSopenharmony_ci    result = OHOS::AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
135a69a01cdSopenharmony_ci    if (result == OHOS::ERR_OK) {
136a69a01cdSopenharmony_ci        INFO_LOG("The specified URI page is opened successfully.");
137a69a01cdSopenharmony_ci    } else {
138a69a01cdSopenharmony_ci        ERROR_LOG("Failed to open the page with the specified URI.");
139a69a01cdSopenharmony_ci    }
140a69a01cdSopenharmony_ci    TRACK_LOG_STR("result %s", std::to_string(result).c_str());
141a69a01cdSopenharmony_ci    return result;
142a69a01cdSopenharmony_ci}
143a69a01cdSopenharmony_ci
144a69a01cdSopenharmony_civoid AppManager::SetAbilityController()
145a69a01cdSopenharmony_ci{
146a69a01cdSopenharmony_ci    if (abilityController_ == nullptr) {
147a69a01cdSopenharmony_ci        abilityController_ = new (std::nothrow) BlockAbilityController();
148a69a01cdSopenharmony_ci    }
149a69a01cdSopenharmony_ci    OHOS::AAFwk::AbilityManagerClient::GetInstance()->SetAbilityController(abilityController_, true);
150a69a01cdSopenharmony_ci}
151a69a01cdSopenharmony_ci}  // namespace WuKong
152a69a01cdSopenharmony_ci}  // namespace OHOS
153