xref: /test/ostest/wukong/common/src/app_manager.cpp (revision a69a01cd)
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 "app_manager.h"
17
18#include "ability_manager_client.h"
19#include "element_name.h"
20#include "wukong_define.h"
21#include "wukong_util.h"
22
23namespace OHOS {
24namespace WuKong {
25namespace {
26const std::string HELP_MSG_NO_ABILITY_NAME_OPTION = "error: -a <ability-name> is expected";
27const std::string HELP_MSG_NO_BUNDLE_NAME_OPTION = "error: -b <bundle-name> is expected";
28
29const std::string STRING_START_ABILITY_OK = "start ability successfully.";
30const std::string STRING_START_ABILITY_NG = "error: failed to start ability.";
31}  // namespace
32
33bool AppManager::BlockAbilityController::AllowAbilityStart(const AAFwk::Want &want, const std::string &bundleName)
34{
35    TRACK_LOG_STD();
36    std::vector<std::string> blocklist;
37    std::vector<std::string> tempAllowList;
38    bool orderFlag;
39    auto util = WuKongUtil::GetInstance();
40
41    tempAllowList = util->GetTempAllowList();
42    // if bundleName in the tempAllow list to allow ability start.
43    auto it = find(tempAllowList.begin(), tempAllowList.end(), bundleName);
44    orderFlag = util->GetOrderFlag();
45    if (orderFlag && tempAllowList.size() != 0) {
46        if (it != tempAllowList.end()) {
47            DEBUG_LOG("bundle start allow");
48            return true;
49        } else {
50            return false;
51        }
52    }
53
54    util->GetBlockList(blocklist);
55    DEBUG_LOG_STR("BundleName: %s", bundleName.c_str());
56
57    // if bundleName in the block list to unallow ability start.
58    it = find(blocklist.begin(), blocklist.end(), bundleName);
59    if (it == blocklist.end()) {
60        DEBUG_LOG("bundle start prohibition");
61        return true;
62    }
63
64    TRACK_LOG_END();
65    return false;
66}
67
68// turn to background
69bool AppManager::BlockAbilityController::AllowAbilityBackground(const std::string &bundleName)
70{
71    if (WuKongUtil::GetInstance()->GetOrderFlag()) {
72        return false;
73    } else {
74        return true;
75    }
76}
77
78ErrCode AppManager::StartAbilityByBundleInfo(std::string abilityName, std::string bundleName)
79{
80    TRACK_LOG_STD();
81    AAFwk::Want want;
82    int result;
83    std::string output;
84    if (abilityName.size() == 0 || bundleName.size() == 0) {
85        if (abilityName.size() == 0) {
86            output.append(HELP_MSG_NO_ABILITY_NAME_OPTION + "\n");
87        }
88        if (bundleName.size() == 0) {
89            output.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
90        }
91        result = OHOS::ERR_INVALID_VALUE;
92    } else {
93        AppExecFwk::ElementName element("", bundleName, abilityName);
94        want.SetElement(element);
95        result = OHOS::AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
96        if (result == OHOS::ERR_OK) {
97            output = STRING_START_ABILITY_OK;
98        } else {
99            output = STRING_START_ABILITY_NG;
100        }
101    }
102    DEBUG_LOG_STR("%s", output.c_str());
103    TRACK_LOG_STR("result %s", std::to_string(result).c_str());
104    return result;
105}
106
107ErrCode AppManager::StartAbilityByUriAndType(const std::string uri, const std::string typeVal)
108{
109    TRACK_LOG_STD();
110    AAFwk::Want want;
111    int result;
112    want.SetUri(uri);
113    want.SetType(typeVal);
114    result = OHOS::AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
115    if (result == OHOS::ERR_OK) {
116        INFO_LOG("The specified URI page is opened successfully.");
117    } else {
118        ERROR_LOG("Failed to open the page with the specified URI.");
119    }
120    TRACK_LOG_STR("result %s", std::to_string(result).c_str());
121    return result;
122}
123
124ErrCode AppManager::StartAbilityByAbilityAndUri(const std::string uri, const std::vector<std::string> &abilityName)
125{
126    TRACK_LOG_STD();
127    AAFwk::Want want;
128    int result;
129    std::vector<std::string> allowList;
130    WuKongUtil::GetInstance()->GetAllowList(allowList);
131    AppExecFwk::ElementName element("", allowList[0], abilityName[0]);
132    want.SetElement(element);
133    want.SetUri(uri);
134    result = OHOS::AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
135    if (result == OHOS::ERR_OK) {
136        INFO_LOG("The specified URI page is opened successfully.");
137    } else {
138        ERROR_LOG("Failed to open the page with the specified URI.");
139    }
140    TRACK_LOG_STR("result %s", std::to_string(result).c_str());
141    return result;
142}
143
144void AppManager::SetAbilityController()
145{
146    if (abilityController_ == nullptr) {
147        abilityController_ = new (std::nothrow) BlockAbilityController();
148    }
149    OHOS::AAFwk::AbilityManagerClient::GetInstance()->SetAbilityController(abilityController_, true);
150}
151}  // namespace WuKong
152}  // namespace OHOS
153