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 "appswitch_input.h"
17
18#include "input_manager.h"
19#include "report.h"
20#include "wukong_define.h"
21
22namespace OHOS {
23namespace WuKong {
24AppswitchInput::AppswitchInput() : InputAction()
25{
26    std::shared_ptr<MultimodeInputMsg> multimodeInputMsg = std::make_shared<MultimodeInputMsg>();
27    multimodeInputMsg->inputType_ = INPUTTYPE_APPSWITCHINPUT;
28    inputedMsgObject_ = multimodeInputMsg;
29}
30
31AppswitchInput::~AppswitchInput()
32{
33}
34
35ErrCode AppswitchInput::OrderInput(const std::shared_ptr<SpcialTestObject>& specialTestObject)
36{
37    AppSwitchParam* appSwitchPtr = static_cast<AppSwitchParam*>(specialTestObject.get());
38    if (appSwitchPtr == nullptr) {
39        return OHOS::ERR_INVALID_VALUE;
40    }
41    std::string bundlename = appSwitchPtr->bundlename_;
42    std::vector<std::string> bundleList(0);
43    std::vector<std::string> abilityList(0);
44    auto util = WuKongUtil::GetInstance();
45    util->GetBundleList(bundleList, abilityList);
46    if (bundleList.size() == 0 || abilityList.size() == 0) {
47        ERROR_LOG_STR("bundleList (%u) or abilityList (%u) is 0", bundleList.size(), abilityList.size());
48        return OHOS::ERR_INVALID_VALUE;
49    }
50    uint32_t index = util->FindElement(bundleList, bundlename);
51    if (index == INVALIDVALUE) {
52        ERROR_LOG("not found bundle");
53        return OHOS::ERR_INVALID_VALUE;
54    }
55
56    // start ability through bundle information
57    ErrCode result = AppManager::GetInstance()->StartAbilityByBundleInfo(abilityList[index], bundleList[index]);
58    // print the result of start event
59    PrintResultOfStartAbility(result, index);
60    usleep(WAIT_TIME);
61    return result;
62}
63
64ErrCode AppswitchInput::RandomInput()
65{
66    std::vector<std::string> bundleList(0);
67    std::vector<std::string> abilityList(0);
68    WuKongUtil::GetInstance()->GetBundleList(bundleList, abilityList);
69    if (bundleList.size() == 0 || abilityList.size() == 0) {
70        ERROR_LOG_STR("bundleList (%u) or abilityList (%u) is 0", bundleList.size(), abilityList.size());
71        return OHOS::ERR_INVALID_VALUE;
72    }
73    uint32_t index = GetAbilityIndex(bundleList);
74    if (index == INVALIDVALUE) {
75        ERROR_LOG("not found bundle");
76        return OHOS::ERR_INVALID_VALUE;
77    }
78    // start ability through bundle information
79    ErrCode result = AppManager::GetInstance()->StartAbilityByBundleInfo(abilityList[index], bundleList[index]);
80    // print the result of start event
81    PrintResultOfStartAbility(result, index);
82    TRACK_LOG_STR("bundle index: %d", index);
83    Report::GetInstance()->SyncInputInfo(inputedMsgObject_);
84    return result;
85}
86
87ErrCode AppswitchInput::RandomInput(const std::string &uri, const std::string &uriType)
88{
89    ErrCode result = AppManager::GetInstance()->StartAbilityByUriAndType(uri, uriType);
90    Report::GetInstance()->SyncInputInfo(inputedMsgObject_);
91    return result;
92}
93
94ErrCode AppswitchInput::RandomInput(const std::string &uri, const std::vector<std::string> &abilityName)
95{
96    ErrCode result = AppManager::GetInstance()->StartAbilityByAbilityAndUri(uri, abilityName);
97    Report::GetInstance()->SyncInputInfo(inputedMsgObject_);
98    return result;
99}
100
101ErrCode AppswitchInput::FocusInput(bool shouldScreenCap)
102{
103    return AppswitchInput::RandomInput();
104}
105
106ErrCode AppswitchInput::PrintResultOfStartAbility(const ErrCode result, uint32_t index)
107{
108    std::vector<std::string> bundleList;
109    std::vector<std::string> abilityList;
110    WuKongUtil::GetInstance()->GetBundleList(bundleList, abilityList);
111    if (result == OHOS::ERR_OK) {
112        INFO_LOG_STR("Bundle Name: (%s) startup successful", bundleList[index].c_str());
113    } else {
114        INFO_LOG_STR("Bundle Name: (%s) startup failed", bundleList[index].c_str());
115    }
116    return OHOS::ERR_OK;
117}
118
119InputType AppswitchInput::GetInputInfo()
120{
121    return INPUTTYPE_APPSWITCHINPUT;
122}
123
124uint32_t AppswitchInput::GetAbilityIndex(std::vector<std::string>& bundlelist)
125{
126    uint32_t index = INVALIDVALUE;
127    std::vector<std::string> allowlist;
128    std::vector<std::string> validlist;
129    WuKongUtil::GetInstance()->GetAllowList(allowlist);
130    WuKongUtil::GetInstance()->GetValidBundleList(validlist);
131    if (bundlelist.size() > 0) {
132        if (allowlist.size() > 0) {
133            index = WuKongUtil::GetInstance()->FindElement(bundlelist, allowlist.at(rand() % allowlist.size()));
134        } else if (validlist.size() > 0) {
135            index = WuKongUtil::GetInstance()->FindElement(bundlelist, validlist.at(rand() % validlist.size()));
136        } else {
137            index = rand() % bundlelist.size();
138        }
139    }
140    return index;
141}
142}  // namespace WuKong
143}  // namespace OHOS
144