Files

157 lines
5.2 KiB
C++

/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "app_manager.h"
#include "ability_manager_client.h"
#include "component_manager.h"
#include "element_name.h"
#include "wukong_define.h"
#include "wukong_util.h"
namespace OHOS {
namespace WuKong {
namespace {
const std::string HELP_MSG_NO_ABILITY_NAME_OPTION = "error: -a <ability-name> is expected";
const std::string HELP_MSG_NO_BUNDLE_NAME_OPTION = "error: -b <bundle-name> is expected";
const std::string STRING_START_ABILITY_OK = "start ability successfully.";
const std::string STRING_START_ABILITY_NG = "error: failed to start ability.";
} // namespace
bool AppManager::BlockAbilityController::AllowAbilityStart(const AAFwk::Want &want, const std::string &bundleName)
{
TRACK_LOG_STD();
std::vector<std::string> blocklist;
std::vector<std::string> tempAllowList;
bool orderFlag;
auto util = WuKongUtil::GetInstance();
tempAllowList = util->GetTempAllowList();
// if bundleName in the tempAllow list to allow ability start.
auto it = find(tempAllowList.begin(), tempAllowList.end(), bundleName);
orderFlag = util->GetOrderFlag();
if (orderFlag && tempAllowList.size() != 0) {
if (it != tempAllowList.end()) {
DEBUG_LOG("bundle start allow");
return true;
} else {
return false;
}
}
util->GetBlockList(blocklist);
DEBUG_LOG_STR("BundleName: %s", bundleName.c_str());
// if bundleName in the block list to unallow ability start.
it = find(blocklist.begin(), blocklist.end(), bundleName);
if (it == blocklist.end()) {
DEBUG_LOG("bundle start prohibition");
return true;
}
TRACK_LOG_END();
return false;
}
// turn to background
bool AppManager::BlockAbilityController::AllowAbilityBackground(const std::string &bundleName)
{
if (WuKongUtil::GetInstance()->GetOrderFlag()) {
return false;
} else {
return true;
}
}
ErrCode AppManager::StartAbilityByBundleInfo(std::string abilityName, std::string bundleName)
{
TRACK_LOG_STD();
AAFwk::Want want;
int result;
std::string output;
if (abilityName.size() == 0 || bundleName.size() == 0) {
if (abilityName.size() == 0) {
output.append(HELP_MSG_NO_ABILITY_NAME_OPTION + "\n");
}
if (bundleName.size() == 0) {
output.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
}
result = OHOS::ERR_INVALID_VALUE;
} else {
AppExecFwk::ElementName element("", bundleName, abilityName);
want.SetElement(element);
ComponentManager::GetInstance()->BackToHome();
result = OHOS::AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
if (result == OHOS::ERR_OK) {
output = STRING_START_ABILITY_OK;
} else {
output = STRING_START_ABILITY_NG;
}
}
DEBUG_LOG_STR("%s", output.c_str());
TRACK_LOG_STR("result %s", std::to_string(result).c_str());
return result;
}
ErrCode AppManager::StartAbilityByUriAndType(const std::string uri, const std::string typeVal)
{
TRACK_LOG_STD();
AAFwk::Want want;
int result;
want.SetUri(uri);
want.SetType(typeVal);
ComponentManager::GetInstance()->BackToHome();
result = OHOS::AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
if (result == OHOS::ERR_OK) {
INFO_LOG("The specified URI page is opened successfully.");
} else {
ERROR_LOG("Failed to open the page with the specified URI.");
}
TRACK_LOG_STR("result %s", std::to_string(result).c_str());
return result;
}
ErrCode AppManager::StartAbilityByAbilityAndUri(const std::string uri, const std::vector<std::string> &abilityName)
{
TRACK_LOG_STD();
AAFwk::Want want;
int result;
std::vector<std::string> allowList;
WuKongUtil::GetInstance()->GetAllowList(allowList);
AppExecFwk::ElementName element("", allowList[0], abilityName[0]);
want.SetElement(element);
want.SetUri(uri);
ComponentManager::GetInstance()->BackToHome();
result = OHOS::AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
if (result == OHOS::ERR_OK) {
INFO_LOG("The specified URI page is opened successfully.");
} else {
ERROR_LOG("Failed to open the page with the specified URI.");
}
TRACK_LOG_STR("result %s", std::to_string(result).c_str());
return result;
}
void AppManager::SetAbilityController()
{
if (abilityController_ == nullptr) {
abilityController_ = new (std::nothrow) BlockAbilityController();
}
OHOS::AAFwk::AbilityManagerClient::GetInstance()->SetAbilityController(abilityController_, true);
}
} // namespace WuKong
} // namespace OHOS