• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
PrintResultOfStartAbility(const ErrCode result,uint32_t index)87 ErrCode AppswitchInput::PrintResultOfStartAbility(const ErrCode result, uint32_t index)
88 {
89     std::vector<std::string> bundleList;
90     std::vector<std::string> abilityList;
91     WuKongUtil::GetInstance()->GetBundleList(bundleList, abilityList);
92     if (result == OHOS::ERR_OK) {
93         INFO_LOG_STR("Bundle Name: (%s) startup successful", bundleList[index].c_str());
94     } else {
95         INFO_LOG_STR("Bundle Name: (%s) startup failed", bundleList[index].c_str());
96     }
97     return OHOS::ERR_OK;
98 }
99 
GetInputInfo()100 ErrCode AppswitchInput::GetInputInfo()
101 {
102     return OHOS::ERR_OK;
103 }
104 
GetAbilityIndex(std::vector<std::string> & bundlelist)105 uint32_t AppswitchInput::GetAbilityIndex(std::vector<std::string>& bundlelist)
106 {
107     uint32_t index = INVALIDVALUE;
108     std::vector<std::string> allowlist;
109     std::vector<std::string> validlist;
110     WuKongUtil::GetInstance()->GetAllowList(allowlist);
111     WuKongUtil::GetInstance()->GetValidBundleList(validlist);
112     if (bundlelist.size() > 0) {
113         if (allowlist.size() > 0) {
114             index = WuKongUtil::GetInstance()->FindElement(bundlelist, allowlist.at(rand() % allowlist.size()));
115         } else if (validlist.size() > 0) {
116             index = WuKongUtil::GetInstance()->FindElement(bundlelist, validlist.at(rand() % validlist.size()));
117         } else {
118             index = rand() % bundlelist.size();
119         }
120     }
121     return index;
122 }
123 }  // namespace WuKong
124 }  // namespace OHOS
125