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 "keyboard_input.h"
17
18#include "multimode_manager.h"
19#include "report.h"
20
21namespace OHOS {
22namespace WuKong {
23namespace {
24const int SINGLE_CODE_PER = 30;
25const int ONE_HUNDRED_PERCENT = 100;
26const int DOWN_TIME = 100;
27}  // namespace
28KeyboardInput::KeyboardInput() : InputAction()
29{
30    std::shared_ptr<MultimodeInputMsg> multimodeInputMsg = std::make_shared<MultimodeInputMsg>();
31    multimodeInputMsg->inputType_ = INPUTTYPE_KEYBOARDINPUT;
32    inputedMsgObject_ = multimodeInputMsg;
33}
34KeyboardInput::~KeyboardInput()
35{
36}
37ErrCode KeyboardInput::RandomInput()
38{
39    ErrCode result = OHOS::ERR_OK;
40    std::vector<int> keycodelist;
41    int keyCodePercent = rand() % ONE_HUNDRED_PERCENT;
42    MultimodeManager::GetInstance()->GetKeycodeList(keycodelist);
43    if (keycodelist.size() > 0) {
44        if (keyCodePercent < SINGLE_CODE_PER) {
45            int keycode = keycodelist[(uint32_t)(rand()) % keycodelist.size()];
46            result = MultimodeManager::GetInstance()->SingleKeyCodeInput(keycode, DOWN_TIME);
47        } else {
48            result = MultimodeManager::GetInstance()->MultiKeyCodeInput(DOWN_TIME);
49        }
50    } else {
51        return OHOS::ERR_NO_INIT;
52    }
53    if (result != OHOS::ERR_OK) {
54        return result;
55    }
56    Report::GetInstance()->SyncInputInfo(inputedMsgObject_);
57    return result;
58}
59
60InputType KeyboardInput::GetInputInfo()
61{
62    return INPUTTYPE_KEYBOARDINPUT;
63}
64}  // namespace WuKong
65}  // namespace OHOS
66