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
22 namespace OHOS {
23 namespace WuKong {
AppswitchInput()24 AppswitchInput::AppswitchInput() : InputAction()
25 {
26 std::shared_ptr<MultimodeInputMsg> multimodeInputMsg = std::make_shared<MultimodeInputMsg>();
27 multimodeInputMsg->inputType_ = INPUTTYPE_APPSWITCHINPUT;
28 inputedMsgObject_ = multimodeInputMsg;
29 }
30
~AppswitchInput()31 AppswitchInput::~AppswitchInput()
32 {
33 }
34
OrderInput(const std::shared_ptr<SpcialTestObject> & specialTestObject)35 ErrCode 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
RandomInput()64 ErrCode 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
FocusInput(bool shouldScreenCap)87 ErrCode AppswitchInput::FocusInput(bool shouldScreenCap)
88 {
89 return AppswitchInput::RandomInput();
90 }
91
PrintResultOfStartAbility(const ErrCode result,uint32_t index)92 ErrCode AppswitchInput::PrintResultOfStartAbility(const ErrCode result, uint32_t index)
93 {
94 std::vector<std::string> bundleList;
95 std::vector<std::string> abilityList;
96 WuKongUtil::GetInstance()->GetBundleList(bundleList, abilityList);
97 if (result == OHOS::ERR_OK) {
98 INFO_LOG_STR("Bundle Name: (%s) startup successful", bundleList[index].c_str());
99 } else {
100 INFO_LOG_STR("Bundle Name: (%s) startup failed", bundleList[index].c_str());
101 }
102 return OHOS::ERR_OK;
103 }
104
GetInputInfo()105 ErrCode AppswitchInput::GetInputInfo()
106 {
107 return OHOS::ERR_OK;
108 }
109
GetAbilityIndex(std::vector<std::string> & bundlelist)110 uint32_t AppswitchInput::GetAbilityIndex(std::vector<std::string>& bundlelist)
111 {
112 uint32_t index = INVALIDVALUE;
113 std::vector<std::string> allowlist;
114 std::vector<std::string> validlist;
115 WuKongUtil::GetInstance()->GetAllowList(allowlist);
116 WuKongUtil::GetInstance()->GetValidBundleList(validlist);
117 if (bundlelist.size() > 0) {
118 if (allowlist.size() > 0) {
119 index = WuKongUtil::GetInstance()->FindElement(bundlelist, allowlist.at(rand() % allowlist.size()));
120 } else if (validlist.size() > 0) {
121 index = WuKongUtil::GetInstance()->FindElement(bundlelist, validlist.at(rand() % validlist.size()));
122 } else {
123 index = rand() % bundlelist.size();
124 }
125 }
126 return index;
127 }
128 } // namespace WuKong
129 } // namespace OHOS
130